From 83ccf9f2ade9df9cc03b3f96ff2504d310c471fa Mon Sep 17 00:00:00 2001
From: Taras Smakula <tarassmakula@gmail.com>
Date: Tue, 27 Feb 2024 17:14:22 +0200
Subject: [PATCH] Use new ds for login

---
 .../auth/feature/log_in/LoginDataSource.kt    | 23 +++++++++++++------
 .../log_in/stages/LoginStagesDataSource.kt    | 12 ++--------
 .../circles/auth/feature/uia/UIADataSource.kt |  4 +++-
 3 files changed, 21 insertions(+), 18 deletions(-)

diff --git a/auth/src/main/java/org/futo/circles/auth/feature/log_in/LoginDataSource.kt b/auth/src/main/java/org/futo/circles/auth/feature/log_in/LoginDataSource.kt
index 3d2e62138..e404b11f9 100644
--- a/auth/src/main/java/org/futo/circles/auth/feature/log_in/LoginDataSource.kt
+++ b/auth/src/main/java/org/futo/circles/auth/feature/log_in/LoginDataSource.kt
@@ -3,8 +3,16 @@ package org.futo.circles.auth.feature.log_in
 import android.content.Context
 import dagger.hilt.android.qualifiers.ApplicationContext
 import org.futo.circles.auth.R
-import org.futo.circles.auth.feature.log_in.stages.LoginStagesDataSource
-import org.futo.circles.auth.feature.sign_up.SignUpDataSource
+import org.futo.circles.auth.feature.uia.UIADataSource
+import org.futo.circles.auth.feature.uia.UIADataSource.Companion.DIRECT_LOGIN_PASSWORD_TYPE
+import org.futo.circles.auth.feature.uia.UIADataSource.Companion.LOGIN_BSSPEKE_VERIFY_TYPE
+import org.futo.circles.auth.feature.uia.UIADataSource.Companion.LOGIN_PASSWORD_TYPE
+import org.futo.circles.auth.feature.uia.UIADataSource.Companion.LOGIN_PASSWORD_USER_ID_TYPE
+import org.futo.circles.auth.feature.uia.UIADataSource.Companion.REGISTRATION_BSSPEKE_SAVE_TYPE
+import org.futo.circles.auth.feature.uia.UIADataSource.Companion.REGISTRATION_EMAIL_SUBMIT_TOKEN_TYPE
+import org.futo.circles.auth.feature.uia.UIADataSource.Companion.TYPE_PARAM_KEY
+import org.futo.circles.auth.feature.uia.UIADataSource.Companion.USER_PARAM_KEY
+import org.futo.circles.auth.feature.uia.UIAFlowType
 import org.futo.circles.core.extensions.createResult
 import org.futo.circles.core.provider.MatrixInstanceProvider
 import org.futo.circles.core.utils.HomeServerUtils.buildHomeServerConfigFromDomain
