[APIv2] Add Feature priority setter and shouldSync method.

This commit is contained in:
Kuba Szczodrzyński 2019-10-12 17:01:21 +02:00
parent 0875d13737
commit b0fb87acdb
5 changed files with 44 additions and 22 deletions

View File

@ -13,6 +13,7 @@ import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikInterface
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.mobidziennik.MobidziennikFeatures
import pl.szczodrzynski.edziennik.api.v2.models.ApiError
import pl.szczodrzynski.edziennik.api.v2.models.Feature
import pl.szczodrzynski.edziennik.data.db.modules.api.*
@ -63,12 +64,13 @@ class Librus(val app: App, val profile: Profile?, val loginStore: LoginStore, va
data.targetEndpointIds.clear()
data.targetLoginMethodIds.clear()
// get all endpoints for every feature, only if possible to login
// get all endpoints for every feature, only if possible to login and possible/necessary to sync
for (featureId in featureIds) {
LibrusFeatures.filter {
it.featureId == featureId && possibleLoginMethods.containsAll(it.requiredLoginMethods)
}
.let {
it.featureId == featureId // feature ID matches
&& possibleLoginMethods.containsAll(it.requiredLoginMethods) // is possible to login
&& it.shouldSync?.invoke(profile, loginStore) ?: true // is necessary/possible to sync
}.let {
endpointList.addAll(it)
}
}

View File

@ -9,6 +9,7 @@ import pl.szczodrzynski.edziennik.App
import pl.szczodrzynski.edziennik.api.v2.*
import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikCallback
import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikInterface
import pl.szczodrzynski.edziennik.api.v2.librus.LibrusFeatures
import pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.MobidziennikData
import pl.szczodrzynski.edziennik.api.v2.mobidziennik.login.MobidziennikLogin
import pl.szczodrzynski.edziennik.api.v2.models.ApiError
@ -64,12 +65,13 @@ class Mobidziennik(val app: App, val profile: Profile?, val loginStore: LoginSto
data.targetEndpointIds.clear()
data.targetLoginMethodIds.clear()
// get all endpoints for every feature, only if possible to login
// get all endpoints for every feature, only if possible to login and possible/necessary to sync
for (featureId in featureIds) {
MobidziennikFeatures.filter {
it.featureId == featureId && possibleLoginMethods.containsAll(it.requiredLoginMethods)
}
.let {
it.featureId == featureId // feature ID matches
&& possibleLoginMethods.containsAll(it.requiredLoginMethods) // is possible to login
&& it.shouldSync?.invoke(profile, loginStore) ?: true // is necessary/possible to sync
}.let {
endpointList.addAll(it)
}
}

View File

@ -1,5 +1,8 @@
package pl.szczodrzynski.edziennik.api.v2.models
import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore
import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile
/**
* A Endpoint descriptor class.
*
@ -20,4 +23,14 @@ data class Feature(
val requiredLoginMethods: List<Int>
) {
var priority = endpointIds.size
fun withPriority(priority: Int): Feature {
this.priority = priority
return this
}
var shouldSync: ((Profile?, LoginStore) -> Boolean)? = null
fun withShouldSync(shouldSync: ((Profile?, LoginStore) -> Boolean)?): Feature {
this.shouldSync = shouldSync
return this
}
}

View File

@ -10,11 +10,13 @@ import pl.szczodrzynski.edziennik.api.v2.CODE_INTERNAL_LIBRUS_ACCOUNT_410
import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_NOT_NEEDED
import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikCallback
import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikInterface
import pl.szczodrzynski.edziennik.api.v2.librus.LibrusFeatures
import pl.szczodrzynski.edziennik.api.v2.models.ApiError
import pl.szczodrzynski.edziennik.api.v2.models.Feature
import pl.szczodrzynski.edziennik.api.v2.template.data.TemplateData
import pl.szczodrzynski.edziennik.api.v2.template.login.TemplateLogin
import pl.szczodrzynski.edziennik.api.v2.templateLoginMethods
import pl.szczodrzynski.edziennik.api.v2.vulcan.VulcanFeatures
import pl.szczodrzynski.edziennik.data.db.modules.api.EndpointTimer
import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS
import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_NEVER
@ -66,12 +68,13 @@ class Template(val app: App, val profile: Profile?, val loginStore: LoginStore,
data.targetEndpointIds.clear()
data.targetLoginMethodIds.clear()
// get all endpoints for every feature, only if possible to login
// get all endpoints for every feature, only if possible to login and possible/necessary to sync
for (featureId in featureIds) {
TemplateFeatures.filter {
it.featureId == featureId && possibleLoginMethods.containsAll(it.requiredLoginMethods)
}
.let {
VulcanFeatures.filter {
it.featureId == featureId // feature ID matches
&& possibleLoginMethods.containsAll(it.requiredLoginMethods) // is possible to login
&& it.shouldSync?.invoke(profile, loginStore) ?: true // is necessary/possible to sync
}.let {
endpointList.addAll(it)
}
}

View File

@ -10,6 +10,7 @@ import pl.szczodrzynski.edziennik.api.v2.CODE_INTERNAL_LIBRUS_ACCOUNT_410
import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_NOT_NEEDED
import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikCallback
import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikInterface
import pl.szczodrzynski.edziennik.api.v2.librus.LibrusFeatures
import pl.szczodrzynski.edziennik.api.v2.models.ApiError
import pl.szczodrzynski.edziennik.api.v2.models.Feature
import pl.szczodrzynski.edziennik.api.v2.vulcan.data.VulcanData
@ -66,12 +67,13 @@ class Vulcan(val app: App, val profile: Profile?, val loginStore: LoginStore, va
data.targetEndpointIds.clear()
data.targetLoginMethodIds.clear()
// get all endpoints for every feature, only if possible to login
// get all endpoints for every feature, only if possible to login and possible/necessary to sync
for (featureId in featureIds) {
VulcanFeatures.filter {
it.featureId == featureId && possibleLoginMethods.containsAll(it.requiredLoginMethods)
}
.let {
it.featureId == featureId // feature ID matches
&& possibleLoginMethods.containsAll(it.requiredLoginMethods) // is possible to login
&& it.shouldSync?.invoke(profile, loginStore) ?: true // is necessary/possible to sync
}.let {
endpointList.addAll(it)
}
}