[APIv2] Add API first login method. Better cancellation handling.

This commit is contained in:
Kuba Szczodrzyński 2019-10-14 10:25:44 +02:00
parent b59887d4e0
commit b35df5ef11
16 changed files with 84 additions and 42 deletions

View File

@ -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()
}
}

View File

@ -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)"
}
}

View File

@ -7,5 +7,6 @@ package pl.szczodrzynski.edziennik.api.v2.interfaces
interface EdziennikInterface {
fun sync(featureIds: List<Int>, viewId: Int? = null)
fun getMessage(messageId: Int)
fun firstLogin()
fun cancel()
}

View File

@ -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()
}

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -943,4 +943,5 @@
<string name="edziennik_progress_endpoint_homework">Pobieranie zadań domowych...</string>
<string name="edziennik_progress_endpoint_attendance_types">Pobieranie kategorii obecności...</string>
<string name="edziennik_progress_endpoint_announcements">Pobieranie ogłoszeń szkolnych...</string>
<string name="edziennik_notification_api_first_login_title">Pierwsze logowanie</string>
</resources>