cashless2ecash

cashless2ecash: pay with cards for digital cash (experimental)
Log | Files | Refs | README

ManageActivity.kt (5669B)


      1 // This file is part of taler-cashless2ecash.
      2 // Copyright (C) 2024 Joel Häberli
      3 //
      4 // taler-cashless2ecash is free software: you can redistribute it and/or modify it
      5 // under the terms of the GNU Affero General Public License as published
      6 // by the Free Software Foundation, either version 3 of the License,
      7 // or (at your option) any later version.
      8 //
      9 // taler-cashless2ecash is distributed in the hope that it will be useful, but
     10 // WITHOUT ANY WARRANTY; without even the implied warranty of
     11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12 // Affero General Public License for more details.
     13 //
     14 // You should have received a copy of the GNU Affero General Public License
     15 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16 //
     17 // SPDX-License-Identifier: AGPL3.0-or-later
     18 
     19 package ch.bfh.habej2.wallee_c2ec.withdrawal
     20 
     21 import android.os.Bundle
     22 import androidx.activity.ComponentActivity
     23 import androidx.activity.compose.setContent
     24 import androidx.compose.foundation.layout.Column
     25 import androidx.compose.foundation.layout.fillMaxSize
     26 import androidx.compose.material3.MaterialTheme
     27 import androidx.compose.material3.Surface
     28 import androidx.compose.runtime.mutableStateOf
     29 import androidx.compose.ui.Alignment
     30 import androidx.compose.ui.Modifier
     31 import ch.bfh.habej2.wallee_c2ec.client.wallee.WalleeResponseHandler
     32 import ch.bfh.habej2.wallee_c2ec.ui.theme.Walleec2ecTheme
     33 import com.squareup.moshi.Json
     34 import com.squareup.moshi.Moshi
     35 import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
     36 import com.wallee.android.till.sdk.ApiClient
     37 import kotlinx.coroutines.asCoroutineDispatcher
     38 import okhttp3.MediaType.Companion.toMediaType
     39 import okhttp3.OkHttpClient
     40 import okhttp3.Request
     41 import okhttp3.RequestBody.Companion.toRequestBody
     42 import java.util.concurrent.Executors
     43 
     44 class ManageActivity : ComponentActivity()  {
     45 
     46     private lateinit var walleeClient: ApiClient
     47 
     48     private var unbound = false
     49     
     50     companion object {
     51 
     52         var SIM_WALLET_ENABLED = mutableStateOf(false)
     53 
     54         var INSTANT_SETTLEMENT_ENABLED = mutableStateOf(false)
     55 
     56         private data class Reg(
     57             @Json(name = "reserve_pub") val reservePubKey: String,
     58             @Json(name = "selected_exchange") val selectedExchange: String
     59         )
     60 
     61         // this is only for testing and simulates a wallet registering parameters
     62         fun simulateParameterRegistration(
     63             exchangeBankIntegrationApiUrl: String,
     64             wopid: String,
     65 
     66         ) {
     67             println("simulating wallet parameter selection")
     68             // we will just use the wopid as reserve pub key...
     69             val reg = Moshi.Builder()
     70                 .add(KotlinJsonAdapterFactory())
     71                 .build()
     72                 .adapter(Reg::class.java)
     73                 .toJson(Reg(wopid, exchangeBankIntegrationApiUrl))
     74                 .toRequestBody("application/json".toMediaType())
     75             Executors.newSingleThreadExecutor().asCoroutineDispatcher().executor.execute {
     76                 val url = "$exchangeBankIntegrationApiUrl/withdrawal-operation/$wopid"
     77                 println("requesting $url")
     78                 val req = Request.Builder()
     79                     .post(reg)
     80                     .url(url)
     81                     .build()
     82                 OkHttpClient.Builder().build().newCall(req).execute().use {
     83                     println("registered parameters: HTTP ${it.code}")
     84                 }
     85             }
     86         }
     87     }
     88 
     89     override fun onCreate(savedInstanceState: Bundle?) {
     90         super.onCreate(savedInstanceState)
     91         setContent {
     92             Walleec2ecTheme {
     93                 // A surface container using the 'background' color from the theme
     94                 Surface(
     95                     modifier = Modifier.fillMaxSize(),
     96                     color = MaterialTheme.colorScheme.background
     97                 ) {
     98                     Column(
     99                         horizontalAlignment = Alignment.CenterHorizontally
    100                     ) {
    101 
    102                         TalerLogo()
    103 
    104                         TalerButton(
    105                             text = "Execute Final Balance",
    106                             onClick = { walleeClient.executeFinalBalance() }
    107                         )
    108 
    109                         TalerButton(
    110                             text = "${if (INSTANT_SETTLEMENT_ENABLED.value) "Disable" else "Enable"} instant settlement",
    111                             onClick = { INSTANT_SETTLEMENT_ENABLED.value = !INSTANT_SETTLEMENT_ENABLED.value }
    112                         )
    113 
    114                         TalerButton(
    115                             text = "Back",
    116                             onClick = {
    117                                 onDestroy()
    118                                 this@ManageActivity.finish()
    119                             }
    120                         )
    121 
    122 //                      The following few lines are meant for local testing, debugging etc.
    123 //                        TestTransactionScreen(walleeClient)
    124 //                        TalerButton(
    125 //                            text = "${if (SIM_WALLET_ENABLED.value) "disable" else "enable"} wallet simulation",
    126 //                            onClick = { SIM_WALLET_ENABLED.value = !SIM_WALLET_ENABLED.value }
    127 //                        )
    128                     }
    129                 }
    130             }
    131         }
    132 
    133         val responseHandler = WalleeResponseHandler(this)
    134         walleeClient = ApiClient(responseHandler)
    135         walleeClient.bind(this)
    136         walleeClient.checkApiServiceCompatibility()
    137     }
    138 
    139     override fun onDestroy() {
    140         super.onDestroy()
    141         if (!unbound) {
    142             walleeClient.unbind(this)
    143             unbound = true
    144         }
    145     }
    146 }