From 9fefae3da34fb5dfd11c9a23f9318df3a149079c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Wed, 23 Oct 2019 19:12:28 +0200 Subject: [PATCH] [APIv2] Improve Librus sync timers. Make saveData update app.profile. Decide when to sync lucky number. --- .../edziennik/api/v2/librus/LibrusFeatures.kt | 3 +- .../api/v2/librus/data/LibrusSynergia.kt | 5 ++- .../api/v2/librus/data/api/LibrusApiMe.kt | 4 +-- .../data/api/LibrusApiTeacherFreeDays.kt | 4 +-- .../data/synergia/LibrusSynergiaHomework.kt | 14 +++++--- .../v2/librus/firstlogin/LibrusFirstLogin.kt | 7 ++-- .../edziennik/api/v2/models/Data.kt | 35 +++++++++++++++++++ .../modules/luckynumber/LuckyNumberDao.java | 2 +- 8 files changed, 55 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/LibrusFeatures.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/LibrusFeatures.kt index e92a2661..7e0b26d3 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/LibrusFeatures.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/LibrusFeatures.kt @@ -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. */ 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 9b449ce0..32e1bc78 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 @@ -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 = emptyMap(), onSuccess: (text: String) -> Unit) { + fun synergiaGet(tag: String, endpoint: String, method: Int = GET, + parameters: Map = emptyMap(), onSuccess: (text: String) -> Unit) { d(tag, "Request: Librus/Synergia - $LIBRUS_SYNERGIA_URL$endpoint") val callback = object : TextCallbackHandler() { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiMe.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiMe.kt index ad1a8efb..8a5df3d0 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiMe.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiMe.kt @@ -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 = diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiTeacherFreeDays.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiTeacherFreeDays.kt index d4791b39..3be6fabb 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiTeacherFreeDays.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiTeacherFreeDays.kt @@ -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() } } 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 60868dc4..5f63bcad 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 @@ -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() } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/firstlogin/LibrusFirstLogin.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/firstlogin/LibrusFirstLogin.kt index 5a92178c..9edb39b8 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/firstlogin/LibrusFirstLogin.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/firstlogin/LibrusFirstLogin.kt @@ -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) } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt index d573068c..5baa62bb 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt @@ -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 diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/luckynumber/LuckyNumberDao.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/luckynumber/LuckyNumberDao.java index ced9b474..fa80f6f6 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/luckynumber/LuckyNumberDao.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/luckynumber/LuckyNumberDao.java @@ -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})