From fabacfdcca080ced73836037846c533dc5f001a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Sat, 5 Oct 2019 12:26:52 +0200 Subject: [PATCH] [APIv2/Librus] Update endpoints: API/Schools, API/Events --- .../edziennik/api/v2/librus/data/LibrusApi.kt | 6 ++++ .../api/v2/librus/data/api/LibrusApiEvents.kt | 4 +-- .../v2/librus/data/api/LibrusApiSchools.kt | 35 +++++++++++-------- .../edziennik/api/v2/models/Data.kt | 5 +++ .../edziennik/data/db/AppDb.java | 19 ++++++++-- .../data/db/modules/lessons/LessonRangeDao.kt | 26 ++++++++++++++ app/src/main/res/values/strings.xml | 1 + 7 files changed, 77 insertions(+), 19 deletions(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/lessons/LessonRangeDao.kt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusApi.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusApi.kt index b79136ae..5484b6fa 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusApi.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusApi.kt @@ -18,6 +18,12 @@ open class LibrusApi(open val data: DataLibrus) { const val TAG = "LibrusApi" } + val profileId + get() = data.profile?.id ?: -1 + + val profile + get() = data.profile + fun apiGet(tag: String, endpoint: String, method: Int = GET, payload: JsonObject? = null, onSuccess: (json: JsonObject?) -> Unit) { val callback = object : JsonCallbackHandler() { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiEvents.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiEvents.kt index 9b8daeb4..2badd084 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiEvents.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiEvents.kt @@ -14,13 +14,13 @@ class LibrusApiEvents(override val data: DataLibrus, } init { - apiGet(LibrusApiMe.TAG, "") { json -> + /*apiGet(LibrusApiMe.TAG, "") { json -> // on error data.error(TAG, ERROR_LIBRUS_API_, response, json) data.setSyncNext(ENDPOINT_LIBRUS_API_, 2 * DAY) onSuccess() - } + }*/ } } \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiSchools.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiSchools.kt index ac41bcdb..53bea29a 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiSchools.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiSchools.kt @@ -6,11 +6,11 @@ package pl.szczodrzynski.edziennik.api.v2.librus.data.api import android.util.Pair import com.google.gson.JsonNull +import pl.szczodrzynski.edziennik.* +import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_LIBRUS_API_SCHOOLS import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi -import pl.szczodrzynski.edziennik.getInt -import pl.szczodrzynski.edziennik.getJsonObject -import pl.szczodrzynski.edziennik.getString +import pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonRange import pl.szczodrzynski.edziennik.utils.models.Time import java.util.* @@ -26,6 +26,8 @@ class LibrusApiSchools(override val data: DataLibrus, val schoolId = school?.getInt("Id") val schoolNameLong = school?.getString("Name") + // create the school's short name using first letters of each long name's word + // append the town name and save to student data var schoolNameShort = "" schoolNameLong?.split(" ")?.forEach { if (it.isBlank()) @@ -35,21 +37,24 @@ class LibrusApiSchools(override val data: DataLibrus, val schoolTown = school?.getString("Town")?.toLowerCase(Locale.getDefault()) data.schoolName = schoolId.toString() + schoolNameShort + "_" + schoolTown - /*lessonRanges.clear() - for ((index, lessonRangeEl) in school.get("LessonsRange").getAsJsonArray().withIndex()) { - val lr = lessonRangeEl.getAsJsonObject() - val from = lr.get("From") - val to = lr.get("To") - if (from != null && to != null && from !is JsonNull && to !is JsonNull) { - lessonRanges.put(index, Pair(Time.fromH_m(from!!.getAsString()), Time.fromH_m(to!!.getAsString()))) + school?.getJsonArray("LessonsRange")?.let { ranges -> + data.lessonRanges.clear() + ranges.forEachIndexed { index, rangeEl -> + val range = rangeEl.asJsonObject + val from = range.getString("From") ?: return@forEachIndexed + val to = range.getString("To") ?: return@forEachIndexed + data.lessonRanges.add( + LessonRange( + profileId, + index, + Time.fromH_m(from), + Time.fromH_m(to) + )) } } - profile.putStudentData("lessonRanges", app.gson.toJson(lessonRanges)) - // on error - data.error(TAG, ERROR_LIBRUS_API_, response, json) - data.setSyncNext(ENDPOINT_LIBRUS_API_, 2 * DAY) - onSuccess()*/ + data.setSyncNext(ENDPOINT_LIBRUS_API_SCHOOLS, 4 * DAY) + onSuccess() } } } \ No newline at end of file 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 58c8e663..d0c9de40 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 @@ -17,6 +17,7 @@ import pl.szczodrzynski.edziennik.data.db.modules.grades.Grade import pl.szczodrzynski.edziennik.data.db.modules.grades.GradeCategory import pl.szczodrzynski.edziennik.data.db.modules.lessons.Lesson import pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonChange +import pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonRange import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore import pl.szczodrzynski.edziennik.data.db.modules.messages.Message import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageRecipient @@ -90,6 +91,7 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) val teacherList = LongSparseArray() val subjectList = LongSparseArray() val teamList = mutableListOf() + var lessonRanges = mutableListOf() var lessonsToRemove: DataRemoveModel? = null val lessonList = mutableListOf() @@ -129,6 +131,8 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) db.endpointTimerDao().getAllNow(profile.id).forEach { endpointTimer -> endpointTimers.add(endpointTimer) } + + lessonRanges = db.lessonRangeDao().getAllNow(profile.id).toMutableList() db.teacherDao().getAllNow(profile.id).forEach { teacher -> teacherList.put(teacher.id, teacher) } @@ -151,6 +155,7 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) teacherList.clear() subjectList.clear() teamList.clear() + lessonRanges.clear() lessonList.clear() lessonChangeList.clear() gradeCategoryList.clear() diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/AppDb.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/AppDb.java index 51928dea..3d77767c 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/AppDb.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/AppDb.java @@ -35,6 +35,7 @@ import pl.szczodrzynski.edziennik.data.db.modules.lessons.Lesson; import pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonChange; import pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonChangeDao; import pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonDao; +import pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonRangeDao; import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore; import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStoreDao; import pl.szczodrzynski.edziennik.data.db.modules.luckynumber.LuckyNumber; @@ -84,7 +85,7 @@ import android.content.Context; MessageRecipient.class, DebugLog.class, EndpointTimer.class, - Metadata.class}, version = 55) + Metadata.class}, version = 56) @TypeConverters({ ConverterTime.class, ConverterDate.class, @@ -115,6 +116,7 @@ public abstract class AppDb extends RoomDatabase { public abstract MessageRecipientDao messageRecipientDao(); public abstract DebugLogDao debugLogDao(); public abstract EndpointTimerDao endpointTimerDao(); + public abstract LessonRangeDao lessonRangeDao(); public abstract MetadataDao metadataDao(); private static volatile AppDb INSTANCE; @@ -575,6 +577,18 @@ public abstract class AppDb extends RoomDatabase { ")"); } }; + private static final Migration MIGRATION_55_56 = new Migration(55, 56) { + @Override + public void migrate(@NonNull SupportSQLiteDatabase database) { + database.execSQL("CREATE TABLE IF NOT EXISTS lessonRanges (" + + "profileId INTEGER NOT NULL," + + "lessonRangeNumber INTEGER NOT NULL," + + "lessonRangeStart TEXT NOT NULL," + + "lessonRangeEnd TEXT NOT NULL," + + "PRIMARY KEY(profileId, lessonRangeNumber)" + + ")"); + } + }; public static AppDb getDatabase(final Context context) { @@ -627,7 +641,8 @@ public abstract class AppDb extends RoomDatabase { MIGRATION_51_52, MIGRATION_52_53, MIGRATION_53_54, - MIGRATION_54_55 + MIGRATION_54_55, + MIGRATION_55_56 ) .allowMainThreadQueries() //.fallbackToDestructiveMigration() diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/lessons/LessonRangeDao.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/lessons/LessonRangeDao.kt new file mode 100644 index 00000000..38bb27db --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/lessons/LessonRangeDao.kt @@ -0,0 +1,26 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-10-5. + */ + +package pl.szczodrzynski.edziennik.data.db.modules.lessons + +import androidx.room.Dao +import androidx.room.Insert +import androidx.room.OnConflictStrategy +import androidx.room.Query + +@Dao +interface LessonRangeDao { + + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun add(lessonRange: LessonRange) + + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun addAll(lessonRangeList: List) + + @Query("SELECT * FROM lessonRanges WHERE profileId = :profileId") + fun getAllNow(profileId: Int): List + + @Query("DELETE FROM lessonRanges WHERE profileId = :profileId") + fun clear(profileId: Int) +} \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2cae0111..be2863e0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -924,4 +924,5 @@ Nieobecni nauczyciele Pobieranie komentarzy ocen Pokazuj nieobecności nauczycieli w Terminarzu + Pobieram informacje o szkole...