@@ -13,9 +21,10 @@ import javax.inject.Inject
 
 class LoginDataSource @Inject constructor(
     @ApplicationContext private val context: Context,
-    private val loginStagesDataSource: LoginStagesDataSource
+    private val uiaFactory: UIADataSource.Factory
 ) {
 
+    private val loginStagesDataSource by lazy { uiaFactory.create(UIAFlowType.Login) }
     private val authService by lazy { MatrixInstanceProvider.matrix.authenticationService() }
 
     suspend fun startLogin(
@@ -25,7 +34,7 @@ class LoginDataSource @Inject constructor(
     ) = createResult {
         authService.cancelPendingLoginOrRegistration()
         val stages = prepareLoginStages(userName, domain, isForgotPassword)
-        loginStagesDataSource.startLoginStages(stages, userName, domain)
+        loginStagesDataSource.startUIAStages(stages, domain, userName)
     }
 
     private suspend fun prepareLoginStages(
@@ -66,17 +75,17 @@ class LoginDataSource @Inject constructor(
     private fun getCircleStagesForLogin(flows: List<List<Stage>>): List<Stage>? =
         flows.firstOrNull { stages ->
             stages.firstOrNull { stage ->
-                (stage as? Stage.Other)?.type == BaseLoginStagesDataSource.LOGIN_BSSPEKE_VERIFY_TYPE
+                (stage as? Stage.Other)?.type == LOGIN_BSSPEKE_VERIFY_TYPE
             } != null
         }
 
     private fun getCircleStagesForForgotPassword(flows: List<List<Stage>>): List<Stage>? =
         flows.firstOrNull { stages ->
             val containsEmailStage = stages.firstOrNull { stage ->
-                (stage as? Stage.Other)?.type == SignUpDataSource.REGISTRATION_EMAIL_SUBMIT_TOKEN_TYPE
+                (stage as? Stage.Other)?.type == REGISTRATION_EMAIL_SUBMIT_TOKEN_TYPE
             } != null
             val containsSetPassword = stages.firstOrNull { stage ->
-                (stage as? Stage.Other)?.type == SignUpDataSource.REGISTRATION_BSSPEKE_SAVE_TYPE
+                (stage as? Stage.Other)?.type == REGISTRATION_BSSPEKE_SAVE_TYPE
             } != null
             containsEmailStage && containsSetPassword
         }
diff --git a/auth/src/main/java/org/futo/circles/auth/feature/log_in/stages/LoginStagesDataSource.kt b/auth/src/main/java/org/futo/circles/auth/feature/log_in/stages/LoginStagesDataSource.kt
index c4f2352c1..a37a98ff2 100644
--- a/auth/src/main/java/org/futo/circles/auth/feature/log_in/stages/LoginStagesDataSource.kt
+++ b/auth/src/main/java/org/futo/circles/auth/feature/log_in/stages/LoginStagesDataSource.kt
@@ -16,14 +16,12 @@ import org.futo.circles.core.extensions.createResult
 import org.futo.circles.core.model.LoadingData
 import org.futo.circles.core.provider.MatrixInstanceProvider
 import org.futo.circles.core.provider.MatrixSessionProvider
-import org.matrix.android.sdk.api.auth.UIABaseAuth
 import org.matrix.android.sdk.api.auth.registration.RegistrationResult
 import org.matrix.android.sdk.api.auth.registration.Stage
 import org.matrix.android.sdk.api.session.Session
 import org.matrix.android.sdk.api.util.JsonDict
 import javax.inject.Inject
 import javax.inject.Singleton
-import kotlin.coroutines.Continuation
 
 enum class LoginNavigationEvent { Main, PassPhrase }
 
@@ -47,8 +45,8 @@ class LoginStagesDataSource @Inject constructor(
         name: String?
     ) {
         userPassword = ""
-        this.userName = name ?: throw IllegalArgumentException("Username is required for login")
-        super.startUIAStages(stages, serverDomain, userName)
+        name ?: throw IllegalArgumentException("Username is required for login")
+        super.startUIAStages(stages, serverDomain, name)
     }
 
     override suspend fun performUIAStage(
@@ -143,10 +141,4 @@ class LoginStagesDataSource @Inject constructor(
     fun navigateToMain() {
         loginNavigationLiveData.postValue(LoginNavigationEvent.Main)
     }
-
-    companion object {
-        //params
-        const val USER_PARAM_KEY = "user"
-        const val LOGIN_PASSWORD_USER_ID_TYPE = "m.id.user"
-    }
 }
\ No newline at end of file
diff --git a/auth/src/main/java/org/futo/circles/auth/feature/uia/UIADataSource.kt b/auth/src/main/java/org/futo/circles/auth/feature/uia/UIADataSource.kt
index 4a9231cef..3e466b0f5 100644
--- a/auth/src/main/java/org/futo/circles/auth/feature/uia/UIADataSource.kt
+++ b/auth/src/main/java/org/futo/circles/auth/feature/uia/UIADataSource.kt
@@ -52,6 +52,7 @@ abstract class UIADataSource(private val context: Context) {
         name: String? = null
     ) {
         currentStage = null
+        this.userName = name ?: ""
         stagesToComplete.clear()
         domain = serverDomain
         stagesToComplete.addAll(stages)
@@ -90,7 +91,6 @@ abstract class UIADataSource(private val context: Context) {
         } ?: navigateToNextStage()
     }
 
-
     private fun isStageRetry(result: RegistrationResult?): Boolean {
         val nextStageType =
             ((result as? RegistrationResult.FlowResponse)?.flowResult?.missingStages?.firstOrNull() as? Stage.Other)?.type
@@ -153,6 +153,8 @@ abstract class UIADataSource(private val context: Context) {
     companion object {
         //params
         const val TYPE_PARAM_KEY = "type"
+        const val USER_PARAM_KEY = "user"
+        const val LOGIN_PASSWORD_USER_ID_TYPE = "m.id.user"
 
         //login stages
         const val LOGIN_PASSWORD_TYPE = "m.login.password"
-- 
GitLab