Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
444 changes: 444 additions & 0 deletions app/schemas/org.phenoapps.intercross.data.IntercrossDatabase/5.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.phenoapps.intercross.data

import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import org.phenoapps.intercross.data.dao.EventsDao
import org.phenoapps.intercross.data.models.Event
Expand Down Expand Up @@ -34,19 +33,66 @@ class EventsRepository
}
}

fun deleteById(eid: Long) {
suspend fun deleteById(eid: Long) {

runBlocking {
withContext(IO) {

eventsDao.deleteById(eid)

}
}

suspend fun deleteByIds(eids: List<Long>) {

withContext(IO) {

eventsDao.deleteByIds(eids)

}
}

suspend fun archiveById(eid: Long) {

withContext(IO) {

eventsDao.archiveEvent(eid)

}
}

suspend fun archiveByIds(eids: List<Long>) {

withContext(IO) {

eventsDao.archiveEvents(eids)

}
}

suspend fun unarchiveById(eid: Long) {

withContext(IO) {

eventsDao.unarchiveEvent(eid)

}
}

suspend fun unarchiveByIds(eids: List<Long>) {

withContext(IO) {

eventsDao.unarchiveEvents(eids)

}
}

fun insert(event: Event): Long = eventsDao.insertEvent(event)

fun loadCrosses() = eventsDao.selectAllLive()

fun selectArchivedEvents() = eventsDao.selectArchivedEvents()

companion object {
@Volatile private var instance: EventsRepository? = null

Expand All @@ -56,4 +102,4 @@ class EventsRepository
.also { instance = it }
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import org.phenoapps.intercross.data.dao.*
import org.phenoapps.intercross.data.migrations.MigrationV2MetaData
import org.phenoapps.intercross.data.migrations.MigrationV3WishlistView
import org.phenoapps.intercross.data.migrations.MigrationV4UniqueWishType
import org.phenoapps.intercross.data.migrations.MigrationV5ArchivedCrosses
import org.phenoapps.intercross.data.models.*
import kotlin.jvm.java

@Database(entities = [Event::class, Parent::class,
Wishlist::class, Settings::class, PollenGroup::class,
Meta::class, MetadataValues::class],
views = [WishlistView::class], version = 3, exportSchema = true)
views = [WishlistView::class], version = 5, exportSchema = true)
@TypeConverters(Converters::class)
abstract class IntercrossDatabase : RoomDatabase() {

Expand Down Expand Up @@ -47,8 +48,9 @@ abstract class IntercrossDatabase : RoomDatabase() {
.addMigrations(MigrationV2MetaData()) //v1 -> v2 migration added JSON based metadata
.addMigrations(MigrationV3WishlistView()) // v2 -> v3 migration for WishlistView
.addMigrations(MigrationV4UniqueWishType()) // v3 -> v4 migration for unique wishlist type
.addMigrations(MigrationV5ArchivedCrosses()) // v4 -> v5 added isArchived column to events table
.setJournalMode(JournalMode.TRUNCATE) //truncate mode makes it easier to export/import database w/o having to manage WAL files.
.build()
}
}
}
}
29 changes: 25 additions & 4 deletions app/src/main/java/org/phenoapps/intercross/data/dao/EventsDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface EventsDao : BaseDao<Event> {
@Query("SELECT * FROM events WHERE events.eid == :eid")
suspend fun getEvent(eid: Long?): Event

@Query("SELECT * FROM events ORDER BY date DESC")
@Query("SELECT * FROM events WHERE isArchived = 0 ORDER BY date DESC")
fun selectAll(): LiveData<List<Event>>

@Query("""
Expand All @@ -30,7 +30,7 @@ interface EventsDao : BaseDao<Event> {
FROM events as y
WHERE y.mom = x.mom and y.dad = x.dad) as count
FROM events as x, parents as male, parents as female
WHERE x.dad = male.codeId and x.mom = female.codeId
WHERE x.dad = male.codeId and x.mom = female.codeId AND x.isArchived = 0
GROUP BY x.mom, "momReadable", x.dad, "dadReadable"
""")
fun getParentCount(): LiveData<List<ParentCount>>
Expand All @@ -40,7 +40,7 @@ interface EventsDao : BaseDao<Event> {
x.person as "person", x.date as "date",
COUNT(*) as count
FROM events as x, parents as male, parents as female
WHERE x.dad = male.codeId and x.mom = female.codeId
WHERE x.dad = male.codeId and x.mom = female.codeId AND x.isArchived = 0
GROUP BY x.mom, "momReadable", x.dad, "dadReadable", x.person, x.date
""")
fun getAllParents(): LiveData<List<ParentCount>>
Expand Down Expand Up @@ -79,6 +79,9 @@ interface EventsDao : BaseDao<Event> {
@Query("DELETE FROM events WHERE events.eid = :eid")
suspend fun deleteById(eid: Long)

@Query("DELETE FROM events WHERE events.eid IN (:eids)")
suspend fun deleteByIds(eids: List<Long>)

@Query("SELECT DISTINCT x.codeId FROM events as x WHERE x.codeId = :code")
fun getEventsWithCode(code: String): List<String>

Expand All @@ -103,4 +106,22 @@ interface EventsDao : BaseDao<Event> {

@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insertEvent(event: Event): Long
}

@Query("UPDATE events SET isArchived = 1 WHERE eid = :eid")
suspend fun archiveEvent(eid: Long)

@Query("UPDATE events SET isArchived = 1 WHERE eid IN (:eids)")
suspend fun archiveEvents(eids: List<Long>)

@Query("UPDATE events SET isArchived = 0 WHERE eid = :eid")
suspend fun unarchiveEvent(eid: Long)

@Query("UPDATE events SET isArchived = 0 WHERE eid IN (:eids)")
suspend fun unarchiveEvents(eids: List<Long>)

@Query("SELECT * FROM events WHERE isArchived = 0 ORDER BY date DESC")
fun selectActiveEvents(): LiveData<List<Event>>

@Query("SELECT * FROM events WHERE isArchived = 1 ORDER BY date DESC")
fun selectArchivedEvents(): LiveData<List<Event>>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.phenoapps.intercross.data.migrations

import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase

/**
* Room database migration class for going from version 4 to 5
* Version 5 adds isArchived column to events table
*/
class MigrationV5ArchivedCrosses : Migration(4, 5) {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL("ALTER TABLE events ADD COLUMN isArchived INTEGER NOT NULL DEFAULT 0")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ data class Event(

var sex: Int = -1, //by default sex is unknown

var isArchived: Boolean = false,

@ColumnInfo(name = "eid")
@PrimaryKey(autoGenerate = true)
var id: Long? = null): BaseTable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,61 @@ class EventListViewModel(private val eventRepo: EventsRepository): BaseViewModel

fun deleteById(eid: Long) {

eventRepo.deleteById(eid)
viewModelScope.launch {

eventRepo.deleteById(eid)

}

}

fun deleteByIds(eids: List<Long>) {

viewModelScope.launch {

eventRepo.deleteByIds(eids)

}

}

fun archiveById(eid: Long) {

viewModelScope.launch {

eventRepo.archiveById(eid)

}

}

fun archiveByIds(eids: List<Long>) {

viewModelScope.launch {

eventRepo.archiveByIds(eids)

}

}

fun unarchiveById(eid: Long) {

viewModelScope.launch {

eventRepo.unarchiveById(eid)

}

}

fun unarchiveByIds(eids: List<Long>) {

viewModelScope.launch {

eventRepo.unarchiveByIds(eids)

}

}

Expand All @@ -38,5 +92,7 @@ class EventListViewModel(private val eventRepo: EventsRepository): BaseViewModel

val events = eventRepo.selectAll()

val archivedEvents = eventRepo.selectArchivedEvents()

val metadata = eventRepo.getMetadata()
}
Loading