From bb0a366ef18f238e7af04f66f22bad4937585280 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Sat, 5 Oct 2019 12:28:45 +0200 Subject: [PATCH] [APIv2/Librus] Refactor structure --- .../edziennik/api/v2/LoginMethods.kt | 16 ++-- .../edziennik/api/v2/librus/Librus.kt | 4 +- .../LibrusData.kt} | 13 ++- .../api/v2/librus/{ => login}/LibrusLogin.kt | 16 ++-- .../{LoginLibrusApi.kt => LibrusLoginApi.kt} | 2 +- ...brusMessages.kt => LibrusLoginMessages.kt} | 2 +- ...inLibrusPortal.kt => LibrusLoginPortal.kt} | 2 +- ...brusSynergia.kt => LibrusLoginSynergia.kt} | 2 +- .../api/v2/librus/login/LoginLibrus.kt | 86 ------------------- 9 files changed, 30 insertions(+), 113 deletions(-) rename app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/{LibrusEndpoints.kt => data/LibrusData.kt} (69%) rename app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/{ => login}/LibrusLogin.kt (76%) rename app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/{LoginLibrusApi.kt => LibrusLoginApi.kt} (99%) rename app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/{LoginLibrusMessages.kt => LibrusLoginMessages.kt} (98%) rename app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/{LoginLibrusPortal.kt => LibrusLoginPortal.kt} (99%) rename app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/{LoginLibrusSynergia.kt => LibrusLoginSynergia.kt} (99%) delete mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrus.kt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/LoginMethods.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/LoginMethods.kt index 1efc3bd6..f7a90179 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/LoginMethods.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/LoginMethods.kt @@ -4,10 +4,10 @@ package pl.szczodrzynski.edziennik.api.v2 -import pl.szczodrzynski.edziennik.api.v2.librus.login.LoginLibrusPortal -import pl.szczodrzynski.edziennik.api.v2.librus.login.LoginLibrusApi -import pl.szczodrzynski.edziennik.api.v2.librus.login.LoginLibrusMessages -import pl.szczodrzynski.edziennik.api.v2.librus.login.LoginLibrusSynergia +import pl.szczodrzynski.edziennik.api.v2.librus.login.LibrusLoginPortal +import pl.szczodrzynski.edziennik.api.v2.librus.login.LibrusLoginApi +import pl.szczodrzynski.edziennik.api.v2.librus.login.LibrusLoginMessages +import pl.szczodrzynski.edziennik.api.v2.librus.login.LibrusLoginSynergia import pl.szczodrzynski.edziennik.api.v2.models.LoginMethod val SYNERGIA_API_ENABLED = "true".toBoolean() @@ -39,13 +39,13 @@ const val LOGIN_METHOD_VULCAN_WEB = 100 const val LOGIN_METHOD_VULCAN_API = 200 val librusLoginMethods = listOf( - LoginMethod(LOGIN_TYPE_LIBRUS, LOGIN_METHOD_LIBRUS_PORTAL, LoginLibrusPortal::class.java) + LoginMethod(LOGIN_TYPE_LIBRUS, LOGIN_METHOD_LIBRUS_PORTAL, LibrusLoginPortal::class.java) .withIsPossible { _, loginStore -> loginStore.mode == LOGIN_MODE_LIBRUS_EMAIL } .withRequiredLoginMethod { _, _ -> LOGIN_METHOD_NOT_NEEDED }, - LoginMethod(LOGIN_TYPE_LIBRUS, LOGIN_METHOD_LIBRUS_API, LoginLibrusApi::class.java) + LoginMethod(LOGIN_TYPE_LIBRUS, LOGIN_METHOD_LIBRUS_API, LibrusLoginApi::class.java) .withIsPossible { _, loginStore -> loginStore.mode != LOGIN_MODE_LIBRUS_SYNERGIA || SYNERGIA_API_ENABLED } @@ -53,13 +53,13 @@ val librusLoginMethods = listOf( if (loginStore.mode == LOGIN_MODE_LIBRUS_EMAIL) LOGIN_METHOD_LIBRUS_PORTAL else LOGIN_METHOD_NOT_NEEDED }, - LoginMethod(LOGIN_TYPE_LIBRUS, LOGIN_METHOD_LIBRUS_SYNERGIA, LoginLibrusSynergia::class.java) + LoginMethod(LOGIN_TYPE_LIBRUS, LOGIN_METHOD_LIBRUS_SYNERGIA, LibrusLoginSynergia::class.java) .withIsPossible { _, _ -> true } .withRequiredLoginMethod { profile, _ -> if (profile?.hasStudentData("accountPassword") == false) LOGIN_METHOD_LIBRUS_API else LOGIN_METHOD_NOT_NEEDED }, - LoginMethod(LOGIN_TYPE_LIBRUS, LOGIN_METHOD_LIBRUS_MESSAGES, LoginLibrusMessages::class.java) + LoginMethod(LOGIN_TYPE_LIBRUS, LOGIN_METHOD_LIBRUS_MESSAGES, LibrusLoginMessages::class.java) .withIsPossible { _, _ -> true } .withRequiredLoginMethod { profile, _ -> if (profile?.hasStudentData("accountPassword") == false) LOGIN_METHOD_LIBRUS_SYNERGIA else LOGIN_METHOD_NOT_NEEDED 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 801040f2..977c9415 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 @@ -12,6 +12,8 @@ import pl.szczodrzynski.edziennik.api.v2.endpoints import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikCallback import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikInterface import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus +import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusData +import pl.szczodrzynski.edziennik.api.v2.librus.login.LibrusLogin import pl.szczodrzynski.edziennik.api.v2.librusLoginMethods import pl.szczodrzynski.edziennik.api.v2.models.ApiError import pl.szczodrzynski.edziennik.api.v2.models.Endpoint @@ -122,7 +124,7 @@ class Librus(val app: App, val profile: Profile?, val loginStore: LoginStore, va Log.d(TAG, "Endpoint IDs: ${data.targetEndpointIds}") LibrusLogin(data) { - LibrusEndpoints(data) { + LibrusData(data) { completed() } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/LibrusEndpoints.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt similarity index 69% rename from app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/LibrusEndpoints.kt rename to app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt index 87f974ee..6cfbd377 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/LibrusEndpoints.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt @@ -1,16 +1,17 @@ /* - * Copyright (c) Kuba Szczodrzyński 2019-10-1. + * Copyright (c) Kuba Szczodrzyński 2019-10-5. */ -package pl.szczodrzynski.edziennik.api.v2.librus +package pl.szczodrzynski.edziennik.api.v2.librus.data import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_LIBRUS_API_ME -import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus +import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_LIBRUS_API_SCHOOLS import pl.szczodrzynski.edziennik.api.v2.librus.data.api.LibrusApiMe +import pl.szczodrzynski.edziennik.api.v2.librus.data.api.LibrusApiSchools import pl.szczodrzynski.edziennik.utils.Utils -class LibrusEndpoints(val data: DataLibrus, val onSuccess: () -> Unit) { +class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) { companion object { private const val TAG = "LibrusEndpoints" } @@ -42,6 +43,10 @@ class LibrusEndpoints(val data: DataLibrus, val onSuccess: () -> Unit) { data.startProgress(R.string.edziennik_progress_endpoint_student_info) LibrusApiMe(data) { onSuccess() } } + ENDPOINT_LIBRUS_API_SCHOOLS -> { + data.startProgress(R.string.edziennik_progress_endpoint_school_info) + LibrusApiSchools(data) { onSuccess() } + } else -> onSuccess() } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/LibrusLogin.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLogin.kt similarity index 76% rename from app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/LibrusLogin.kt rename to app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLogin.kt index ce9e848a..725c716d 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/LibrusLogin.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLogin.kt @@ -1,8 +1,8 @@ /* - * Copyright (c) Kuba Szczodrzyński 2019-10-1. + * Copyright (c) Kuba Szczodrzyński 2019-10-5. */ -package pl.szczodrzynski.edziennik.api.v2.librus +package pl.szczodrzynski.edziennik.api.v2.librus.login import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_LIBRUS_API @@ -10,10 +10,6 @@ import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_LIBRUS_MESSAGES import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_LIBRUS_PORTAL import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_LIBRUS_SYNERGIA import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus -import pl.szczodrzynski.edziennik.api.v2.librus.login.LoginLibrusApi -import pl.szczodrzynski.edziennik.api.v2.librus.login.LoginLibrusMessages -import pl.szczodrzynski.edziennik.api.v2.librus.login.LoginLibrusPortal -import pl.szczodrzynski.edziennik.api.v2.librus.login.LoginLibrusSynergia import pl.szczodrzynski.edziennik.utils.Utils class LibrusLogin(val data: DataLibrus, val onSuccess: () -> Unit) { @@ -53,19 +49,19 @@ class LibrusLogin(val data: DataLibrus, val onSuccess: () -> Unit) { when (loginMethodId) { LOGIN_METHOD_LIBRUS_PORTAL -> { data.startProgress(R.string.edziennik_progress_login_librus_portal) - LoginLibrusPortal(data) { onSuccess(loginMethodId) } + LibrusLoginPortal(data) { onSuccess(loginMethodId) } } LOGIN_METHOD_LIBRUS_API -> { data.startProgress(R.string.edziennik_progress_login_librus_api) - LoginLibrusApi(data) { onSuccess(loginMethodId) } + LibrusLoginApi(data) { onSuccess(loginMethodId) } } LOGIN_METHOD_LIBRUS_SYNERGIA -> { data.startProgress(R.string.edziennik_progress_login_librus_synergia) - LoginLibrusSynergia(data) { onSuccess(loginMethodId) } + LibrusLoginSynergia(data) { onSuccess(loginMethodId) } } LOGIN_METHOD_LIBRUS_MESSAGES -> { data.startProgress(R.string.edziennik_progress_login_librus_messages) - LoginLibrusMessages(data) { onSuccess(loginMethodId) } + LibrusLoginMessages(data) { onSuccess(loginMethodId) } } } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusApi.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginApi.kt similarity index 99% rename from app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusApi.kt rename to app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginApi.kt index 56624210..cd528e9c 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusApi.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginApi.kt @@ -15,7 +15,7 @@ import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus import pl.szczodrzynski.edziennik.api.v2.models.ApiError import java.net.HttpURLConnection.* -class LoginLibrusApi { +class LibrusLoginApi { companion object { private const val TAG = "LoginLibrusApi" } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusMessages.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginMessages.kt similarity index 98% rename from app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusMessages.kt rename to app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginMessages.kt index a6921aee..d6adf3b1 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusMessages.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginMessages.kt @@ -16,7 +16,7 @@ import pl.szczodrzynski.edziennik.api.v2.models.ApiError import pl.szczodrzynski.edziennik.currentTimeUnix import pl.szczodrzynski.edziennik.getUnixDate -class LoginLibrusMessages(val data: DataLibrus, val onSuccess: () -> Unit) { +class LibrusLoginMessages(val data: DataLibrus, val onSuccess: () -> Unit) { companion object { private const val TAG = "LoginLibrusMessages" } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusPortal.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginPortal.kt similarity index 99% rename from app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusPortal.kt rename to app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginPortal.kt index d1524740..5aa864ec 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusPortal.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginPortal.kt @@ -15,7 +15,7 @@ import java.net.HttpURLConnection.HTTP_UNAUTHORIZED import java.util.ArrayList import java.util.regex.Pattern -class LoginLibrusPortal(val data: DataLibrus, val onSuccess: () -> Unit) { +class LibrusLoginPortal(val data: DataLibrus, val onSuccess: () -> Unit) { companion object { private const val TAG = "LoginLibrusPortal" } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusSynergia.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginSynergia.kt similarity index 99% rename from app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusSynergia.kt rename to app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginSynergia.kt index aadb1ae7..d64808df 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusSynergia.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginSynergia.kt @@ -17,7 +17,7 @@ import pl.szczodrzynski.edziennik.getString import pl.szczodrzynski.edziennik.getUnixDate import java.net.HttpURLConnection -class LoginLibrusSynergia(val data: DataLibrus, val onSuccess: () -> Unit) { +class LibrusLoginSynergia(val data: DataLibrus, val onSuccess: () -> Unit) { companion object { private const val TAG = "LoginLibrusSynergia" } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrus.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrus.kt deleted file mode 100644 index 4f28b5d8..00000000 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrus.kt +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright (c) Kuba Szczodrzyński 2019-9-21. - */ - -package pl.szczodrzynski.edziennik.api.v2.librus.login - -import pl.szczodrzynski.edziennik.R -import pl.szczodrzynski.edziennik.api.v2.* -import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus -import pl.szczodrzynski.edziennik.api.v2.models.LoginMethod -import pl.szczodrzynski.edziennik.utils.Utils.d -import kotlin.math.log - -class LoginLibrus(val data: DataLibrus, vararg loginMethodIds: Int, val onSuccess: () -> Unit) { - companion object { - private const val TAG = "LoginLibrus" - } - - private var loginMethodList = mutableListOf() - - init { - for (loginMethodId in loginMethodIds) { - var requiredLoginMethod: Int? = loginMethodId - while (requiredLoginMethod != LOGIN_METHOD_NOT_NEEDED) { - librusLoginMethods.singleOrNull { it.loginMethodId == requiredLoginMethod }?.let { loginMethod -> - if (requiredLoginMethod != null) - loginMethodList.add(requiredLoginMethod!!) - requiredLoginMethod = loginMethod.requiredLoginMethod(data.profile, data.loginStore) - } - } - } - loginMethodList = loginMethodList.toHashSet().toMutableList() - loginMethodList.sort() - - //data.satisfyLoginMethods() - nextLoginMethod() - } - - private fun nextLoginMethod() { - if (loginMethodList.isEmpty()) { - onSuccess() - return - } - useLoginMethod(loginMethodList.removeAt(0)) { - nextLoginMethod() - } - } - - private fun useLoginMethod(loginMethodId: Int, onSuccess: () -> Unit) { - if (data.loginMethods.contains(loginMethodId)) { - onSuccess() - return - } - d(TAG, "Using login method $loginMethodId") - when (loginMethodId) { - LOGIN_METHOD_LIBRUS_PORTAL -> { - data.startProgress(R.string.edziennik_progress_login_librus_portal) - LoginLibrusPortal(data) { - data.loginMethods.add(loginMethodId) - onSuccess() - } - } - LOGIN_METHOD_LIBRUS_API -> { - data.startProgress(R.string.edziennik_progress_login_librus_api) - LoginLibrusApi(data) { - data.loginMethods.add(loginMethodId) - onSuccess() - } - } - LOGIN_METHOD_LIBRUS_SYNERGIA -> { - data.startProgress(R.string.edziennik_progress_login_librus_synergia) - LoginLibrusSynergia(data) { - data.loginMethods.add(loginMethodId) - onSuccess() - } - } - LOGIN_METHOD_LIBRUS_MESSAGES -> { - data.startProgress(R.string.edziennik_progress_login_librus_messages) - LoginLibrusMessages(data) { - data.loginMethods.add(loginMethodId) - onSuccess() - } - } - } - } -} \ No newline at end of file