[APIv2/Librus] Fix announcements duplicate ids (migrate from crc16 to crc32)

This commit is contained in:
Kacper Ziubryniewicz 2019-12-08 15:00:38 +01:00
parent 90343e1e39
commit b085d94fea
4 changed files with 26 additions and 12 deletions

View File

@ -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.announcements.Announcement
import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS 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.metadata.Metadata
import pl.szczodrzynski.edziennik.utils.Utils
import pl.szczodrzynski.edziennik.utils.models.Date import pl.szczodrzynski.edziennik.utils.models.Date
class LibrusApiAnnouncements(override val data: DataLibrus, class LibrusApiAnnouncements(override val data: DataLibrus,
@ -20,19 +19,19 @@ class LibrusApiAnnouncements(override val data: DataLibrus,
const val TAG = "LibrusApiAnnouncements" const val TAG = "LibrusApiAnnouncements"
} }
init { init { data.profile?.also { profile ->
apiGet(TAG, "SchoolNotices") { json -> apiGet(TAG, "SchoolNotices") { json ->
val announcements = json.getJsonArray("SchoolNotices").asJsonObjectList() val announcements = json.getJsonArray("SchoolNotices").asJsonObjectList()
announcements?.forEach { announcement -> announcements?.forEach { announcement ->
val id = Utils.crc16(announcement.getString("Id")?.toByteArray() val id = announcement.getString("Id")?.crc32() ?: return@forEach
?: return@forEach).toLong()
val subject = announcement.getString("Subject") ?: "" val subject = announcement.getString("Subject") ?: ""
val text = announcement.getString("Content") ?: "" val text = announcement.getString("Content") ?: ""
val startDate = Date.fromY_m_d(announcement.getString("StartDate")) val startDate = Date.fromY_m_d(announcement.getString("StartDate"))
val endDate = Date.fromY_m_d(announcement.getString("EndDate")) val endDate = Date.fromY_m_d(announcement.getString("EndDate"))
val teacherId = announcement.getJsonObject("AddedBy")?.getLong("Id") ?: -1 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 read = announcement.getBoolean("WasRead") ?: false
val announcementObject = Announcement( val announcementObject = Announcement(
@ -51,7 +50,7 @@ class LibrusApiAnnouncements(override val data: DataLibrus,
Metadata.TYPE_ANNOUNCEMENT, Metadata.TYPE_ANNOUNCEMENT,
id, id,
read, read,
read, profile.empty || read,
addedDate addedDate
)) ))
} }
@ -59,5 +58,5 @@ class LibrusApiAnnouncements(override val data: DataLibrus,
data.setSyncNext(ENDPOINT_LIBRUS_API_ANNOUNCEMENTS, SYNC_ALWAYS) data.setSyncNext(ENDPOINT_LIBRUS_API_ANNOUNCEMENTS, SYNC_ALWAYS)
onSuccess() onSuccess()
} }
} }}
} }

View File

