11package org.openedx.auth.presentation.signin
22
3- import android.content.res.Resources
43import android.app.Activity
4+ import android.content.res.Resources
55import androidx.fragment.app.Fragment
66import androidx.fragment.app.FragmentManager
77import androidx.lifecycle.LiveData
@@ -63,26 +63,36 @@ class SignInViewModel(
6363 val infoType : String? ,
6464 val authCode : String ,
6565) : BaseViewModel() {
66-
6766 private val logger = Logger (" SignInViewModel" )
6867
69- private val _uiState = MutableStateFlow (
70- SignInUIState (
71- isLoginRegistrationFormEnabled = config.isLoginRegistrationEnabled(),
72- isSSOLoginEnabled = config.isSSOLoginEnabled(),
73- ssoButtonTitle = config.getSSOButtonTitle(key = Resources .getSystem().getConfiguration().locales[0 ].language.uppercase(), " " ),
74- isSSODefaultLoginButton = config.isSSODefaultLoginButton(),
75- isFacebookAuthEnabled = config.getFacebookConfig().isEnabled(),
76- isGoogleAuthEnabled = config.getGoogleConfig().isEnabled(),
77- isMicrosoftAuthEnabled = config.getMicrosoftConfig().isEnabled(),
78- isBrowserLoginEnabled = config.isBrowserLoginEnabled(),
79- isBrowserRegistrationEnabled = config.isBrowserRegistrationEnabled(),
80- isSocialAuthEnabled = config.isSocialAuthEnabled(),
81- isLogistrationEnabled = config.isPreLoginExperienceEnabled(),
82- isRegistrationEnabled = config.isRegistrationEnabled(),
83- agreement = agreementProvider.getAgreement(isSignIn = true )?.createHonorCodeField(),
68+ private val _uiState =
69+ MutableStateFlow (
70+ SignInUIState (
71+ isLoginRegistrationFormEnabled = config.isLoginRegistrationEnabled(),
72+ isSSOLoginEnabled = config.isSSOLoginEnabled(),
73+ ssoButtonTitle =
74+ config.getSSOButtonTitle(
75+ key =
76+ Resources
77+ .getSystem()
78+ .getConfiguration()
79+ .locales[0 ]
80+ .language
81+ .uppercase(),
82+ " " ,
83+ ),
84+ isSSODefaultLoginButton = config.isSSODefaultLoginButton(),
85+ isFacebookAuthEnabled = config.getFacebookConfig().isEnabled(),
86+ isGoogleAuthEnabled = config.getGoogleConfig().isEnabled(),
87+ isMicrosoftAuthEnabled = config.getMicrosoftConfig().isEnabled(),
88+ isBrowserLoginEnabled = config.isBrowserLoginEnabled(),
89+ isBrowserRegistrationEnabled = config.isBrowserRegistrationEnabled(),
90+ isSocialAuthEnabled = config.isSocialAuthEnabled(),
91+ isLogistrationEnabled = config.isPreLoginExperienceEnabled(),
92+ isRegistrationEnabled = config.isRegistrationEnabled(),
93+ agreement = agreementProvider.getAgreement(isSignIn = true )?.createHonorCodeField(),
94+ ),
8495 )
85- )
8696 internal val uiState: StateFlow <SignInUIState > = _uiState
8797
8898 private val _uiMessage = SingleEventLiveData <UIMessage >()
@@ -98,7 +108,10 @@ class SignInViewModel(
98108 logSignInScreenEvent()
99109 }
100110
101- fun login (username : String , password : String ) {
111+ fun login (
112+ username : String ,
113+ password : String ,
114+ ) {
102115 logEvent(AuthAnalyticsEvent .USER_SIGN_IN_CLICKED )
103116 if (! validator.isEmailOrUserNameValid(username)) {
104117 _uiMessage .value =
@@ -126,9 +139,9 @@ class SignInViewModel(
126139 buildMap {
127140 put(
128141 AuthAnalyticsKey .METHOD .key,
129- AuthType .PASSWORD .methodName.lowercase()
142+ AuthType .PASSWORD .methodName.lowercase(),
130143 )
131- }
144+ },
132145 )
133146 appNotifier.send(SignInEvent ())
134147 } catch (e: Exception ) {
@@ -158,7 +171,6 @@ class SignInViewModel(
158171 fun ssoLogin (token : String ) {
159172 logEvent(AuthAnalyticsEvent .USER_SIGN_IN_CLICKED )
160173
161-
162174 _uiState .update { it.copy(showProgress = true ) }
163175 viewModelScope.launch {
164176 try {
@@ -171,9 +183,9 @@ class SignInViewModel(
171183 buildMap {
172184 put(
173185 AuthAnalyticsKey .METHOD .key,
174- AuthType .PASSWORD .methodName.lowercase()
186+ AuthType .PASSWORD .methodName.lowercase(),
175187 )
176- }
188+ },
177189 )
178190 } catch (e: Exception ) {
179191 if (e is EdxError .InvalidGrantException ) {
@@ -201,15 +213,17 @@ class SignInViewModel(
201213 }
202214 }
203215
204- fun socialAuth (fragment : Fragment , authType : AuthType ) {
216+ fun socialAuth (
217+ fragment : Fragment ,
218+ authType : AuthType ,
219+ ) {
205220 _uiState .update { it.copy(showProgress = true ) }
206221 viewModelScope.launch {
207222 withContext(Dispatchers .IO ) {
208223 runCatching {
209224 oAuthHelper.socialAuth(fragment, authType)
210225 }
211- }
212- .getOrNull()
226+ }.getOrNull()
213227 .checkToken()
214228 }
215229 }
@@ -235,17 +249,16 @@ class SignInViewModel(
235249 viewModelScope.launch {
236250 runCatching {
237251 interactor.loginAuthCode(authCode)
252+ }.onFailure {
253+ logger.e { " OAuth2 code error: $it " }
254+ onUnknownError()
255+ _uiState .update { it.copy(loginFailure = true ) }
256+ }.onSuccess {
257+ _uiState .update { it.copy(loginSuccess = true ) }
258+ setUserId()
259+ appNotifier.send(SignInEvent ())
260+ _uiState .update { it.copy(showProgress = false ) }
238261 }
239- .onFailure {
240- logger.e { " OAuth2 code error: $it " }
241- onUnknownError()
242- _uiState .update { it.copy(loginFailure = true ) }
243- }.onSuccess {
244- _uiState .update { it.copy(loginSuccess = true ) }
245- setUserId()
246- appNotifier.send(SignInEvent ())
247- _uiState .update { it.copy(showProgress = false ) }
248- }
249262 }
250263 }
251264
@@ -259,7 +272,10 @@ class SignInViewModel(
259272 oAuthHelper.clear()
260273 }
261274
262- private suspend fun exchangeToken (token : String , authType : AuthType ) {
275+ private suspend fun exchangeToken (
276+ token : String ,
277+ authType : AuthType ,
278+ ) {
263279 runCatching {
264280 interactor.loginSocial(token, authType)
265281 }.onFailure { error ->
@@ -278,9 +294,10 @@ class SignInViewModel(
278294 message?.let {
279295 logger.e { it() }
280296 }
281- _uiMessage .value = UIMessage .SnackBarMessage (
282- resourceManager.getString(CoreRes .string.core_error_unknown_error)
283- )
297+ _uiMessage .value =
298+ UIMessage .SnackBarMessage (
299+ resourceManager.getString(CoreRes .string.core_error_unknown_error),
300+ )
284301 _uiState .update { it.copy(showProgress = false ) }
285302 }
286303
@@ -300,7 +317,11 @@ class SignInViewModel(
300317 } ? : onUnknownError()
301318 }
302319
303- fun openLink (fragmentManager : FragmentManager , links : Map <String , String >, link : String ) {
320+ fun openLink (
321+ fragmentManager : FragmentManager ,
322+ links : Map <String , String >,
323+ link : String ,
324+ ) {
304325 links.forEach { (key, value) ->
305326 if (value == link) {
306327 router.navigateToWebContent(fragmentManager, key, value)
@@ -317,13 +338,13 @@ class SignInViewModel(
317338 router.navigateToWhatsNew(
318339 parentFragmentManager,
319340 courseId,
320- infoType
341+ infoType,
321342 )
322343 } else {
323344 router.navigateToMain(
324345 parentFragmentManager,
325346 courseId,
326- infoType
347+ infoType,
327348 )
328349 }
329350 }
@@ -335,20 +356,22 @@ class SignInViewModel(
335356 ) {
336357 analytics.logEvent(
337358 event = event.eventName,
338- params = buildMap {
339- put(AuthAnalyticsKey .NAME .key, event.biValue)
340- putAll(params)
341- }
359+ params =
360+ buildMap {
361+ put(AuthAnalyticsKey .NAME .key, event.biValue)
362+ putAll(params)
363+ },
342364 )
343365 }
344366
345367 private fun logSignInScreenEvent () {
346368 val event = AuthAnalyticsEvent .SIGN_IN
347369 analytics.logScreenEvent(
348370 screenName = event.eventName,
349- params = buildMap {
350- put(AuthAnalyticsKey .NAME .key, event.biValue)
351- }
371+ params =
372+ buildMap {
373+ put(AuthAnalyticsKey .NAME .key, event.biValue)
374+ },
352375 )
353376 }
354377}
0 commit comments