From b35df5ef112199071a761cf6dfb99bd30f1af452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Mon, 14 Oct 2019 10:25:44 +0200 Subject: [PATCH] [APIv2] Add API first login method. Better cancellation handling. --- .../edziennik/api/v2/ApiService.kt | 34 +++++++++++++------ .../v2/events/requests/FirstLoginRequest.kt | 10 ++++++ .../api/v2/interfaces/EdziennikInterface.kt | 1 + .../edziennik/api/v2/librus/Librus.kt | 4 +++ .../api/v2/librus/data/LibrusData.kt | 8 ++--- .../api/v2/librus/login/LibrusLogin.kt | 8 ++--- .../api/v2/mobidziennik/Mobidziennik.kt | 4 +++ .../v2/mobidziennik/data/MobidziennikData.kt | 8 ++--- .../mobidziennik/login/MobidziennikLogin.kt | 8 ++--- .../edziennik/api/v2/template/Template.kt | 4 +++ .../api/v2/template/data/TemplateData.kt | 8 ++--- .../api/v2/template/login/TemplateLogin.kt | 8 ++--- .../edziennik/api/v2/vulcan/Vulcan.kt | 4 +++ .../api/v2/vulcan/data/VulcanData.kt | 8 ++--- .../api/v2/vulcan/login/VulcanLogin.kt | 8 ++--- app/src/main/res/values/strings.xml | 1 + 16 files changed, 84 insertions(+), 42 deletions(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/api/v2/events/requests/FirstLoginRequest.kt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/ApiService.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/ApiService.kt index f76a792b..4ffc122c 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/ApiService.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/ApiService.kt @@ -87,6 +87,7 @@ class ApiService : Service() { apiError.throwable?.printStackTrace() if (apiError.isCritical) { notification.setCriticalError().post() + taskRunningObject = null taskRunning = false taskRunningId = -1 sync() @@ -148,18 +149,30 @@ class ApiService : Service() { return } - // get the requested profile and login store - val profile: Profile? = app.db.profileDao().getByIdNow(task.profileId) - if (profile == null || !profile.syncEnabled) { - return + val profile: Profile? + val loginStore: LoginStore + if (task is FirstLoginRequest) { + // get the requested profile and login store + profile = null + loginStore = task.loginStore + // save the profile ID and name as the current task's + taskProfileId = -1 + taskProfileName = getString(R.string.edziennik_notification_api_first_login_title) } - val loginStore: LoginStore? = app.db.loginStoreDao().getByIdNow(profile.loginStoreId) - if (loginStore == null) { - return + else { + // get the requested profile and login store + profile = app.db.profileDao().getByIdNow(task.profileId) + if (profile == null || !profile.syncEnabled) { + return + } + loginStore = app.db.loginStoreDao().getByIdNow(profile.loginStoreId) + if (loginStore == null) { + return + } + // save the profile ID and name as the current task's + taskProfileId = profile.id + taskProfileName = profile.name } - // save the profile ID and name as the current task's - taskProfileId = profile.id - taskProfileName = profile.name taskProgress = 0 taskProgressRes = null @@ -182,6 +195,7 @@ class ApiService : Service() { featureIds = task.viewIds?.flatMap { Features.getIdsByView(it.first, it.second) } ?: Features.getAllIds(), viewId = task.viewIds?.get(0)?.first) is MessageGetRequest -> edziennikInterface?.getMessage(task.messageId) + is FirstLoginRequest -> edziennikInterface?.firstLogin() } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/events/requests/FirstLoginRequest.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/events/requests/FirstLoginRequest.kt new file mode 100644 index 00000000..75e92793 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/events/requests/FirstLoginRequest.kt @@ -0,0 +1,10 @@ +package pl.szczodrzynski.edziennik.api.v2.events.requests + +import pl.szczodrzynski.edziennik.api.v2.models.ApiTask +import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore + +data class FirstLoginRequest(val loginStore: LoginStore) : ApiTask(-1) { + override fun toString(): String { + return "FirstLoginRequest(loginStore=$loginStore)" + } +} \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/interfaces/EdziennikInterface.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/interfaces/EdziennikInterface.kt index 8a2b6076..20535a0e 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/interfaces/EdziennikInterface.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/interfaces/EdziennikInterface.kt @@ -7,5 +7,6 @@ package pl.szczodrzynski.edziennik.api.v2.interfaces interface EdziennikInterface { fun sync(featureIds: List, viewId: Int? = null) fun getMessage(messageId: Int) + fun firstLogin() fun cancel() } \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/Librus.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/Librus.kt index d782f721..c1d479bf 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/Librus.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/Librus.kt @@ -132,6 +132,10 @@ class Librus(val app: App, val profile: Profile?, val loginStore: LoginStore, va } + override fun firstLogin() { + // TODO + } + override fun cancel() { data.cancel() } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt index 16c944fe..cab131f4 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt @@ -23,11 +23,11 @@ class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) { onSuccess() return } + if (data.cancelled) { + onSuccess() + return + } useEndpoint(data.targetEndpointIds.removeAt(0)) { - if (data.cancelled) { - onSuccess() - return@useEndpoint - } nextEndpoint(onSuccess) } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLogin.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLogin.kt index b0dea3d4..6cc54567 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLogin.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLogin.kt @@ -28,13 +28,13 @@ class LibrusLogin(val data: DataLibrus, val onSuccess: () -> Unit) { onSuccess() return } + if (cancelled) { + onSuccess() + return + } useLoginMethod(data.targetLoginMethodIds.removeAt(0)) { usedMethodId -> if (usedMethodId != -1) data.loginMethods.add(usedMethodId) - if (cancelled) { - onSuccess() - return@useLoginMethod - } nextLoginMethod(onSuccess) } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/Mobidziennik.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/Mobidziennik.kt index c0df3eb5..e62dd984 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/Mobidziennik.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/Mobidziennik.kt @@ -133,6 +133,10 @@ class Mobidziennik(val app: App, val profile: Profile?, val loginStore: LoginSto } + override fun firstLogin() { + // TODO + } + override fun cancel() { Utils.d(TAG, "Cancelled") cancelled = true diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/MobidziennikData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/MobidziennikData.kt index 304f036e..553c1e0d 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/MobidziennikData.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/MobidziennikData.kt @@ -27,11 +27,11 @@ class MobidziennikData(val data: DataMobidziennik, val onSuccess: () -> Unit) { onSuccess() return } + if (data.cancelled) { + onSuccess() + return + } useEndpoint(data.targetEndpointIds.removeAt(0)) { - if (data.cancelled) { - onSuccess() - return@useEndpoint - } nextEndpoint(onSuccess) } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/login/MobidziennikLogin.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/login/MobidziennikLogin.kt index 4baf451c..e6b17e1a 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/login/MobidziennikLogin.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/login/MobidziennikLogin.kt @@ -26,13 +26,13 @@ class MobidziennikLogin(val data: DataMobidziennik, val onSuccess: () -> Unit) { onSuccess() return } + if (cancelled) { + onSuccess() + return + } useLoginMethod(data.targetLoginMethodIds.removeAt(0)) { usedMethodId -> if (usedMethodId != -1) data.loginMethods.add(usedMethodId) - if (cancelled) { - onSuccess() - return@useLoginMethod - } nextLoginMethod(onSuccess) } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/Template.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/Template.kt index 0c1da62d..bdf8e0dd 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/Template.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/Template.kt @@ -136,6 +136,10 @@ class Template(val app: App, val profile: Profile?, val loginStore: LoginStore, } + override fun firstLogin() { + // TODO + } + override fun cancel() { Utils.d(TAG, "Cancelled") cancelled = true diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/TemplateData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/TemplateData.kt index 35b2f004..9aef7cbe 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/TemplateData.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/TemplateData.kt @@ -30,11 +30,11 @@ class TemplateData(val data: DataTemplate, val onSuccess: () -> Unit) { onSuccess() return } + if (cancelled) { + onSuccess() + return + } useEndpoint(data.targetEndpointIds.removeAt(0)) { - if (cancelled) { - onSuccess() - return@useEndpoint - } nextEndpoint(onSuccess) } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/login/TemplateLogin.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/login/TemplateLogin.kt index 111f0fd1..1f08ef44 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/login/TemplateLogin.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/login/TemplateLogin.kt @@ -25,13 +25,13 @@ class TemplateLogin(val data: DataTemplate, val onSuccess: () -> Unit) { onSuccess() return } + if (cancelled) { + onSuccess() + return + } useLoginMethod(data.targetLoginMethodIds.removeAt(0)) { usedMethodId -> if (usedMethodId != -1) data.loginMethods.add(usedMethodId) - if (cancelled) { - onSuccess() - return@useLoginMethod - } nextLoginMethod(onSuccess) } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/Vulcan.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/Vulcan.kt index ae8362bc..70d852f9 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/Vulcan.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/Vulcan.kt @@ -135,6 +135,10 @@ class Vulcan(val app: App, val profile: Profile?, val loginStore: LoginStore, va } + override fun firstLogin() { + // TODO + } + override fun cancel() { Utils.d(TAG, "Cancelled") cancelled = true diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/VulcanData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/VulcanData.kt index 554c060a..1023cb4f 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/VulcanData.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/VulcanData.kt @@ -23,11 +23,11 @@ class VulcanData(val data: DataVulcan, val onSuccess: () -> Unit) { onSuccess() return } + if (cancelled) { + onSuccess() + return + } useEndpoint(data.targetEndpointIds.removeAt(0)) { - if (cancelled) { - onSuccess() - return@useEndpoint - } nextEndpoint(onSuccess) } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/login/VulcanLogin.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/login/VulcanLogin.kt index 6ecfeb19..8cfdb960 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/login/VulcanLogin.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/login/VulcanLogin.kt @@ -25,13 +25,13 @@ class VulcanLogin(val data: DataVulcan, val onSuccess: () -> Unit) { onSuccess() return } + if (cancelled) { + onSuccess() + return + } useLoginMethod(data.targetLoginMethodIds.removeAt(0)) { usedMethodId -> if (usedMethodId != -1) data.loginMethods.add(usedMethodId) - if (cancelled) { - onSuccess() - return@useLoginMethod - } nextLoginMethod(onSuccess) } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bba42794..b8767b62 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -943,4 +943,5 @@ Pobieranie zadań domowych... Pobieranie kategorii obecności... Pobieranie ogłoszeń szkolnych... + Pierwsze logowanie