IncomingPushPaymentFragment.kt (4045B)
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.util.Log 21 import android.view.LayoutInflater 22 import android.view.View 23 import android.view.ViewGroup 24 import androidx.compose.ui.platform.ComposeView 25 import androidx.core.os.bundleOf 26 import androidx.fragment.app.Fragment 27 import androidx.fragment.app.activityViewModels 28 import androidx.lifecycle.Lifecycle 29 import androidx.lifecycle.lifecycleScope 30 import androidx.lifecycle.repeatOnLifecycle 31 import androidx.navigation.fragment.findNavController 32 import kotlinx.coroutines.launch 33 import net.taler.common.showError 34 import net.taler.wallet.main.MainViewModel 35 import net.taler.wallet.R 36 import net.taler.wallet.main.TAG 37 import net.taler.wallet.compose.TalerSurface 38 import net.taler.wallet.compose.collectAsStateLifecycleAware 39 import net.taler.wallet.showError 40 41 class IncomingPushPaymentFragment : Fragment() { 42 private val model: MainViewModel by activityViewModels() 43 private val peerManager get() = model.peerManager 44 private val exchangeManager get() = model.exchangeManager 45 private val transactionManager get() = model.transactionManager 46 47 override fun onCreateView( 48 inflater: LayoutInflater, 49 container: ViewGroup?, 50 savedInstanceState: Bundle?, 51 ): View { 52 return ComposeView(requireContext()).apply { 53 setContent { 54 TalerSurface { 55 val state = peerManager.incomingPushState.collectAsStateLifecycleAware() 56 IncomingComposable(state, incomingPush) { terms -> 57 if (terms is IncomingTosReview) { 58 val args = bundleOf("exchangeBaseUrl" to terms.exchangeBaseUrl) 59 findNavController().navigate(R.id.action_global_reviewExchangeTOS, args) 60 } else { 61 peerManager.confirmPeerPushCredit(terms) 62 } 63 } 64 } 65 } 66 } 67 } 68 69 override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 70 super.onViewCreated(view, savedInstanceState) 71 lifecycleScope.launch { 72 viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { 73 peerManager.incomingPushState.collect { 74 Log.d(TAG, "incomingPushState is $it") 75 if (it is IncomingAccepted) { 76 if (transactionManager.selectTransaction(it.transactionId)) { 77 findNavController().navigate(R.id.action_global_transactionPeer) 78 } else { 79 findNavController().navigate(R.id.action_global_main) 80 } 81 } else if (it is IncomingError) { 82 if (model.devMode.value == true) { 83 showError(it.info) 84 } else { 85 showError(it.info.userFacingMsg) 86 } 87 } 88 } 89 } 90 } 91 92 exchangeManager.exchanges.observe(viewLifecycleOwner) { exchanges -> 93 peerManager.refreshPeerPushCreditTos(exchanges) 94 } 95 } 96 97 override fun onStart() { 98 super.onStart() 99 activity?.setTitle(R.string.receive_peer_payment_title) 100 } 101 }