From e138ca6eab3b5ed33fd90c245ed897b16877fe4e Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Sat, 19 Oct 2019 13:33:50 +0200 Subject: [PATCH] [APIv2/Librus] Add getting and showing teacher absence reason --- .../edziennik/api/v2/librus/LibrusFeatures.kt | 2 + .../api/v2/librus/data/LibrusData.kt | 4 ++ .../data/api/LibrusApiTeacherFreeDayTypes.kt | 46 +++++++++++++++++++ .../data/api/LibrusApiTeacherFreeDays.kt | 2 + .../edziennik/api/v2/models/Data.kt | 17 ++++++- .../edziennik/data/api/Librus.java | 1 + .../edziennik/data/db/AppDb.java | 20 +++++++- .../db/modules/teachers/TeacherAbsence.kt | 3 ++ .../db/modules/teachers/TeacherAbsenceFull.kt | 4 +- .../db/modules/teachers/TeacherAbsenceType.kt | 20 ++++++++ .../modules/teachers/TeacherAbsenceTypeDao.kt | 25 ++++++++++ .../teacherabsence/TeacherAbsenceAdapter.kt | 10 ++++ .../row_dialog_teacher_absence_item.xml | 9 ++++ app/src/main/res/values/strings.xml | 1 + 14 files changed, 158 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiTeacherFreeDayTypes.kt create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsenceType.kt create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsenceTypeDao.kt 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 a565cb38..48218bc0 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 @@ -40,6 +40,7 @@ const val ENDPOINT_LIBRUS_API_ATTENDANCE_TYPES = 1080 const val ENDPOINT_LIBRUS_API_ATTENDANCES = 1081 const val ENDPOINT_LIBRUS_API_ANNOUNCEMENTS = 1090 const val ENDPOINT_LIBRUS_API_PT_MEETINGS = 1100 +const val ENDPOINT_LIBRUS_API_TEACHER_FREE_DAY_TYPES = 1109 const val ENDPOINT_LIBRUS_API_TEACHER_FREE_DAYS = 1110 const val ENDPOINT_LIBRUS_API_SCHOOL_FREE_DAYS = 1120 const val ENDPOINT_LIBRUS_API_CLASS_FREE_DAYS = 1130 @@ -79,6 +80,7 @@ val LibrusFeatures = listOf( ENDPOINT_LIBRUS_API_EVENTS to LOGIN_METHOD_LIBRUS_API, ENDPOINT_LIBRUS_API_EVENT_TYPES to LOGIN_METHOD_LIBRUS_API, ENDPOINT_LIBRUS_API_PT_MEETINGS to LOGIN_METHOD_LIBRUS_API, + ENDPOINT_LIBRUS_API_TEACHER_FREE_DAY_TYPES to LOGIN_METHOD_LIBRUS_API, ENDPOINT_LIBRUS_API_TEACHER_FREE_DAYS to LOGIN_METHOD_LIBRUS_API, ENDPOINT_LIBRUS_API_SCHOOL_FREE_DAYS to LOGIN_METHOD_LIBRUS_API, ENDPOINT_LIBRUS_API_CLASS_FREE_DAYS to LOGIN_METHOD_LIBRUS_API diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt index 94d440ca..98a6b653 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt @@ -75,6 +75,10 @@ class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) { data.startProgress(R.string.edziennik_progress_endpoint_classes) LibrusApiClasses(data) { onSuccess() } } + ENDPOINT_LIBRUS_API_TEACHER_FREE_DAY_TYPES -> { + data.startProgress(R.string.edziennik_progress_endpoint_teacher_free_day_types) + LibrusApiTeacherFreeDayTypes(data) { onSuccess() } + } ENDPOINT_LIBRUS_API_TEACHER_FREE_DAYS -> { data.startProgress(R.string.edziennik_progress_endpoint_teacher_free_days) LibrusApiTeacherFreeDays(data) { onSuccess() } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiTeacherFreeDayTypes.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiTeacherFreeDayTypes.kt new file mode 100644 index 00000000..dcfdcbeb --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiTeacherFreeDayTypes.kt @@ -0,0 +1,46 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2019-10-19 + */ + +package pl.szczodrzynski.edziennik.api.v2.librus.data.api + +import pl.szczodrzynski.edziennik.DAY +import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus +import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_TEACHER_FREE_DAY_TYPES +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.teachers.TeacherAbsenceType +import pl.szczodrzynski.edziennik.getJsonArray +import pl.szczodrzynski.edziennik.getLong +import pl.szczodrzynski.edziennik.getString + +class LibrusApiTeacherFreeDayTypes(override val data: DataLibrus, + val onSuccess: () -> Unit) : LibrusApi(data) { + companion object { + const val TAG = "LibrusApiTeacherFreeDayTypes" + } + + init { + apiGet(TAG, "TeacherFreeDays/Types") { json -> + val teacherAbsenceTypes = json.getJsonArray("Types") + + teacherAbsenceTypes?.forEach { teacherAbsenceTypeEl -> + val teacherAbsenceType = teacherAbsenceTypeEl.asJsonObject + + val id = teacherAbsenceType.getLong("Id") ?: return@forEach + val name = teacherAbsenceType.getString("Name") ?: return@forEach + + val teacherAbsenceTypeObject = TeacherAbsenceType( + profileId, + id, + name + ) + + data.teacherAbsenceTypes.put(id, teacherAbsenceTypeObject) + } + + data.setSyncNext(ENDPOINT_LIBRUS_API_TEACHER_FREE_DAY_TYPES, 4 * DAY) + onSuccess() + } + } +} 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 cb5efb6a..756d64e1 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 @@ -30,6 +30,7 @@ class LibrusApiTeacherFreeDays(override val data: DataLibrus, val teacherId = teacherAbsence.getJsonObject("Teacher")?.getLong("Id") ?: return@forEach val type = teacherAbsence.getJsonObject("Type").getLong("Id") ?: return@forEach + val name = data.teacherAbsenceTypes.singleOrNull { it.id == type }?.name val dateFrom = Date.fromY_m_d(teacherAbsence.getString("DateFrom")) val dateTo = Date.fromY_m_d(teacherAbsence.getString("DateTo")) val timeFrom = teacherAbsence.getString("TimeFrom")?.let { Time.fromH_m_s(it) } @@ -40,6 +41,7 @@ class LibrusApiTeacherFreeDays(override val data: DataLibrus, id, teacherId, type, + name, dateFrom, dateTo, timeFrom, 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 fd008d31..d573068c 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 @@ -32,6 +32,7 @@ import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile import pl.szczodrzynski.edziennik.data.db.modules.subjects.Subject import pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher import pl.szczodrzynski.edziennik.data.db.modules.teachers.TeacherAbsence +import pl.szczodrzynski.edziennik.data.db.modules.teachers.TeacherAbsenceType import pl.szczodrzynski.edziennik.data.db.modules.teams.Team import pl.szczodrzynski.edziennik.singleOrNull import pl.szczodrzynski.edziennik.toSparseArray @@ -102,6 +103,7 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) val lessonRanges = SparseArray() val gradeCategories = LongSparseArray() val attendanceTypes = SparseArray>() + val teacherAbsenceTypes = LongSparseArray() private var mTeamClass: Team? = null var teamClass: Team? @@ -156,6 +158,7 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) db.teamDao().getAllNow(profileId).toSparseArray(teamList) { it.id } db.lessonRangeDao().getAllNow(profileId).toSparseArray(lessonRanges) { it.lessonNumber } db.gradeCategoryDao().getAllNow(profileId).toSparseArray(gradeCategories) { it.categoryId } + db.teacherAbsenceTypeDao().getAllNow(profileId).toSparseArray(teacherAbsenceTypes) { it.id } } } @@ -205,6 +208,8 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) db.lessonRangeDao().addAll(lessonRanges.values()) db.gradeCategoryDao().clear(profileId) db.gradeCategoryDao().addAll(gradeCategories.values()) + db.teacherAbsenceTypeDao().clear(profileId) + db.teacherAbsenceTypeDao().addAll(teacherAbsenceTypes.values()) gradesToRemove?.let { it -> it.removeAll?.let { _ -> db.gradeDao().clear(profileId) } @@ -297,8 +302,11 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) else -> errorCode } } - error(ApiError(tag, code).apply { profileId = profile?.id ?: -1 }.withResponse(response).withThrowable(throwable).withApiResponse(apiResponse)) + error(ApiError(tag, code).apply { + profileId = profile?.id ?: -1 + }.withResponse(response).withThrowable(throwable).withApiResponse(apiResponse)) } + fun error(tag: String, errorCode: Int, response: Response? = null, apiResponse: String? = null) { val code = when (null) { is UnknownHostException, is SSLException, is InterruptedIOException -> CODE_NO_INTERNET @@ -308,16 +316,21 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) else -> errorCode } } - error(ApiError(tag, code).apply { profileId = profile?.id ?: -1 }.withResponse(response).withApiResponse(apiResponse)) + error(ApiError(tag, code).apply { + profileId = profile?.id ?: -1 + }.withResponse(response).withApiResponse(apiResponse)) } + fun error(apiError: ApiError) { if (apiError.isCritical) cancel() callback.onError(apiError) } + fun progress(step: Int) { callback.onProgress(step) } + fun startProgress(stringRes: Int) { callback.onStartProgress(stringRes) } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Librus.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Librus.java index a554b25c..9baf3c4c 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Librus.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Librus.java @@ -3201,6 +3201,7 @@ public class Librus implements EdziennikInterface { id, teacherId, type, + null, dateFrom, dateTo, timeFrom, 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 1ba10997..4cbcb3a7 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 @@ -61,6 +61,8 @@ import pl.szczodrzynski.edziennik.data.db.modules.subjects.SubjectDao; import pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher; import pl.szczodrzynski.edziennik.data.db.modules.teachers.TeacherAbsence; import pl.szczodrzynski.edziennik.data.db.modules.teachers.TeacherAbsenceDao; +import pl.szczodrzynski.edziennik.data.db.modules.teachers.TeacherAbsenceType; +import pl.szczodrzynski.edziennik.data.db.modules.teachers.TeacherAbsenceTypeDao; import pl.szczodrzynski.edziennik.data.db.modules.teachers.TeacherDao; import pl.szczodrzynski.edziennik.data.db.modules.teams.Team; import pl.szczodrzynski.edziennik.data.db.modules.teams.TeamDao; @@ -71,6 +73,7 @@ import pl.szczodrzynski.edziennik.utils.models.Date; //GradeCategory.class, Teacher.class, TeacherAbsence.class, + TeacherAbsenceType.class, Subject.class, Notice.class, Lesson.class, @@ -91,7 +94,7 @@ import pl.szczodrzynski.edziennik.utils.models.Date; EndpointTimer.class, LessonRange.class, Notification.class, - Metadata.class}, version = 60) + Metadata.class}, version = 61) @TypeConverters({ ConverterTime.class, ConverterDate.class, @@ -105,6 +108,7 @@ public abstract class AppDb extends RoomDatabase { //public abstract GradeCategoryDao gradeCategoryDao(); public abstract TeacherDao teacherDao(); public abstract TeacherAbsenceDao teacherAbsenceDao(); + public abstract TeacherAbsenceTypeDao teacherAbsenceTypeDao(); public abstract SubjectDao subjectDao(); public abstract NoticeDao noticeDao(); public abstract LessonDao lessonDao(); @@ -667,6 +671,17 @@ public abstract class AppDb extends RoomDatabase { database.execSQL("ALTER TABLE profiles ADD COLUMN disabledNotifications TEXT DEFAULT NULL"); } }; + private static final Migration MIGRATION_60_61 = new Migration(60, 61) { + @Override + public void migrate(@NonNull SupportSQLiteDatabase database) { + database.execSQL("CREATE TABLE IF NOT EXISTS teacherAbsenceTypes (" + + "profileId INTEGER NOT NULL," + + "teacherAbsenceTypeId INTEGER NOT NULL," + + "teacherAbsenceTypeName TEXT NOT NULL," + + "PRIMARY KEY(profileId, teacherAbsenceTypeId))"); + database.execSQL("ALTER TABLE teacherAbsence ADD COLUMN teacherAbsenceName TEXT DEFAULT NULL"); + } + }; public static AppDb getDatabase(final Context context) { @@ -724,7 +739,8 @@ public abstract class AppDb extends RoomDatabase { MIGRATION_56_57, MIGRATION_57_58, MIGRATION_58_59, - MIGRATION_59_60 + MIGRATION_59_60, + MIGRATION_60_61 ) .allowMainThreadQueries() //.fallbackToDestructiveMigration() diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsence.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsence.kt index 76ed7450..c73dc490 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsence.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsence.kt @@ -19,6 +19,9 @@ open class TeacherAbsence ( @ColumnInfo(name = "teacherAbsenceType") val type: Long, + @ColumnInfo(name = "teacherAbsenceName") + val name: String?, + @ColumnInfo(name = "teacherAbsenceDateFrom") val dateFrom: Date, diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsenceFull.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsenceFull.kt index 1c36c19c..800f2d00 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsenceFull.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsenceFull.kt @@ -3,9 +3,9 @@ package pl.szczodrzynski.edziennik.data.db.modules.teachers import pl.szczodrzynski.edziennik.utils.models.Date import pl.szczodrzynski.edziennik.utils.models.Time -class TeacherAbsenceFull(profileId: Int, id: Long, teacherId: Long, type: Long, +class TeacherAbsenceFull(profileId: Int, id: Long, teacherId: Long, type: Long, name: String?, dateFrom: Date, dateTo: Date, timeFrom: Time?, timeTo: Time?) - : TeacherAbsence(profileId, id, teacherId, type, dateFrom, dateTo, timeFrom, timeTo) { + : TeacherAbsence(profileId, id, teacherId, type, name, dateFrom, dateTo, timeFrom, timeTo) { var teacherFullName = "" diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsenceType.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsenceType.kt new file mode 100644 index 00000000..ca9db678 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsenceType.kt @@ -0,0 +1,20 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2019-10-18 + */ + +package pl.szczodrzynski.edziennik.data.db.modules.teachers + +import androidx.room.ColumnInfo +import androidx.room.Entity + +@Entity(tableName = "teacherAbsenceTypes", + primaryKeys = ["profileId", "teacherAbsenceTypeId"]) +data class TeacherAbsenceType ( + val profileId: Int, + + @ColumnInfo(name = "teacherAbsenceTypeId") + val id: Long, + + @ColumnInfo(name = "teacherAbsenceTypeName") + val name: String +) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsenceTypeDao.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsenceTypeDao.kt new file mode 100644 index 00000000..12dbf591 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/teachers/TeacherAbsenceTypeDao.kt @@ -0,0 +1,25 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2019-10-18 + */ + +package pl.szczodrzynski.edziennik.data.db.modules.teachers + +import androidx.room.Dao +import androidx.room.Insert +import androidx.room.OnConflictStrategy +import androidx.room.Query + +@Dao +interface TeacherAbsenceTypeDao { + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun add(teacherAbsence: TeacherAbsenceType) + + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun addAll(teacherAbsenceList: List) + + @Query("DELETE FROM teacherAbsenceTypes WHERE profileId = :profileId") + fun clear(profileId: Int) + + @Query("SELECT * FROM teacherAbsenceTypes WHERE profileId = :profileId") + fun getAllNow(profileId: Int): List +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/teacherabsence/TeacherAbsenceAdapter.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/teacherabsence/TeacherAbsenceAdapter.kt index 2d8eb2c6..b8e165d1 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/teacherabsence/TeacherAbsenceAdapter.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/teacherabsence/TeacherAbsenceAdapter.kt @@ -1,6 +1,7 @@ package pl.szczodrzynski.edziennik.ui.dialogs.teacherabsence import android.content.Context +import android.opengl.Visibility import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -47,10 +48,19 @@ class TeacherAbsenceAdapter( } holder.teacherAbsenceTime.text = time + + if (teacherAbsence.name != null) { + holder.teacherAbsenceName.visibility = View.VISIBLE + holder.teacherAbsenceName.text = teacherAbsence.name + } else { + holder.teacherAbsenceName.visibility = View.GONE + } + } class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { var teacherAbsenceTeacher: TextView = itemView.findViewById(R.id.teacherAbsenceTeacher) var teacherAbsenceTime: TextView = itemView.findViewById(R.id.teacherAbsenceTime) + var teacherAbsenceName: TextView = itemView.findViewById(R.id.teacherAbsenceName) } } diff --git a/app/src/main/res/layout/row_dialog_teacher_absence_item.xml b/app/src/main/res/layout/row_dialog_teacher_absence_item.xml index 6aacce23..8d303464 100644 --- a/app/src/main/res/layout/row_dialog_teacher_absence_item.xml +++ b/app/src/main/res/layout/row_dialog_teacher_absence_item.xml @@ -36,6 +36,15 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" tools:text="Jan Kowalski"/> + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d6e6a75e..e8158af5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -949,4 +949,5 @@ Pierwsze logowanie Pobieranie informacji o klasie... Pobieranie nieobecności nauczycieli... + Pobieranie rodzajów nieobecności nauczycieli...