From c9fb036798fc533a07b4b75386b51151b31f8be0 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Mon, 20 Jul 2020 16:37:46 -0300 Subject: [pos] create merchant-lib and move first v1 API endpoint there --- merchant-terminal/build.gradle | 8 +++++- .../java/net/taler/merchantpos/MainViewModel.kt | 4 ++- .../net/taler/merchantpos/config/ConfigManager.kt | 31 +++++++++++++--------- merchant-terminal/src/main/res/values/strings.xml | 1 + 4 files changed, 30 insertions(+), 14 deletions(-) (limited to 'merchant-terminal') diff --git a/merchant-terminal/build.gradle b/merchant-terminal/build.gradle index 8a7eac7..2ba1a66 100644 --- a/merchant-terminal/build.gradle +++ b/merchant-terminal/build.gradle @@ -46,10 +46,16 @@ android { // https://github.com/material-components/material-components-android/issues/504 ignore "WrongConstant" } + + packagingOptions { + exclude 'META-INF/common.kotlin_module' + exclude 'META-INF/*.kotlin_module' + } } dependencies { implementation project(":taler-kotlin-common") + implementation project(":merchant-lib") implementation 'com.google.android.material:material:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' @@ -63,7 +69,7 @@ dependencies { // HTTP Requests implementation 'com.android.volley:volley:1.1.1' - implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0" + implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0" // JSON parsing and serialization implementation "com.fasterxml.jackson.module:jackson-module-kotlin:2.10.2" diff --git a/merchant-terminal/src/main/java/net/taler/merchantpos/MainViewModel.kt b/merchant-terminal/src/main/java/net/taler/merchantpos/MainViewModel.kt index 3fe472d..2dd2c24 100644 --- a/merchant-terminal/src/main/java/net/taler/merchantpos/MainViewModel.kt +++ b/merchant-terminal/src/main/java/net/taler/merchantpos/MainViewModel.kt @@ -23,6 +23,7 @@ import com.android.volley.toolbox.Volley import com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.KotlinModule +import net.taler.merchantlib.MerchantApi import net.taler.merchantpos.config.ConfigManager import net.taler.merchantpos.history.HistoryManager import net.taler.merchantpos.history.RefundManager @@ -31,13 +32,14 @@ import net.taler.merchantpos.payment.PaymentManager class MainViewModel(app: Application) : AndroidViewModel(app) { + private val api = MerchantApi() private val mapper = ObjectMapper() .registerModule(KotlinModule()) .configure(FAIL_ON_UNKNOWN_PROPERTIES, false) private val queue = Volley.newRequestQueue(app) val orderManager = OrderManager(app, mapper) - val configManager = ConfigManager(app, viewModelScope, mapper, queue).apply { + val configManager = ConfigManager(app, viewModelScope, api, mapper, queue).apply { addConfigurationReceiver(orderManager) } val paymentManager = PaymentManager(configManager, queue, mapper) diff --git a/merchant-terminal/src/main/java/net/taler/merchantpos/config/ConfigManager.kt b/merchant-terminal/src/main/java/net/taler/merchantpos/config/ConfigManager.kt index 171cf28..eee7905 100644 --- a/merchant-terminal/src/main/java/net/taler/merchantpos/config/ConfigManager.kt +++ b/merchant-terminal/src/main/java/net/taler/merchantpos/config/ConfigManager.kt @@ -34,10 +34,14 @@ import com.fasterxml.jackson.module.kotlin.readValue import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import net.taler.merchantlib.ConfigResponse +import net.taler.merchantlib.MerchantApi import net.taler.merchantpos.LogErrorListener import net.taler.merchantpos.R import org.json.JSONObject +private const val VERSION = "0:0:0" + private const val SETTINGS_NAME = "taler-merchant-terminal" private const val SETTINGS_CONFIG_URL = "configUrl" @@ -60,6 +64,7 @@ interface ConfigurationReceiver { class ConfigManager( private val context: Context, private val scope: CoroutineScope, + private val api: MerchantApi, private val mapper: ObjectMapper, private val queue: RequestQueue ) { @@ -114,25 +119,27 @@ class ConfigManager( return } - val params = mapOf("instance" to merchantConfig.instance) - val req = MerchantRequest(GET, merchantConfig, "config", params, null, - Listener { onMerchantConfigReceived(config, json, merchantConfig, it) }, - LogErrorListener { onNetworkError(it) } - ) - queue.add(req) + scope.launch(Dispatchers.IO) { + val configResponse = api.getConfig(merchantConfig.baseUrl, merchantConfig.apiKey) + onMerchantConfigReceived(config, json, merchantConfig, configResponse) + } } private fun onMerchantConfigReceived( newConfig: Config?, configJson: JSONObject, merchantConfig: MerchantConfig, - json: JSONObject + configResponse: ConfigResponse ) = scope.launch(Dispatchers.Default) { - val currency = json.getString("currency") - + // TODO do real matching + if (VERSION != configResponse.version) { + val str = context.getString(R.string.config_error_version) + mConfigUpdateResult.postValue(ConfigUpdateResult.Error(str)) + return@launch + } for (receiver in configurationReceivers) { val result = try { - receiver.onConfigurationReceived(configJson, currency) + receiver.onConfigurationReceived(configJson, configResponse.currency) } catch (e: Exception) { Log.e(TAG, "Error handling configuration by ${receiver::class.java.simpleName}", e) context.getString(R.string.config_error_unknown) @@ -146,8 +153,8 @@ class ConfigManager( config = it saveConfig(it) } - this@ConfigManager.merchantConfig = merchantConfig.copy(currency = currency) - mConfigUpdateResult.postValue(ConfigUpdateResult.Success(currency)) + this@ConfigManager.merchantConfig = merchantConfig.copy(currency = configResponse.currency) + mConfigUpdateResult.postValue(ConfigUpdateResult.Success(configResponse.currency)) } fun forgetPassword() { diff --git a/merchant-terminal/src/main/res/values/strings.xml b/merchant-terminal/src/main/res/values/strings.xml index b3dcd8d..931f31c 100644 --- a/merchant-terminal/src/main/res/values/strings.xml +++ b/merchant-terminal/src/main/res/values/strings.xml @@ -22,6 +22,7 @@ Password Fetch configuration Error: Invalid username or password + Error: Incompatible backend version Error: Could not connect to configuration server Error: No valid product category found Error: The configuration JSON is malformed -- cgit v1.2.3