[APIv2] Improve Librus sync timers. Make saveData update app.profile. Decide when to sync lucky number.

This commit is contained in:
Kuba Szczodrzyński 2019-10-23 19:12:28 +02:00
parent 74ce9cd38d
commit 9fefae3da3
8 changed files with 55 additions and 19 deletions

View File

@ -5,7 +5,6 @@
package pl.szczodrzynski.edziennik.api.v2.librus
import pl.szczodrzynski.edziennik.api.v2.*
import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusData
import pl.szczodrzynski.edziennik.api.v2.models.Feature
const val ENDPOINT_LIBRUS_API_ME = 1001
@ -167,7 +166,7 @@ val LibrusFeatures = listOf(
*/
Feature(LOGIN_TYPE_LIBRUS, FEATURE_LUCKY_NUMBER, listOf(
ENDPOINT_LIBRUS_API_LUCKY_NUMBER to LOGIN_METHOD_LIBRUS_API
), listOf(LOGIN_METHOD_LIBRUS_API)),
), listOf(LOGIN_METHOD_LIBRUS_API)).withShouldSync { data -> data.shouldSyncLuckyNumber() },
/**
* Teacher list - using API.
*/

View File

@ -12,7 +12,6 @@ import pl.szczodrzynski.edziennik.api.v2.*
import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus
import pl.szczodrzynski.edziennik.api.v2.models.ApiError
import pl.szczodrzynski.edziennik.utils.Utils.d
import java.lang.Exception
open class LibrusSynergia(open val data: DataLibrus) {
companion object {
@ -25,8 +24,8 @@ open class LibrusSynergia(open val data: DataLibrus) {
val profile
get() = data.profile
fun apiGet(tag: String, endpoint: String, method: Int = GET,
parameters: Map<String, Any> = emptyMap(), onSuccess: (text: String) -> Unit) {
fun synergiaGet(tag: String, endpoint: String, method: Int = GET,
parameters: Map<String, Any> = emptyMap(), onSuccess: (text: String) -> Unit) {
d(tag, "Request: Librus/Synergia - $LIBRUS_SYNERGIA_URL$endpoint")
val callback = object : TextCallbackHandler() {

View File

@ -5,8 +5,8 @@
package pl.szczodrzynski.edziennik.api.v2.librus.data.api
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_ME
import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus
import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_ME
import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi
class LibrusApiMe(override val data: DataLibrus,
@ -21,7 +21,7 @@ class LibrusApiMe(override val data: DataLibrus,
val account = me?.getJsonObject("Account")
val user = me?.getJsonObject("User")
data.isPremium = account?.getBoolean("isPremium") == true || account?.getBoolean("isPremiumDemo") == true
data.isPremium = account?.getBoolean("IsPremium") == true || account?.getBoolean("IsPremiumDemo") == true
val isParent = account?.getInt("GroupId") == 5
data.profile?.accountNameLong =

View File

@ -5,10 +5,10 @@
package pl.szczodrzynski.edziennik.api.v2.librus.data.api
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.MainActivity.Companion.DRAWER_ITEM_AGENDA
import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus
import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_TEACHER_FREE_DAYS
import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi
import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS
import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata
import pl.szczodrzynski.edziennik.data.db.modules.teachers.TeacherAbsence
import pl.szczodrzynski.edziennik.utils.models.Date
@ -60,7 +60,7 @@ class LibrusApiTeacherFreeDays(override val data: DataLibrus,
))
}
data.setSyncNext(ENDPOINT_LIBRUS_API_TEACHER_FREE_DAYS, SYNC_ALWAYS)
data.setSyncNext(ENDPOINT_LIBRUS_API_TEACHER_FREE_DAYS, 6*HOUR, DRAWER_ITEM_AGENDA)
onSuccess()
}
}

View File

@ -5,11 +5,12 @@
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
import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus
import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_SYNERGIA_HOMEWORK
import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusSynergia
import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS
import pl.szczodrzynski.edziennik.data.db.modules.events.Event
import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata
import pl.szczodrzynski.edziennik.get
@ -22,8 +23,12 @@ class LibrusSynergiaHomework(override val data: DataLibrus, val onSuccess: () ->
}
init {
apiGet(TAG, "moje_zadania", method = POST, parameters = mapOf(
"dataOd" to Date.getToday().stringY_m_d,
synergiaGet(TAG, "moje_zadania", method = POST, parameters = mapOf(
"dataOd" to
if (data.profile?.empty != false)
profile!!.getSemesterStart(1).stringY_m_d
else
Date.getToday().stringY_m_d,
"dataDo" to profile!!.getSemesterEnd(profile?.currentSemester ?: 2).stringY_m_d,
"przedmiot" to -1
@ -87,7 +92,8 @@ class LibrusSynergiaHomework(override val data: DataLibrus, val onSuccess: () ->
}
}
data.setSyncNext(ENDPOINT_LIBRUS_SYNERGIA_HOMEWORK, SYNC_ALWAYS)
// because this requires a synergia login (2 more requests) sync this every two hours or if explicit :D
data.setSyncNext(ENDPOINT_LIBRUS_SYNERGIA_HOMEWORK, 2*HOUR, DRAWER_ITEM_HOMEWORK)
onSuccess()
}
}

View File

@ -1,6 +1,7 @@
package pl.szczodrzynski.edziennik.api.v2.librus.firstlogin
import org.greenrobot.eventbus.EventBus
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.api.v2.ERROR_NO_STUDENTS_IN_ACCOUNT
import pl.szczodrzynski.edziennik.api.v2.LIBRUS_ACCOUNTS_URL
import pl.szczodrzynski.edziennik.api.v2.LOGIN_MODE_LIBRUS_EMAIL
@ -14,10 +15,6 @@ import pl.szczodrzynski.edziennik.api.v2.models.ApiError
import pl.szczodrzynski.edziennik.data.api.AppError.CODE_LIBRUS_DISCONNECTED
import pl.szczodrzynski.edziennik.data.api.AppError.CODE_SYNERGIA_NOT_ACTIVATED
import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile
import pl.szczodrzynski.edziennik.getInt
import pl.szczodrzynski.edziennik.getJsonArray
import pl.szczodrzynski.edziennik.getLong
import pl.szczodrzynski.edziennik.getString
class LibrusFirstLogin(val data: DataLibrus, val onSuccess: () -> Unit) {
companion object {
@ -81,7 +78,7 @@ class LibrusFirstLogin(val data: DataLibrus, val onSuccess: () -> Unit) {
newProfile.putStudentData("accountId", accountIds[index])
newProfile.putStudentData("accountLogin", accountLogins[index])
newProfile.putStudentData("accountToken", accountTokens[index])
newProfile.putStudentData("accountTokenTime", accountDataTime ?: 0)
newProfile.putStudentData("accountTokenTime", (accountDataTime ?: 0) + DAY)
profileList.add(newProfile)
}

View File

@ -197,6 +197,37 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore)
db.profileDao().add(profile)
db.loginStoreDao().add(loginStore)
if (profile.id == app.profile?.id) {
app.profile.apply {
name = profile.name
subname = profile.subname
syncEnabled = profile.syncEnabled
loggedIn = profile.loggedIn
empty = profile.empty
archived = profile.archived
studentNameLong = profile.studentNameLong
studentNameShort = profile.studentNameShort
studentNumber = profile.studentNumber
studentData = profile.studentData
accountNameLong = profile.accountNameLong
yearAverageMode = profile.yearAverageMode
currentSemester = profile.currentSemester
attendancePercentage = profile.attendancePercentage
dateSemester1Start = profile.dateSemester1Start
dateSemester2Start = profile.dateSemester2Start
dateYearEnd = profile.dateYearEnd
luckyNumberEnabled = profile.luckyNumberEnabled
luckyNumber = profile.luckyNumber
luckyNumberDate = profile.luckyNumberDate
lastFullSync = profile.lastFullSync
lastReceiversSync = profile.lastReceiversSync
}
}
if (loginStore.id == app.profile?.loginStoreId) {
app.loginStore = loginStore.data
app.profile.loginStoreData = loginStore.data
}
db.endpointTimerDao().addAll(endpointTimers)
db.teacherDao().clear(profileId)
db.teacherDao().addAll(teacherList.values())
@ -293,6 +324,10 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore)
saveData()
}
fun shouldSyncLuckyNumber(): Boolean {
return (db.luckyNumberDao().getNearestFutureNow(profileId, Date.getToday()) ?: -1) == -1
}
fun error(tag: String, errorCode: Int, response: Response? = null, throwable: Throwable? = null, apiResponse: JsonObject? = null) {
val code = when (throwable) {
is UnknownHostException, is SSLException, is InterruptedIOException -> CODE_NO_INTERNET

View File

@ -36,7 +36,7 @@ public abstract class LuckyNumberDao {
public abstract LuckyNumber getByDateNow(int profileId, Date date);
@Nullable
@Query("SELECT * FROM luckyNumbers WHERE profileId = :profileId AND luckyNumberDate > :date ORDER BY luckyNumberDate ASC LIMIT 1")
@Query("SELECT * FROM luckyNumbers WHERE profileId = :profileId AND luckyNumberDate >= :date ORDER BY luckyNumberDate DESC LIMIT 1")
public abstract LuckyNumber getNearestFutureNow(int profileId, Date date);
@RawQuery(observedEntities = {LuckyNumber.class})