diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebEvents.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebEvents.kt index cf846fce..52833b9a 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebEvents.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebEvents.kt @@ -39,17 +39,16 @@ class EdudziennikWebEvents(override val data: DataEdudziennik, ?: return@forEach val eventObject = Event( - profileId, - id, - date, - null, - title, - -1, - Event.TYPE_CLASS_EVENT, - false, - -1, - -1, - data.teamClass?.id ?: -1 + profileId = profileId, + id = id, + date = date, + time = null, + topic = title, + color = null, + type = Event.TYPE_CLASS_EVENT, + teacherId = -1, + subjectId = -1, + teamId = data.teamClass?.id ?: -1 ) data.eventList.add(eventObject) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebExams.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebExams.kt index 8eb7d04e..8e7836fd 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebExams.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebExams.kt @@ -56,17 +56,16 @@ class EdudziennikWebExams(override val data: DataEdudziennik, val eventType = data.getEventType(eventTypeId, eventTypeName) val eventObject = Event( - profileId, - id, - date, - startTime, - topic, - -1, - eventType.id, - false, - -1, - subject.id, - data.teamClass?.id ?: -1 + profileId = profileId, + id = id, + date = date, + time = startTime, + topic = topic, + color = null, + type = eventType.id, + teacherId = -1, + subjectId = subject.id, + teamId = data.teamClass?.id ?: -1 ) data.eventList.add(eventObject) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebHomework.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebHomework.kt index 9d9a336a..1a9ea5ae 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebHomework.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebHomework.kt @@ -52,17 +52,16 @@ class EdudziennikWebHomework(override val data: DataEdudziennik, val topic = homeworkElement.child(4).text() val eventObject = Event( - profileId, - id, - date, - startTime, - topic, - -1, - Event.TYPE_HOMEWORK, - false, - teacher.id, - subject.id, - data.teamClass?.id ?: -1 + profileId = profileId, + id = id, + date = date, + time = startTime, + topic = topic, + color = null, + type = Event.TYPE_HOMEWORK, + teacherId = teacher.id, + subjectId = subject.id, + teamId = data.teamClass?.id ?: -1 ) data.eventList.add(eventObject) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/web/IdziennikWebExams.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/web/IdziennikWebExams.kt index 42202f4e..3a3b9d3b 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/web/IdziennikWebExams.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/web/IdziennikWebExams.kt @@ -80,17 +80,16 @@ class IdziennikWebExams(override val data: DataIdziennik, } val eventObject = Event( - profileId, - id, - examDate, - startTime, - topic, - -1, - eventType, - false, - teacherId, - subjectId, - data.teamClass?.id ?: -1 + profileId = profileId, + id = id, + date = examDate, + time = startTime, + topic = topic, + color = null, + type = eventType, + teacherId = teacherId, + subjectId = subjectId, + teamId = data.teamClass?.id ?: -1 ) data.eventList.add(eventObject) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/web/IdziennikWebHomework.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/web/IdziennikWebHomework.kt index 345e7da4..71ea7ea9 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/web/IdziennikWebHomework.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/web/IdziennikWebHomework.kt @@ -67,17 +67,16 @@ class IdziennikWebHomework(override val data: DataIdziennik, val eventObject = Event( - profileId, - id, - eventDate, - startTime, - topic, - -1, - Event.TYPE_HOMEWORK, - false, - teacherId, - subjectId, - data.teamClass?.id ?: -1 + profileId = profileId, + id = id, + date = eventDate, + time = startTime, + topic = topic, + color = null, + type = Event.TYPE_HOMEWORK, + teacherId = teacherId, + subjectId = subjectId, + teamId = data.teamClass?.id ?: -1 ) data.eventList.add(eventObject) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiEvents.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiEvents.kt index a04c2863..1dff3e7d 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiEvents.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiEvents.kt @@ -47,17 +47,16 @@ class LibrusApiEvents(override val data: DataLibrus, val addedDate = Date.fromIso(event.getString("AddDate")) val eventObject = Event( - profileId, - id, - eventDate, - startTime, - topic, - -1, - type, - false, - teacherId, - subjectId, - teamId + profileId = profileId, + id = id, + date = eventDate, + time = startTime, + topic = topic, + color = null, + type = type, + teacherId = teacherId, + subjectId = subjectId, + teamId = teamId ) data.eventList.add(eventObject) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiHomework.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiHomework.kt index 16d66c6f..d47fbfdf 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiHomework.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiHomework.kt @@ -34,17 +34,16 @@ class LibrusApiHomework(override val data: DataLibrus, val addedDate = Date.fromY_m_d(homework.getString("Date")) val eventObject = Event( - profileId, - id, - eventDate, - null, - topic, - -1, - -1, - false, - teacherId, - -1, - -1 + profileId = profileId, + id = id, + date = eventDate, + time = null, + topic = topic, + color = null, + type = -1, + teacherId = teacherId, + subjectId = -1, + teamId = -1 ) data.eventList.add(eventObject) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiPtMeetings.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiPtMeetings.kt index 3a58d8e1..de696a99 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiPtMeetings.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiPtMeetings.kt @@ -39,17 +39,16 @@ class LibrusApiPtMeetings(override val data: DataLibrus, } val eventObject = Event( - profileId, - id, - eventDate, - startTime, - topic, - -1, - Event.TYPE_PT_MEETING, - false, - teacherId, - -1, - data.teamClass?.id ?: -1 + profileId = profileId, + id = id, + date = eventDate, + time = startTime, + topic = topic, + color = null, + type = Event.TYPE_PT_MEETING, + teacherId = teacherId, + subjectId = -1, + teamId = data.teamClass?.id ?: -1 ) data.eventList.add(eventObject) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/synergia/LibrusSynergiaHomework.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/synergia/LibrusSynergiaHomework.kt index 87cd88cf..20c122d3 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/synergia/LibrusSynergiaHomework.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/synergia/LibrusSynergiaHomework.kt @@ -89,17 +89,16 @@ class LibrusSynergiaHomework(override val data: DataLibrus, } val eventObject = Event( - profileId, - id, - eventDate, - startTime, - "$topic\n$description", - -1, - Event.TYPE_HOMEWORK, - false, - teacherId, - subjectId, - data.teamClass?.id ?: -1 + profileId = profileId, + id = id, + date = eventDate, + time = startTime, + topic = "$topic\n$description", + color = null, + type = Event.TYPE_HOMEWORK, + teacherId = teacherId, + subjectId = subjectId, + teamId = data.teamClass?.id ?: -1 ) data.eventList.add(eventObject) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/api/MobidziennikApiEvents.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/api/MobidziennikApiEvents.kt index f5cb912b..bfc6eef3 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/api/MobidziennikApiEvents.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/api/MobidziennikApiEvents.kt @@ -51,17 +51,16 @@ class MobidziennikApiEvents(val data: DataMobidziennik, rows: List) { val eventObject = Event( - data.profileId, - id, - eventDate, - startTime, - topic, - -1, - type, - false, - teacherId, - subjectId, - teamId) + profileId = data.profileId, + id = id, + date = eventDate, + time = startTime, + topic = topic, + color = null, + type = type, + teacherId = teacherId, + subjectId = subjectId, + teamId = teamId) data.eventList.add(eventObject) data.metadataList.add( diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/api/MobidziennikApiHomework.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/api/MobidziennikApiHomework.kt index 2f83f1d6..fe75674f 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/api/MobidziennikApiHomework.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/api/MobidziennikApiHomework.kt @@ -31,17 +31,16 @@ class MobidziennikApiHomework(val data: DataMobidziennik, rows: List) { val startTime = Time.fromYmdHm(cols[3]) val eventObject = Event( - data.profileId, - id, - eventDate, - startTime, - topic, - -1, - Event.TYPE_HOMEWORK, - false, - teacherId, - subjectId, - teamId) + profileId = data.profileId, + id = id, + date = eventDate, + time = startTime, + topic = topic, + color = null, + type = Event.TYPE_HOMEWORK, + teacherId = teacherId, + subjectId = subjectId, + teamId = teamId) data.eventList.add(eventObject) data.metadataList.add( diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/web/MobidziennikWebCalendar.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/web/MobidziennikWebCalendar.kt index 9664a68b..c1749d76 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/web/MobidziennikWebCalendar.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/web/MobidziennikWebCalendar.kt @@ -61,26 +61,25 @@ class MobidziennikWebCalendar(override val data: DataMobidziennik, val title = event.getString("title") val comment = event.getString("comment") - var topic = title + var topic = title ?: "" if (title != comment) { topic += "\n" + comment } if (id == -1L) { - id = crc16(topic?.toByteArray()).toLong() + id = crc16(topic.toByteArray()).toLong() } val eventObject = Event( - profileId, - id, - eventDate, null, - topic, - -1, - eventType, - false, - -1, - -1, - data.teamClass?.id ?: -1 + profileId = profileId, + id = id, + date = eventDate, time = null, + topic = topic, + color = null, + type = eventType, + teacherId = -1, + subjectId = -1, + teamId = data.teamClass?.id ?: -1 ) data.eventList.add(eventObject) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/api/VulcanApiEvents.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/api/VulcanApiEvents.kt index c55dac5d..0c4a0aa3 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/api/VulcanApiEvents.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/api/VulcanApiEvents.kt @@ -72,17 +72,16 @@ class VulcanApiEvents(override val data: DataVulcan, val teamId = event.getLong("IdOddzial") ?: data.teamClass?.id ?: -1 val eventObject = Event( - profileId, - id, - eventDate, - startTime, - topic, - -1, - type, - false, - teacherId, - subjectId, - teamId + profileId = profileId, + id = id, + date = eventDate, + time = startTime, + topic = topic, + color = null, + type = type, + teacherId = teacherId, + subjectId = subjectId, + teamId = teamId ) data.eventList.add(eventObject) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/SzkolnyApi.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/SzkolnyApi.kt index eb70ab9a..bdd14f33 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/SzkolnyApi.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/SzkolnyApi.kt @@ -196,6 +196,11 @@ class SzkolnyApi(val app: App) : CoroutineScope { // skip blacklisted events if (event.id in blacklistedIds) return@forEach + + // force nullable non-negative colors + if (event.color == -1) + event.color = null + // create the event for every matching team and profile teams.filter { it.code == event.teamCode }.onEach { team -> val profile = profiles.firstOrNull { it.id == team.profileId } ?: return@onEach diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/EventShareRequest.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/EventShareRequest.kt index 624b8b0f..1977d1b2 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/EventShareRequest.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/request/EventShareRequest.kt @@ -12,7 +12,8 @@ data class EventShareRequest ( val action: String = "event", - val sharedByName: String, + /* If null, the server shows an error */ + val sharedByName: String?, val shareTeamCode: String? = null, val unshareTeamCode: String? = null, val requesterName: String? = null, diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/task/AppSync.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/task/AppSync.kt index 56d5132a..df552f2f 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/task/AppSync.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/task/AppSync.kt @@ -34,7 +34,7 @@ class AppSync(val app: App, val notifications: MutableList, val pr if (events.isNotEmpty()) { val today = Date.getToday() app.db.metadataDao().addAllIgnore(events.map { event -> - val isPast = event.eventDate < today + val isPast = event.date < today Metadata( event.profileId, Metadata.TYPE_EVENT, diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/task/Notifications.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/task/Notifications.kt index 7c049f5a..73756508 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/task/Notifications.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/task/Notifications.kt @@ -58,7 +58,7 @@ class Notifications(val app: App, val notifications: MutableList, } private fun eventNotifications() { - for (event in app.db.eventDao().notNotifiedNow.filter { it.eventDate >= today }) { + for (event in app.db.eventDao().notNotifiedNow.filter { it.date >= today }) { val text = if (event.type == Event.TYPE_HOMEWORK) app.getString( if (event.subjectLongName.isNullOrEmpty()) @@ -66,7 +66,7 @@ class Notifications(val app: App, val notifications: MutableList, else R.string.notification_homework_format, event.subjectLongName, - event.eventDate.formattedString + event.date.formattedString ) else app.getString( @@ -74,8 +74,8 @@ class Notifications(val app: App, val notifications: MutableList, R.string.notification_event_no_subject_format else R.string.notification_event_format, - event.typeName, - event.eventDate.formattedString, + event.typeName ?: "wydarzenie", + event.date.formattedString, event.subjectLongName ) val type = if (event.type == Event.TYPE_HOMEWORK) Notification.TYPE_NEW_HOMEWORK else Notification.TYPE_NEW_EVENT @@ -88,17 +88,17 @@ class Notifications(val app: App, val notifications: MutableList, profileName = profiles.singleOrNull { it.id == event.profileId }?.name, viewId = if (event.type == Event.TYPE_HOMEWORK) MainActivity.DRAWER_ITEM_HOMEWORK else MainActivity.DRAWER_ITEM_AGENDA, addedDate = event.addedDate - ).addExtra("eventId", event.id).addExtra("eventDate", event.eventDate.value.toLong()) + ).addExtra("eventId", event.id).addExtra("eventDate", event.date.value.toLong()) } } fun sharedEventNotifications() { - for (event in app.db.eventDao().notNotifiedNow.filter { it.eventDate >= today && it.sharedBy != null }) { + for (event in app.db.eventDao().notNotifiedNow.filter { it.date >= today && it.sharedBy != null }) { val text = app.getString( R.string.notification_shared_event_format, event.sharedByName, event.typeName ?: "wydarzenie", - event.eventDate.formattedString, + event.date.formattedString, event.topic ) val type = if (event.type == Event.TYPE_HOMEWORK) Notification.TYPE_NEW_HOMEWORK else Notification.TYPE_NEW_EVENT @@ -111,7 +111,7 @@ class Notifications(val app: App, val notifications: MutableList, profileName = profiles.singleOrNull { it.id == event.profileId }?.name, viewId = if (event.type == Event.TYPE_HOMEWORK) MainActivity.DRAWER_ITEM_HOMEWORK else MainActivity.DRAWER_ITEM_AGENDA, addedDate = event.addedDate - ).addExtra("eventId", event.id).addExtra("eventDate", event.eventDate.value.toLong()) + ).addExtra("eventId", event.id).addExtra("eventDate", event.date.value.toLong()) } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/AppDb.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/AppDb.kt index 5b01d476..a02e8913 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/AppDb.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/AppDb.kt @@ -43,7 +43,7 @@ import pl.szczodrzynski.edziennik.data.db.migration.* LibrusLesson::class, TimetableManual::class, Metadata::class -], version = 79) +], version = 80) @TypeConverters( ConverterTime::class, ConverterDate::class, @@ -164,7 +164,8 @@ abstract class AppDb : RoomDatabase() { Migration76(), Migration77(), Migration78(), - Migration79() + Migration79(), + Migration80() ).allowMainThreadQueries().build() } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/EventDao.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/EventDao.java deleted file mode 100644 index 083b499a..00000000 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/EventDao.java +++ /dev/null @@ -1,186 +0,0 @@ -/* - * Copyright (c) Kacper Ziubryniewicz 2020-1-6 - */ - -package pl.szczodrzynski.edziennik.data.db.dao; - -import android.util.Log; - -import androidx.annotation.NonNull; -import androidx.lifecycle.LiveData; -import androidx.room.Dao; -import androidx.room.Insert; -import androidx.room.OnConflictStrategy; -import androidx.room.Query; -import androidx.room.RawQuery; -import androidx.room.Transaction; -import androidx.sqlite.db.SimpleSQLiteQuery; -import androidx.sqlite.db.SupportSQLiteQuery; - -import java.util.List; - -import pl.szczodrzynski.edziennik.data.db.entity.Event; -import pl.szczodrzynski.edziennik.data.db.full.EventFull; -import pl.szczodrzynski.edziennik.utils.models.Date; -import pl.szczodrzynski.edziennik.utils.models.Time; - -import static pl.szczodrzynski.edziennik.data.db.entity.Metadata.TYPE_EVENT; -import static pl.szczodrzynski.edziennik.data.db.entity.Metadata.TYPE_HOMEWORK; -import static pl.szczodrzynski.edziennik.data.db.entity.Metadata.TYPE_LESSON_CHANGE; - -@Dao -public abstract class EventDao { - @Insert(onConflict = OnConflictStrategy.REPLACE) - public abstract long add(Event event); - - @Insert(onConflict = OnConflictStrategy.REPLACE) - public abstract long[] addAll(List eventList); - - @Query("DELETE FROM events WHERE profileId = :profileId") - public abstract void clear(int profileId); - - @Query("DELETE FROM events WHERE profileId = :profileId AND eventId = :id") - public abstract void remove(int profileId, long id); - @Query("DELETE FROM metadata WHERE profileId = :profileId AND thingType = :thingType AND thingId = :thingId") - public abstract void removeMetadata(int profileId, int thingType, long thingId); - @Transaction - public void remove(int profileId, long type, long id) { - remove(profileId, id); - removeMetadata(profileId, type == Event.TYPE_HOMEWORK ? TYPE_HOMEWORK : TYPE_EVENT, id); - } - @Transaction - public void remove(Event event) { - remove(event.profileId, event.type, event.id); - } - @Transaction - public void remove(int profileId, Event event) { - remove(profileId, event.type, event.id); - } - @Query("DELETE FROM events WHERE teamId = :teamId AND eventId = :id") - public abstract void removeByTeamId(long teamId, long id); - - @RawQuery(observedEntities = {Event.class}) - abstract LiveData> getAll(SupportSQLiteQuery query); - public LiveData> getAll(int profileId, String filter, String limit) { - String query = "SELECT \n" + - "*, \n" + - "teachers.teacherName || ' ' || teachers.teacherSurname AS teacherFullName,\n" + - "eventTypes.eventTypeName AS typeName,\n" + - "eventTypes.eventTypeColor AS typeColor\n" + - "FROM events\n" + - "LEFT JOIN subjects USING(profileId, subjectId)\n" + - "LEFT JOIN teachers USING(profileId, teacherId)\n" + - "LEFT JOIN teams USING(profileId, teamId)\n" + - "LEFT JOIN eventTypes USING(profileId, eventType)\n" + - "LEFT JOIN metadata ON eventId = thingId AND (thingType = " + TYPE_EVENT + " OR thingType = " + TYPE_HOMEWORK + ") AND metadata.profileId = "+profileId+"\n" + - "WHERE events.profileId = "+profileId+" AND events.eventBlacklisted = 0 AND "+filter+"\n" + - "GROUP BY eventId\n" + - "ORDER BY eventDate, eventStartTime ASC "+limit; - Log.d("DB", query); - return getAll(new SimpleSQLiteQuery(query)); - } - public LiveData> getAll(int profileId) { - return getAll(profileId, "1", ""); - } - public List getAllNow(int profileId) { - return getAllNow(profileId, "1"); - } - public LiveData> getAllWhere(int profileId, String filter) { - return getAll(profileId, filter, ""); - } - public LiveData> getAllByType(int profileId, long type, String filter) { - return getAll(profileId, "eventType = "+type+" AND "+filter, ""); - } - public LiveData> getAllByDate(int profileId, @NonNull Date date) { - return getAll(profileId, "eventDate = '"+date.getStringY_m_d()+"'", ""); - } - public List getAllByDateNow(int profileId, @NonNull Date date) { - return getAllNow(profileId, "eventDate = '"+date.getStringY_m_d()+"'"); - } - public LiveData> getAllByDateTime(int profileId, @NonNull Date date, Time time) { - if (time == null) - return getAllByDate(profileId, date); - return getAll(profileId, "eventDate = '"+date.getStringY_m_d()+"' AND eventStartTime = '"+time.getStringValue()+"'", ""); - } - public LiveData> getAllNearest(int profileId, @NonNull Date today, int limit) { - return getAll(profileId, "eventDate >= '"+today.getStringY_m_d()+"'", "LIMIT "+limit); - } - - @RawQuery - abstract List getAllNow(SupportSQLiteQuery query); - public List getAllNow(int profileId, String filter) { - return getAllNow(new SimpleSQLiteQuery("SELECT \n" + - "*, \n" + - "teachers.teacherName || ' ' || teachers.teacherSurname AS teacherFullName,\n" + - "eventTypes.eventTypeName AS typeName,\n" + - "eventTypes.eventTypeColor AS typeColor\n" + - "FROM events \n" + - "LEFT JOIN subjects USING(profileId, subjectId)\n" + - "LEFT JOIN teachers USING(profileId, teacherId)\n" + - "LEFT JOIN teams USING(profileId, teamId)\n" + - "LEFT JOIN eventTypes USING(profileId, eventType)\n" + - "LEFT JOIN metadata ON eventId = thingId AND (thingType = " + TYPE_EVENT + " OR thingType = " + TYPE_HOMEWORK + ") AND metadata.profileId = "+profileId+"\n" + - "WHERE events.profileId = "+profileId+" AND events.eventBlacklisted = 0 AND "+filter+"\n" + - "GROUP BY eventId\n" + - "ORDER BY eventStartTime, addedDate ASC")); - } - public List getNotNotifiedNow(int profileId) { - return getAllNow(profileId, "notified = 0"); - } - - @Query("SELECT eventId FROM events WHERE profileId = :profileId AND eventBlacklisted = 1") - public abstract List getBlacklistedIds(int profileId); - @Query("SELECT eventId FROM events WHERE eventBlacklisted = 1") - public abstract List getBlacklistedIds(); - - @Query("SELECT " + - "*, " + - "eventTypes.eventTypeName AS typeName, " + - "eventTypes.eventTypeColor AS typeColor " + - "FROM events " + - "LEFT JOIN subjects USING(profileId, subjectId) " + - "LEFT JOIN eventTypes USING(profileId, eventType) " + - "LEFT JOIN metadata ON eventId = thingId AND (thingType = " + TYPE_EVENT + " OR thingType = " + TYPE_HOMEWORK + ") AND metadata.profileId = events.profileId " + - "WHERE events.eventBlacklisted = 0 AND notified = 0 " + - "GROUP BY eventId " + - "ORDER BY addedDate ASC") - public abstract List getNotNotifiedNow(); - - public EventFull getByIdNow(int profileId, long eventId) { - List eventList = getAllNow(profileId, "eventId = "+eventId); - return eventList.size() == 0 ? null : eventList.get(0); - } - - @Query("UPDATE events SET eventAddedManually = 1 WHERE profileId = :profileId AND eventDate < :date") - public abstract void convertOlderToManual(int profileId, Date date); - - @Query("DELETE FROM events WHERE profileId = :profileId AND eventAddedManually = 0") - public abstract void removeNotManual(int profileId); - - @RawQuery - abstract long removeFuture(SupportSQLiteQuery query); - @Transaction - public void removeFuture(int profileId, Date todayDate, String filter) { - removeFuture(new SimpleSQLiteQuery("DELETE FROM events WHERE profileId = " + profileId - + " AND eventAddedManually = 0 AND eventDate >= '" + todayDate.getStringY_m_d() + "'" + - " AND " + filter)); - } - - @Query("DELETE FROM events WHERE profileId = :profileId AND eventAddedManually = 0 AND eventDate >= :todayDate AND eventType = :type") - public abstract void removeFutureWithType(int profileId, Date todayDate, long type); - - @Query("DELETE FROM events WHERE profileId = :profileId AND eventAddedManually = 0 AND eventDate >= :todayDate AND eventType != :exceptType") - public abstract void removeFutureExceptType(int profileId, Date todayDate, long exceptType); - - @Transaction - public void removeFutureExceptTypes(int profileId, Date todayDate, List exceptTypes) { - removeFuture(profileId, todayDate, "eventType NOT IN " + exceptTypes.toString().replace('[', '(').replace(']', ')')); - } - - @Query("UPDATE metadata SET seen = :seen WHERE profileId = :profileId AND (thingType = "+TYPE_EVENT+" OR thingType = "+TYPE_LESSON_CHANGE+" OR thingType = "+TYPE_HOMEWORK+") AND thingId IN (SELECT eventId FROM events WHERE profileId = :profileId AND eventDate = :date)") - public abstract void setSeenByDate(int profileId, Date date, boolean seen); - - @Query("UPDATE events SET eventBlacklisted = :blacklisted WHERE profileId = :profileId AND eventId = :eventId") - public abstract void setBlacklisted(int profileId, long eventId, boolean blacklisted); -} - diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/EventDao.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/EventDao.kt new file mode 100644 index 00000000..ad47e90c --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/EventDao.kt @@ -0,0 +1,184 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2020-1-6 + */ +package pl.szczodrzynski.edziennik.data.db.dao + +import androidx.lifecycle.LiveData +import androidx.room.* +import androidx.sqlite.db.SimpleSQLiteQuery +import androidx.sqlite.db.SupportSQLiteQuery +import pl.szczodrzynski.edziennik.data.db.entity.Event +import pl.szczodrzynski.edziennik.data.db.entity.Metadata +import pl.szczodrzynski.edziennik.data.db.full.EventFull +import pl.szczodrzynski.edziennik.utils.models.Date +import pl.szczodrzynski.edziennik.utils.models.Time + +@Dao +abstract class EventDao { + @Insert(onConflict = OnConflictStrategy.REPLACE) + abstract fun add(event: Event): Long + + @Insert(onConflict = OnConflictStrategy.REPLACE) + abstract fun addAll(eventList: List): LongArray + + @Query("DELETE FROM events WHERE profileId = :profileId") + abstract fun clear(profileId: Int) + + @Query("DELETE FROM events WHERE profileId = :profileId AND eventId = :id") + abstract fun remove(profileId: Int, id: Long) + + @Query("DELETE FROM metadata WHERE profileId = :profileId AND thingType = :thingType AND thingId = :thingId") + abstract fun removeMetadata(profileId: Int, thingType: Int, thingId: Long) + + @Transaction + open fun remove(profileId: Int, type: Long, id: Long) { + remove(profileId, id) + removeMetadata(profileId, if (type == Event.TYPE_HOMEWORK) Metadata.TYPE_HOMEWORK else Metadata.TYPE_EVENT, id) + } + + @Transaction + open fun remove(event: Event) { + remove(event.profileId, event.type, event.id) + } + + @Transaction + open fun remove(profileId: Int, event: Event) { + remove(profileId, event.type, event.id) + } + + @Query("DELETE FROM events WHERE teamId = :teamId AND eventId = :id") + abstract fun removeByTeamId(teamId: Long, id: Long) + + @RawQuery(observedEntities = [Event::class]) + abstract fun getAll(query: SupportSQLiteQuery): LiveData> + + fun getAll(profileId: Int, filter: String, limit: String): LiveData> { + return getAll(SimpleSQLiteQuery("SELECT \n" + + "*, \n" + + "teachers.teacherName || ' ' || teachers.teacherSurname AS teacherFullName,\n" + + "eventTypes.eventTypeName AS typeName,\n" + + "eventTypes.eventTypeColor AS typeColor\n" + + "FROM events\n" + + "LEFT JOIN subjects USING(profileId, subjectId)\n" + + "LEFT JOIN teachers USING(profileId, teacherId)\n" + + "LEFT JOIN teams USING(profileId, teamId)\n" + + "LEFT JOIN eventTypes USING(profileId, eventType)\n" + + "LEFT JOIN metadata ON eventId = thingId AND (thingType = " + Metadata.TYPE_EVENT + " OR thingType = " + Metadata.TYPE_HOMEWORK + ") AND metadata.profileId = " + profileId + "\n" + + "WHERE events.profileId = " + profileId + " AND events.eventBlacklisted = 0 AND " + filter + "\n" + + "GROUP BY eventId\n" + + "ORDER BY eventDate, eventTime ASC " + limit)) + } + + fun getAll(profileId: Int): LiveData> { + return getAll(profileId, "1", "") + } + + fun getAllNow(profileId: Int): List { + return getAllNow(profileId, "1") + } + + fun getAllWhere(profileId: Int, filter: String): LiveData> { + return getAll(profileId, filter, "") + } + + fun getAllByType(profileId: Int, type: Long, filter: String): LiveData> { + return getAll(profileId, "eventType = $type AND $filter", "") + } + + fun getAllByDate(profileId: Int, date: Date): LiveData> { + return getAll(profileId, "eventDate = '" + date.stringY_m_d + "'", "") + } + + fun getAllByDateNow(profileId: Int, date: Date): List { + return getAllNow(profileId, "eventDate = '" + date.stringY_m_d + "'") + } + + fun getAllByDateTime(profileId: Int, date: Date, time: Time?): LiveData> { + return if (time == null) getAllByDate(profileId, date) else getAll(profileId, "eventDate = '" + date.stringY_m_d + "' AND eventTime = '" + time.stringValue + "'", "") + } + + fun getAllNearest(profileId: Int, today: Date, limit: Int): LiveData> { + return getAll(profileId, "eventDate >= '" + today.stringY_m_d + "'", "LIMIT $limit") + } + + @RawQuery + abstract fun getAllNow(query: SupportSQLiteQuery): List + + fun getAllNow(profileId: Int, filter: String): List { + return getAllNow(SimpleSQLiteQuery("SELECT \n" + + "*, \n" + + "teachers.teacherName || ' ' || teachers.teacherSurname AS teacherFullName,\n" + + "eventTypes.eventTypeName AS typeName,\n" + + "eventTypes.eventTypeColor AS typeColor\n" + + "FROM events \n" + + "LEFT JOIN subjects USING(profileId, subjectId)\n" + + "LEFT JOIN teachers USING(profileId, teacherId)\n" + + "LEFT JOIN teams USING(profileId, teamId)\n" + + "LEFT JOIN eventTypes USING(profileId, eventType)\n" + + "LEFT JOIN metadata ON eventId = thingId AND (thingType = " + Metadata.TYPE_EVENT + " OR thingType = " + Metadata.TYPE_HOMEWORK + ") AND metadata.profileId = " + profileId + "\n" + + "WHERE events.profileId = " + profileId + " AND events.eventBlacklisted = 0 AND " + filter + "\n" + + "GROUP BY eventId\n" + + "ORDER BY eventTime, addedDate ASC")) + } + + fun getNotNotifiedNow(profileId: Int): List { + return getAllNow(profileId, "notified = 0") + } + + @Query("SELECT eventId FROM events WHERE profileId = :profileId AND eventBlacklisted = 1") + abstract fun getBlacklistedIds(profileId: Int): List + + @get:Query("SELECT eventId FROM events WHERE eventBlacklisted = 1") + abstract val blacklistedIds: List + + @get:Query("SELECT " + + "*, " + + "eventTypes.eventTypeName AS typeName, " + + "eventTypes.eventTypeColor AS typeColor " + + "FROM events " + + "LEFT JOIN subjects USING(profileId, subjectId) " + + "LEFT JOIN eventTypes USING(profileId, eventType) " + + "LEFT JOIN metadata ON eventId = thingId AND (thingType = " + Metadata.TYPE_EVENT + " OR thingType = " + Metadata.TYPE_HOMEWORK + ") AND metadata.profileId = events.profileId " + + "WHERE events.eventBlacklisted = 0 AND notified = 0 " + + "GROUP BY eventId " + + "ORDER BY addedDate ASC") + abstract val notNotifiedNow: List + + fun getByIdNow(profileId: Int, eventId: Long): EventFull? { + val eventList = getAllNow(profileId, "eventId = $eventId") + return if (eventList.isEmpty()) null else eventList[0] + } + + @Query("UPDATE events SET eventAddedManually = 1 WHERE profileId = :profileId AND eventDate < :date") + abstract fun convertOlderToManual(profileId: Int, date: Date?) + + @Query("DELETE FROM events WHERE profileId = :profileId AND eventAddedManually = 0") + abstract fun removeNotManual(profileId: Int) + + @RawQuery + abstract fun removeFuture(query: SupportSQLiteQuery?): Long + + @Transaction + open fun removeFuture(profileId: Int, todayDate: Date, filter: String) { + removeFuture(SimpleSQLiteQuery("DELETE FROM events WHERE profileId = " + profileId + + " AND eventAddedManually = 0 AND eventDate >= '" + todayDate.stringY_m_d + "'" + + " AND " + filter)) + } + + @Query("DELETE FROM events WHERE profileId = :profileId AND eventAddedManually = 0 AND eventDate >= :todayDate AND eventType = :type") + abstract fun removeFutureWithType(profileId: Int, todayDate: Date, type: Long) + + @Query("DELETE FROM events WHERE profileId = :profileId AND eventAddedManually = 0 AND eventDate >= :todayDate AND eventType != :exceptType") + abstract fun removeFutureExceptType(profileId: Int, todayDate: Date, exceptType: Long) + + @Transaction + open fun removeFutureExceptTypes(profileId: Int, todayDate: Date, exceptTypes: List) { + removeFuture(profileId, todayDate, "eventType NOT IN " + exceptTypes.toString().replace('[', '(').replace(']', ')')) + } + + @Query("UPDATE metadata SET seen = :seen WHERE profileId = :profileId AND (thingType = " + Metadata.TYPE_EVENT + " OR thingType = " + Metadata.TYPE_LESSON_CHANGE + " OR thingType = " + Metadata.TYPE_HOMEWORK + ") AND thingId IN (SELECT eventId FROM events WHERE profileId = :profileId AND eventDate = :date)") + abstract fun setSeenByDate(profileId: Int, date: Date, seen: Boolean) + + @Query("UPDATE events SET eventBlacklisted = :blacklisted WHERE profileId = :profileId AND eventId = :eventId") + abstract fun setBlacklisted(profileId: Int, eventId: Long, blacklisted: Boolean) +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/EventTypeDao.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/EventTypeDao.java deleted file mode 100644 index 25366699..00000000 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/EventTypeDao.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) Kacper Ziubryniewicz 2020-1-6 - */ - -package pl.szczodrzynski.edziennik.data.db.dao; - -import androidx.lifecycle.LiveData; -import androidx.room.Dao; -import androidx.room.Insert; -import androidx.room.OnConflictStrategy; -import androidx.room.Query; - -import java.util.List; - -import pl.szczodrzynski.edziennik.data.db.entity.EventType; - -@Dao -public interface EventTypeDao { - @Insert(onConflict = OnConflictStrategy.REPLACE) - void add(EventType gradeCategory); - - @Insert(onConflict = OnConflictStrategy.REPLACE) - void addAll(List gradeCategoryList); - - @Query("DELETE FROM eventTypes WHERE profileId = :profileId") - void clear(int profileId); - - @Query("SELECT * FROM eventTypes WHERE profileId = :profileId AND eventType = :typeId") - EventType getByIdNow(int profileId, long typeId); - - @Query("SELECT * FROM eventTypes WHERE profileId = :profileId") - LiveData> getAll(int profileId); - - @Query("SELECT * FROM eventTypes WHERE profileId = :profileId") - List getAllNow(int profileId); - - @Query("SELECT * FROM eventTypes") - List getAllNow(); -} 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 new file mode 100644 index 00000000..28be6d65 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/EventTypeDao.kt @@ -0,0 +1,77 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2020-1-6 + */ +package pl.szczodrzynski.edziennik.data.db.dao + +import android.content.Context +import androidx.lifecycle.LiveData +import androidx.room.Dao +import androidx.room.Insert +import androidx.room.OnConflictStrategy +import androidx.room.Query +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_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_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 +import pl.szczodrzynski.edziennik.data.db.entity.EventType + +@Dao +abstract class EventTypeDao { + @Insert(onConflict = OnConflictStrategy.REPLACE) + abstract fun add(eventType: EventType) + + @Insert(onConflict = OnConflictStrategy.REPLACE) + abstract fun addAll(eventTypeList: List) + + @Query("DELETE FROM eventTypes WHERE profileId = :profileId") + abstract fun clear(profileId: Int) + + @Query("SELECT * FROM eventTypes WHERE profileId = :profileId AND eventType = :typeId") + abstract fun getByIdNow(profileId: Int, typeId: Long): EventType? + + @Query("SELECT * FROM eventTypes WHERE profileId = :profileId") + abstract fun getAll(profileId: Int): LiveData> + + @Query("SELECT * FROM eventTypes WHERE profileId = :profileId") + abstract fun getAllNow(profileId: Int): List + + @get:Query("SELECT * FROM eventTypes") + abstract val allNow: List + + fun addDefaultTypes(context: Context, profileId: Int): List { + val typeList = listOf( + EventType(profileId, TYPE_HOMEWORK, context.getString(R.string.event_type_homework), COLOR_HOMEWORK), + EventType(profileId, TYPE_DEFAULT, context.getString(R.string.event_other), COLOR_DEFAULT), + EventType(profileId, TYPE_EXAM, context.getString(R.string.event_exam), COLOR_EXAM), + EventType(profileId, TYPE_SHORT_QUIZ, context.getString(R.string.event_short_quiz), COLOR_SHORT_QUIZ), + EventType(profileId, TYPE_ESSAY, context.getString(R.string.event_essay), COLOR_ESSAY), + EventType(profileId, TYPE_PROJECT, context.getString(R.string.event_project), COLOR_PROJECT), + EventType(profileId, TYPE_PT_MEETING, context.getString(R.string.event_pt_meeting), COLOR_PT_MEETING), + EventType(profileId, TYPE_EXCURSION, context.getString(R.string.event_excursion), COLOR_EXCURSION), + EventType(profileId, TYPE_READING, context.getString(R.string.event_reading), COLOR_READING), + EventType(profileId, TYPE_CLASS_EVENT, context.getString(R.string.event_class_event), COLOR_CLASS_EVENT), + EventType(profileId, TYPE_INFORMATION, context.getString(R.string.event_information), COLOR_INFORMATION) + ) + addAll(typeList) + return typeList + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/MetadataDao.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/MetadataDao.java index 845941de..510306fe 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/MetadataDao.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/MetadataDao.java @@ -78,8 +78,8 @@ public abstract class MetadataDao { } } if (o instanceof Event) { - if (add(new Metadata(profileId, ((Event) o).type == Event.TYPE_HOMEWORK ? TYPE_HOMEWORK : TYPE_EVENT, ((Event) o).id, seen, false, 0)) == -1) { - updateSeen(profileId, ((Event) o).type == Event.TYPE_HOMEWORK ? TYPE_HOMEWORK : TYPE_EVENT, ((Event) o).id, seen); + if (add(new Metadata(profileId, ((Event) o).getType() == Event.TYPE_HOMEWORK ? TYPE_HOMEWORK : TYPE_EVENT, ((Event) o).getId(), seen, false, 0)) == -1) { + updateSeen(profileId, ((Event) o).getType() == Event.TYPE_HOMEWORK ? TYPE_HOMEWORK : TYPE_EVENT, ((Event) o).getId(), seen); } } if (o instanceof LessonFull) { @@ -117,8 +117,8 @@ public abstract class MetadataDao { } } if (o instanceof Event) { - if (add(new Metadata(profileId, ((Event) o).type == Event.TYPE_HOMEWORK ? TYPE_HOMEWORK : TYPE_EVENT, ((Event) o).id, false, notified, 0)) == -1) { - updateNotified(profileId, ((Event) o).type == Event.TYPE_HOMEWORK ? TYPE_HOMEWORK : TYPE_EVENT, ((Event) o).id, notified); + if (add(new Metadata(profileId, ((Event) o).getType() == Event.TYPE_HOMEWORK ? TYPE_HOMEWORK : TYPE_EVENT, ((Event) o).getId(), false, notified, 0)) == -1) { + updateNotified(profileId, ((Event) o).getType() == Event.TYPE_HOMEWORK ? TYPE_HOMEWORK : TYPE_EVENT, ((Event) o).getId(), notified); } } if (o instanceof LessonFull) { @@ -141,9 +141,9 @@ public abstract class MetadataDao { @Transaction public void setBoth(int profileId, Event o, boolean seen, boolean notified, long addedDate) { if (o != null) { - if (add(new Metadata(profileId, o.type == Event.TYPE_HOMEWORK ? TYPE_HOMEWORK : TYPE_EVENT, o.id, seen, notified, addedDate)) == -1) { - updateSeen(profileId, o.type == Event.TYPE_HOMEWORK ? TYPE_HOMEWORK : TYPE_EVENT, o.id, seen); - updateNotified(profileId, o.type == Event.TYPE_HOMEWORK ? TYPE_HOMEWORK : TYPE_EVENT, o.id, notified); + if (add(new Metadata(profileId, o.getType() == Event.TYPE_HOMEWORK ? TYPE_HOMEWORK : TYPE_EVENT, o.getId(), seen, notified, addedDate)) == -1) { + updateSeen(profileId, o.getType() == Event.TYPE_HOMEWORK ? TYPE_HOMEWORK : TYPE_EVENT, o.getId(), seen); + updateNotified(profileId, o.getType() == Event.TYPE_HOMEWORK ? TYPE_HOMEWORK : TYPE_EVENT, o.getId(), notified); } } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Event.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Event.java deleted file mode 100644 index bed95bdf..00000000 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Event.java +++ /dev/null @@ -1,161 +0,0 @@ -/* - * Copyright (c) Kacper Ziubryniewicz 2020-1-6 - */ - -package pl.szczodrzynski.edziennik.data.db.entity; - -import androidx.annotation.Nullable; -import androidx.room.ColumnInfo; -import androidx.room.Entity; -import androidx.room.Ignore; -import androidx.room.Index; - -import java.util.Calendar; - -import pl.szczodrzynski.edziennik.data.db.full.EventFull; -import pl.szczodrzynski.edziennik.utils.models.Date; -import pl.szczodrzynski.edziennik.utils.models.Time; - -@Entity(tableName = "events", - primaryKeys = {"profileId", "eventId"}, - indices = {@Index(value = {"profileId", "eventDate", "eventStartTime"}), @Index(value = {"profileId", "eventType"})}) -public class Event { - public int profileId; - - @ColumnInfo(name = "eventId") - public long id; - - @ColumnInfo(name = "eventDate") - public Date eventDate; - @ColumnInfo(name = "eventStartTime") - @Nullable - public Time startTime; // null for allDay - @ColumnInfo(name = "eventTopic") - public String topic; - @ColumnInfo(name = "eventColor") - public int color = -1; - public static final long TYPE_UNDEFINED = -2; - public static final long TYPE_HOMEWORK = -1; - public static final long TYPE_DEFAULT = 0; - public static final long TYPE_EXAM = 1; - public static final long TYPE_SHORT_QUIZ = 2; - public static final long TYPE_ESSAY = 3; - public static final long TYPE_PROJECT = 4; - public static final long TYPE_PT_MEETING = 5; - public static final long TYPE_EXCURSION = 6; - public static final long TYPE_READING = 7; - public static final long TYPE_CLASS_EVENT = 8; - public static final long TYPE_INFORMATION = 9; - public static final long TYPE_TEACHER_ABSENCE = 10; - public static final int COLOR_HOMEWORK = 0xff795548; - public static final int COLOR_DEFAULT = 0xffffc107; - public static final int COLOR_EXAM = 0xfff44336; - public static final int COLOR_SHORT_QUIZ = 0xff76ff03; - public static final int COLOR_ESSAY = 0xFF4050B5; - public static final int COLOR_PROJECT = 0xFF673AB7; - public static final int COLOR_PT_MEETING = 0xff90caf9; - public static final int COLOR_EXCURSION = 0xFF4CAF50; - public static final int COLOR_READING = 0xFFFFEB3B; - public static final int COLOR_CLASS_EVENT = 0xff388e3c; - public static final int COLOR_INFORMATION = 0xff039be5; - public static final int COLOR_TEACHER_ABSENCE = 0xff039be5; - @ColumnInfo(name = "eventType") - public long type = TYPE_DEFAULT; - @ColumnInfo(name = "eventAddedManually") - public boolean addedManually; - @ColumnInfo(name = "eventSharedBy") - public String sharedBy = null; - @ColumnInfo(name = "eventSharedByName") - public String sharedByName = null; - @ColumnInfo(name = "eventBlacklisted") - public boolean blacklisted = false; - - - public long teacherId; - public long subjectId; - public long teamId; - - @Ignore - public Event() {} - - public Event(int profileId, long id, Date eventDate, @Nullable Time startTime, String topic, int color, long type, boolean addedManually, long teacherId, long subjectId, long teamId) - { - this.profileId = profileId; - this.id = id; - this.eventDate = eventDate; - this.startTime = startTime; - this.topic = topic; - this.color = color; - this.type = type; - this.addedManually = addedManually; - this.teacherId = teacherId; - this.subjectId = subjectId; - this.teamId = teamId; - } - - @Ignore - public EventFull withMetadata(Metadata metadata) { - return new EventFull(this, metadata); - } - - @Ignore - public Calendar getStartTimeCalendar() { - Calendar c = Calendar.getInstance(); - c.set( - eventDate.year, - eventDate.month - 1, - eventDate.day, - (startTime == null) ? 0 : startTime.hour, - (startTime == null) ? 0 : startTime.minute, - (startTime == null) ? 0 : startTime.second - ); - return c; - } - - @Ignore - public Calendar getEndTimeCalendar() { - Calendar c = Calendar.getInstance(); - c.setTimeInMillis(getStartTimeCalendar().getTimeInMillis() + (45 * 60 * 1000)); - return c; - } - - @Override - public Event clone() { - Event event = new Event( - profileId, - id, - eventDate.clone(), - startTime == null ? null : startTime.clone(), - topic, - color, - type, - addedManually, - subjectId, - teacherId, - teamId - ); - event.sharedBy = sharedBy; - event.sharedByName = sharedByName; - event.blacklisted = blacklisted; - return event; - } - - @Override - public String toString() { - return "Event{" + - "profileId=" + profileId + - ", id=" + id + - ", eventDate=" + eventDate + - ", startTime=" + startTime + - ", topic='" + topic + '\'' + - ", color=" + color + - ", type=" + type + - ", addedManually=" + addedManually + - ", sharedBy='" + sharedBy + '\'' + - ", sharedByName='" + sharedByName + '\'' + - ", teacherId=" + teacherId + - ", subjectId=" + subjectId + - ", teamId=" + teamId + - '}'; - } -} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Event.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Event.kt new file mode 100644 index 00000000..64c6de8e --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Event.kt @@ -0,0 +1,107 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2020-1-6 + */ +package pl.szczodrzynski.edziennik.data.db.entity + +import androidx.room.ColumnInfo +import androidx.room.Entity +import androidx.room.Ignore +import androidx.room.Index +import com.google.gson.annotations.SerializedName +import pl.szczodrzynski.edziennik.MINUTE +import pl.szczodrzynski.edziennik.data.db.full.EventFull +import pl.szczodrzynski.edziennik.utils.models.Date +import pl.szczodrzynski.edziennik.utils.models.Time +import java.util.* + +@Entity(tableName = "events", + primaryKeys = ["profileId", "eventId"], + indices = [ + Index(value = ["profileId", "eventDate", "eventTime"]), + Index(value = ["profileId", "eventType"]) + ]) +open class Event( + /* This needs to be mutable: see SzkolnyApi.getEvents() */ + var profileId: Int, + @ColumnInfo(name = "eventId") + var id: Long, + @ColumnInfo(name = "eventDate") + @SerializedName("eventDate") + var date: Date, + @ColumnInfo(name = "eventTime") + @SerializedName("startTime") + var time: Time?, + + @ColumnInfo(name = "eventTopic") + var topic: String, + @ColumnInfo(name = "eventColor") + var color: Int?, + @ColumnInfo(name = "eventType") + var type: Long, + + var teacherId: Long, + var subjectId: Long, + var teamId: Long +) { + companion object { + const val TYPE_UNDEFINED = -2L + const val TYPE_HOMEWORK = -1L + const val TYPE_DEFAULT = 0L + const val TYPE_EXAM = 1L + const val TYPE_SHORT_QUIZ = 2L + const val TYPE_ESSAY = 3L + const val TYPE_PROJECT = 4L + const val TYPE_PT_MEETING = 5L + const val TYPE_EXCURSION = 6L + const val TYPE_READING = 7L + const val TYPE_CLASS_EVENT = 8L + const val TYPE_INFORMATION = 9L + const val TYPE_TEACHER_ABSENCE = 10L + const val COLOR_HOMEWORK = 0xff795548.toInt() + const val COLOR_DEFAULT = 0xffffc107.toInt() + const val COLOR_EXAM = 0xfff44336.toInt() + const val COLOR_SHORT_QUIZ = 0xff76ff03.toInt() + const val COLOR_ESSAY = 0xFF4050B5.toInt() + const val COLOR_PROJECT = 0xFF673AB7.toInt() + const val COLOR_PT_MEETING = 0xff90caf9.toInt() + const val COLOR_EXCURSION = 0xFF4CAF50.toInt() + const val COLOR_READING = 0xFFFFEB3B.toInt() + const val COLOR_CLASS_EVENT = 0xff388e3c.toInt() + const val COLOR_INFORMATION = 0xff039be5.toInt() + const val COLOR_TEACHER_ABSENCE = 0xff039be5.toInt() + } + + @ColumnInfo(name = "eventAddedManually") + var addedManually: Boolean = false + @ColumnInfo(name = "eventSharedBy") + var sharedBy: String? = null + @ColumnInfo(name = "eventSharedByName") + var sharedByName: String? = null + @ColumnInfo(name = "eventBlacklisted") + var blacklisted: Boolean = false + + var homeworkBody: String? = null + var attachmentIds: List? = null + var attachmentNames: List? = null + + @Ignore + var showAsUnseen = false + + val startTimeCalendar: Calendar + get() = Calendar.getInstance().also { it.set( + date.year, + date.month - 1, + date.day, + time?.hour ?: 0, + time?.minute ?: 0, + time?.second ?: 0 + ) } + + val endTimeCalendar: Calendar + get() = startTimeCalendar.also { + it.timeInMillis += 45 * MINUTE * 1000 + } + + @Ignore + fun withMetadata(metadata: Metadata) = EventFull(this, metadata) +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/full/EventFull.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/full/EventFull.java deleted file mode 100644 index beaf794e..00000000 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/full/EventFull.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright (c) Kacper Ziubryniewicz 2020-1-6 - */ - -package pl.szczodrzynski.edziennik.data.db.full; - -import pl.szczodrzynski.edziennik.data.db.entity.Event; -import pl.szczodrzynski.edziennik.data.db.entity.Metadata; - -public class EventFull extends Event { - public String typeName = ""; - public int typeColor = -1; - - public String teacherFullName = ""; - - public String subjectLongName = ""; - public String subjectShortName = ""; - - public String teamName = ""; - public String teamCode = null; - - // metadata - public boolean seen; - public boolean notified; - public long addedDate; - - public EventFull() {} - - public EventFull(Event event) { - super( - event.profileId, - event.id, - event.eventDate.clone(), - event.startTime == null ? null : event.startTime.clone(), - event.topic, - event.color, - event.type, - event.addedManually, - event.teacherId, - event.subjectId, - event.teamId - ); - - this.sharedBy = event.sharedBy; - this.sharedByName = event.sharedByName; - this.blacklisted = event.blacklisted; - } - public EventFull(EventFull event) { - super( - event.profileId, - event.id, - event.eventDate.clone(), - event.startTime == null ? null : event.startTime.clone(), - event.topic, - event.color, - event.type, - event.addedManually, - event.teacherId, - event.subjectId, - event.teamId - ); - - this.sharedBy = event.sharedBy; - this.sharedByName = event.sharedByName; - this.blacklisted = event.blacklisted; - this.typeName = event.typeName; - this.typeColor = event.typeColor; - this.teacherFullName = event.teacherFullName; - this.subjectLongName = event.subjectLongName; - this.subjectShortName = event.subjectShortName; - this.teamName = event.teamName; - this.teamCode = event.teamCode; - this.seen = event.seen; - this.notified = event.notified; - this.addedDate = event.addedDate; - } - - public EventFull(Event event, Metadata metadata) { - this(event); - - this.seen = metadata.seen; - this.notified = metadata.notified; - this.addedDate = metadata.addedDate; - } - - public int getColor() { - return color == -1 ? typeColor : color; - } - - @Override - public String toString() { - return "EventFull{" + - "profileId=" + profileId + - ", id=" + id + - ", eventDate=" + eventDate + - ", startTime=" + startTime + - ", topic='" + topic + '\'' + - ", color=" + color + - ", type=" + type + - ", addedManually=" + addedManually + - ", sharedBy='" + sharedBy + '\'' + - ", sharedByName='" + sharedByName + '\'' + - ", blacklisted=" + blacklisted + - ", teacherId=" + teacherId + - ", subjectId=" + subjectId + - ", teamId=" + teamId + - ", typeName='" + typeName + '\'' + - ", teacherFullName='" + teacherFullName + '\'' + - ", subjectLongName='" + subjectLongName + '\'' + - ", subjectShortName='" + subjectShortName + '\'' + - ", teamName='" + teamName + '\'' + - ", seen=" + seen + - ", notified=" + notified + - ", addedDate=" + addedDate + - '}'; - } -} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/full/EventFull.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/full/EventFull.kt new file mode 100644 index 00000000..16a6a5c3 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/full/EventFull.kt @@ -0,0 +1,55 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2020-1-6 + */ +package pl.szczodrzynski.edziennik.data.db.full + +import pl.szczodrzynski.edziennik.data.db.entity.Event +import pl.szczodrzynski.edziennik.data.db.entity.Metadata +import pl.szczodrzynski.edziennik.utils.models.Date +import pl.szczodrzynski.edziennik.utils.models.Time + +class EventFull( + profileId: Int, id: Long, date: Date, time: Time?, + topic: String, color: Int?, type: Long, + teacherId: Long, subjectId: Long, teamId: Long +) : Event( + profileId, id, date, time, + topic, color, type, + teacherId, subjectId, teamId +) { + constructor(event: Event, metadata: Metadata? = null) : this( + event.profileId, event.id, event.date, event.time, + event.topic, event.color, event.type, + event.teacherId, event.subjectId, event.teamId) { + event.let { + addedManually = it.addedManually + sharedBy = it.sharedBy + sharedByName = it.sharedByName + blacklisted = it.blacklisted + homeworkBody = it.homeworkBody + attachmentIds = it.attachmentIds + attachmentNames = it.attachmentNames + } + metadata?.let { + seen = it.seen + notified = it.notified + addedDate = it.addedDate + } + } + + var typeName: String? = null + var typeColor: Int? = null + + var teacherFullName: String? = null + var subjectLongName: String? = null + var subjectShortName: String? = null + var teamName: String? = null + var teamCode: String? = null + // metadata + var seen = false + var notified = false + var addedDate: Long = 0 + + val eventColor + get() = color ?: typeColor ?: 0xff2196f3.toInt() +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/migration/Migration80.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/migration/Migration80.kt new file mode 100644 index 00000000..86394792 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/migration/Migration80.kt @@ -0,0 +1,48 @@ +/* + * Copyright (c) Kuba SzczodrzyƄski 2020-3-27. + */ + +package pl.szczodrzynski.edziennik.data.db.migration + +import androidx.room.migration.Migration +import androidx.sqlite.db.SupportSQLiteDatabase + +class Migration80 : Migration(79, 80) { + override fun migrate(database: SupportSQLiteDatabase) { + // The Homework Update + database.execSQL("ALTER TABLE events RENAME TO _events;") + database.execSQL("""CREATE TABLE events ( + profileId INTEGER NOT NULL, + eventId INTEGER NOT NULL, + eventDate TEXT NOT NULL, + eventTime TEXT, + eventTopic TEXT NOT NULL, + eventColor INTEGER, + eventType INTEGER NOT NULL, + teacherId INTEGER NOT NULL, + subjectId INTEGER NOT NULL, + teamId INTEGER NOT NULL, + eventAddedManually INTEGER NOT NULL DEFAULT 0, + eventSharedBy TEXT DEFAULT NULL, + eventSharedByName TEXT DEFAULT NULL, + eventBlacklisted INTEGER NOT NULL DEFAULT 0, + homeworkBody TEXT DEFAULT NULL, + attachmentIds TEXT DEFAULT NULL, + attachmentNames TEXT DEFAULT NULL, + PRIMARY KEY(profileId, eventId) + )""") + database.execSQL("DROP INDEX IF EXISTS index_events_profileId_eventDate_eventStartTime") + database.execSQL("DROP INDEX IF EXISTS index_events_profileId_eventType") + database.execSQL("CREATE INDEX index_events_profileId_eventDate_eventTime ON events (profileId, eventDate, eventTime)") + database.execSQL("CREATE INDEX index_events_profileId_eventType ON events (profileId, eventType)") + database.execSQL(""" + INSERT INTO events (profileId, eventId, eventDate, eventTime, eventTopic, eventColor, eventType, teacherId, subjectId, teamId, eventAddedManually, eventSharedBy, eventSharedByName, eventBlacklisted) + SELECT profileId, eventId, eventDate, eventStartTime, eventTopic, + CASE eventColor WHEN -1 THEN NULL ELSE eventColor END, + eventType, teacherId, subjectId, teamId, + eventAddedManually, eventSharedBy, eventSharedByName, eventBlacklisted + FROM _events + """) + database.execSQL("DROP TABLE _events") + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyAppFirebase.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyAppFirebase.kt index 78d03873..4aa7a855 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyAppFirebase.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/firebase/SzkolnyAppFirebase.kt @@ -105,18 +105,19 @@ class SzkolnyAppFirebase(val app: App, val profiles: List, val message: if (profile.registration != Profile.REGISTRATION_ENABLED) return@forEach val event = Event( - team.profileId, - json.getLong("id") ?: return, - json.getInt("eventDate")?.let { Date.fromValue(it) } ?: return, - json.getInt("startTime")?.let { Time.fromValue(it) }, - json.getString("topic") ?: "", - json.getInt("color") ?: -1, - json.getLong("type") ?: 0, - true, - json.getLong("teacherId") ?: -1, - json.getLong("subjectId") ?: -1, - team.id + profileId = team.profileId, + id = json.getLong("id") ?: return, + date = json.getInt("eventDate")?.let { Date.fromValue(it) } ?: return, + time = json.getInt("startTime")?.let { Time.fromValue(it) }, + topic = json.getString("topic") ?: "", + color = json.getInt("color"), + type = json.getLong("type") ?: 0, + teacherId = json.getLong("teacherId") ?: -1, + subjectId = json.getLong("subjectId") ?: -1, + teamId = team.id ) + if (event.color == -1) + event.color = null event.sharedBy = json.getString("sharedBy") event.sharedByName = json.getString("sharedByName") @@ -144,7 +145,7 @@ class SzkolnyAppFirebase(val app: App, val profiles: List, val message: profileName = profile.name, viewId = if (event.type == Event.TYPE_HOMEWORK) MainActivity.DRAWER_ITEM_HOMEWORK else MainActivity.DRAWER_ITEM_AGENDA, addedDate = metadata.addedDate - ).addExtra("eventId", event.id).addExtra("eventDate", event.eventDate.value.toLong()) + ).addExtra("eventId", event.id).addExtra("eventDate", event.date.value.toLong()) notificationList += notification } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventDetailsDialog.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventDetailsDialog.kt index e4d1c144..b31edc01 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventDetailsDialog.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventDetailsDialog.kt @@ -87,11 +87,11 @@ class EventDetailsDialog( val colorSecondary = android.R.attr.textColorSecondary.resolveAttr(activity) try { - b.monthName = app.resources.getStringArray(R.array.months_day_of_array)[event.eventDate.month - 1] + b.monthName = app.resources.getStringArray(R.array.months_day_of_array)[event.date.month - 1] } catch (_: Exception) {} - b.typeColor.background?.setTintColor(event.getColor()) + b.typeColor.background?.setTintColor(event.eventColor) b.details = mutableListOf( event.subjectLongName, @@ -125,7 +125,7 @@ class EventDetailsDialog( b.goToTimetableButton.setOnClickListener { dialog.dismiss() - val dateStr = event.eventDate?.stringY_m_d ?: return@setOnClickListener + val dateStr = event.date.stringY_m_d val intent = if (activity is MainActivity && activity.navTargetId == MainActivity.DRAWER_ITEM_TIMETABLE) @@ -245,7 +245,7 @@ class EventDetailsDialog( } private fun openInCalendar() { launch { - val title = (event.typeName ?: "") + + val title = event.typeName ?: "" + (if (event.typeName.isNotNullNorBlank() && event.subjectLongName.isNotNullNorBlank()) " - " else " ") + (event.subjectLongName ?: "") @@ -254,12 +254,12 @@ class EventDetailsDialog( putExtra(Events.TITLE, title) putExtra(Events.DESCRIPTION, event.topic) - if (event.startTime == null) { + if (event.time == null) { putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true) - putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, event.eventDate.inMillis) - putExtra(CalendarContract.EXTRA_EVENT_END_TIME, event.eventDate.inMillis) + putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, event.date.inMillis) + putExtra(CalendarContract.EXTRA_EVENT_END_TIME, event.date.inMillis) } else { - val startTime = event.eventDate.combineWith(event.startTime) + val startTime = event.date.combineWith(event.time) val endTime = startTime + 45 * 60 * 1000 /* 45 min */ putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, false) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventListAdapter.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventListAdapter.kt index c70e97cf..6f4f6eba 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventListAdapter.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventListAdapter.kt @@ -50,10 +50,10 @@ class EventListAdapter( b.topic.text = event.topic b.details.text = mutableListOf( - if (showWeekDay) Week.getFullDayName(event.eventDate.weekDay) else null, - if (showDate) event.eventDate.getRelativeString(context, 7) ?: event.eventDate.formattedStringShort else null, + if (showWeekDay) Week.getFullDayName(event.date.weekDay) else null, + if (showDate) event.date.getRelativeString(context, 7) ?: event.date.formattedStringShort else null, event.typeName, - if (simpleMode) null else event.startTime?.stringHM ?: app.getString(R.string.event_all_day), + if (simpleMode) null else event.time?.stringHM ?: app.getString(R.string.event_all_day), if (simpleMode) null else event.subjectLongName ).concat(bullet) @@ -72,7 +72,7 @@ class EventListAdapter( event.teamName?.let { bullet+it } ?: "" ) - b.typeColor.background?.setTintColor(event.getColor()) + b.typeColor.background?.setTintColor(event.eventColor) b.editButton.visibility = if (event.addedManually && !simpleMode) View.VISIBLE else View.GONE b.editButton.onClick { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventManualDialog.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventManualDialog.kt index 7af42647..ecf1ff71 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventManualDialog.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventManualDialog.kt @@ -216,7 +216,7 @@ class EventManualDialog( progressDialog?.dismiss() launch { b.timeDropdown.loadItems() - b.timeDropdown.selectDefault(editingEvent?.startTime) + b.timeDropdown.selectDefault(editingEvent?.time) b.timeDropdown.selectDefault(defaultLesson?.displayStartTime ?: defaultTime) } } @@ -251,7 +251,7 @@ class EventManualDialog( nextLessonTeamId = it.displayTeamId } loadItems() - selectDefault(editingEvent?.eventDate) + selectDefault(editingEvent?.date) selectDefault(defaultLesson?.displayDate ?: defaultDate) onDateSelected = { date, lesson -> b.timeDropdown.deselect() @@ -276,8 +276,8 @@ class EventManualDialog( displayMode = DISPLAY_LESSONS if (!loadItems()) syncTimetable(lessonsDate ?: Date.getToday()) - selectDefault(editingEvent?.startTime) - if (editingEvent != null && editingEvent.startTime == null) + selectDefault(editingEvent?.time) + if (editingEvent != null && editingEvent.time == null) select(0L) selectDefault(defaultLesson?.displayStartTime ?: defaultTime) onLessonSelected = { lesson -> @@ -319,7 +319,12 @@ class EventManualDialog( val deferred = async(Dispatchers.Default) { // get the event type list - val eventTypes = app.db.eventTypeDao().getAllNow(profileId) + var eventTypes = app.db.eventTypeDao().getAllNow(profileId) + + if (eventTypes.none { it.id in -1L..10L }) { + eventTypes = app.db.eventTypeDao().addDefaultTypes(activity, profileId) + } + b.typeDropdown.clear() b.typeDropdown += eventTypes.map { TextInputDropDown.Item(it.id, it.name, tag = it) } } @@ -338,10 +343,10 @@ class EventManualDialog( // copy IDs from event being edited editingEvent?.let { b.topic.setText(it.topic) - b.typeDropdown.select(it.type.toLong())?.let { item -> + b.typeDropdown.select(it.type)?.let { item -> customColor = (item.tag as EventType).color } - if (it.color != -1) + if (it.color != null && it.color != -1) customColor = it.color } @@ -464,22 +469,25 @@ class EventManualDialog( (timeSelected as? Pair<*, *>)?.first as? Time if (isError) return + date ?: return + topic ?: return val id = System.currentTimeMillis() val eventObject = Event( - profileId, - editingEvent?.id ?: id, - date, - startTime, - topic, - customColor ?: -1, - type ?: Event.TYPE_DEFAULT, - true, - teacherId ?: -1, - subjectId ?: -1, - teamId ?: -1 - ) + profileId = profileId, + id = editingEvent?.id ?: id, + date = date, + time = startTime, + topic = topic, + color = customColor, + type = type ?: Event.TYPE_DEFAULT, + teacherId = teacherId ?: -1, + subjectId = subjectId ?: -1, + teamId = teamId ?: -1 + ).also { + it.addedManually = true + } val metadataObject = Metadata( profileId, @@ -590,6 +598,7 @@ class EventManualDialog( activity.reloadTarget() } private fun finishRemoving() { + editingEvent ?: return launch { withContext(Dispatchers.Default) { app.db.eventDao().remove(editingEvent) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/agenda/AgendaFragment.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/agenda/AgendaFragment.kt index bddd805a..6395fa8c 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/agenda/AgendaFragment.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/agenda/AgendaFragment.kt @@ -222,22 +222,22 @@ class AgendaFragment : Fragment(), CoroutineScope { events.forEach { event -> eventList.add(BaseCalendarEvent( - "${event.typeName} - ${event.topic}", + "${event.typeName ?: "wydarzenie"} - ${event.topic}", "", - (if (event.startTime == null) getString(R.string.agenda_event_all_day) else event.startTime!!.stringHM) + + (if (event.time == null) getString(R.string.agenda_event_all_day) else event.time!!.stringHM) + (event.subjectLongName?.let { ", $it" } ?: "") + (event.teacherFullName?.let { ", $it" } ?: "") + (event.teamName?.let { ", $it" } ?: ""), - event.getColor(), - Colors.legibleTextColor(event.getColor()), + event.eventColor, + Colors.legibleTextColor(event.eventColor), event.startTimeCalendar, event.endTimeCalendar, - event.startTime == null, + event.time == null, event.id, !event.seen )) - if (!event.seen) unreadEventDates.add(event.eventDate.value) + if (!event.seen) unreadEventDates.add(event.date.value) } b.agendaDefaultView.init(eventList, minDate, maxDate, Locale.getDefault(), object : CalendarPickerController { @@ -281,11 +281,11 @@ class AgendaFragment : Fragment(), CoroutineScope { val eventIcon = IconicsDrawable(activity) .icon(CommunityMaterial.Icon.cmd_checkbox_blank_circle) .size(IconicsSize.dp(10)) - .color(IconicsColor.colorInt(event.getColor())) + .color(IconicsColor.colorInt(event.eventColor)) dayList.add(EventDay(event.startTimeCalendar, eventIcon)) - if (!event.seen) unreadEventDates.add(event.eventDate.value) + if (!event.seen) unreadEventDates.add(event.date.value) } b.agendaCalendarView.setEvents(dayList) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/homework/HomeworkAdapter.java b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/homework/HomeworkAdapter.java index c8b42fbb..b017543b 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/homework/HomeworkAdapter.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/homework/HomeworkAdapter.java @@ -50,17 +50,17 @@ public class HomeworkAdapter extends RecyclerView.Adapter { App.db.metadataDao().setSeen(App.Companion.getProfileId(), homework, true); }); @@ -69,11 +69,11 @@ public class HomeworkAdapter extends RecyclerView.Adapter { new EventManualDialog( (MainActivity) context, - homework.profileId, + homework.getProfileId(), null, null, null, @@ -83,11 +83,11 @@ public class HomeworkAdapter extends RecyclerView.Adapter if (lesson.type == TYPE_NO_LESSONS) @@ -345,9 +345,9 @@ class WidgetTimetableProvider : AppWidgetProvider() { // add every event on this lesson for (event in events) { - if (event.startTime == null || event.startTime != lesson.displayStartTime) + if (event.time == null || event.time != lesson.displayStartTime) continue - model.eventColors.add(if (event.type == TYPE_HOMEWORK) ItemWidgetTimetableModel.EVENT_COLOR_HOMEWORK else event.getColor()) + model.eventColors.add(if (event.type == TYPE_HOMEWORK) ItemWidgetTimetableModel.EVENT_COLOR_HOMEWORK else event.eventColor) } models += model diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/Date.java b/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/Date.java index 5f889bfb..e1216f3c 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/Date.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/Date.java @@ -254,6 +254,7 @@ public class Date implements Comparable { /** * @return 2019-06-02 */ + @NonNull public String getStringY_m_d() { return year + (month < 10 ? "-0" : "-") + month + (day < 10 ? "-0" : "-") + day; } diff --git a/app/src/main/res/layout/dialog_event_details.xml b/app/src/main/res/layout/dialog_event_details.xml index f23e9093..9c9bbe58 100644 --- a/app/src/main/res/layout/dialog_event_details.xml +++ b/app/src/main/res/layout/dialog_event_details.xml @@ -81,14 +81,14 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="@style/NavView.TextView.Helper" - android:text="@{event.startTime == null ? @string/event_all_day : event.startTime.stringHM}" + android:text="@{event.time == null ? @string/event_all_day : event.time.stringHM}" tools:text="14:50"/>