From 67be456bb0196efd52dbe0cf302847065c8d4622 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Fri, 21 Feb 2020 22:49:24 +0100 Subject: [PATCH] [Firebase/Librus] Implement Librus push registration and receiving. Fix not passing lastSync to endpoint. --- .../szczodrzynski/edziennik/MainActivity.kt | 2 +- .../edziennik/data/api/EndpointChooser.kt | 13 ++++-- .../data/api/edziennik/EdziennikTask.kt | 5 ++- .../api/edziennik/edudziennik/Edudziennik.kt | 4 +- .../edudziennik/data/EdudziennikData.kt | 7 ++- .../data/api/edziennik/idziennik/Idziennik.kt | 4 +- .../edziennik/idziennik/data/IdziennikData.kt | 7 ++- .../data/api/edziennik/librus/DataLibrus.kt | 10 +++++ .../data/api/edziennik/librus/Librus.kt | 4 +- .../api/edziennik/librus/LibrusFeatures.kt | 2 - .../api/edziennik/librus/data/LibrusData.kt | 12 +++--- .../librus/data/api/LibrusApiPushConfig.kt | 39 +++++++++++++++++ .../librus/data/api/LibrusApiTemplate.kt | 7 +-- .../edziennik/mobidziennik/Mobidziennik.kt | 4 +- .../mobidziennik/data/MobidziennikData.kt | 7 ++- .../data/api/edziennik/template/Template.kt | 4 +- .../edziennik/template/data/TemplateData.kt | 7 ++- .../data/api/edziennik/vulcan/Vulcan.kt | 4 +- .../api/edziennik/vulcan/data/VulcanData.kt | 7 ++- .../data/api/interfaces/EdziennikInterface.kt | 6 +-- .../firebase/MyFirebaseMessagingService.java | 4 +- .../data/firebase/SzkolnyLibrusFirebase.kt | 43 ++++++++++++++++--- 22 files changed, 143 insertions(+), 59 deletions(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiPushConfig.kt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt b/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt index 8a5e1e52..ac2d8fc4 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt @@ -567,7 +567,7 @@ class MainActivity : AppCompatActivity(), CoroutineScope { EdziennikTask.syncProfile( App.profileId, listOf(navTargetId to fragmentParam), - arguments + arguments = arguments ).enqueue(this) } @Subscribe(threadMode = ThreadMode.MAIN) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/EndpointChooser.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/EndpointChooser.kt index 7a95872e..7f882123 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/EndpointChooser.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/EndpointChooser.kt @@ -7,7 +7,7 @@ import pl.szczodrzynski.edziennik.data.db.entity.EndpointTimer import pl.szczodrzynski.edziennik.data.db.entity.SYNC_ALWAYS import pl.szczodrzynski.edziennik.data.db.entity.SYNC_NEVER -fun Data.prepare(loginMethods: List, features: List, featureIds: List, viewId: Int?) { +fun Data.prepare(loginMethods: List, features: List, featureIds: List, viewId: Int?, onlyEndpoints: List?) { val data = this val possibleLoginMethods = data.loginMethods.toMutableList() @@ -46,13 +46,18 @@ fun Data.prepare(loginMethods: List, features: List, featu // add all endpoint IDs and required login methods, filtering using timers .onEach { feature -> feature.endpointIds.forEach { endpoint -> + if (onlyEndpoints?.contains(endpoint.first) == false) + return@forEach (data.endpointTimers .singleOrNull { it.endpointId == endpoint.first } ?: EndpointTimer(data.profile?.id ?: -1, endpoint.first)) .let { timer -> - if (timer.nextSync == SYNC_ALWAYS || - (viewId != null && timer.viewId == viewId) || - (timer.nextSync != SYNC_NEVER && timer.nextSync < timestamp)) { + if ( + onlyEndpoints?.contains(endpoint.first) == true || + timer.nextSync == SYNC_ALWAYS || + viewId != null && timer.viewId == viewId || + timer.nextSync != SYNC_NEVER && timer.nextSync < timestamp + ) { data.targetEndpointIds[endpoint.first] = timer.lastSync requiredLoginMethods.add(endpoint.second) } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/EdziennikTask.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/EdziennikTask.kt index 319479cb..db8186c9 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/EdziennikTask.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/EdziennikTask.kt @@ -30,7 +30,7 @@ open class EdziennikTask(override val profileId: Int, val request: Any) : IApiTa fun firstLogin(loginStore: LoginStore) = EdziennikTask(-1, FirstLoginRequest(loginStore)) fun sync() = EdziennikTask(-1, SyncRequest()) - fun syncProfile(profileId: Int, viewIds: List>? = null, arguments: JsonObject? = null) = EdziennikTask(profileId, SyncProfileRequest(viewIds, arguments)) + fun syncProfile(profileId: Int, viewIds: List>? = null, onlyEndpoints: List? = null, arguments: JsonObject? = null) = EdziennikTask(profileId, SyncProfileRequest(viewIds, onlyEndpoints, arguments)) fun syncProfileList(profileList: List) = EdziennikTask(-1, SyncProfileListRequest(profileList)) fun messageGet(profileId: Int, message: MessageFull) = EdziennikTask(profileId, MessageGetRequest(message)) fun messageSend(profileId: Int, recipients: List, subject: String, text: String) = EdziennikTask(profileId, MessageSendRequest(recipients, subject, text)) @@ -85,6 +85,7 @@ open class EdziennikTask(override val profileId: Int, val request: Any) : IApiTa featureIds = request.viewIds?.flatMap { Features.getIdsByView(it.first, it.second) } ?: Features.getAllIds(), viewId = request.viewIds?.get(0)?.first, + onlyEndpoints = request.onlyEndpoints, arguments = request.arguments) is MessageGetRequest -> edziennikInterface?.getMessage(request.message) is MessageSendRequest -> edziennikInterface?.sendMessage(request.recipients, request.subject, request.text) @@ -106,7 +107,7 @@ open class EdziennikTask(override val profileId: Int, val request: Any) : IApiTa data class FirstLoginRequest(val loginStore: LoginStore) class SyncRequest - data class SyncProfileRequest(val viewIds: List>? = null, val arguments: JsonObject? = null) + data class SyncProfileRequest(val viewIds: List>? = null, val onlyEndpoints: List? = null, val arguments: JsonObject? = null) data class SyncProfileListRequest(val profileList: List) data class MessageGetRequest(val message: MessageFull) data class MessageSendRequest(val recipients: List, val subject: String, val text: String) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/Edudziennik.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/Edudziennik.kt index 7ab9acb6..7bc50511 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/Edudziennik.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/Edudziennik.kt @@ -52,9 +52,9 @@ class Edudziennik(val app: App, val profile: Profile?, val loginStore: LoginStor |_| |_| |_|\___| /_/ \_\_|\__, |\___/|_| |_|\__|_| |_|_| |_| |_| __/ | |__*/ - override fun sync(featureIds: List, viewId: Int?, arguments: JsonObject?) { + override fun sync(featureIds: List, viewId: Int?, onlyEndpoints: List?, arguments: JsonObject?) { data.arguments = arguments - data.prepare(edudziennikLoginMethods, EdudziennikFeatures, featureIds, viewId) + data.prepare(edudziennikLoginMethods, EdudziennikFeatures, featureIds, viewId, onlyEndpoints) login() } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/EdudziennikData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/EdudziennikData.kt index a7baefcb..7eb58d1c 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/EdudziennikData.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/EdudziennikData.kt @@ -28,15 +28,14 @@ class EdudziennikData(val data: DataEdudziennik, val onSuccess: () -> Unit) { return } val id = data.targetEndpointIds.firstKey() - data.targetEndpointIds.remove(id) - useEndpoint(id) { endpointId -> + val lastSync = data.targetEndpointIds.remove(id) + useEndpoint(id, lastSync) { endpointId -> data.progress(data.progressStep) nextEndpoint(onSuccess) } } - private fun useEndpoint(endpointId: Int, onSuccess: (endpointId: Int) -> Unit) { - val lastSync = data.targetEndpointIds[endpointId] + private fun useEndpoint(endpointId: Int, lastSync: Long?, onSuccess: (endpointId: Int) -> Unit) { Utils.d(TAG, "Using endpoint $endpointId. Last sync time = $lastSync") when (endpointId) { ENDPOINT_EDUDZIENNIK_WEB_START -> { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/Idziennik.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/Idziennik.kt index 2a5223e5..be0d0d1c 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/Idziennik.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/Idziennik.kt @@ -54,9 +54,9 @@ class Idziennik(val app: App, val profile: Profile?, val loginStore: LoginStore, |_| |_| |_|\___| /_/ \_\_|\__, |\___/|_| |_|\__|_| |_|_| |_| |_| __/ | |__*/ - override fun sync(featureIds: List, viewId: Int?, arguments: JsonObject?) { + override fun sync(featureIds: List, viewId: Int?, onlyEndpoints: List?, arguments: JsonObject?) { data.arguments = arguments - data.prepare(idziennikLoginMethods, IdziennikFeatures, featureIds, viewId) + data.prepare(idziennikLoginMethods, IdziennikFeatures, featureIds, viewId, onlyEndpoints) login() } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/IdziennikData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/IdziennikData.kt index e428d6cc..8acb04cb 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/IdziennikData.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/IdziennikData.kt @@ -31,15 +31,14 @@ class IdziennikData(val data: DataIdziennik, val onSuccess: () -> Unit) { return } val id = data.targetEndpointIds.firstKey() - data.targetEndpointIds.remove(id) - useEndpoint(id) { endpointId -> + val lastSync = data.targetEndpointIds.remove(id) + useEndpoint(id, lastSync) { endpointId -> data.progress(data.progressStep) nextEndpoint(onSuccess) } } - private fun useEndpoint(endpointId: Int, onSuccess: (endpointId: Int) -> Unit) { - val lastSync = data.targetEndpointIds[endpointId] + private fun useEndpoint(endpointId: Int, lastSync: Long?, onSuccess: (endpointId: Int) -> Unit) { Utils.d(TAG, "Using endpoint $endpointId. Last sync time = $lastSync") when (endpointId) { ENDPOINT_IDZIENNIK_WEB_TIMETABLE -> { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/DataLibrus.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/DataLibrus.kt index 930377b4..c43172f2 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/DataLibrus.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/DataLibrus.kt @@ -191,6 +191,16 @@ class DataLibrus(app: App, profile: Profile?, loginStore: LoginStore) : Data(app get() { mApiTokenExpiryTime = mApiTokenExpiryTime ?: profile?.getStudentData("accountTokenTime", 0L); return mApiTokenExpiryTime ?: 0L } set(value) { mApiTokenExpiryTime = value; profile?.putStudentData("accountTokenTime", value) ?: return; } + /** + * A push device ID, generated by Librus when registering + * a FCM token. I don't really know if this has any use, + * but it may be worthy to save that ID. + */ + private var mPushDeviceId: Int? = null + var pushDeviceId: Int + get() { mPushDeviceId = mPushDeviceId ?: profile?.getStudentData("pushDeviceId", 0); return mPushDeviceId ?: 0 } + set(value) { mPushDeviceId = value; profile?.putStudentData("pushDeviceId", value) ?: return; } + /* _____ _ / ____| (_) | (___ _ _ _ __ ___ _ __ __ _ _ __ _ diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/Librus.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/Librus.kt index 0e864f62..b5438bd1 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/Librus.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/Librus.kt @@ -56,9 +56,9 @@ class Librus(val app: App, val profile: Profile?, val loginStore: LoginStore, va |_| |_| |_|\___| /_/ \_\_|\__, |\___/|_| |_|\__|_| |_|_| |_| |_| __/ | |__*/ - override fun sync(featureIds: List, viewId: Int?, arguments: JsonObject?) { + override fun sync(featureIds: List, viewId: Int?, onlyEndpoints: List?, arguments: JsonObject?) { data.arguments = arguments - data.prepare(librusLoginMethods, LibrusFeatures, featureIds, viewId) + data.prepare(librusLoginMethods, LibrusFeatures, featureIds, viewId, onlyEndpoints) login() } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/LibrusFeatures.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/LibrusFeatures.kt index d0fd82c1..2d8af69b 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/LibrusFeatures.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/LibrusFeatures.kt @@ -53,8 +53,6 @@ const val ENDPOINT_LIBRUS_SYNERGIA_HOMEWORK = 2030 const val ENDPOINT_LIBRUS_MESSAGES_RECEIVED = 3010 const val ENDPOINT_LIBRUS_MESSAGES_SENT = 3020 const val ENDPOINT_LIBRUS_MESSAGES_TRASH = 3030 -const val ENDPOINT_LIBRUS_MESSAGES_RECEIVERS = 3040 -const val ENDPOINT_LIBRUS_MESSAGES_GET = 3040 val LibrusFeatures = listOf( diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/LibrusData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/LibrusData.kt index 3b2b68cb..bf1fdab4 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/LibrusData.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/LibrusData.kt @@ -32,15 +32,14 @@ class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) { return } val id = data.targetEndpointIds.firstKey() - data.targetEndpointIds.remove(id) - useEndpoint(id) { endpointId -> + val lastSync = data.targetEndpointIds.remove(id) + useEndpoint(id, lastSync) { endpointId -> data.progress(data.progressStep) nextEndpoint(onSuccess) } } - private fun useEndpoint(endpointId: Int, onSuccess: (endpointId: Int) -> Unit) { - val lastSync = data.targetEndpointIds[endpointId] + private fun useEndpoint(endpointId: Int, lastSync: Long?, onSuccess: (endpointId: Int) -> Unit) { Utils.d(TAG, "Using endpoint $endpointId. Last sync time = $lastSync") when (endpointId) { /** @@ -82,7 +81,10 @@ class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) { data.startProgress(R.string.edziennik_progress_endpoint_lessons) LibrusApiLessons(data, lastSync, onSuccess) } - // TODO push config + ENDPOINT_LIBRUS_API_PUSH_CONFIG -> { + data.startProgress(R.string.edziennik_progress_endpoint_push_config) + LibrusApiPushConfig(data, lastSync, onSuccess) + } ENDPOINT_LIBRUS_API_TIMETABLES -> { data.startProgress(R.string.edziennik_progress_endpoint_timetable) LibrusApiTimetables(data, lastSync, onSuccess) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiPushConfig.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiPushConfig.kt new file mode 100644 index 00000000..09142cc5 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiPushConfig.kt @@ -0,0 +1,39 @@ +/* + * Copyright (c) Kuba SzczodrzyƄski 2020-2-21. + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.api + +import pl.szczodrzynski.edziennik.JsonObject +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.DataLibrus +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_PUSH_CONFIG +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.LibrusApi +import pl.szczodrzynski.edziennik.data.db.entity.SYNC_ALWAYS +import pl.szczodrzynski.edziennik.getInt +import pl.szczodrzynski.edziennik.getJsonObject + +class LibrusApiPushConfig(override val data: DataLibrus, + override val lastSync: Long?, + val onSuccess: (endpointId: Int) -> Unit +) : LibrusApi(data, lastSync) { + companion object { + const val TAG = "LibrusApiPushConfig" + } + + init { data.app.config.sync.tokenLibrus?.also { tokenLibrus -> + apiGet(TAG, "ChangeRegister", payload = JsonObject( + "provider" to "FCM", + "device" to tokenLibrus, + "sendPush" to "1", + "appVersion" to 4 + )) { json -> + json.getJsonObject("ChangeRegister")?.getInt("Id")?.let { data.pushDeviceId = it } + + // sync always: this endpoint has .shouldSync set + data.setSyncNext(ENDPOINT_LIBRUS_API_PUSH_CONFIG, SYNC_ALWAYS) + data.app.config.sync.tokenLibrusList = + data.app.config.sync.tokenLibrusList + profileId + onSuccess(ENDPOINT_LIBRUS_API_PUSH_CONFIG) + } + } ?: onSuccess(ENDPOINT_LIBRUS_API_PUSH_CONFIG) } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiTemplate.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiTemplate.kt index b47215b9..9fd6afee 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiTemplate.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiTemplate.kt @@ -8,8 +8,9 @@ import pl.szczodrzynski.edziennik.data.api.edziennik.librus.DataLibrus import pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.LibrusApi class LibrusApiTemplate(override val data: DataLibrus, - val onSuccess: () -> Unit -) : LibrusApi(data, null) { + override val lastSync: Long?, + val onSuccess: (endpointId: Int) -> Unit +) : LibrusApi(data, lastSync) { companion object { const val TAG = "LibrusApi" } @@ -18,7 +19,7 @@ class LibrusApiTemplate(override val data: DataLibrus, /*apiGet(TAG, "") { json -> data.setSyncNext(ENDPOINT_LIBRUS_API_, SYNC_ALWAYS) - onSuccess() + onSuccess(ENDPOINT_LIBRUS_API_) }*/ } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/Mobidziennik.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/Mobidziennik.kt index b805ea6a..5e08f6c4 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/Mobidziennik.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/Mobidziennik.kt @@ -56,9 +56,9 @@ class Mobidziennik(val app: App, val profile: Profile?, val loginStore: LoginSto |_| |_| |_|\___| /_/ \_\_|\__, |\___/|_| |_|\__|_| |_|_| |_| |_| __/ | |__*/ - override fun sync(featureIds: List, viewId: Int?, arguments: JsonObject?) { + override fun sync(featureIds: List, viewId: Int?, onlyEndpoints: List?, arguments: JsonObject?) { data.arguments = arguments - data.prepare(mobidziennikLoginMethods, MobidziennikFeatures, featureIds, viewId) + data.prepare(mobidziennikLoginMethods, MobidziennikFeatures, featureIds, viewId, onlyEndpoints) login() } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/MobidziennikData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/MobidziennikData.kt index f98c5b51..09e1e9fb 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/MobidziennikData.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/MobidziennikData.kt @@ -30,15 +30,14 @@ class MobidziennikData(val data: DataMobidziennik, val onSuccess: () -> Unit) { return } val id = data.targetEndpointIds.firstKey() - data.targetEndpointIds.remove(id) - useEndpoint(id) { endpointId -> + val lastSync = data.targetEndpointIds.remove(id) + useEndpoint(id, lastSync) { endpointId -> data.progress(data.progressStep) nextEndpoint(onSuccess) } } - private fun useEndpoint(endpointId: Int, onSuccess: (endpointId: Int) -> Unit) { - val lastSync = data.targetEndpointIds[endpointId] + private fun useEndpoint(endpointId: Int, lastSync: Long?, onSuccess: (endpointId: Int) -> Unit) { Utils.d(TAG, "Using endpoint $endpointId. Last sync time = $lastSync") when (endpointId) { ENDPOINT_MOBIDZIENNIK_API_MAIN -> { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/template/Template.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/template/Template.kt index 209f5083..9048bbcf 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/template/Template.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/template/Template.kt @@ -51,9 +51,9 @@ class Template(val app: App, val profile: Profile?, val loginStore: LoginStore, |_| |_| |_|\___| /_/ \_\_|\__, |\___/|_| |_|\__|_| |_|_| |_| |_| __/ | |__*/ - override fun sync(featureIds: List, viewId: Int?, arguments: JsonObject?) { + override fun sync(featureIds: List, viewId: Int?, onlyEndpoints: List?, arguments: JsonObject?) { data.arguments = arguments - data.prepare(templateLoginMethods, TemplateFeatures, featureIds, viewId) + data.prepare(templateLoginMethods, TemplateFeatures, featureIds, viewId, onlyEndpoints) d(TAG, "LoginMethod IDs: ${data.targetLoginMethodIds}") d(TAG, "Endpoint IDs: ${data.targetEndpointIds}") TemplateLogin(data) { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/template/data/TemplateData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/template/data/TemplateData.kt index 3e9d5dc7..3489555f 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/template/data/TemplateData.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/template/data/TemplateData.kt @@ -33,15 +33,14 @@ class TemplateData(val data: DataTemplate, val onSuccess: () -> Unit) { return } val id = data.targetEndpointIds.firstKey() - data.targetEndpointIds.remove(id) - useEndpoint(id) { endpointId -> + val lastSync = data.targetEndpointIds.remove(id) + useEndpoint(id, lastSync) { endpointId -> data.progress(data.progressStep) nextEndpoint(onSuccess) } } - private fun useEndpoint(endpointId: Int, onSuccess: (endpointId: Int) -> Unit) { - val lastSync = data.targetEndpointIds[endpointId] + private fun useEndpoint(endpointId: Int, lastSync: Long?, onSuccess: (endpointId: Int) -> Unit) { Utils.d(TAG, "Using endpoint $endpointId. Last sync time = $lastSync") when (endpointId) { ENDPOINT_TEMPLATE_WEB_SAMPLE -> { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/Vulcan.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/Vulcan.kt index 512fae0d..4e6def85 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/Vulcan.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/Vulcan.kt @@ -55,9 +55,9 @@ class Vulcan(val app: App, val profile: Profile?, val loginStore: LoginStore, va |_| |_| |_|\___| /_/ \_\_|\__, |\___/|_| |_|\__|_| |_|_| |_| |_| __/ | |__*/ - override fun sync(featureIds: List, viewId: Int?, arguments: JsonObject?) { + override fun sync(featureIds: List, viewId: Int?, onlyEndpoints: List?, arguments: JsonObject?) { data.arguments = arguments - data.prepare(vulcanLoginMethods, VulcanFeatures, featureIds, viewId) + data.prepare(vulcanLoginMethods, VulcanFeatures, featureIds, viewId, onlyEndpoints) login() } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanData.kt index b3887584..9ab94c30 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanData.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanData.kt @@ -28,15 +28,14 @@ class VulcanData(val data: DataVulcan, val onSuccess: () -> Unit) { return } val id = data.targetEndpointIds.firstKey() - data.targetEndpointIds.remove(id) - useEndpoint(id) { endpointId -> + val lastSync = data.targetEndpointIds.remove(id) + useEndpoint(id, lastSync) { endpointId -> data.progress(data.progressStep) nextEndpoint(onSuccess) } } - private fun useEndpoint(endpointId: Int, onSuccess: (endpointId: Int) -> Unit) { - val lastSync = data.targetEndpointIds[endpointId] + private fun useEndpoint(endpointId: Int, lastSync: Long?, onSuccess: (endpointId: Int) -> Unit) { Utils.d(TAG, "Using endpoint $endpointId. Last sync time = $lastSync") when (endpointId) { ENDPOINT_VULCAN_API_UPDATE_SEMESTER -> { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/interfaces/EdziennikInterface.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/interfaces/EdziennikInterface.kt index a9015473..e19a19ee 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/interfaces/EdziennikInterface.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/interfaces/EdziennikInterface.kt @@ -5,13 +5,13 @@ package pl.szczodrzynski.edziennik.data.api.interfaces import com.google.gson.JsonObject -import pl.szczodrzynski.edziennik.data.db.full.AnnouncementFull import pl.szczodrzynski.edziennik.data.db.entity.Message -import pl.szczodrzynski.edziennik.data.db.full.MessageFull import pl.szczodrzynski.edziennik.data.db.entity.Teacher +import pl.szczodrzynski.edziennik.data.db.full.AnnouncementFull +import pl.szczodrzynski.edziennik.data.db.full.MessageFull interface EdziennikInterface { - fun sync(featureIds: List, viewId: Int? = null, arguments: JsonObject? = null) + fun sync(featureIds: List, viewId: Int? = null, onlyEndpoints: List? = null, arguments: JsonObject? = null) fun getMessage(message: MessageFull) fun sendMessage(recipients: List, subject: String, text: String) fun markAllAnnouncementsAsRead() diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/MyFirebaseMessagingService.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/MyFirebaseMessagingService.java index 2a9d52a8..34e341ed 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/MyFirebaseMessagingService.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/MyFirebaseMessagingService.java @@ -87,7 +87,7 @@ public class MyFirebaseMessagingService extends FirebaseMessagingService { if (remoteMessage.getData().get("id_wiadomosci") != null) { d(TAG, "Syncing profile " + profile.getId()); - EdziennikTask.Companion.syncProfile(profile.getId(), null, null).enqueue(app); + EdziennikTask.Companion.syncProfile(profile.getId(), null, null, null).enqueue(app); } else { /*app.notifier.add(new Notification(app.getContext(), remoteMessage.getData().get("message")) .withProfileData(profile.id, profile.name) @@ -98,7 +98,7 @@ public class MyFirebaseMessagingService extends FirebaseMessagingService { app.notifier.postAll(profile); app.saveConfig("notifications");*/ d(TAG, "Syncing profile " + profile.getId()); - EdziennikTask.Companion.syncProfile(profile.getId(), null, null).enqueue(app); + EdziennikTask.Companion.syncProfile(profile.getId(), null, null, null).enqueue(app); } } }); diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyLibrusFirebase.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyLibrusFirebase.kt index b6baf2bd..cc59faf4 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyLibrusFirebase.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyLibrusFirebase.kt @@ -5,7 +5,10 @@ package pl.szczodrzynski.edziennik.data.firebase import pl.szczodrzynski.edziennik.App +import pl.szczodrzynski.edziennik.MainActivity +import pl.szczodrzynski.edziennik.data.api.LOGIN_TYPE_LIBRUS import pl.szczodrzynski.edziennik.data.api.edziennik.EdziennikTask +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.* import pl.szczodrzynski.edziennik.data.api.task.IApiTask import pl.szczodrzynski.edziennik.data.db.entity.Profile import pl.szczodrzynski.edziennik.getString @@ -23,14 +26,44 @@ class SzkolnyLibrusFirebase(val app: App, val profiles: List, val messa "objectType": "Calendars/TeacherFreeDays", }*/ init { run { - val apiLogin = message.data.getString("userId") ?: return@run + val type = message.data.getString("objectType") ?: return@run + val accountLogin = message.data.getString("userId")?.replace("u", "") ?: return@run - val tasks = profiles.filter { - it.getStudentData("accountLogin", "") == apiLogin - }.map { - EdziennikTask.syncProfile(it.id) + /* ./src/store/modules/helpers/change-processor.js */ + val endpoints = when (type) { + "Notes" -> listOf(ENDPOINT_LIBRUS_API_NOTICES) + "Grades" -> listOf(ENDPOINT_LIBRUS_API_NORMAL_GRADES, ENDPOINT_LIBRUS_API_NORMAL_GRADE_CATEGORIES, ENDPOINT_LIBRUS_API_NORMAL_GRADE_COMMENTS) + "PointGrades" -> listOf(ENDPOINT_LIBRUS_API_POINT_GRADES, ENDPOINT_LIBRUS_API_POINT_GRADE_CATEGORIES) + "DescriptiveGrades" -> listOf(ENDPOINT_LIBRUS_API_DESCRIPTIVE_GRADES) + "DescriptiveGrades/Text/Categories" -> listOf(ENDPOINT_LIBRUS_API_DESCRIPTIVE_GRADE_CATEGORIES) + "DescriptiveTextGrades" -> listOf(ENDPOINT_LIBRUS_API_DESCRIPTIVE_TEXT_GRADES) + "TextGrades" -> listOf(ENDPOINT_LIBRUS_API_TEXT_GRADES) + "BehaviourGrades/Points" -> listOf(ENDPOINT_LIBRUS_API_BEHAVIOUR_GRADES, ENDPOINT_LIBRUS_API_BEHAVIOUR_GRADE_CATEGORIES, ENDPOINT_LIBRUS_API_BEHAVIOUR_GRADE_COMMENTS) + "BehaviourGrades" -> listOf() + "Attendances" -> listOf(ENDPOINT_LIBRUS_API_ATTENDANCES) + "HomeWorks" -> listOf(ENDPOINT_LIBRUS_API_EVENTS) + "ParentTeacherConferences" -> listOf(ENDPOINT_LIBRUS_API_PT_MEETINGS) + "Calendars/ClassFreeDays" -> listOf(ENDPOINT_LIBRUS_API_CLASS_FREE_DAYS) + "Calendars/TeacherFreeDays" -> listOf(ENDPOINT_LIBRUS_API_TEACHER_FREE_DAYS) + "Calendars/SchoolFreeDays" -> listOf(ENDPOINT_LIBRUS_API_SCHOOL_FREE_DAYS) + "Calendars/Substitutions" -> listOf(ENDPOINT_LIBRUS_API_TIMETABLES) + "HomeWorkAssignments" -> listOf(ENDPOINT_LIBRUS_API_HOMEWORK, ENDPOINT_LIBRUS_SYNERGIA_HOMEWORK) + "SchoolNotices" -> listOf(ENDPOINT_LIBRUS_API_ANNOUNCEMENTS) + "Messages" -> listOf(ENDPOINT_LIBRUS_MESSAGES_RECEIVED) + "LuckyNumbers" -> listOf(ENDPOINT_LIBRUS_API_LUCKY_NUMBER) + "Timetables" -> listOf(ENDPOINT_LIBRUS_API_TIMETABLES) + else -> return@run } + if (endpoints.isEmpty()) + return@run + + val tasks = profiles.filter { + it.loginStoreType == LOGIN_TYPE_LIBRUS && + it.getStudentData("accountLogin", "")?.replace("u", "") == accountLogin + }.map { + EdziennikTask.syncProfile(it.id, listOf(MainActivity.DRAWER_ITEM_HOME to 0), onlyEndpoints = endpoints) + } IApiTask.enqueueAll(app, tasks) }} }