From b085d94fead1c6445000ef6fe91d84287c5aae3c Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Sun, 8 Dec 2019 15:00:38 +0100 Subject: [PATCH] [APIv2/Librus] Fix announcements duplicate ids (migrate from crc16 to crc32) --- .../v2/librus/data/api/LibrusApiAnnouncements.kt | 13 ++++++------- .../pl/szczodrzynski/edziennik/data/db/AppDb.java | 13 +++++++++++-- .../db/modules/announcements/AnnouncementDao.java | 4 ++-- .../modules/announcements/AnnouncementsAdapter.java | 8 +++++++- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiAnnouncements.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiAnnouncements.kt index e05f1ef0..e547d292 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiAnnouncements.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiAnnouncements.kt @@ -11,7 +11,6 @@ import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi import pl.szczodrzynski.edziennik.data.db.modules.announcements.Announcement import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata -import pl.szczodrzynski.edziennik.utils.Utils import pl.szczodrzynski.edziennik.utils.models.Date class LibrusApiAnnouncements(override val data: DataLibrus, @@ -20,19 +19,19 @@ class LibrusApiAnnouncements(override val data: DataLibrus, const val TAG = "LibrusApiAnnouncements" } - init { + init { data.profile?.also { profile -> apiGet(TAG, "SchoolNotices") { json -> val announcements = json.getJsonArray("SchoolNotices").asJsonObjectList() announcements?.forEach { announcement -> - val id = Utils.crc16(announcement.getString("Id")?.toByteArray() - ?: return@forEach).toLong() + val id = announcement.getString("Id")?.crc32() ?: return@forEach val subject = announcement.getString("Subject") ?: "" val text = announcement.getString("Content") ?: "" val startDate = Date.fromY_m_d(announcement.getString("StartDate")) val endDate = Date.fromY_m_d(announcement.getString("EndDate")) val teacherId = announcement.getJsonObject("AddedBy")?.getLong("Id") ?: -1 - val addedDate = Date.fromIso(announcement.getString("CreationDate")) + val addedDate = announcement.getString("CreationDate")?.let { Date.fromIso(it) } + ?: System.currentTimeMillis() val read = announcement.getBoolean("WasRead") ?: false val announcementObject = Announcement( @@ -51,7 +50,7 @@ class LibrusApiAnnouncements(override val data: DataLibrus, Metadata.TYPE_ANNOUNCEMENT, id, read, - read, + profile.empty || read, addedDate )) } @@ -59,5 +58,5 @@ class LibrusApiAnnouncements(override val data: DataLibrus, data.setSyncNext(ENDPOINT_LIBRUS_API_ANNOUNCEMENTS, SYNC_ALWAYS) onSuccess() } - } + }} } 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 e811f765..15352942 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 @@ -108,7 +108,7 @@ import pl.szczodrzynski.edziennik.utils.models.Date; AttendanceType.class, pl.szczodrzynski.edziennik.data.db.modules.timetable.Lesson.class, ConfigEntry.class, - Metadata.class}, version = 67) + Metadata.class}, version = 68) @TypeConverters({ ConverterTime.class, ConverterDate.class, @@ -815,6 +815,14 @@ public abstract class AppDb extends RoomDatabase { database.execSQL("DROP TABLE _gradeCategories"); } }; + private static final Migration MIGRATION_67_68 = new Migration(67, 68) { + @Override + public void migrate(@NonNull SupportSQLiteDatabase database) { + /* Migration from crc16 to crc32 id */ + database.execSQL("DELETE FROM announcements"); + database.execSQL("DELETE FROM metadata WHERE thingType=7"); + } + }; public static AppDb getDatabase(final Context context) { @@ -879,7 +887,8 @@ public abstract class AppDb extends RoomDatabase { MIGRATION_63_64, MIGRATION_64_65, MIGRATION_65_66, - MIGRATION_66_67 + MIGRATION_66_67, + MIGRATION_67_68 ) .allowMainThreadQueries() //.fallbackToDestructiveMigration() diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/announcements/AnnouncementDao.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/announcements/AnnouncementDao.java index a7a81283..e14c966a 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/announcements/AnnouncementDao.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/announcements/AnnouncementDao.java @@ -36,7 +36,7 @@ public abstract class AnnouncementDao { "LEFT JOIN teachers USING(profileId, teacherId)\n" + "LEFT JOIN metadata ON announcementId = thingId AND thingType = "+TYPE_ANNOUNCEMENT+" AND metadata.profileId = "+profileId+"\n" + "WHERE announcements.profileId = "+profileId+" AND "+filter+"\n" + - "ORDER BY announcementStartDate DESC")); + "ORDER BY addedDate DESC")); } public LiveData> getAll(int profileId) { return getAll(profileId, "1"); @@ -55,7 +55,7 @@ public abstract class AnnouncementDao { "LEFT JOIN teachers USING(profileId, teacherId)\n" + "LEFT JOIN metadata ON announcementId = thingId AND thingType = "+TYPE_ANNOUNCEMENT+" AND metadata.profileId = "+profileId+"\n" + "WHERE announcements.profileId = "+profileId+" AND "+filter+"\n" + - "ORDER BY announcementStartDate DESC")); + "ORDER BY addedDate DESC")); } public List getNotNotifiedNow(int profileId) { return getAllNow(profileId, "notified = 0"); diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/announcements/AnnouncementsAdapter.java b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/announcements/AnnouncementsAdapter.java index 8d02a678..a5501472 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/announcements/AnnouncementsAdapter.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/announcements/AnnouncementsAdapter.java @@ -62,7 +62,13 @@ public class AnnouncementsAdapter extends RecyclerView.Adapter