[APIv2/Librus] Add marking all announcements as read using Synergia

This commit is contained in:
Kacper Ziubryniewicz 2019-10-26 14:36:21 +02:00
parent 843a8e4298
commit 3e99c111bd
13 changed files with 90 additions and 16 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -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<String, Any> = emptyMap(), onSuccess: (doc: Document) -> Unit) {
parameters: Map<String, Any> = 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)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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