From a229b33997842a66295aa4d8dfb5dab6d2f769df Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Wed, 1 Jun 2022 13:31:38 +0200 Subject: use new annotation for polymorphic serialization --- .../net/taler/wallet/backend/WalletBackendApi.kt | 5 --- .../net/taler/wallet/backend/WalletResponse.kt | 6 +-- .../net/taler/wallet/payment/PaymentResponses.kt | 8 ++-- .../net/taler/wallet/settings/SettingsFragment.kt | 46 +++++++++++++++++----- 4 files changed, 44 insertions(+), 21 deletions(-) (limited to 'wallet/src/main/java') diff --git a/wallet/src/main/java/net/taler/wallet/backend/WalletBackendApi.kt b/wallet/src/main/java/net/taler/wallet/backend/WalletBackendApi.kt index a2b48b6..76aceb2 100644 --- a/wallet/src/main/java/net/taler/wallet/backend/WalletBackendApi.kt +++ b/wallet/src/main/java/net/taler/wallet/backend/WalletBackendApi.kt @@ -30,7 +30,6 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import kotlinx.serialization.KSerializer import kotlinx.serialization.json.Json -import net.taler.lib.android.CustomClassDiscriminator import net.taler.wallet.backend.WalletBackendService.Companion.MSG_COMMAND import net.taler.wallet.backend.WalletBackendService.Companion.MSG_NOTIFY import net.taler.wallet.backend.WalletBackendService.Companion.MSG_REPLY @@ -42,7 +41,6 @@ import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.atomic.AtomicInteger import kotlin.coroutines.resume import kotlin.coroutines.suspendCoroutine -import kotlin.reflect.full.companionObjectInstance class WalletBackendApi( private val app: Application, @@ -152,9 +150,6 @@ class WalletBackendApi( suspendCoroutine { cont -> val json = Json { ignoreUnknownKeys = true - (T::class.companionObjectInstance as? CustomClassDiscriminator)?.let { - classDiscriminator = it.discriminator - } } sendRequest(operation, args?.invoke(JSONObject())) { isError, message -> val response = try { diff --git a/wallet/src/main/java/net/taler/wallet/backend/WalletResponse.kt b/wallet/src/main/java/net/taler/wallet/backend/WalletResponse.kt index 1071e9a..e52fd4f 100644 --- a/wallet/src/main/java/net/taler/wallet/backend/WalletResponse.kt +++ b/wallet/src/main/java/net/taler/wallet/backend/WalletResponse.kt @@ -32,13 +32,13 @@ sealed class WalletResponse { @Serializable @SerialName("response") data class Success( - val result: T + val result: T, ) : WalletResponse() @Serializable @SerialName("error") data class Error( - val error: TalerErrorInfo + val error: TalerErrorInfo, ) : WalletResponse() fun onSuccess(block: (result: T) -> Unit): WalletResponse { @@ -67,7 +67,7 @@ data class TalerErrorInfo( // Error details @Serializable(JSONObjectDeserializer::class) - val details: JSONObject? = null + val details: JSONObject? = null, ) { val userFacingMsg: String get() { diff --git a/wallet/src/main/java/net/taler/wallet/payment/PaymentResponses.kt b/wallet/src/main/java/net/taler/wallet/payment/PaymentResponses.kt index fb206be..7e03472 100644 --- a/wallet/src/main/java/net/taler/wallet/payment/PaymentResponses.kt +++ b/wallet/src/main/java/net/taler/wallet/payment/PaymentResponses.kt @@ -16,18 +16,18 @@ package net.taler.wallet.payment +import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable +import kotlinx.serialization.json.JsonClassDiscriminator import net.taler.common.Amount import net.taler.common.ContractTerms -import net.taler.lib.android.CustomClassDiscriminator import net.taler.wallet.backend.TalerErrorInfo +@OptIn(ExperimentalSerializationApi::class) @Serializable +@JsonClassDiscriminator("status") sealed class PreparePayResponse { - companion object : CustomClassDiscriminator { - override val discriminator: String = "status" - } @Serializable @SerialName("payment-possible") diff --git a/wallet/src/main/java/net/taler/wallet/settings/SettingsFragment.kt b/wallet/src/main/java/net/taler/wallet/settings/SettingsFragment.kt index bd6ce1a..1a17d78 100644 --- a/wallet/src/main/java/net/taler/wallet/settings/SettingsFragment.kt +++ b/wallet/src/main/java/net/taler/wallet/settings/SettingsFragment.kt @@ -18,6 +18,8 @@ package net.taler.wallet.settings import android.os.Bundle import android.view.View +import android.widget.Toast +import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AlertDialog import androidx.fragment.app.activityViewModels import androidx.preference.Preference @@ -26,7 +28,6 @@ import androidx.preference.SwitchPreferenceCompat import com.google.android.material.snackbar.BaseTransientBottomBar.LENGTH_SHORT import com.google.android.material.snackbar.Snackbar import net.taler.common.showError -import net.taler.common.showLogViewer import net.taler.common.toRelativeTime import net.taler.wallet.BuildConfig.FLAVOR import net.taler.wallet.BuildConfig.VERSION_CODE @@ -64,6 +65,11 @@ class SettingsFragment : PreferenceFragmentCompat() { ) } + val createDocumentActivity = + registerForActivityResult(ActivityResultContracts.CreateDocument()) { uri -> + + } + override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { setPreferencesFromResource(R.xml.settings_main, rootKey) prefBackup = findPreference("pref_backup")!! @@ -80,12 +86,12 @@ class SettingsFragment : PreferenceFragmentCompat() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - model.lastBackup.observe(viewLifecycleOwner, { + model.lastBackup.observe(viewLifecycleOwner) { val time = it.toRelativeTime(requireContext()) prefBackup.summary = getString(R.string.backup_last, time) - }) + } - model.devMode.observe(viewLifecycleOwner, { enabled -> + model.devMode.observe(viewLifecycleOwner) { enabled -> prefDevMode.isChecked = enabled if (enabled) { prefVersionApp.summary = "$VERSION_NAME ($FLAVOR $VERSION_CODE)" @@ -94,13 +100,13 @@ class SettingsFragment : PreferenceFragmentCompat() { model.merchantVersion?.let { prefVersionMerchant.summary = it } } devPrefs.forEach { it.isVisible = enabled } - }) + } prefDevMode.setOnPreferenceChangeListener { _, newValue -> model.devMode.value = newValue as Boolean true } - withdrawManager.testWithdrawalStatus.observe(viewLifecycleOwner, { status -> + withdrawManager.testWithdrawalStatus.observe(viewLifecycleOwner) { status -> if (status == null) return@observe val loading = status is WithdrawTestStatus.Withdrawing prefWithdrawTest.isEnabled = !loading @@ -109,14 +115,37 @@ class SettingsFragment : PreferenceFragmentCompat() { requireActivity().showError(R.string.withdraw_error_test, status.message) } withdrawManager.testWithdrawalStatus.value = null - }) + } prefWithdrawTest.setOnPreferenceClickListener { withdrawManager.withdrawTestkudos() true } prefLogcat.setOnPreferenceClickListener { - requireContext().showLogViewer("taler-wallet") + val toast = + Toast.makeText(requireActivity(), "Log export currently unavailable", Toast.LENGTH_LONG) + toast.show() + +// val myPid = android.os.Process.myPid() +// val proc = Runtime.getRuntime() +// .exec(arrayOf("logcat", "-d", "--pid=$myPid", "*:V")) +// val bytes = proc.inputStream.readBytes() +// val f = File(requireActivity().getExternalFilesDir(null), +// "taler-wallet-log-${System.currentTimeMillis()}.txt") +// f.writeBytes(bytes) +// val toast = Toast.makeText(requireActivity(), "Saved to ${f.absolutePath}", Toast.LENGTH_LONG) +// toast.show() +// val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply { +// addCategory(Intent.CATEGORY_OPENABLE) +// type = "application/pdf" +// putExtra(Intent.EXTRA_TITLE, "invoice.pdf") +// +// // Optionally, specify a URI for the directory that should be opened in +// // the system file picker before your app creates the document. +// putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri) +// } +// startActivityForResult(intent, CREATE_FILE) +// ActivityResultContracts.CreateDocument true } @@ -138,5 +167,4 @@ class SettingsFragment : PreferenceFragmentCompat() { } .show() } - } -- cgit v1.2.3