[Firebase/Vulcan] Implement push notifications sync.

This commit is contained in:
Kuba Szczodrzyński 2020-02-20 21:20:51 +01:00
parent c2b8f71467
commit 15c8134d13
6 changed files with 82 additions and 4 deletions

View File

@ -8,6 +8,7 @@ import pl.szczodrzynski.edziennik.data.api.*
import pl.szczodrzynski.edziennik.data.api.models.Feature
const val ENDPOINT_VULCAN_API_UPDATE_SEMESTER = 1000
const val ENDPOINT_VULCAN_API_PUSH_CONFIG = 1005
const val ENDPOINT_VULCAN_API_DICTIONARIES = 1010
const val ENDPOINT_VULCAN_API_TIMETABLE = 1020
const val ENDPOINT_VULCAN_API_EVENTS = 1030
@ -53,6 +54,13 @@ val VulcanFeatures = listOf(
ENDPOINT_VULCAN_API_MESSAGES_SENT to LOGIN_METHOD_VULCAN_API
), listOf(LOGIN_METHOD_VULCAN_API)),
// push config
Feature(LOGIN_TYPE_VULCAN, FEATURE_PUSH_CONFIG, listOf(
ENDPOINT_VULCAN_API_PUSH_CONFIG to LOGIN_METHOD_VULCAN_API
), listOf(LOGIN_METHOD_VULCAN_API)).withShouldSync { data ->
!data.app.config.sync.tokenVulcanList.contains(data.profileId)
},
Feature(LOGIN_TYPE_VULCAN, FEATURE_ALWAYS_NEEDED, listOf(
ENDPOINT_VULCAN_API_UPDATE_SEMESTER to LOGIN_METHOD_VULCAN_API,
ENDPOINT_VULCAN_API_DICTIONARIES to LOGIN_METHOD_VULCAN_API

View File

@ -68,6 +68,7 @@ open class VulcanApi(open val data: DataVulcan, open val lastSync: Long?) {
is Long -> finalPayload.addProperty(name, value)
is Float -> finalPayload.addProperty(name, value)
is Char -> finalPayload.addProperty(name, value)
is Boolean -> finalPayload.addProperty(name, value)
}
}
finalPayload.addProperty("RemoteMobileTimeKey", System.currentTimeMillis() / 1000)

View File

@ -42,6 +42,10 @@ class VulcanData(val data: DataVulcan, val onSuccess: () -> Unit) {
data.startProgress(R.string.edziennik_progress_endpoint_student_info)
VulcanApiUpdateSemester(data, lastSync, onSuccess)
}
ENDPOINT_VULCAN_API_PUSH_CONFIG -> {
data.startProgress(R.string.edziennik_progress_endpoint_push_config)
VulcanApiPushConfig(data, lastSync, onSuccess)
}
ENDPOINT_VULCAN_API_DICTIONARIES -> {
data.startProgress(R.string.edziennik_progress_endpoint_dictionaries)
VulcanApiDictionaries(data, lastSync, onSuccess)

View File

@ -0,0 +1,37 @@
/*
* Copyright (c) Kuba Szczodrzyński 2020-2-20.
*/
package pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.data.api
import pl.szczodrzynski.edziennik.data.api.VULCAN_API_ENDPOINT_PUSH
import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.DataVulcan
import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.ENDPOINT_VULCAN_API_PUSH_CONFIG
import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.data.VulcanApi
import pl.szczodrzynski.edziennik.data.db.entity.SYNC_ALWAYS
class VulcanApiPushConfig(override val data: DataVulcan,
override val lastSync: Long?,
val onSuccess: (endpointId: Int) -> Unit
) : VulcanApi(data, lastSync) {
companion object {
const val TAG = "VulcanApiPushConfig"
}
init { data.app.config.sync.tokenVulcan?.also { tokenVulcan ->
apiGet(TAG, VULCAN_API_ENDPOINT_PUSH, parameters = mapOf(
"Token" to tokenVulcan,
"IdUczen" to data.studentId,
"PushOcena" to true,
"PushFrekwencja" to true,
"PushUwaga" to true,
"PushWiadomosc" to true
)) { _, _ ->
// sync always: this endpoint has .shouldSync set
data.setSyncNext(ENDPOINT_VULCAN_API_PUSH_CONFIG, SYNC_ALWAYS)
data.app.config.sync.tokenVulcanList =
data.app.config.sync.tokenVulcanList + profileId
onSuccess(ENDPOINT_VULCAN_API_PUSH_CONFIG)
}
} ?: onSuccess(ENDPOINT_VULCAN_API_PUSH_CONFIG) }
}

View File

@ -9,6 +9,7 @@ import pl.szczodrzynski.edziennik.MainActivity.Companion.DRAWER_ITEM_ATTENDANCE
import pl.szczodrzynski.edziennik.MainActivity.Companion.DRAWER_ITEM_BEHAVIOUR
import pl.szczodrzynski.edziennik.MainActivity.Companion.DRAWER_ITEM_GRADES
import pl.szczodrzynski.edziennik.MainActivity.Companion.DRAWER_ITEM_MESSAGES
import pl.szczodrzynski.edziennik.data.api.LOGIN_TYPE_MOBIDZIENNIK
import pl.szczodrzynski.edziennik.data.api.edziennik.EdziennikTask
import pl.szczodrzynski.edziennik.data.api.task.IApiTask
import pl.szczodrzynski.edziennik.data.db.entity.Message.TYPE_RECEIVED
@ -60,7 +61,8 @@ class SzkolnyMobidziennikFirebase(val app: App, val profiles: List<Profile>, val
}
val tasks = profiles.filter {
it.getStudentData("globalId", 0L) == globalId
it.loginStoreType == LOGIN_TYPE_MOBIDZIENNIK &&
it.getStudentData("globalId", 0L) == globalId
}.map {
EdziennikTask.syncProfile(it.id, listOf(viewIdPair))
}

View File

@ -4,8 +4,13 @@
package pl.szczodrzynski.edziennik.data.firebase
import pl.szczodrzynski.edziennik.App
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.data.api.LOGIN_TYPE_VULCAN
import pl.szczodrzynski.edziennik.data.api.edziennik.EdziennikTask
import pl.szczodrzynski.edziennik.data.api.task.IApiTask
import pl.szczodrzynski.edziennik.data.db.entity.Message
import pl.szczodrzynski.edziennik.data.db.entity.Profile
import java.util.*
class SzkolnyVulcanFirebase(val app: App, val profiles: List<Profile>, val message: FirebaseService.Message) {
/*{
@ -20,7 +25,28 @@ class SzkolnyVulcanFirebase(val app: App, val profiles: List<Profile>, val messa
"title": "Frekwencja",
"message: "Uczeń Janósz otrzymał nieobecność na 7 lekcji"
}*/
init {
init { run {
val data = message.data.getJsonObject("data") ?: return@run
val type = data.getString("table") ?: return@run
val studentId = data.getInt("pupilid")
}
/* pl.vulcan.uonetmobile.auxilary.enums.CDCPushEnum */
val viewIdPair = when (type.toLowerCase(Locale.ROOT)) {
"wiadomosc" -> MainActivity.DRAWER_ITEM_MESSAGES to Message.TYPE_RECEIVED
"ocena" -> MainActivity.DRAWER_ITEM_GRADES to 0
"uwaga" -> MainActivity.DRAWER_ITEM_BEHAVIOUR to 0
"frekwencja" -> MainActivity.DRAWER_ITEM_ATTENDANCE to 0
// this type is not even implemented in Dzienniczek+
"sprawdzian" -> MainActivity.DRAWER_ITEM_AGENDA to 0
else -> return@run
}
val tasks = profiles.filter {
it.loginStoreType == LOGIN_TYPE_VULCAN &&
it.getStudentData("studentId", 0) == studentId
}.map {
EdziennikTask.syncProfile(it.id, listOf(viewIdPair))
}
IApiTask.enqueueAll(app, tasks)
}}
}