mirror of
https://github.com/szkolny-eu/szkolny-android.git
synced 2025-01-18 21:06:44 -06:00
[Refactor] Refactor EventDao class.
This commit is contained in:
parent
ef4527f140
commit
d4d548846f
@ -58,7 +58,7 @@ class Notifications(val app: App, val notifications: MutableList<Notification>,
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun eventNotifications() {
|
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)
|
val text = if (event.type == Event.TYPE_HOMEWORK)
|
||||||
app.getString(
|
app.getString(
|
||||||
if (event.subjectLongName.isNullOrEmpty())
|
if (event.subjectLongName.isNullOrEmpty())
|
||||||
@ -93,7 +93,7 @@ class Notifications(val app: App, val notifications: MutableList<Notification>,
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun sharedEventNotifications() {
|
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(
|
val text = app.getString(
|
||||||
R.string.notification_shared_event_format,
|
R.string.notification_shared_event_format,
|
||||||
event.sharedByName,
|
event.sharedByName,
|
||||||
|
@ -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<T, F> {
|
||||||
|
@RawQuery
|
||||||
|
fun getRaw(query: SupportSQLiteQuery): LiveData<List<F>>
|
||||||
|
fun getRaw(query: String) = getRaw(SimpleSQLiteQuery(query))
|
||||||
|
@RawQuery
|
||||||
|
fun getRawNow(query: SupportSQLiteQuery): List<F>
|
||||||
|
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<T>): LongArray
|
||||||
|
|
||||||
|
fun clear(profileId: Int)
|
||||||
|
}
|
@ -4,7 +4,10 @@
|
|||||||
package pl.szczodrzynski.edziennik.data.db.dao
|
package pl.szczodrzynski.edziennik.data.db.dao
|
||||||
|
|
||||||
import androidx.lifecycle.LiveData
|
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.SimpleSQLiteQuery
|
||||||
import androidx.sqlite.db.SupportSQLiteQuery
|
import androidx.sqlite.db.SupportSQLiteQuery
|
||||||
import pl.szczodrzynski.edziennik.data.db.entity.Event
|
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
|
import pl.szczodrzynski.edziennik.utils.models.Time
|
||||||
|
|
||||||
@Dao
|
@Dao
|
||||||
abstract class EventDao {
|
abstract class EventDao : BaseDao<Event, EventFull> {
|
||||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
companion object {
|
||||||
abstract fun add(event: Event): Long
|
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)
|
private const val ORDER_BY = """GROUP BY eventId ORDER BY addedDate ASC"""
|
||||||
abstract fun addAll(eventList: List<Event>): LongArray
|
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")
|
@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")
|
@Query("DELETE FROM events WHERE profileId = :profileId AND eventId = :id")
|
||||||
abstract fun remove(profileId: Int, id: Long)
|
abstract fun remove(profileId: Int, id: Long)
|
||||||
@ -46,84 +71,39 @@ abstract class EventDao {
|
|||||||
remove(profileId, event.type, event.id)
|
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])
|
@RawQuery(observedEntities = [Event::class])
|
||||||
abstract fun getAll(query: SupportSQLiteQuery): LiveData<List<EventFull>>
|
abstract override fun getRaw(query: SupportSQLiteQuery): LiveData<List<EventFull>>
|
||||||
|
|
||||||
fun getAll(profileId: Int, filter: String, limit: String): LiveData<List<EventFull>> {
|
|
||||||
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<List<EventFull>> {
|
|
||||||
return getAll(profileId, "1", "")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getAllNow(profileId: Int): List<EventFull> {
|
fun getAll(profileId: Int) =
|
||||||
return getAllNow(profileId, "1")
|
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<List<EventFull>> {
|
|
||||||
return getAll(profileId, filter, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getAllByType(profileId: Int, type: Long, filter: String): LiveData<List<EventFull>> {
|
|
||||||
return getAll(profileId, "eventType = $type AND $filter", "")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getAllByDate(profileId: Int, date: Date): LiveData<List<EventFull>> {
|
fun getAllNow(profileId: Int) =
|
||||||
return getAll(profileId, "eventDate = '" + date.stringY_m_d + "'", "")
|
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<EventFull> {
|
|
||||||
return getAllNow(profileId, "eventDate = '" + date.stringY_m_d + "'")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getAllByDateTime(profileId: Int, date: Date, time: Time?): LiveData<List<EventFull>> {
|
|
||||||
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<List<EventFull>> {
|
fun getByIdNow(profileId: Int, id: Long) =
|
||||||
return getAll(profileId, "eventDate >= '" + today.stringY_m_d + "'", "LIMIT $limit")
|
getOneNow("$QUERY WHERE events.profileId = $profileId AND eventId = $id")
|
||||||
}
|
|
||||||
|
|
||||||
@RawQuery
|
|
||||||
abstract fun getAllNow(query: SupportSQLiteQuery): List<EventFull>
|
|
||||||
|
|
||||||
fun getAllNow(profileId: Int, filter: String): List<EventFull> {
|
|
||||||
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<EventFull> {
|
|
||||||
return getAllNow(profileId, "notified = 0")
|
|
||||||
}
|
|
||||||
|
|
||||||
@Query("SELECT eventId FROM events WHERE profileId = :profileId AND eventBlacklisted = 1")
|
@Query("SELECT eventId FROM events WHERE profileId = :profileId AND eventBlacklisted = 1")
|
||||||
abstract fun getBlacklistedIds(profileId: Int): List<Long>
|
abstract fun getBlacklistedIds(profileId: Int): List<Long>
|
||||||
@ -131,27 +111,12 @@ abstract class EventDao {
|
|||||||
@get:Query("SELECT eventId FROM events WHERE eventBlacklisted = 1")
|
@get:Query("SELECT eventId FROM events WHERE eventBlacklisted = 1")
|
||||||
abstract val blacklistedIds: List<Long>
|
abstract val blacklistedIds: List<Long>
|
||||||
|
|
||||||
@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<EventFull>
|
|
||||||
|
|
||||||
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")
|
@Query("UPDATE events SET eventAddedManually = 1 WHERE profileId = :profileId AND eventDate < :date")
|
||||||
abstract fun convertOlderToManual(profileId: Int, date: 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")
|
@Query("DELETE FROM events WHERE profileId = :profileId AND eventAddedManually = 0")
|
||||||
abstract fun removeNotManual(profileId: Int)
|
abstract fun removeNotManual(profileId: Int)
|
||||||
|
|
||||||
@ -181,4 +146,5 @@ abstract class EventDao {
|
|||||||
|
|
||||||
@Query("UPDATE events SET eventBlacklisted = :blacklisted WHERE profileId = :profileId AND eventId = :eventId")
|
@Query("UPDATE events SET eventBlacklisted = :blacklisted WHERE profileId = :profileId AND eventId = :eventId")
|
||||||
abstract fun setBlacklisted(profileId: Int, eventId: Long, blacklisted: Boolean)
|
abstract fun setBlacklisted(profileId: Int, eventId: Long, blacklisted: Boolean)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user