From d4d548846f7fa9d145d4f3ea1d39fa696bb81d7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Sat, 28 Mar 2020 11:17:39 +0100 Subject: [PATCH] [Refactor] Refactor EventDao class. --- .../edziennik/data/api/task/Notifications.kt | 4 +- .../edziennik/data/db/dao/BaseDao.kt | 34 ++++ .../edziennik/data/db/dao/EventDao.kt | 148 +++++++----------- 3 files changed, 93 insertions(+), 93 deletions(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/BaseDao.kt 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 73756508..deeb5337 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.date >= today }) { + for (event in app.db.eventDao().getNotNotifiedNow().filter { it.date >= today }) { val text = if (event.type == Event.TYPE_HOMEWORK) app.getString( if (event.subjectLongName.isNullOrEmpty()) @@ -93,7 +93,7 @@ class Notifications(val app: App, val notifications: MutableList, } fun sharedEventNotifications() { - for (event in app.db.eventDao().notNotifiedNow.filter { it.date >= today && it.sharedBy != null }) { + for (event in app.db.eventDao().getNotNotifiedNow().filter { it.date >= today && it.sharedBy != null }) { val text = app.getString( R.string.notification_shared_event_format, event.sharedByName, diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/BaseDao.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/BaseDao.kt new file mode 100644 index 00000000..7c9583bc --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/BaseDao.kt @@ -0,0 +1,34 @@ +/* + * Copyright (c) Kuba SzczodrzyƄski 2020-3-28. + */ + +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.RawQuery +import androidx.sqlite.db.SimpleSQLiteQuery +import androidx.sqlite.db.SupportSQLiteQuery + +@Dao +interface BaseDao { + @RawQuery + fun getRaw(query: SupportSQLiteQuery): LiveData> + fun getRaw(query: String) = getRaw(SimpleSQLiteQuery(query)) + @RawQuery + fun getRawNow(query: SupportSQLiteQuery): List + fun getRawNow(query: String) = getRawNow(SimpleSQLiteQuery(query)) + @RawQuery + fun getOneNow(query: SupportSQLiteQuery): F? + fun getOneNow(query: String) = getOneNow(SimpleSQLiteQuery(query)) + + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun add(item: T): Long + + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun addAll(items: List): LongArray + + fun clear(profileId: Int) +} 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 index ad47e90c..e90353b5 100644 --- 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 @@ -4,7 +4,10 @@ package pl.szczodrzynski.edziennik.data.db.dao import androidx.lifecycle.LiveData -import androidx.room.* +import androidx.room.Dao +import androidx.room.Query +import androidx.room.RawQuery +import androidx.room.Transaction import androidx.sqlite.db.SimpleSQLiteQuery import androidx.sqlite.db.SupportSQLiteQuery import pl.szczodrzynski.edziennik.data.db.entity.Event @@ -14,15 +17,37 @@ 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 +abstract class EventDao : BaseDao { + companion object { + private const val QUERY = """ + SELECT + *, + teachers.teacherName ||" "|| teachers.teacherSurname AS teacherName, + eventTypes.eventTypeName AS typeName, + eventTypes.eventTypeColor AS typeColor + FROM events + LEFT JOIN subjects USING(profileId, subjectId) + LEFT JOIN teachers USING(profileId, teacherId) + LEFT JOIN teams USING(profileId, teamId) + 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 + """ - @Insert(onConflict = OnConflictStrategy.REPLACE) - abstract fun addAll(eventList: List): LongArray + private const val ORDER_BY = """GROUP BY eventId ORDER BY addedDate ASC""" + private const val NOT_BLACKLISTED = """events.eventBlacklisted = 0""" + } + + //abstract fun queryRaw(query: SupportSQLiteQuery) + //private fun queryRaw(query: String) = queryRaw(SimpleSQLiteQuery(query)) @Query("DELETE FROM events WHERE profileId = :profileId") - abstract fun clear(profileId: Int) + abstract override fun clear(profileId: Int) + + /*fun update(event: Event) = + queryRaw("""UPDATE events SET + eventDate = '${event.date.stringY_m_d}', + eventTime = ${event.time?.stringValue}, + eventTopic = '${event.topic}'""")*/ @Query("DELETE FROM events WHERE profileId = :profileId AND eventId = :id") abstract fun remove(profileId: Int, id: Long) @@ -46,84 +71,39 @@ abstract class EventDao { 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> + abstract override fun getRaw(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 getAll(profileId: Int) = + getRaw("$QUERY WHERE $NOT_BLACKLISTED AND events.profileId = $profileId $ORDER_BY") + fun getAllByType(profileId: Int, type: Long, filter: String = "1") = + getRaw("$QUERY WHERE $NOT_BLACKLISTED AND events.profileId = $profileId AND eventType = $type AND $filter $ORDER_BY") + fun getAllByDate(profileId: Int, date: Date) = + getRaw("$QUERY WHERE $NOT_BLACKLISTED AND events.profileId = $profileId AND eventDate = '${date.stringY_m_d}' $ORDER_BY") + fun getAllByDateTime(profileId: Int, date: Date, time: Time) = + getRaw("$QUERY WHERE $NOT_BLACKLISTED AND events.profileId = $profileId AND eventDate = '${date.stringY_m_d}' AND eventTime = ${time.stringValue}") + fun getAllNearest(profileId: Int, today: Date, limit: Int) = + getRaw("$QUERY WHERE $NOT_BLACKLISTED AND events.profileId = $profileId AND eventDate >= '${today.stringY_m_d}' $ORDER_BY LIMIT $limit") - 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 getAllNow(profileId: Int) = + getRawNow("$QUERY WHERE $NOT_BLACKLISTED AND events.profileId = $profileId $ORDER_BY") + fun getNotNotifiedNow() = + getRawNow("$QUERY WHERE $NOT_BLACKLISTED AND notified = 0 $ORDER_BY") + fun getNotNotifiedNow(profileId: Int) = + getRawNow("$QUERY WHERE $NOT_BLACKLISTED AND events.profileId = $profileId AND notified = 0 $ORDER_BY") + fun getAllByDateNow(profileId: Int, date: Date) = + getRawNow("$QUERY WHERE $NOT_BLACKLISTED AND events.profileId = $profileId AND eventDate = '${date.stringY_m_d}' $ORDER_BY") - 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") - } + fun getByIdNow(profileId: Int, id: Long) = + getOneNow("$QUERY WHERE events.profileId = $profileId AND eventId = $id") - @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 @@ -131,27 +111,12 @@ abstract class EventDao { @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 teamId = :teamId AND eventId = :id") + abstract fun removeByTeamId(teamId: Long, id: Long) + @Query("DELETE FROM events WHERE profileId = :profileId AND eventAddedManually = 0") abstract fun removeNotManual(profileId: Int) @@ -181,4 +146,5 @@ abstract class EventDao { @Query("UPDATE events SET eventBlacklisted = :blacklisted WHERE profileId = :profileId AND eventId = :eventId") abstract fun setBlacklisted(profileId: Int, eventId: Long, blacklisted: Boolean) + }