From 648699547ebad9575b49e4a43689e4c8b816aa0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Fri, 4 Oct 2019 20:24:47 +0200 Subject: [PATCH] [APIv2] Fix receivers and other weird things --- .../edziennik/api/v2/ApiService.kt | 14 ++++- .../v2/events/requests/SyncProfileRequest.kt | 2 +- .../api/v2/interfaces/EdziennikInterface.kt | 2 +- .../edziennik/api/v2/librus/Librus.kt | 3 +- .../api/v2/librus/data/DataLibrus.kt | 5 ++ .../api/v2/librus/data/api/LibrusApiEvents.kt | 26 +++++++++ .../v2/librus/data/api/LibrusApiSchools.kt | 55 +++++++++++++++++++ .../v2/librus/data/api/LibrusApiTemplate.kt | 27 +++++++++ .../data/db/modules/lessons/LessonRange.kt | 25 +++++++++ .../edziennik/receivers/SzkolnyReceiver.kt | 2 + 10 files changed, 155 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiEvents.kt create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiSchools.kt create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiTemplate.kt create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/lessons/LessonRange.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 758ff166..0ce24631 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 @@ -36,6 +36,7 @@ class ApiService : Service() { private val errorList = mutableListOf() private var queueHasErrorReportTask = false + private var serviceClosed = false private var taskCancelled = false private var taskRunning = false private var taskRunningId = -1 @@ -110,7 +111,8 @@ class ApiService : Service() { private fun sync() { if (taskRunning) return - if (taskQueue.size <= 0) { + if (taskQueue.size <= 0 || serviceClosed) { + serviceClosed = false allCompleted() return } @@ -124,6 +126,7 @@ class ApiService : Service() { notification .setCurrentTask(taskRunningId, null) .setProgressRes(R.string.edziennik_notification_api_error_report_title) + .post() return } @@ -155,7 +158,7 @@ class ApiService : Service() { when (task) { is SyncProfileRequest -> edziennikInterface?.sync(task.featureIds ?: Features.getAllIds()) - is SyncViewRequest -> edziennikInterface?.sync(Features.getIdsByView(task.targetId)) + is SyncViewRequest -> edziennikInterface?.sync(Features.getIdsByView(task.targetId), task.targetId) is MessageGetRequest -> edziennikInterface?.getMessage(task.messageId) } } @@ -213,6 +216,13 @@ class ApiService : Service() { taskCancelled = true edziennikInterface?.cancel() } + @Subscribe(threadMode = ThreadMode.ASYNC) + fun onServiceCloseRequest(serviceCloseRequest: ServiceCloseRequest) { + serviceClosed = true + taskCancelled = true + edziennikInterface?.cancel() + stopSelf() + } /* _____ _ _ _ / ____| (_) (_) | | diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/events/requests/SyncProfileRequest.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/events/requests/SyncProfileRequest.kt index 75e21406..902e5069 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/events/requests/SyncProfileRequest.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/events/requests/SyncProfileRequest.kt @@ -6,7 +6,7 @@ package pl.szczodrzynski.edziennik.api.v2.events.requests import pl.szczodrzynski.edziennik.api.v2.models.ApiTask -data class SyncProfileRequest(override val profileId: Int, val featureIds: List?) : ApiTask(profileId) { +data class SyncProfileRequest(override val profileId: Int, val featureIds: List? = null) : ApiTask(profileId) { override fun toString(): String { return "SyncProfileRequest(profileId=$profileId, featureIds=$featureIds)" } 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 38367b8a..8a2b6076 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 @@ -5,7 +5,7 @@ package pl.szczodrzynski.edziennik.api.v2.interfaces interface EdziennikInterface { - fun sync(featureIds: List) + fun sync(featureIds: List, viewId: Int? = null) fun getMessage(messageId: Int) 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 4bdc2453..801040f2 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 @@ -49,7 +49,7 @@ class Librus(val app: App, val profile: Profile?, val loginStore: LoginStore, va |_| |_| |_|\___| /_/ \_\_|\__, |\___/|_| |_|\__|_| |_|_| |_| |_| __/ | |__*/ - override fun sync(featureIds: List) { + override fun sync(featureIds: List, viewId: Int?) { val possibleLoginMethods = data.loginMethods.toMutableList() for (loginMethod in librusLoginMethods) { @@ -75,7 +75,6 @@ class Librus(val app: App, val profile: Profile?, val loginStore: LoginStore, va } val timestamp = System.currentTimeMillis() - val viewId = 0 endpointList = endpointList // sort the endpoint list by feature ID and priority diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/DataLibrus.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/DataLibrus.kt index ee3d18ac..dec33957 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/DataLibrus.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/DataLibrus.kt @@ -206,4 +206,9 @@ class DataLibrus(app: App, profile: Profile?, loginStore: LoginStore) : Data(app get() = profile?.getStudentData("isPremium", false) ?: false set(value) { profile?.putStudentData("isPremium", value) } + private var mSchoolName: String? = null + var schoolName: String? + get() { mSchoolName = mSchoolName ?: profile?.getStudentData("schoolName", null); return mSchoolName } + set(value) { profile?.putStudentData("schoolName", value) ?: return; mSchoolName = value } + } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiEvents.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiEvents.kt new file mode 100644 index 00000000..9b8daeb4 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiEvents.kt @@ -0,0 +1,26 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-10-4. + */ + +package pl.szczodrzynski.edziennik.api.v2.librus.data.api + +import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus +import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi + +class LibrusApiEvents(override val data: DataLibrus, + val onSuccess: () -> Unit) : LibrusApi(data) { + companion object { + const val TAG = "LibrusApiEvents" + } + + init { + apiGet(LibrusApiMe.TAG, "") { json -> + + // on error + data.error(TAG, ERROR_LIBRUS_API_, response, json) + + data.setSyncNext(ENDPOINT_LIBRUS_API_, 2 * DAY) + onSuccess() + } + } +} \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiSchools.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiSchools.kt new file mode 100644 index 00000000..ac41bcdb --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiSchools.kt @@ -0,0 +1,55 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-10-4. + */ + +package pl.szczodrzynski.edziennik.api.v2.librus.data.api + +import android.util.Pair +import com.google.gson.JsonNull +import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus +import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi +import pl.szczodrzynski.edziennik.getInt +import pl.szczodrzynski.edziennik.getJsonObject +import pl.szczodrzynski.edziennik.getString +import pl.szczodrzynski.edziennik.utils.models.Time +import java.util.* + +class LibrusApiSchools(override val data: DataLibrus, + val onSuccess: () -> Unit) : LibrusApi(data) { + companion object { + const val TAG = "LibrusApiSchools" + } + + init { + apiGet(LibrusApiMe.TAG, "") { json -> + val school = json?.getJsonObject("School") + val schoolId = school?.getInt("Id") + val schoolNameLong = school?.getString("Name") + + var schoolNameShort = "" + schoolNameLong?.split(" ")?.forEach { + if (it.isBlank()) + return@forEach + schoolNameShort += it[0].toLowerCase() + } + val schoolTown = school?.getString("Town")?.toLowerCase(Locale.getDefault()) + data.schoolName = schoolId.toString() + schoolNameShort + "_" + schoolTown + + /*lessonRanges.clear() + for ((index, lessonRangeEl) in school.get("LessonsRange").getAsJsonArray().withIndex()) { + val lr = lessonRangeEl.getAsJsonObject() + val from = lr.get("From") + val to = lr.get("To") + if (from != null && to != null && from !is JsonNull && to !is JsonNull) { + lessonRanges.put(index, Pair(Time.fromH_m(from!!.getAsString()), Time.fromH_m(to!!.getAsString()))) + } + } + profile.putStudentData("lessonRanges", app.gson.toJson(lessonRanges)) + // on error + data.error(TAG, ERROR_LIBRUS_API_, response, json) + + data.setSyncNext(ENDPOINT_LIBRUS_API_, 2 * DAY) + onSuccess()*/ + } + } +} \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiTemplate.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiTemplate.kt new file mode 100644 index 00000000..cacd1215 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiTemplate.kt @@ -0,0 +1,27 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-10-4. + */ + +package pl.szczodrzynski.edziennik.api.v2.librus.data.api + +import pl.szczodrzynski.edziennik.DAY +import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus +import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi + +class LibrusApiTemplate(override val data: DataLibrus, + val onSuccess: () -> Unit) : LibrusApi(data) { + companion object { + const val TAG = "LibrusApi" + } + + init { + /*apiGet(LibrusApiMe.TAG, "") { json -> + + // on error + data.error(TAG, ERROR_LIBRUS_API_, response, json) + + data.setSyncNext(ENDPOINT_LIBRUS_API_, 2 * DAY) + onSuccess() + }*/ + } +} \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/lessons/LessonRange.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/lessons/LessonRange.kt new file mode 100644 index 00000000..61c8c814 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/lessons/LessonRange.kt @@ -0,0 +1,25 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-10-4. + */ + +package pl.szczodrzynski.edziennik.data.db.modules.lessons + +import androidx.room.ColumnInfo +import androidx.room.Entity +import pl.szczodrzynski.edziennik.utils.models.Time + +@Entity(tableName = "lessonRanges", + primaryKeys = ["profileId", "lessonRangeNumber"]) +class LessonRange ( + + val profileId: Int, + + @ColumnInfo(name = "lessonRangeNumber") + val lessonNumber: Int, + + @ColumnInfo(name = "lessonRangeStart") + val startTime: Time, + + @ColumnInfo(name = "lessonRangeEnd") + val endTime: Time +) \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/receivers/SzkolnyReceiver.kt b/app/src/main/java/pl/szczodrzynski/edziennik/receivers/SzkolnyReceiver.kt index 540ea73d..e9d3fb9d 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/receivers/SzkolnyReceiver.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/receivers/SzkolnyReceiver.kt @@ -9,6 +9,7 @@ import android.content.Context import android.content.Intent import org.greenrobot.eventbus.EventBus import pl.szczodrzynski.edziennik.api.v2.events.requests.ServiceCloseRequest +import pl.szczodrzynski.edziennik.api.v2.events.requests.SyncProfileRequest import pl.szczodrzynski.edziennik.api.v2.events.requests.SyncRequest import pl.szczodrzynski.edziennik.api.v2.events.requests.TaskCancelRequest @@ -18,6 +19,7 @@ class SzkolnyReceiver : BroadcastReceiver() { "ServiceCloseRequest" -> EventBus.getDefault().post(ServiceCloseRequest()) "TaskCancelRequest" -> EventBus.getDefault().post(TaskCancelRequest(intent.extras?.getInt("taskId", -1) ?: return)) "SyncRequest" -> EventBus.getDefault().post(SyncRequest()) + "SyncProfileRequest" -> EventBus.getDefault().post(SyncProfileRequest(intent.extras?.getInt("profileId", -1) ?: return)) } } } \ No newline at end of file