taler-android

Android apps for GNU Taler (wallet, PoS, cashier)
Log | Files | Refs | README | LICENSE

OutgoingPullFragment.kt (5267B)


      1 /*
      2  * This file is part of GNU Taler
      3  * (C) 2022 Taler Systems S.A.
      4  *
      5  * GNU Taler is free software; you can redistribute it and/or modify it under the
      6  * terms of the GNU General Public License as published by the Free Software
      7  * Foundation; either version 3, or (at your option) any later version.
      8  *
      9  * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
     10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11  * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License along with
     14  * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15  */
     16 
     17 package net.taler.wallet.peer
     18 
     19 import android.os.Bundle
     20 import android.view.LayoutInflater
     21 import android.view.View
     22 import android.view.ViewGroup
     23 import androidx.compose.runtime.getValue
     24 import androidx.compose.runtime.livedata.observeAsState
     25 import androidx.compose.runtime.remember
     26 import androidx.compose.ui.platform.ComposeView
     27 import androidx.core.os.bundleOf
     28 import androidx.fragment.app.Fragment
     29 import androidx.fragment.app.activityViewModels
     30 import androidx.lifecycle.Lifecycle
     31 import androidx.lifecycle.lifecycleScope
     32 import androidx.lifecycle.repeatOnLifecycle
     33 import androidx.navigation.fragment.findNavController
     34 import kotlinx.coroutines.launch
     35 import net.taler.wallet.main.MainViewModel
     36 import net.taler.wallet.R
     37 import net.taler.wallet.main.ViewMode
     38 import net.taler.wallet.compose.AmountScope
     39 import net.taler.wallet.compose.TalerSurface
     40 import net.taler.wallet.compose.collectAsStateLifecycleAware
     41 import net.taler.wallet.showError
     42 
     43 class OutgoingPullFragment : Fragment() {
     44     private val model: MainViewModel by activityViewModels()
     45     private val peerManager get() = model.peerManager
     46     private val transactionManager get() = model.transactionManager
     47     private val exchangeManager get() = model.exchangeManager
     48     private val balanceManager get() = model.balanceManager
     49 
     50     override fun onCreateView(
     51         inflater: LayoutInflater,
     52         container: ViewGroup?,
     53         savedInstanceState: Bundle?,
     54     ): View {
     55         return ComposeView(requireContext()).apply {
     56             setContent {
     57                 TalerSurface {
     58                     val state by peerManager.pullState.collectAsStateLifecycleAware()
     59                     val viewMode by model.viewMode.collectAsStateLifecycleAware()
     60                     val devMode by model.devMode.observeAsState()
     61                     OutgoingPullComposable(
     62                         state = state,
     63                         onCreateInvoice = this@OutgoingPullFragment::onCreateInvoice,
     64                         onTosAccept = this@OutgoingPullFragment::onTosAccept,
     65                         defaultScope = remember { (viewMode as? ViewMode.Transactions)?.selectedScope },
     66                         scopes = balanceManager.getScopes(true),
     67                         devMode = devMode == true,
     68                         getCurrencySpec = exchangeManager::getSpecForScopeInfo,
     69                         checkPeerPullCredit = { amount, loading ->
     70                             model.selectScope(amount.scope)
     71                             peerManager.checkPeerPullCredit(
     72                                 amount.amount,
     73                                 scopeInfo = amount.scope,
     74                                 loading = loading,
     75                             )
     76                         },
     77                     )
     78                 }
     79             }
     80         }
     81     }
     82 
     83     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
     84         super.onViewCreated(view, savedInstanceState)
     85         lifecycleScope.launch {
     86             viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
     87                 peerManager.pullState.collect {
     88                     if (it is OutgoingResponse) {
     89                         if (transactionManager.selectTransaction(it.transactionId)) {
     90                             findNavController().navigate(R.id.action_global_transactionPeer)
     91                         } else {
     92                             findNavController().navigate(R.id.action_global_main)
     93                         }
     94                     }
     95 
     96                     if (it is OutgoingError && model.devMode.value == true) {
     97                         showError(it.info)
     98                     }
     99                 }
    100             }
    101         }
    102 
    103         exchangeManager.exchanges.observe(viewLifecycleOwner) { exchanges ->
    104             // detect ToS acceptation
    105             peerManager.refreshPeerPullCreditTos(exchanges)
    106         }
    107     }
    108 
    109     override fun onStart() {
    110         super.onStart()
    111         activity?.setTitle(R.string.receive_peer_title)
    112     }
    113 
    114     override fun onDestroy() {
    115         super.onDestroy()
    116         if (!requireActivity().isChangingConfigurations) peerManager.resetPullPayment()
    117     }
    118 
    119     private fun onTosAccept(exchangeBaseUrl: String) {
    120         val bundle = bundleOf("exchangeBaseUrl" to exchangeBaseUrl)
    121         findNavController().navigate(R.id.action_global_reviewExchangeTOS, bundle)
    122     }
    123 
    124     private fun onCreateInvoice(amount: AmountScope, summary: String, hours: Long, exchangeBaseUrl: String) {
    125         peerManager.initiatePeerPullCredit(amount.amount, summary, hours, exchangeBaseUrl)
    126     }
    127 }