@ -108,7 +108,7 @@ import pl.szczodrzynski.edziennik.utils.models.Date;
AttendanceType.class, AttendanceType.class,
pl.szczodrzynski.edziennik.data.db.modules.timetable.Lesson.class, pl.szczodrzynski.edziennik.data.db.modules.timetable.Lesson.class,
ConfigEntry.class, ConfigEntry.class,
Metadata.class}, version = 67) Metadata.class}, version = 68)
@TypeConverters({ @TypeConverters({
ConverterTime.class, ConverterTime.class,
ConverterDate.class, ConverterDate.class,
@ -815,6 +815,14 @@ public abstract class AppDb extends RoomDatabase {
database.execSQL("DROP TABLE _gradeCategories"); 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) { public static AppDb getDatabase(final Context context) {
@ -879,7 +887,8 @@ public abstract class AppDb extends RoomDatabase {
MIGRATION_63_64, MIGRATION_63_64,
MIGRATION_64_65, MIGRATION_64_65,
MIGRATION_65_66, MIGRATION_65_66,
MIGRATION_66_67 MIGRATION_66_67,
MIGRATION_67_68
) )
.allowMainThreadQueries() .allowMainThreadQueries()
//.fallbackToDestructiveMigration() //.fallbackToDestructiveMigration()

View File

@ -36,7 +36,7 @@ public abstract class AnnouncementDao {
"LEFT JOIN teachers USING(profileId, teacherId)\n" + "LEFT JOIN teachers USING(profileId, teacherId)\n" +
"LEFT JOIN metadata ON announcementId = thingId AND thingType = "+TYPE_ANNOUNCEMENT+" AND metadata.profileId = "+profileId+"\n" + "LEFT JOIN metadata ON announcementId = thingId AND thingType = "+TYPE_ANNOUNCEMENT+" AND metadata.profileId = "+profileId+"\n" +
"WHERE announcements.profileId = "+profileId+" AND "+filter+"\n" + "WHERE announcements.profileId = "+profileId+" AND "+filter+"\n" +
"ORDER BY announcementStartDate DESC")); "ORDER BY addedDate DESC"));
} }
public LiveData<List<AnnouncementFull>> getAll(int profileId) { public LiveData<List<AnnouncementFull>> getAll(int profileId) {
return getAll(profileId, "1"); return getAll(profileId, "1");
@ -55,7 +55,7 @@ public abstract class AnnouncementDao {
"LEFT JOIN teachers USING(profileId, teacherId)\n" + "LEFT JOIN teachers USING(profileId, teacherId)\n" +
"LEFT JOIN metadata ON announcementId = thingId AND thingType = "+TYPE_ANNOUNCEMENT+" AND metadata.profileId = "+profileId+"\n" + "LEFT JOIN metadata ON announcementId = thingId AND thingType = "+TYPE_ANNOUNCEMENT+" AND metadata.profileId = "+profileId+"\n" +
"WHERE announcements.profileId = "+profileId+" AND "+filter+"\n" + "WHERE announcements.profileId = "+profileId+" AND "+filter+"\n" +
"ORDER BY announcementStartDate DESC")); "ORDER BY addedDate DESC"));
} }
public List<AnnouncementFull> getNotNotifiedNow(int profileId) { public List<AnnouncementFull> getNotNotifiedNow(int profileId) {
return getAllNow(profileId, "notified = 0"); return getAllNow(profileId, "notified = 0");

View File

@ -62,7 +62,13 @@ public class AnnouncementsAdapter extends RecyclerView.Adapter<AnnouncementsAdap
b.announcementsItemSender.setText(item.teacherFullName); b.announcementsItemSender.setText(item.teacherFullName);
b.announcementsItemTitle.setText(item.subject); b.announcementsItemTitle.setText(item.subject);
b.announcementsItemText.setText(item.text); b.announcementsItemText.setText(item.text);
b.announcementsItemDate.setText(item.startDate.getFormattedString());
if (item.endDate == null) {
b.announcementsItemDate.setText(item.startDate.getFormattedString());
} else {
b.announcementsItemDate.setText(context.getString(R.string.date_relative_format, item.startDate.getFormattedStringShort(), item.endDate.getFormattedStringShort()));
}
if (!item.seen) { if (!item.seen) {
b.announcementsItemTitle.setBackground(context.getResources().getDrawable(R.drawable.bg_rounded_8dp)); b.announcementsItemTitle.setBackground(context.getResources().getDrawable(R.drawable.bg_rounded_8dp));
b.announcementsItemTitle.getBackground().setColorFilter(new PorterDuffColorFilter(0x692196f3, PorterDuff.Mode.MULTIPLY)); b.announcementsItemTitle.getBackground().setColorFilter(new PorterDuffColorFilter(0x692196f3, PorterDuff.Mode.MULTIPLY));