taler-android

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

OutgoingPushFragment.kt (5136B)


      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.activity.OnBackPressedCallback
     24 import androidx.compose.runtime.getValue
     25 import androidx.compose.runtime.livedata.observeAsState
     26 import androidx.compose.runtime.remember
     27 import androidx.compose.ui.platform.ComposeView
     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 OutgoingPushFragment : 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 balanceManager get() = model.balanceManager
     48     private val exchangeManager get() = model.exchangeManager
     49 
     50     // hacky way to change back action until we have navigation for compose
     51     private val backPressedCallback = object : OnBackPressedCallback(false) {
     52         override fun handleOnBackPressed() {
     53             findNavController().navigate(R.id.action_global_main)
     54         }
     55     }
     56 
     57     override fun onCreateView(
     58         inflater: LayoutInflater,
     59         container: ViewGroup?,
     60         savedInstanceState: Bundle?,
     61     ): View {
     62         requireActivity().onBackPressedDispatcher.addCallback(
     63             viewLifecycleOwner, backPressedCallback
     64         )
     65 
     66         return ComposeView(requireContext()).apply {
     67             setContent {
     68                 TalerSurface {
     69                     val state = peerManager.pushState.collectAsStateLifecycleAware().value
     70                     val viewMode by model.viewMode.collectAsStateLifecycleAware()
     71                     val devMode by model.devMode.observeAsState()
     72                     OutgoingPushComposable(
     73                         state = state,
     74                         defaultScope = remember { (viewMode as? ViewMode.Transactions)?.selectedScope },
     75                         scopes = balanceManager.getScopes(true),
     76                         devMode = devMode == true,
     77                         getCurrencySpec = exchangeManager::getSpecForScopeInfo,
     78                         getFees = {
     79                             model.selectScope(it.scope)
     80                             peerManager.checkPeerPushFees(it.amount, restrictScope = it.scope)
     81                         },
     82                         onSend = this@OutgoingPushFragment::onSend,
     83                     )
     84                 }
     85             }
     86         }
     87     }
     88 
     89     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
     90         super.onViewCreated(view, savedInstanceState)
     91         lifecycleScope.launch {
     92             viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
     93                 peerManager.pushState.collect {
     94                     if (it is OutgoingResponse) {
     95                         if (transactionManager.selectTransaction(it.transactionId)) {
     96                             findNavController().navigate(R.id.action_global_transactionPeer)
     97                         } else {
     98                             findNavController().navigate(R.id.action_global_main)
     99                         }
    100                     }
    101 
    102                     if (it is OutgoingError && model.devMode.value == true) {
    103                         showError(it.info)
    104                     }
    105 
    106                     // Disable back navigation when tx is being created
    107                     backPressedCallback.isEnabled = it !is OutgoingCreating
    108                 }
    109             }
    110         }
    111     }
    112 
    113     override fun onStart() {
    114         super.onStart()
    115         activity?.setTitle(R.string.send_peer_title)
    116     }
    117 
    118     override fun onDestroy() {
    119         super.onDestroy()
    120         if (!requireActivity().isChangingConfigurations) peerManager.resetPushPayment()
    121     }
    122 
    123     private fun onSend(amount: AmountScope, summary: String, hours: Long) {
    124         peerManager.initiatePeerPushDebit(amount.amount, summary, hours, restrictScope = amount.scope)
    125     }
    126 }