From aaa3b8626ea13a9179058328b609bf6d6af75f0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Mon, 26 Dec 2022 14:01:25 +0100 Subject: [PATCH] [UI] Update event types for university school. --- .../szczodrzynski/edziennik/config/AppData.kt | 2 +- .../edziennik/config/ProfileConfig.kt | 2 +- .../config/utils/ProfileConfigMigration.kt | 16 +++++- .../edziennik/data/db/dao/EventTypeDao.kt | 22 +++++++- .../edziennik/data/db/entity/EventType.kt | 55 ------------------- .../edziennik/ui/agenda/AgendaFragment.kt | 8 +-- .../edziennik/ui/debug/LabPageFragment.kt | 7 +++ .../edziennik/ui/views/EventTypeDropdown.kt | 9 +-- app/src/main/res/layout/lab_fragment.xml | 7 +++ app/src/main/res/raw/app_data.json | 27 ++++----- 10 files changed, 66 insertions(+), 89 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/config/AppData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/config/AppData.kt index 6f72b82e..b2aa8797 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/config/AppData.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/config/AppData.kt @@ -62,7 +62,7 @@ data class AppData( ) data class EventType( - val id: Int, + val id: Long, val color: String, val name: String, ) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/config/ProfileConfig.kt b/app/src/main/java/pl/szczodrzynski/edziennik/config/ProfileConfig.kt index 4df2ef47..c36b6fae 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/config/ProfileConfig.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/config/ProfileConfig.kt @@ -15,7 +15,7 @@ class ProfileConfig( entries: List?, ) : BaseConfig(db, profileId, entries) { companion object { - const val DATA_VERSION = 4 + const val DATA_VERSION = 5 } val grades by lazy { ProfileConfigGrades(this) } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/config/utils/ProfileConfigMigration.kt b/app/src/main/java/pl/szczodrzynski/edziennik/config/utils/ProfileConfigMigration.kt index c5b56690..ab915b1d 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/config/utils/ProfileConfigMigration.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/config/utils/ProfileConfigMigration.kt @@ -16,6 +16,8 @@ import pl.szczodrzynski.edziennik.utils.managers.GradesManager.Companion.YEAR_AL class ProfileConfigMigration(config: ProfileConfig) { init { config.apply { + val profile = db.profileDao().getByIdNow(profileId ?: -1) + if (dataVersion < 2) { sync.notificationFilter = sync.notificationFilter + NotificationType.TEACHER_ABSENCE @@ -37,11 +39,23 @@ class ProfileConfigMigration(config: ProfileConfig) { // switch to new event types (USOS) dataVersion = 4 - val profile = db.profileDao().getByIdNow(profileId ?: -1) if (profile?.loginStoreType?.schoolType == SchoolType.UNIVERSITY) { db.eventTypeDao().clear(profileId ?: -1) db.eventTypeDao().addDefaultTypes(profile) } } + + if (dataVersion < 5) { + // update USOS event types and the appropriate events (2022-12-25) + dataVersion = 5 + + if (profile?.loginStoreType?.schoolType == SchoolType.UNIVERSITY) { + db.eventTypeDao().getAllWithDefaults(profile) + // wejściówka (4) -> kartkówka (3) + db.eventDao().getRawNow("UPDATE events SET eventType = 3 WHERE profileId = $profileId AND eventType = 4;") + // zadanie (6) -> zadanie domowe (-1) + db.eventDao().getRawNow("UPDATE events SET eventType = -1 WHERE profileId = $profileId AND eventType = 6;") + } + } }} } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/EventTypeDao.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/EventTypeDao.kt index c6c9ee44..2f3f5c94 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/EventTypeDao.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/EventTypeDao.kt @@ -25,6 +25,9 @@ abstract class EventTypeDao { @Query("DELETE FROM eventTypes WHERE profileId = :profileId") abstract fun clear(profileId: Int) + @Query("DELETE FROM eventTypes WHERE profileId = :profileId AND eventTypeSource = :source") + abstract fun clearBySource(profileId: Int, source: Int) + @Query("SELECT * FROM eventTypes WHERE profileId = :profileId AND eventType = :typeId") abstract fun getByIdNow(profileId: Int, typeId: Long): EventType? @@ -43,7 +46,7 @@ abstract class EventTypeDao { val typeList = data.eventTypes.map { EventType( profileId = profile.id, - id = it.id.toLong(), + id = it.id, name = it.name, color = Color.parseColor(it.color), order = order++, @@ -53,4 +56,21 @@ abstract class EventTypeDao { addAll(typeList) return typeList } + + fun getAllWithDefaults(profile: Profile): List { + val eventTypes = getAllNow(profile.id) + + val defaultIdsExpected = AppData.get(profile.loginStoreType).eventTypes + .map { it.id } + val defaultIdsFound = eventTypes.filter { it.source == SOURCE_DEFAULT } + .sortedBy { it.order } + .map { it.id } + + if (defaultIdsExpected == defaultIdsFound) + return eventTypes + + clearBySource(profile.id, SOURCE_DEFAULT) + addDefaultTypes(profile) + return eventTypes + } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/EventType.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/EventType.kt index 7753b38a..00ba4c04 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/EventType.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/EventType.kt @@ -5,31 +5,6 @@ package pl.szczodrzynski.edziennik.data.db.entity import androidx.room.ColumnInfo import androidx.room.Entity -import pl.szczodrzynski.edziennik.R -import pl.szczodrzynski.edziennik.data.db.entity.Event.Companion.COLOR_CLASS_EVENT -import pl.szczodrzynski.edziennik.data.db.entity.Event.Companion.COLOR_DEFAULT -import pl.szczodrzynski.edziennik.data.db.entity.Event.Companion.COLOR_ELEARNING -import pl.szczodrzynski.edziennik.data.db.entity.Event.Companion.COLOR_ESSAY -import pl.szczodrzynski.edziennik.data.db.entity.Event.Companion.COLOR_EXAM -import pl.szczodrzynski.edziennik.data.db.entity.Event.Companion.COLOR_EXCURSION -import pl.szczodrzynski.edziennik.data.db.entity.Event.Companion.COLOR_HOMEWORK -import pl.szczodrzynski.edziennik.data.db.entity.Event.Companion.COLOR_INFORMATION -import pl.szczodrzynski.edziennik.data.db.entity.Event.Companion.COLOR_PROJECT -import pl.szczodrzynski.edziennik.data.db.entity.Event.Companion.COLOR_PT_MEETING -import pl.szczodrzynski.edziennik.data.db.entity.Event.Companion.COLOR_READING -import pl.szczodrzynski.edziennik.data.db.entity.Event.Companion.COLOR_SHORT_QUIZ -import pl.szczodrzynski.edziennik.data.db.entity.Event.Companion.TYPE_CLASS_EVENT -import pl.szczodrzynski.edziennik.data.db.entity.Event.Companion.TYPE_DEFAULT -import pl.szczodrzynski.edziennik.data.db.entity.Event.Companion.TYPE_ELEARNING -import pl.szczodrzynski.edziennik.data.db.entity.Event.Companion.TYPE_ESSAY -import pl.szczodrzynski.edziennik.data.db.entity.Event.Companion.TYPE_EXAM -import pl.szczodrzynski.edziennik.data.db.entity.Event.Companion.TYPE_EXCURSION -import pl.szczodrzynski.edziennik.data.db.entity.Event.Companion.TYPE_HOMEWORK -import pl.szczodrzynski.edziennik.data.db.entity.Event.Companion.TYPE_INFORMATION -import pl.szczodrzynski.edziennik.data.db.entity.Event.Companion.TYPE_PROJECT -import pl.szczodrzynski.edziennik.data.db.entity.Event.Companion.TYPE_PT_MEETING -import pl.szczodrzynski.edziennik.data.db.entity.Event.Companion.TYPE_READING -import pl.szczodrzynski.edziennik.data.db.entity.Event.Companion.TYPE_SHORT_QUIZ @Entity( tableName = "eventTypes", @@ -55,35 +30,5 @@ class EventType( const val SOURCE_REGISTER = 1 const val SOURCE_CUSTOM = 2 const val SOURCE_SHARED = 3 - - fun getTypeColorMap() = mapOf( - TYPE_ELEARNING to COLOR_ELEARNING, - TYPE_HOMEWORK to COLOR_HOMEWORK, - TYPE_DEFAULT to COLOR_DEFAULT, - TYPE_EXAM to COLOR_EXAM, - TYPE_SHORT_QUIZ to COLOR_SHORT_QUIZ, - TYPE_ESSAY to COLOR_ESSAY, - TYPE_PROJECT to COLOR_PROJECT, - TYPE_PT_MEETING to COLOR_PT_MEETING, - TYPE_EXCURSION to COLOR_EXCURSION, - TYPE_READING to COLOR_READING, - TYPE_CLASS_EVENT to COLOR_CLASS_EVENT, - TYPE_INFORMATION to COLOR_INFORMATION - ) - - fun getTypeNameMap() = mapOf( - TYPE_ELEARNING to R.string.event_type_elearning, - TYPE_HOMEWORK to R.string.event_type_homework, - TYPE_DEFAULT to R.string.event_other, - TYPE_EXAM to R.string.event_exam, - TYPE_SHORT_QUIZ to R.string.event_short_quiz, - TYPE_ESSAY to R.string.event_essay, - TYPE_PROJECT to R.string.event_project, - TYPE_PT_MEETING to R.string.event_pt_meeting, - TYPE_EXCURSION to R.string.event_excursion, - TYPE_READING to R.string.event_reading, - TYPE_CLASS_EVENT to R.string.event_class_event, - TYPE_INFORMATION to R.string.event_information - ) } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/agenda/AgendaFragment.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/agenda/AgendaFragment.kt index 52c9f345..cbdbce7b 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/agenda/AgendaFragment.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/agenda/AgendaFragment.kt @@ -142,13 +142,7 @@ class AgendaFragment : Fragment(), CoroutineScope { private suspend fun checkEventTypes() { withContext(Dispatchers.Default) { - val eventTypes = app.db.eventTypeDao().getAllNow(app.profileId).map { - it.id - } - val defaultEventTypes = EventType.getTypeColorMap().keys - if (!eventTypes.containsAll(defaultEventTypes)) { - app.db.eventTypeDao().addDefaultTypes(app.profile) - } + app.db.eventTypeDao().getAllWithDefaults(app.profile) } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/debug/LabPageFragment.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/debug/LabPageFragment.kt index 5aae3c11..792811eb 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/debug/LabPageFragment.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/debug/LabPageFragment.kt @@ -23,6 +23,7 @@ import kotlinx.coroutines.launch import pl.szczodrzynski.edziennik.* import pl.szczodrzynski.edziennik.config.Config import pl.szczodrzynski.edziennik.data.api.szkolny.interceptor.SignatureInterceptor +import pl.szczodrzynski.edziennik.data.db.entity.EventType.Companion.SOURCE_DEFAULT import pl.szczodrzynski.edziennik.databinding.LabFragmentBinding import pl.szczodrzynski.edziennik.ext.* import pl.szczodrzynski.edziennik.ui.base.lazypager.LazyFragment @@ -65,6 +66,7 @@ class LabPageFragment : LazyFragment(), CoroutineScope { b.clearEndpointTimers.isVisible = false b.rodo.isVisible = false b.removeHomework.isVisible = false + b.resetEventTypes.isVisible = false b.unarchive.isVisible = false b.profile.isVisible = false } @@ -100,6 +102,11 @@ class LabPageFragment : LazyFragment(), CoroutineScope { app.db.eventDao().getRawNow("UPDATE events SET homeworkBody = NULL WHERE profileId = ${App.profileId}") } + b.resetEventTypes.onClick { + app.db.eventTypeDao().clearBySource(App.profileId, SOURCE_DEFAULT) + app.db.eventTypeDao().getAllWithDefaults(App.profile) + } + b.chucker.isChecked = App.enableChucker b.chucker.onChange { _, isChecked -> app.config.enableChucker = isChecked diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/views/EventTypeDropdown.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/views/EventTypeDropdown.kt index d6a8d1cd..48d327fc 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/views/EventTypeDropdown.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/views/EventTypeDropdown.kt @@ -33,13 +33,8 @@ class EventTypeDropdown : TextInputDropDown { suspend fun loadItems() { val types = withContext(Dispatchers.Default) { val list = mutableListOf() - - var types = db.eventTypeDao().getAllNow(profileId) - - if (types.none { it.id in -1L..10L }) { - val profile = db.profileDao().getByIdNow(profileId) ?: return@withContext listOf() - types = db.eventTypeDao().addDefaultTypes(profile) - } + val types = db.eventTypeDao().getAllNow(profileId) + .sortedBy { it.order } list += types.map { Item(it.id, it.name, tag = it, icon = IconicsDrawable(context).apply { diff --git a/app/src/main/res/layout/lab_fragment.xml b/app/src/main/res/layout/lab_fragment.xml index d290525c..e41a66f2 100644 --- a/app/src/main/res/layout/lab_fragment.xml +++ b/app/src/main/res/layout/lab_fragment.xml @@ -100,6 +100,13 @@ android:text="Remove all homework body (null)" android:textAllCaps="false" /> +