taler-android

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

IncomingPullPaymentFragment.kt (3359B)


      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.ui.platform.ComposeView
     24 import androidx.fragment.app.Fragment
     25 import androidx.fragment.app.activityViewModels
     26 import androidx.lifecycle.Lifecycle
     27 import androidx.lifecycle.lifecycleScope
     28 import androidx.lifecycle.repeatOnLifecycle
     29 import androidx.navigation.fragment.findNavController
     30 import kotlinx.coroutines.launch
     31 import net.taler.common.showError
     32 import net.taler.wallet.main.MainViewModel
     33 import net.taler.wallet.R
     34 import net.taler.wallet.compose.TalerSurface
     35 import net.taler.wallet.compose.collectAsStateLifecycleAware
     36 import net.taler.wallet.showError
     37 
     38 class IncomingPullPaymentFragment : Fragment() {
     39     private val model: MainViewModel by activityViewModels()
     40     private val peerManager get() = model.peerManager
     41     private val transactionManager get() = model.transactionManager
     42 
     43     override fun onCreateView(
     44         inflater: LayoutInflater,
     45         container: ViewGroup?,
     46         savedInstanceState: Bundle?,
     47     ): View {
     48         return ComposeView(requireContext()).apply {
     49             setContent {
     50                 TalerSurface {
     51                     val state = peerManager.incomingPullState.collectAsStateLifecycleAware()
     52                     IncomingComposable(state, incomingPull) { terms ->
     53                         peerManager.confirmPeerPullDebit(terms)
     54                     }
     55                 }
     56             }
     57         }
     58     }
     59 
     60     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
     61         super.onViewCreated(view, savedInstanceState)
     62         lifecycleScope.launch {
     63             viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
     64                 peerManager.incomingPullState.collect {
     65                     if (it is IncomingAccepted) {
     66                         if (transactionManager.selectTransaction(it.transactionId)) {
     67                             findNavController().navigate(R.id.action_global_transactionPeer)
     68                         } else {
     69                             findNavController().navigate(R.id.action_global_main)
     70                         }
     71                     } else if (it is IncomingError) {
     72                         if (model.devMode.value == true) {
     73                             showError(it.info)
     74                         } else {
     75                             showError(it.info.userFacingMsg)
     76                         }
     77                     }
     78                 }
     79             }
     80         }
     81     }
     82 
     83     override fun onStart() {
     84         super.onStart()
     85         activity?.setTitle(R.string.pay_peer_title)
     86     }
     87 }