From 4fc62fb417328191ac2fc5901d71354acf1c2540 Mon Sep 17 00:00:00 2001
From: Taras <tarassmakula@gmail.com>
Date: Tue, 22 Mar 2022 14:35:47 +0200
Subject: [PATCH] Add submitUrl param fallback

---
 app/src/main/java/com/futo/circles/core/Constants.kt |  2 ++
 .../feature/validate_email/ValidateEmailFragment.kt  |  5 +++--
 .../feature/validate_email/ValidateEmailViewModel.kt |  3 ++-
 .../data_source/ValidateEmailDataSource.kt           | 12 ++++++++++--
 4 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/app/src/main/java/com/futo/circles/core/Constants.kt b/app/src/main/java/com/futo/circles/core/Constants.kt
index b0ed95603..522dc4441 100644
--- a/app/src/main/java/com/futo/circles/core/Constants.kt
+++ b/app/src/main/java/com/futo/circles/core/Constants.kt
@@ -11,3 +11,5 @@ const val DEFAULT_TERMS_NAME = "Terms and Conditions"
 const val TERMS_URL_EXTENSION = "_matrix/consent"
 
 const val REGISTRATION_TOKEN_KEY = "org.matrix.msc3231.login.registration_token"
+
+const val VALIDATION_TOKEN_SUBMIT_URL_PREFIX = "_matrix/identity/api/v1/validate/email/submitToken"
diff --git a/app/src/main/java/com/futo/circles/feature/validate_email/ValidateEmailFragment.kt b/app/src/main/java/com/futo/circles/feature/validate_email/ValidateEmailFragment.kt
index c7bed9ff3..88611cc32 100644
--- a/app/src/main/java/com/futo/circles/feature/validate_email/ValidateEmailFragment.kt
+++ b/app/src/main/java/com/futo/circles/feature/validate_email/ValidateEmailFragment.kt
@@ -55,7 +55,8 @@ class ValidateEmailFragment : Fragment(R.layout.validate_email_fragment), HasLoa
     }
 
     private fun setupObservers() {
-        viewModel.sendCodeLiveData.observeResponse(this) { validationCodeSentState() }
+        viewModel.sendCodeLiveData.observeResponse(this,
+            success = { validationCodeSentState() })
         viewModel.validateEmailLiveData.observeResponse(this)
     }
 
@@ -64,7 +65,7 @@ class ValidateEmailFragment : Fragment(R.layout.validate_email_fragment), HasLoa
     private fun validationCodeSentState() {
         showSuccess(getString(R.string.validation_code_sent_to_format, getEmailInput()))
         binding.tilValidationCode.visible()
-        binding.btnValidate.isEnabled = false
+        binding.btnValidate.isEnabled = binding.tilValidationCode.editText?.text.isNullOrEmpty().not()
     }
 
 }
\ No newline at end of file
diff --git a/app/src/main/java/com/futo/circles/feature/validate_email/ValidateEmailViewModel.kt b/app/src/main/java/com/futo/circles/feature/validate_email/ValidateEmailViewModel.kt
index a8875c9ba..6f9b78421 100644
--- a/app/src/main/java/com/futo/circles/feature/validate_email/ValidateEmailViewModel.kt
+++ b/app/src/main/java/com/futo/circles/feature/validate_email/ValidateEmailViewModel.kt
@@ -7,12 +7,13 @@ import com.futo.circles.extensions.Response
 import com.futo.circles.extensions.launchBg
 import com.futo.circles.feature.validate_email.data_source.ValidateEmailDataSource
 import org.matrix.android.sdk.api.auth.registration.RegistrationResult
+import org.matrix.android.sdk.internal.auth.registration.AddThreePidRegistrationResponse
 
 class ValidateEmailViewModel(
     private val dataSource: ValidateEmailDataSource
 ) : ViewModel() {
 
-    val sendCodeLiveData = MutableLiveData<Response<RegistrationResult>>()
+    val sendCodeLiveData = MutableLiveData<Response<AddThreePidRegistrationResponse>>()
     val validateEmailLiveData = SingleEventLiveData<Response<RegistrationResult>>()
 
 
diff --git a/app/src/main/java/com/futo/circles/feature/validate_email/data_source/ValidateEmailDataSource.kt b/app/src/main/java/com/futo/circles/feature/validate_email/data_source/ValidateEmailDataSource.kt
index 8df473074..4c6e24996 100644
--- a/app/src/main/java/com/futo/circles/feature/validate_email/data_source/ValidateEmailDataSource.kt
+++ b/app/src/main/java/com/futo/circles/feature/validate_email/data_source/ValidateEmailDataSource.kt
@@ -1,11 +1,14 @@
 package com.futo.circles.feature.validate_email.data_source
 
+import com.futo.circles.BuildConfig
+import com.futo.circles.core.VALIDATION_TOKEN_SUBMIT_URL_PREFIX
 import com.futo.circles.extensions.Response
 import com.futo.circles.extensions.createResult
 import com.futo.circles.feature.sign_up.data_source.SignUpDataSource
 import com.futo.circles.provider.MatrixInstanceProvider
 import org.matrix.android.sdk.api.auth.registration.RegisterThreePid
 import org.matrix.android.sdk.api.auth.registration.RegistrationResult
+import org.matrix.android.sdk.internal.auth.registration.AddThreePidRegistrationResponse
 
 class ValidateEmailDataSource(
     private val signUpDataSource: SignUpDataSource
@@ -15,12 +18,17 @@ class ValidateEmailDataSource(
         MatrixInstanceProvider.matrix.authenticationService().getRegistrationWizard()
     }
 
-    suspend fun sendValidationCode(email: String): Response<RegistrationResult> =
+    suspend fun sendValidationCode(email: String): Response<AddThreePidRegistrationResponse> =
         createResult { wizard.addThreePid(RegisterThreePid.Email(email)) }
 
 
     suspend fun validateEmail(code: String): Response<RegistrationResult> {
-        val result = createResult { wizard.handleValidateThreePid(code) }
+        val result = createResult {
+            wizard.handleValidateThreePid(
+                code,
+                BuildConfig.MATRIX_HOME_SERVER_URL + VALIDATION_TOKEN_SUBMIT_URL_PREFIX
+            )
+        }
         (result as? Response.Success)?.let { signUpDataSource.stageCompleted(result.data) }
         return result
     }
-- 
GitLab