taler-android

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

OutgoingPullScreen.kt (4269B)


      1 /*
      2  * This file is part of GNU Taler
      3  * (C) 2026 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 androidx.compose.foundation.layout.fillMaxSize
     20 import androidx.compose.foundation.layout.padding
     21 import androidx.compose.material3.Text
     22 import androidx.compose.runtime.Composable
     23 import androidx.compose.runtime.DisposableEffect
     24 import androidx.compose.runtime.LaunchedEffect
     25 import androidx.compose.runtime.getValue
     26 import androidx.compose.runtime.livedata.observeAsState
     27 import androidx.compose.runtime.remember
     28 import androidx.compose.ui.Modifier
     29 import androidx.compose.ui.res.stringResource
     30 import net.taler.wallet.NavigateCallback
     31 import net.taler.wallet.R
     32 import net.taler.wallet.WalletDestination
     33 import net.taler.wallet.backend.TalerErrorInfo
     34 import net.taler.wallet.compose.GlobalScaffold
     35 import net.taler.wallet.compose.TalerSurface
     36 import net.taler.wallet.compose.collectAsStateLifecycleAware
     37 import net.taler.wallet.main.MainViewModel
     38 import net.taler.wallet.main.ViewMode
     39 
     40 @Composable
     41 fun OutgoingPullScreen(
     42     model: MainViewModel,
     43     onNavigate: NavigateCallback,
     44     onNavigateBack: () -> Unit,
     45     onShowError: (TalerErrorInfo) -> Unit,
     46 ) {
     47     val peerManager = model.peerManager
     48     val transactionManager = model.transactionManager
     49     val balanceManager = model.balanceManager
     50     val exchangeManager = model.exchangeManager
     51 
     52     val state by peerManager.pullState.collectAsStateLifecycleAware()
     53     val viewMode by model.viewMode.collectAsStateLifecycleAware()
     54     val devMode by model.devMode.observeAsState(false)
     55     val exchanges by exchangeManager.exchanges.observeAsState()
     56 
     57     DisposableEffect(Unit) {
     58         onDispose {
     59             peerManager.resetPullPayment()
     60         }
     61     }
     62 
     63     LaunchedEffect(exchanges) {
     64         exchanges?.let {
     65             peerManager.refreshPeerPullCreditTos(it)
     66         }
     67     }
     68 
     69     LaunchedEffect(state) {
     70         val s = state
     71         if (s is OutgoingResponse) {
     72             if (transactionManager.selectTransaction(s.transactionId)) {
     73                 onNavigate(WalletDestination.TransactionPeer, true)
     74             } else {
     75                 onNavigateBack()
     76             }
     77         }
     78 
     79         if (s is OutgoingError) {
     80             onShowError(s.info)
     81         }
     82     }
     83 
     84     TalerSurface {
     85         GlobalScaffold(
     86             model = model,
     87             modifier = Modifier.fillMaxSize(),
     88             title = { Text(stringResource(R.string.receive_peer_title)) },
     89             onNavigateBack = onNavigateBack,
     90         ) { paddingValues ->
     91             OutgoingPullComposable(
     92                 modifier = Modifier.padding(paddingValues),
     93                 state = state,
     94                 onCreateInvoice = { amount, summary, hours, exchangeBaseUrl ->
     95                     peerManager.initiatePeerPullCredit(amount.amount, summary, hours, exchangeBaseUrl)
     96                 },
     97                 onTosAccept = { exchangeBaseUrl ->
     98                     onNavigate(WalletDestination.ReviewExchangeTOS(exchangeBaseUrl), false)
     99                 },
    100                 defaultScope = remember { (viewMode as? ViewMode.Transactions)?.selectedScope },
    101                 scopes = balanceManager.getScopes(true),
    102                 devMode = devMode,
    103                 getCurrencySpec = exchangeManager::getSpecForScopeInfo,
    104                 checkPeerPullCredit = { amount, loading ->
    105                     model.selectScope(amount.scope)
    106                     peerManager.checkPeerPullCredit(
    107                         amount.amount,
    108                         scopeInfo = amount.scope,
    109                         loading = loading,
    110                     )
    111                 },
    112             )
    113         }
    114     }
    115 }