From 13ae15491fc7a6f3289a06eeda570db3fce79af0 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 16 Feb 2022 19:35:34 +0100 Subject: [PATCH] Get feedback on request --- .../dccaggregator/activities/CertsManager.kt | 19 +++++++++++---- .../viewmodel/CertsManagerViewModel.kt | 23 ++++++++++++++----- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/org/communiquons/dccaggregator/activities/CertsManager.kt b/app/src/main/java/org/communiquons/dccaggregator/activities/CertsManager.kt index 0f3e9ed..45566b9 100644 --- a/app/src/main/java/org/communiquons/dccaggregator/activities/CertsManager.kt +++ b/app/src/main/java/org/communiquons/dccaggregator/activities/CertsManager.kt @@ -40,6 +40,8 @@ class CertsManager : BaseActivity() { private val TAG = CertsManager::class.java.canonicalName + private var loadingDialog: AlertDialog? = null + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -61,6 +63,17 @@ class CertsManager : BaseActivity() { processGivenCertificate(this) } + // Follow request progress + viewModel.reqStatus.observe(this) { + if (it == "") return@observe + + loadingDialog?.cancel() + if (it != null) + Toast.makeText(this, it, Toast.LENGTH_SHORT).show() + else + finish() + } + refreshList() } @@ -160,18 +173,14 @@ class CertsManager : BaseActivity() { } private fun generateNewCertificate() { - val dialog = AlertDialog.Builder(this) + loadingDialog = AlertDialog.Builder(this) .setView(R.layout.generating_dialog) .show().apply { setCancelable(false) setCanceledOnTouchOutside(false) } - // TODO: hum hum viewModel.requestCertificate() - - // Close dialog - dialog.cancel() } } \ No newline at end of file diff --git a/app/src/main/java/org/communiquons/dccaggregator/viewmodel/CertsManagerViewModel.kt b/app/src/main/java/org/communiquons/dccaggregator/viewmodel/CertsManagerViewModel.kt index 08710b0..2e05395 100644 --- a/app/src/main/java/org/communiquons/dccaggregator/viewmodel/CertsManagerViewModel.kt +++ b/app/src/main/java/org/communiquons/dccaggregator/viewmodel/CertsManagerViewModel.kt @@ -1,6 +1,8 @@ package org.communiquons.dccaggregator.viewmodel import android.util.Log +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import kotlinx.coroutines.Dispatchers @@ -9,26 +11,35 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import org.communiquons.dccaggregator.apis.TousAntiCovidAPIClient import org.communiquons.dccaggregator.helper.CertificatesList -import java.lang.Exception class CertsManagerViewModel() : ViewModel() { lateinit var certsManager: CertificatesList + private val _reqStatus = MutableLiveData("") + val reqStatus: LiveData = _reqStatus + fun requestCertificate(): Job { return viewModelScope.launch(Dispatchers.IO) { try { - return@launch withContext(Dispatchers.IO) { + withContext(Dispatchers.IO) { // First, fetch current signing key val config = TousAntiCovidAPIClient().getConfig() - Log.d("testpk", config.activityPassGenerationServerPublicKey) + Log.d("Server Primary Key", config.activityPassGenerationServerPublicKey) + + throw java.lang.Exception("TODO: send request") + + withContext(Dispatchers.Main) { + _reqStatus.value = null + } } } catch (e: Exception) { e.printStackTrace() - // return false - } - // return true + withContext(Dispatchers.Main) { + _reqStatus.value = e.message ?: "Request failed!" + } + } } }