From 6c96875c8332b8902d7ecf19038a58ce1cd465c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Fri, 14 Oct 2022 19:44:22 +0200 Subject: [PATCH] [API/Login] Allow passing LoginStore params in user action requests. --- .../edziennik/ui/login/LoginFormFragment.kt | 7 ++++- .../ui/login/LoginPlatformListFragment.kt | 2 +- .../utils/managers/UserActionManager.kt | 30 ++++++++++--------- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/login/LoginFormFragment.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/login/LoginFormFragment.kt index 5586dd77..b403568b 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/login/LoginFormFragment.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/login/LoginFormFragment.kt @@ -91,6 +91,11 @@ class LoginFormFragment : Fragment(), CoroutineScope { val loginMode = arguments?.getInt("loginMode") ?: return val mode = register.loginModes.firstOrNull { it.loginMode == loginMode } ?: return + if (mode.credentials.isEmpty()) { + login(loginType, loginMode) + return + } + b.title.setText(R.string.login_form_title_format, app.getString(register.registerName)) b.subTitle.text = platformName ?: app.getString(mode.name) b.text.text = platformGuideText ?: app.getString(mode.guideText) @@ -252,7 +257,7 @@ class LoginFormFragment : Fragment(), CoroutineScope { } if (platformStoreKey == null) - payload.putAll(platformData?.toBundle()) + payload.putAll(platformData?.toBundle() ?: Bundle()) else payload.putBundle(platformStoreKey, platformData?.toBundle()) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/login/LoginPlatformListFragment.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/login/LoginPlatformListFragment.kt index cff801a4..5629487f 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/login/LoginPlatformListFragment.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/login/LoginPlatformListFragment.kt @@ -68,7 +68,7 @@ class LoginPlatformListFragment : Fragment(), CoroutineScope { "platformName" to platform.name, "platformDescription" to platform.description, "platformFormFields" to platform.formFields.joinToString(";"), - "platformData" to platform.data, + "platformData" to platform.data.toString(), "platformStoreKey" to platform.storeKey, ), activity.navOptions) } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/utils/managers/UserActionManager.kt b/app/src/main/java/pl/szczodrzynski/edziennik/utils/managers/UserActionManager.kt index 537915ac..bbdb8602 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/utils/managers/UserActionManager.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/utils/managers/UserActionManager.kt @@ -19,11 +19,9 @@ import pl.szczodrzynski.edziennik.data.api.ERROR_USOS_OAUTH_LOGIN_REQUEST import pl.szczodrzynski.edziennik.data.api.edziennik.EdziennikTask import pl.szczodrzynski.edziennik.data.api.events.UserActionRequiredEvent import pl.szczodrzynski.edziennik.data.api.models.ApiError -import pl.szczodrzynski.edziennik.ext.Bundle -import pl.szczodrzynski.edziennik.ext.Intent -import pl.szczodrzynski.edziennik.ext.JsonObject -import pl.szczodrzynski.edziennik.ext.pendingIntentFlag +import pl.szczodrzynski.edziennik.ext.* import pl.szczodrzynski.edziennik.ui.captcha.LibrusCaptchaDialog +import pl.szczodrzynski.edziennik.utils.Utils.d class UserActionManager(val app: App) { companion object { @@ -90,8 +88,9 @@ class UserActionManager(val app: App) { onSuccess: ((params: Bundle) -> Unit)? = null, onFailure: (() -> Unit)? = null ) { + d(TAG, "Running user action ($type) with params: ${params?.toJsonObject()}") when (type) { - UserActionRequiredEvent.CAPTCHA_LIBRUS -> executeLibrus(activity, profileId, onSuccess, onFailure) + UserActionRequiredEvent.CAPTCHA_LIBRUS -> executeLibrus(activity, profileId, params, onSuccess, onFailure) UserActionRequiredEvent.OAUTH_USOS -> executeOauth(activity, profileId, params, onSuccess, onFailure) } } @@ -99,27 +98,29 @@ class UserActionManager(val app: App) { private fun executeLibrus( activity: AppCompatActivity, profileId: Int?, + params: Bundle?, onSuccess: ((params: Bundle) -> Unit)?, onFailure: (() -> Unit)?, ) { if (profileId == null) return + val extras = params?.getBundle("extras") // show captcha dialog // use passed onSuccess listener, else sync profile LibrusCaptchaDialog( activity = activity, onSuccess = { code -> + val args = Bundle( + "recaptchaCode" to code, + "recaptchaTime" to System.currentTimeMillis(), + ) + if (extras != null) + args.putAll(extras) + if (onSuccess != null) { - val params = Bundle( - "recaptchaCode" to code, - "recaptchaTime" to System.currentTimeMillis(), - ) - onSuccess(params) + onSuccess(args) } else { - EdziennikTask.syncProfile(profileId, arguments = JsonObject( - "recaptchaCode" to code, - "recaptchaTime" to System.currentTimeMillis(), - )).enqueue(activity) + EdziennikTask.syncProfile(profileId, arguments = args.toJsonObject()).enqueue(activity) } }, onFailure = onFailure @@ -135,6 +136,7 @@ class UserActionManager(val app: App) { ) { if (profileId == null || params == null) return + val extras = params.getBundle("extras") } }