From 3e99c111bdd200edff6b5d7939bebf6601bcb56d Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Sat, 26 Oct 2019 14:36:21 +0200 Subject: [PATCH] [APIv2/Librus] Add marking all announcements as read using Synergia --- .../edziennik/api/v2/ApiService.kt | 14 ++++++++++++- .../requests/AnnouncementsReadRequest.kt | 13 ++++++++++++ .../api/v2/interfaces/EdziennikInterface.kt | 3 ++- .../edziennik/api/v2/librus/Librus.kt | 15 +++++++++++++- .../api/v2/librus/data/LibrusSynergia.kt | 5 ++--- .../data/synergia/LibrusSynergiaHomework.kt | 4 +++- .../data/synergia/LibrusSynergiaInfo.kt | 4 +++- ...ibrusSynergiaMarkAllAnnouncementsAsRead.kt | 20 +++++++++++++++++++ .../data/synergia/LibrusSynergiaTemplate.kt | 4 +++- .../api/v2/mobidziennik/Mobidziennik.kt | 6 +++++- .../edziennik/api/v2/template/Template.kt | 6 +++++- .../edziennik/api/v2/vulcan/Vulcan.kt | 4 ++++ .../edziennik/receivers/SzkolnyReceiver.kt | 8 +++----- 13 files changed, 90 insertions(+), 16 deletions(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/api/v2/events/requests/AnnouncementsReadRequest.kt create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaMarkAllAnnouncementsAsRead.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 64086764..0b2ceefc 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 @@ -232,6 +232,7 @@ class ApiService : Service() { viewId = task.viewIds?.get(0)?.first) is MessageGetRequest -> edziennikInterface?.getMessage(task.messageId) is FirstLoginRequest -> edziennikInterface?.firstLogin() + is AnnouncementsReadRequest -> edziennikInterface?.markAllAnnouncementsAsRead() } } @@ -294,6 +295,17 @@ class ApiService : Service() { sync() } + @Subscribe(sticky = true, threadMode = ThreadMode.ASYNC) + fun onAnnouncementsReadRequest(request: AnnouncementsReadRequest) { + EventBus.getDefault().removeStickyEvent(request) + Log.d(TAG, request.toString()) + + taskQueue += request.apply { + taskId = ++taskMaximumId + } + sync() + } + @Subscribe(sticky = true, threadMode = ThreadMode.ASYNC) fun onFirstLoginRequest(request: FirstLoginRequest) { EventBus.getDefault().removeStickyEvent(request) @@ -347,4 +359,4 @@ class ApiService : Service() { override fun onBind(intent: Intent?): IBinder? { return null } -} \ No newline at end of file +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/events/requests/AnnouncementsReadRequest.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/events/requests/AnnouncementsReadRequest.kt new file mode 100644 index 00000000..60e35840 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/events/requests/AnnouncementsReadRequest.kt @@ -0,0 +1,13 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2019-10-26 + */ + +package pl.szczodrzynski.edziennik.api.v2.events.requests + +import pl.szczodrzynski.edziennik.api.v2.models.ApiTask + +data class AnnouncementsReadRequest(override val profileId: Int) : ApiTask(profileId) { + override fun toString(): String { + return "AnnouncementsReadRequest(profileId=$profileId)" + } +} 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 20535a0e..935b778d 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 @@ -7,6 +7,7 @@ package pl.szczodrzynski.edziennik.api.v2.interfaces interface EdziennikInterface { fun sync(featureIds: List, viewId: Int? = null) fun getMessage(messageId: Int) + fun markAllAnnouncementsAsRead() fun firstLogin() 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 0e3d093e..dbd99c5f 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 @@ -10,8 +10,11 @@ import pl.szczodrzynski.edziennik.api.v2.CODE_INTERNAL_LIBRUS_ACCOUNT_410 import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikCallback import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikInterface import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusData +import pl.szczodrzynski.edziennik.api.v2.librus.data.synergia.LibrusSynergiaMarkAllAnnouncementsAsRead import pl.szczodrzynski.edziennik.api.v2.librus.firstlogin.LibrusFirstLogin import pl.szczodrzynski.edziennik.api.v2.librus.login.LibrusLogin +import pl.szczodrzynski.edziennik.api.v2.librus.login.LibrusLoginApi +import pl.szczodrzynski.edziennik.api.v2.librus.login.LibrusLoginSynergia import pl.szczodrzynski.edziennik.api.v2.librusLoginMethods import pl.szczodrzynski.edziennik.api.v2.models.ApiError import pl.szczodrzynski.edziennik.api.v2.prepare @@ -64,6 +67,16 @@ class Librus(val app: App, val profile: Profile?, val loginStore: LoginStore, va } + override fun markAllAnnouncementsAsRead() { + LibrusLoginApi(data) { + LibrusLoginSynergia(data) { + LibrusSynergiaMarkAllAnnouncementsAsRead(data) { + completed() + } + } + } + } + override fun firstLogin() { LibrusFirstLogin(data) { completed() @@ -105,4 +118,4 @@ class Librus(val app: App, val profile: Profile?, val loginStore: LoginStore, va } } } -} \ No newline at end of file +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusSynergia.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusSynergia.kt index e16f00e7..691c6d57 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusSynergia.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusSynergia.kt @@ -27,7 +27,7 @@ open class LibrusSynergia(open val data: DataLibrus) { get() = data.profile fun synergiaGet(tag: String, endpoint: String, method: Int = GET, - parameters: Map = emptyMap(), onSuccess: (doc: Document) -> Unit) { + parameters: Map = emptyMap(), onSuccess: (text: String) -> Unit) { d(tag, "Request: Librus/Synergia - $LIBRUS_SYNERGIA_URL/$endpoint") val callback = object : TextCallbackHandler() { @@ -41,8 +41,7 @@ open class LibrusSynergia(open val data: DataLibrus) { // TODO: Error handling try { - val doc = Jsoup.parse(text) - onSuccess(doc) + onSuccess(text) } catch (e: Exception) { data.error(ApiError(tag, EXCEPTION_LIBRUS_SYNERGIA_REQUEST) .withResponse(response) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaHomework.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaHomework.kt index 09f767b6..860eb958 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaHomework.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaHomework.kt @@ -4,6 +4,7 @@ package pl.szczodrzynski.edziennik.api.v2.librus.data.synergia +import org.jsoup.Jsoup import pl.szczodrzynski.edziennik.HOUR import pl.szczodrzynski.edziennik.MainActivity.Companion.DRAWER_ITEM_HOMEWORK import pl.szczodrzynski.edziennik.api.v2.POST @@ -31,7 +32,8 @@ class LibrusSynergiaHomework(override val data: DataLibrus, val onSuccess: () -> "dataDo" to profile!!.getSemesterEnd(profile?.currentSemester ?: 2).stringY_m_d, "przedmiot" to -1 - )) { doc -> + )) { text -> + val doc = Jsoup.parse(text) doc.select("table.myHomeworkTable > tbody").firstOrNull()?.also { homeworkTable -> val homeworkElements = homeworkTable.children() diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaInfo.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaInfo.kt index cb638523..7777cf6a 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaInfo.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaInfo.kt @@ -4,6 +4,7 @@ package pl.szczodrzynski.edziennik.api.v2.librus.data.synergia +import org.jsoup.Jsoup import pl.szczodrzynski.edziennik.MONTH import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_SYNERGIA_INFO @@ -15,7 +16,8 @@ class LibrusSynergiaInfo(override val data: DataLibrus, val onSuccess: () -> Uni } init { - synergiaGet(TAG, "informacja") { doc -> + synergiaGet(TAG, "informacja") { text -> + val doc = Jsoup.parse(text) doc.select("table.form tbody").firstOrNull()?.children()?.also { info -> val studentNumber = info[2].select("td").text().trim().toIntOrNull() diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaMarkAllAnnouncementsAsRead.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaMarkAllAnnouncementsAsRead.kt new file mode 100644 index 00000000..5976954a --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaMarkAllAnnouncementsAsRead.kt @@ -0,0 +1,20 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2019-10-26 + */ + +package pl.szczodrzynski.edziennik.api.v2.librus.data.synergia + +import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus +import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusSynergia + +class LibrusSynergiaMarkAllAnnouncementsAsRead(override val data: DataLibrus, val onSuccess: () -> Unit) : LibrusSynergia(data) { + companion object { + const val TAG = "LibrusSynergiaMarkAllAnnouncementsAsRead" + } + + init { + synergiaGet(TAG, "ogloszenia") { + onSuccess() + } + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaTemplate.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaTemplate.kt index 0aa850b6..1cc0097e 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaTemplate.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaTemplate.kt @@ -4,6 +4,7 @@ package pl.szczodrzynski.edziennik.api.v2.librus.data.synergia +import org.jsoup.Jsoup import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusSynergia import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS @@ -14,7 +15,8 @@ class LibrusSynergiaTemplate(override val data: DataLibrus, val onSuccess: () -> } init { - /* synergiaGet(TAG, "") { doc -> + /* synergiaGet(TAG, "") { text -> + val doc = Jsoup.parse(text) data.setSyncNext(ENDPOINT_LIBRUS_SYNERGIA_, SYNC_ALWAYS) onSuccess() diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/Mobidziennik.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/Mobidziennik.kt index f94d7ec9..7656c921 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/Mobidziennik.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/Mobidziennik.kt @@ -64,6 +64,10 @@ class Mobidziennik(val app: App, val profile: Profile?, val loginStore: LoginSto } + override fun markAllAnnouncementsAsRead() { + + } + override fun firstLogin() { MobidziennikFirstLogin(data) { completed() @@ -105,4 +109,4 @@ class Mobidziennik(val app: App, val profile: Profile?, val loginStore: LoginSto } } } -} \ No newline at end of file +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/Template.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/Template.kt index b9949410..61b4ce81 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/Template.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/Template.kt @@ -63,6 +63,10 @@ class Template(val app: App, val profile: Profile?, val loginStore: LoginStore, } + override fun markAllAnnouncementsAsRead() { + + } + override fun firstLogin() { // TODO } @@ -102,4 +106,4 @@ class Template(val app: App, val profile: Profile?, val loginStore: LoginStore, } } } -} \ No newline at end of file +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/Vulcan.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/Vulcan.kt index 5997b72d..84cd1326 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/Vulcan.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/Vulcan.kt @@ -64,6 +64,10 @@ class Vulcan(val app: App, val profile: Profile?, val loginStore: LoginStore, va } + override fun markAllAnnouncementsAsRead() { + + } + override fun firstLogin() { VulcanFirstLogin(data) { completed() 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 8cf52b11..292b3875 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/receivers/SzkolnyReceiver.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/receivers/SzkolnyReceiver.kt @@ -9,10 +9,7 @@ import android.content.Context import android.content.Intent import org.greenrobot.eventbus.EventBus import pl.szczodrzynski.edziennik.api.v2.ApiService -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 +import pl.szczodrzynski.edziennik.api.v2.events.requests.* class SzkolnyReceiver : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { @@ -22,6 +19,7 @@ class SzkolnyReceiver : BroadcastReceiver() { "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)) + "AnnouncementsReadRequest" -> EventBus.getDefault().post(AnnouncementsReadRequest(intent.extras?.getInt("profileId", -1) ?: return)) } } -} \ No newline at end of file +}