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
@@ -6,10 +6,8 @@ import android.os.Bundle
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import com.denizk0461.studip.R
import com.denizk0461.studip.data.StudIPParser
import com.denizk0461.studip.data.getThemedColor
import com.denizk0461.studip.databinding.ActivityFetcherBinding
import com.denizk0461.studip.viewmodel.FetcherViewModel
@@ -72,21 +70,25 @@ class FetcherActivity : Activity() {
) { p0 ->
try {
// Decode HTML and parse it into a list of StudIPEvent.kt
StudIPParser().parse(URLDecoder.decode(p0, "UTF-8")) { events ->
// Delete all previously fetched elements
viewModel.nukeEvents()
// Insert the list into the database
viewModel.insertEvents(events)
// Notify the user that the fetch was successful
Toast.makeText(
this, R.string.toast_fetch_finished, Toast.LENGTH_SHORT
).show()
// Close the activity
finish()
}
// TODO this exception handling and finish() is broken
viewModel.parse(URLDecoder.decode(p0, "UTF-8"))
// StudIPParser(application).parse(URLDecoder.decode(p0, "UTF-8")) { events ->
//
// // Delete all previously fetched elements
//// viewModel.nukeEvents()
//// viewModel.replaceEvents(events)
//
// // Insert the list into the database
//// viewModel.insertEvents(events)
//
// // Notify the user that the fetch was successful
// Toast.makeText(
// this, R.string.toast_fetch_finished, Toast.LENGTH_SHORT
// ).show()
//
// // Close the activity
// finish()
// }
} catch (e: IOException) {
// Let the user know that an error occurred
@@ -1,12 +1,11 @@
package com.denizk0461.studip.activity
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.WindowCompat
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.Lifecycle
import com.denizk0461.studip.R
import com.denizk0461.studip.data.Dependencies
import com.denizk0461.studip.databinding.ActivityMainBinding
import com.denizk0461.studip.db.AppRepository
import com.denizk0461.studip.fragment.EventFragment
@@ -17,7 +16,7 @@ import com.denizk0461.studip.model.SettingsPreferences
/**
* Main activity that handles all common fragments. This is opened on app launch.
*/
class MainActivity : AppCompatActivity() {
class MainActivity : FragmentActivity() {
// View binding
private lateinit var binding: ActivityMainBinding
@@ -36,7 +35,7 @@ class MainActivity : AppCompatActivity() {
* Instantiate repository object that is accessed by the fragments' view models to retrieve
* data.
*/
Dependencies.repo = AppRepository(application)
// Dependencies.repo = AppRepository(application)
// Inflate view binding and bind to this activity
binding = ActivityMainBinding.inflate(layoutInflater)
@@ -44,7 +43,8 @@ class MainActivity : AppCompatActivity() {
// Open the fragment that the user specified to open on app start
if (
Dependencies.repo.getBooleanPreference(SettingsPreferences.LAUNCH_CANTEEN_ON_START)
AppRepository.getRepositoryInstance(application)
.getBooleanPreference(SettingsPreferences.LAUNCH_CANTEEN_ON_START)
) {
// Open canteen fragment
binding.contentMain.navView.selectedItemId = R.id.food