From 6a01b3013373a28b5ee66687d365a2b2eab7de5c Mon Sep 17 00:00:00 2001
From: ialokim <ialokim@mailbox.org>
Date: Fri, 14 Jan 2022 17:27:18 +0100
Subject: [PATCH] show error to user in case of failed account creation

---
 .../briar/desktop/login/ErrorScreen.kt        | 26 ++++++++++++-------
 .../briar/desktop/login/ErrorViewHolder.kt    |  6 +++--
 .../desktop/login/RegistrationViewHolder.kt   |  5 ++--
 .../briar/desktop/login/StartupViewModel.kt   |  9 ++++---
 .../resources/strings/BriarDesktop.properties |  1 +
 5 files changed, 31 insertions(+), 16 deletions(-)

diff --git a/src/main/kotlin/org/briarproject/briar/desktop/login/ErrorScreen.kt b/src/main/kotlin/org/briarproject/briar/desktop/login/ErrorScreen.kt
index 1c344a4b1b..3744c8608d 100644
--- a/src/main/kotlin/org/briarproject/briar/desktop/login/ErrorScreen.kt
+++ b/src/main/kotlin/org/briarproject/briar/desktop/login/ErrorScreen.kt
@@ -37,11 +37,14 @@ import org.briarproject.briar.desktop.utils.InternationalizationUtils.i18n
 import org.briarproject.briar.desktop.utils.PreviewUtils.preview
 
 fun main() = preview {
-    var error by remember { mutableStateOf(SUCCESS) }
+    var error: ErrorViewHolder.Error by remember { mutableStateOf(RegistrationViewHolder.RegistrationError) }
 
     Row(horizontalArrangement = spacedBy(8.dp)) {
+        Button(onClick = { error = RegistrationViewHolder.RegistrationError }) {
+            Text("Registration")
+        }
         for (e in StartResult.values().filterNot { it in listOf(SUCCESS, ALREADY_RUNNING) }) {
-            Button(onClick = { error = e }) {
+            Button(onClick = { error = StartupViewModel.StartingError(e) }) {
                 Text(e.name.removeSuffix("_ERROR"))
             }
         }
@@ -56,7 +59,7 @@ fun ErrorScreen(viewHolder: ErrorViewHolder) =
 
 @Composable
 fun ErrorScreen(
-    error: StartResult,
+    error: ErrorViewHolder.Error,
     onBackButton: () -> Unit,
 ) = Surface {
     IconButton(onClick = onBackButton) {
@@ -78,12 +81,17 @@ fun ErrorScreen(
         Text(i18n("sorry"), style = MaterialTheme.typography.h5)
 
         val text = when (error) {
-            CLOCK_ERROR -> i18n("startup.failed.clock_error")
-            DB_ERROR -> i18n("startup.failed.db_error")
-            DATA_TOO_OLD_ERROR -> i18n("startup.failed.data_too_old_error")
-            DATA_TOO_NEW_ERROR -> i18n("startup.failed.data_too_new_error")
-            SERVICE_ERROR -> i18n("startup.failed.service_error")
-            else -> ""
+            is RegistrationViewHolder.RegistrationError -> i18n("startup.failed.registration")
+            is StartupViewModel.StartingError -> {
+                when (error.error) {
+                    CLOCK_ERROR -> i18n("startup.failed.clock_error")
+                    DB_ERROR -> i18n("startup.failed.db_error")
+                    DATA_TOO_OLD_ERROR -> i18n("startup.failed.data_too_old_error")
+                    DATA_TOO_NEW_ERROR -> i18n("startup.failed.data_too_new_error")
+                    SERVICE_ERROR -> i18n("startup.failed.service_error")
+                    else -> ""
+                }
+            }
         }
         Text(
             text = text,
diff --git a/src/main/kotlin/org/briarproject/briar/desktop/login/ErrorViewHolder.kt b/src/main/kotlin/org/briarproject/briar/desktop/login/ErrorViewHolder.kt
index 1f43078d38..8d540501ab 100644
--- a/src/main/kotlin/org/briarproject/briar/desktop/login/ErrorViewHolder.kt
+++ b/src/main/kotlin/org/briarproject/briar/desktop/login/ErrorViewHolder.kt
@@ -4,6 +4,8 @@ import org.briarproject.bramble.api.lifecycle.LifecycleManager
 
 class ErrorViewHolder(
     private val viewModel: StartupViewModel,
-    val error: LifecycleManager.StartResult,
+    val error: Error,
     val onBackButton: () -> Unit,
-) : StartupViewModel.ViewHolder
+) : StartupViewModel.ViewHolder {
+    sealed interface Error
+}
diff --git a/src/main/kotlin/org/briarproject/briar/desktop/login/RegistrationViewHolder.kt b/src/main/kotlin/org/briarproject/briar/desktop/login/RegistrationViewHolder.kt
index e83d4e6b3d..ca2a309376 100644
--- a/src/main/kotlin/org/briarproject/briar/desktop/login/RegistrationViewHolder.kt
+++ b/src/main/kotlin/org/briarproject/briar/desktop/login/RegistrationViewHolder.kt
@@ -24,6 +24,8 @@ class RegistrationViewHolder(
         private val LOG = KotlinLogging.logger {}
     }
 
+    object RegistrationError : ErrorViewHolder.Error
+
     enum class State {
         INSERT_NICKNAME, INSERT_PASSWORD, CREATING, CREATED
     }
@@ -100,8 +102,7 @@ class RegistrationViewHolder(
                 viewModel.startBriarCore()
             } else {
                 LOG.warn { "Failed to create account" }
-                _state.value = INSERT_NICKNAME
-                // todo: show (meaningful) error to user
+                viewModel.showError(RegistrationError)
             }
         }
     }
diff --git a/src/main/kotlin/org/briarproject/briar/desktop/login/StartupViewModel.kt b/src/main/kotlin/org/briarproject/briar/desktop/login/StartupViewModel.kt
index 9f81218940..bf42613767 100644
--- a/src/main/kotlin/org/briarproject/briar/desktop/login/StartupViewModel.kt
+++ b/src/main/kotlin/org/briarproject/briar/desktop/login/StartupViewModel.kt
@@ -36,6 +36,9 @@ constructor(
         fun lifecycleStateChanged(s: LifecycleManager.LifecycleState) {}
     }
 
+    class StartingError(val error: LifecycleManager.StartResult):
+        ErrorViewHolder.Error
+
     private val _mode = mutableStateOf(decideMode())
     val mode = _mode.asState()
 
@@ -59,10 +62,10 @@ constructor(
         _mode.value = makeRegistration()
     }
 
-    private fun makeError(error: LifecycleManager.StartResult) = ErrorViewHolder(
+    private fun makeError(error: ErrorViewHolder.Error) = ErrorViewHolder(
         this, error, onBackButton = { _mode.value = decideMode() }
     )
-    fun showError(error: LifecycleManager.StartResult) {
+    fun showError(error: ErrorViewHolder.Error) {
         _mode.value = makeError(error)
     }
 
@@ -79,7 +82,7 @@ constructor(
             ALREADY_RUNNING -> LOG.info { "Already running" }
             else -> {
                 LOG.warn { "Startup failed: $result" }
-                showError(result)
+                showError(StartingError(result))
             }
         }
     }
diff --git a/src/main/resources/strings/BriarDesktop.properties b/src/main/resources/strings/BriarDesktop.properties
index 39f99f3c24..4f6b716a71 100644
--- a/src/main/resources/strings/BriarDesktop.properties
+++ b/src/main/resources/strings/BriarDesktop.properties
@@ -139,6 +139,7 @@ startup.error.decryption.text=Briar cannot check your password. Please try reboo
 startup.password_forgotten.button=I have forgotten my password
 startup.password_forgotten.title=Lost Password
 startup.password_forgotten.text=Your Briar account is stored encrypted on your device, not in the cloud, so we can't reset your password. Would you like to delete your account and start again?\n\nCaution: Your identities, contacts and messages will be permanently lost.
+startup.failed.registration=Briar was unable to create your account.\n\nPlease upgrade to the latest version and try again.
 startup.failed.clock_error=Briar was unable to start because your device's clock is wrong.\n\nPlease set your device's clock to the right time and try again.
 startup.failed.db_error=Briar was unable to open the database containing your account, your contacts and your messages.\n\nPlease upgrade to the latest version of the app and try again, or set up a new account by choosing 'I have forgotten my password' at the password prompt.
 startup.failed.data_too_old_error=Your account was created with an old version of this app and cannot be opened with this version.\n\nYou must either reinstall the old version or set up a new account by choosing 'I have forgotten my password' at the password prompt.
-- 
GitLab