Possibly fixed-for-good a bug that caused events to be fetched but not stored properly in the database; I believe this was caused by FetcherActivity.kt sequentially executing nukeEvents() and insertEvents(), but doing both asynchronously, and individually, which could mean that insertEvents() finishes before nukeEvents() and therefore nukeEvents() would delete the old events as well as the new ones. This doesn't seem entirely logical to me, as no ConflictStrategy has been configured, and inserting new events before the old ones have been cleared (since they both start their primary keys at 0) would have crashed the app.

This commit is contained in:
denizk0461
2023-04-28 15:14:50 +02:00
parent aa5d32e64a
commit 509a0434c5
15 changed files with 82 additions and 69 deletions
@@ -3,7 +3,6 @@ package com.denizk0461.studip.viewmodel
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.denizk0461.studip.data.Dependencies
import com.denizk0461.studip.db.AppRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@@ -21,7 +20,7 @@ open class AppViewModel(app: Application) : AndroidViewModel(app) {
/**
* Reference to the app's repository for database transactions.
*/
protected val repo: AppRepository = Dependencies.repo
protected val repo: AppRepository = AppRepository.getRepositoryInstance(app)
/**
* Execute a function asynchronously on the I/O thread. Be careful not to execute UI commands
@@ -13,7 +13,7 @@ import com.denizk0461.studip.model.*
class CanteenViewModel(app: Application) : AppViewModel(app) {
// Instantiate a parser, should the user want to refresh the canteen offers
private val parser = StwParser()
private val parser = StwParser(app)
/**
* Retrieves all Stud.IP events.
@@ -1,7 +1,7 @@
package com.denizk0461.studip.viewmodel
import android.app.Application
import com.denizk0461.studip.model.StudIPEvent
import com.denizk0461.studip.data.StudIPParser
/**
* View model for [com.denizk0461.studip.activity.FetcherActivity]
@@ -10,15 +10,7 @@ import com.denizk0461.studip.model.StudIPEvent
*/
class FetcherViewModel(app: Application) : AppViewModel(app) {
/**
* Save a list of Stud.IP events to persistent storage asynchronously.
*
* @param events list of events to be saved
*/
fun insertEvents(events: List<StudIPEvent>) { doAsync { repo.insertEvents(events) }}
private val parser: StudIPParser = StudIPParser(app)
/**
* Delete all Stud.IP events from the database asynchronously.
*/
fun nukeEvents() { doAsync { repo.nukeEvents() } }
fun parse(html: String) { doAsync { parser.parse(html, {}) }}
}