From 05ce790587c0266f330e286a88f702f83f23cb72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Wed, 23 Oct 2019 23:00:20 +0200 Subject: [PATCH] [APIv2] Add Librus Units. Add Classroom, NoticeType, AttendanceType entities. --- .../edziennik/api/v2/librus/DataLibrus.kt | 19 +++++++- .../api/v2/librus/data/api/LibrusApiUnits.kt | 44 +++++++++++++++++++ .../edziennik/data/db/AppDb.java | 42 +++++++++++++++++- .../db/modules/attendance/AttendanceType.kt | 23 ++++++++++ .../modules/attendance/AttendanceTypeDao.kt | 25 +++++++++++ .../data/db/modules/classrooms/Classroom.kt | 19 ++++++++ .../db/modules/classrooms/ClassroomDao.kt | 25 +++++++++++ .../data/db/modules/notices/NoticeType.kt | 19 ++++++++ .../data/db/modules/notices/NoticeTypeDao.kt | 25 +++++++++++ 9 files changed, 238 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiUnits.kt create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/attendance/AttendanceType.kt create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/attendance/AttendanceTypeDao.kt create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/classrooms/Classroom.kt create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/classrooms/ClassroomDao.kt create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/notices/NoticeType.kt create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/notices/NoticeTypeDao.kt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/DataLibrus.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/DataLibrus.kt index 2f2a0d10..600453b1 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/DataLibrus.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/DataLibrus.kt @@ -215,4 +215,21 @@ class DataLibrus(app: App, profile: Profile?, loginStore: LoginStore) : Data(app get() { mUnitId = mUnitId ?: profile?.getStudentData("unitId", 0L); return mUnitId ?: 0L } set(value) { profile?.putStudentData("unitId", value) ?: return; mUnitId = value } -} + private var mStartPointsSemester1: Int? = null + var startPointsSemester1: Int + get() { mStartPointsSemester1 = mStartPointsSemester1 ?: profile?.getStudentData("startPointsSemester1", 0); return mStartPointsSemester1 ?: 0 } + set(value) { profile?.putStudentData("startPointsSemester1", value) ?: return; mStartPointsSemester1 = value } + private var mStartPointsSemester2: Int? = null + var startPointsSemester2: Int + get() { mStartPointsSemester2 = mStartPointsSemester2 ?: profile?.getStudentData("startPointsSemester2", 0); return mStartPointsSemester2 ?: 0 } + set(value) { profile?.putStudentData("startPointsSemester2", value) ?: return; mStartPointsSemester2 = value } + + private var mEnablePointGrades: Boolean? = null + var enablePointGrades: Boolean + get() { mEnablePointGrades = mEnablePointGrades ?: profile?.getStudentData("enablePointGrades", true); return mEnablePointGrades ?: true } + set(value) { profile?.putStudentData("enablePointGrades", value) ?: return; mEnablePointGrades = value } + private var mEnableDescriptiveGrades: Boolean? = null + var enableDescriptiveGrades: Boolean + get() { mEnableDescriptiveGrades = mEnableDescriptiveGrades ?: profile?.getStudentData("enableDescriptiveGrades", true); return mEnableDescriptiveGrades ?: true } + set(value) { profile?.putStudentData("enableDescriptiveGrades", value) ?: return; mEnableDescriptiveGrades = value } +} \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiUnits.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiUnits.kt new file mode 100644 index 00000000..78d10103 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiUnits.kt @@ -0,0 +1,44 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-10-23. + */ + +package pl.szczodrzynski.edziennik.api.v2.librus.data.api + +import pl.szczodrzynski.edziennik.* +import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus +import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_UNITS +import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi + +class LibrusApiUnits(override val data: DataLibrus, + val onSuccess: () -> Unit) : LibrusApi(data) { + companion object { + const val TAG = "LibrusApiUnits" + } + + init { run { + if (data.unitId == 0L) { + data.setSyncNext(ENDPOINT_LIBRUS_API_UNITS, 12 * DAY) + onSuccess() + return@run + } + + apiGet(TAG, "Units") { json -> + val units = json.getJsonArray("Units") + units?.singleOrNull { it.asJsonObject.getLong("Id") == data.unitId }?.also { unitEl -> + val unit = unitEl.asJsonObject + val startPoints = unit.getJsonObject("BehaviourGradesSettings")?.getJsonObject("StartPoints") + startPoints?.apply { + data.startPointsSemester1 = getInt("Semester1", defaultValue = 0) + data.startPointsSemester2 = getInt("Semester2", defaultValue = data.startPointsSemester1) + } + unit.getJsonObject("GradesSettings")?.apply { + data.enablePointGrades = getBoolean("PointGradesEnabled", true) + data.enableDescriptiveGrades = getBoolean("DescriptiveGradesEnabled", true) + } + } + + data.setSyncNext(ENDPOINT_LIBRUS_API_UNITS, 7 * DAY) + 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 6d850b7b..d43d5301 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 @@ -22,6 +22,10 @@ import pl.szczodrzynski.edziennik.data.db.modules.api.EndpointTimer; import pl.szczodrzynski.edziennik.data.db.modules.api.EndpointTimerDao; import pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance; import pl.szczodrzynski.edziennik.data.db.modules.attendance.AttendanceDao; +import pl.szczodrzynski.edziennik.data.db.modules.attendance.AttendanceType; +import pl.szczodrzynski.edziennik.data.db.modules.attendance.AttendanceTypeDao; +import pl.szczodrzynski.edziennik.data.db.modules.classrooms.Classroom; +import pl.szczodrzynski.edziennik.data.db.modules.classrooms.ClassroomDao; import pl.szczodrzynski.edziennik.data.db.modules.debuglog.DebugLog; import pl.szczodrzynski.edziennik.data.db.modules.debuglog.DebugLogDao; import pl.szczodrzynski.edziennik.data.db.modules.events.Event; @@ -52,6 +56,8 @@ import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata; import pl.szczodrzynski.edziennik.data.db.modules.metadata.MetadataDao; import pl.szczodrzynski.edziennik.data.db.modules.notices.Notice; import pl.szczodrzynski.edziennik.data.db.modules.notices.NoticeDao; +import pl.szczodrzynski.edziennik.data.db.modules.notices.NoticeType; +import pl.szczodrzynski.edziennik.data.db.modules.notices.NoticeTypeDao; import pl.szczodrzynski.edziennik.data.db.modules.notification.Notification; import pl.szczodrzynski.edziennik.data.db.modules.notification.NotificationDao; import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile; @@ -94,7 +100,10 @@ import pl.szczodrzynski.edziennik.utils.models.Date; EndpointTimer.class, LessonRange.class, Notification.class, - Metadata.class}, version = 61) + Classroom.class, + NoticeType.class, + AttendanceType.class, + Metadata.class}, version = 62) @TypeConverters({ ConverterTime.class, ConverterDate.class, @@ -129,6 +138,9 @@ public abstract class AppDb extends RoomDatabase { public abstract EndpointTimerDao endpointTimerDao(); public abstract LessonRangeDao lessonRangeDao(); public abstract NotificationDao notificationDao(); + public abstract ClassroomDao classroomDao(); + public abstract NoticeTypeDao noticeTypeDao(); + public abstract AttendanceTypeDao attendanceTypeDao(); public abstract MetadataDao metadataDao(); private static volatile AppDb INSTANCE; @@ -684,6 +696,31 @@ public abstract class AppDb extends RoomDatabase { database.execSQL("ALTER TABLE teacherAbsence ADD COLUMN teacherAbsenceName TEXT DEFAULT NULL"); } }; + private static final Migration MIGRATION_61_62 = new Migration(61, 62) { + @Override + public void migrate(@NonNull SupportSQLiteDatabase database) { + database.execSQL("CREATE TABLE IF NOT EXISTS classrooms (\n" + + " profileId INTEGER NOT NULL,\n" + + " id INTEGER NOT NULL,\n" + + " name TEXT NOT NULL,\n" + + " PRIMARY KEY(profileId, id)\n" + + ")"); + database.execSQL("CREATE TABLE IF NOT EXISTS noticeTypes (\n" + + " profileId INTEGER NOT NULL,\n" + + " id INTEGER NOT NULL,\n" + + " name TEXT NOT NULL,\n" + + " PRIMARY KEY(profileId, id)\n" + + ")"); + database.execSQL("CREATE TABLE IF NOT EXISTS attendanceTypes (\n" + + " profileId INTEGER NOT NULL,\n" + + " id INTEGER NOT NULL,\n" + + " name TEXT NOT NULL,\n" + + " type INTEGER NOT NULL,\n" + + " color INTEGER NOT NULL,\n" + + " PRIMARY KEY(profileId, id)\n" + + ")"); + } + }; public static AppDb getDatabase(final Context context) { @@ -742,7 +779,8 @@ public abstract class AppDb extends RoomDatabase { MIGRATION_57_58, MIGRATION_58_59, MIGRATION_59_60, - MIGRATION_60_61 + MIGRATION_60_61, + MIGRATION_61_62 ) .allowMainThreadQueries() //.fallbackToDestructiveMigration() diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/attendance/AttendanceType.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/attendance/AttendanceType.kt new file mode 100644 index 00000000..701b852a --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/attendance/AttendanceType.kt @@ -0,0 +1,23 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-10-23. + */ + +package pl.szczodrzynski.edziennik.data.db.modules.attendance + +import androidx.room.Entity + +@Entity(tableName = "attendanceTypes", + primaryKeys = ["profileId", "id"]) +data class AttendanceType ( + + val profileId: Int, + + val id: Long, + + val name: String, + + val type: Int, + + val color: Int + +) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/attendance/AttendanceTypeDao.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/attendance/AttendanceTypeDao.kt new file mode 100644 index 00000000..ec9e630a --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/attendance/AttendanceTypeDao.kt @@ -0,0 +1,25 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-10-23. + */ + +package pl.szczodrzynski.edziennik.data.db.modules.attendance + +import androidx.room.Dao +import androidx.room.Insert +import androidx.room.OnConflictStrategy +import androidx.room.Query + +@Dao +interface AttendanceTypeDao { + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun add(attendanceType: AttendanceType) + + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun addAll(attendanceTypeList: List) + + @Query("DELETE FROM attendanceTypes WHERE profileId = :profileId") + fun clear(profileId: Int) + + @Query("SELECT * FROM attendanceTypes WHERE profileId = :profileId ORDER BY id ASC") + fun getAllNow(profileId: Int): List +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/classrooms/Classroom.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/classrooms/Classroom.kt new file mode 100644 index 00000000..fc2fe404 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/classrooms/Classroom.kt @@ -0,0 +1,19 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-10-23. + */ + +package pl.szczodrzynski.edziennik.data.db.modules.classrooms + +import androidx.room.Entity + +@Entity(tableName = "classrooms", + primaryKeys = ["profileId", "id"]) +data class Classroom ( + + val profileId: Int, + + val id: Long, + + val name: String + +) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/classrooms/ClassroomDao.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/classrooms/ClassroomDao.kt new file mode 100644 index 00000000..79659f97 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/classrooms/ClassroomDao.kt @@ -0,0 +1,25 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-10-23. + */ + +package pl.szczodrzynski.edziennik.data.db.modules.classrooms + +import androidx.room.Dao +import androidx.room.Insert +import androidx.room.OnConflictStrategy +import androidx.room.Query + +@Dao +interface ClassroomDao { + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun add(classroom: Classroom) + + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun addAll(classroomList: List) + + @Query("DELETE FROM classrooms WHERE profileId = :profileId") + fun clear(profileId: Int) + + @Query("SELECT * FROM classrooms WHERE profileId = :profileId ORDER BY id ASC") + fun getAllNow(profileId: Int): List +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/notices/NoticeType.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/notices/NoticeType.kt new file mode 100644 index 00000000..a9112fbb --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/notices/NoticeType.kt @@ -0,0 +1,19 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-10-23. + */ + +package pl.szczodrzynski.edziennik.data.db.modules.notices + +import androidx.room.Entity + +@Entity(tableName = "noticeTypes", + primaryKeys = ["profileId", "id"]) +data class NoticeType ( + + val profileId: Int, + + val id: Long, + + val name: String + +) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/notices/NoticeTypeDao.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/notices/NoticeTypeDao.kt new file mode 100644 index 00000000..5415c442 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/notices/NoticeTypeDao.kt @@ -0,0 +1,25 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-10-23. + */ + +package pl.szczodrzynski.edziennik.data.db.modules.notices + +import androidx.room.Dao +import androidx.room.Insert +import androidx.room.OnConflictStrategy +import androidx.room.Query + +@Dao +interface NoticeTypeDao { + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun add(noticeType: NoticeType) + + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun addAll(noticeTypeList: List) + + @Query("DELETE FROM noticeTypes WHERE profileId = :profileId") + fun clear(profileId: Int) + + @Query("SELECT * FROM noticeTypes WHERE profileId = :profileId ORDER BY id ASC") + fun getAllNow(profileId: Int): List +}