Skip to content

Commit

Permalink
Merge pull request #99 from snuhcs-course/fix/weight-table-update-logic
Browse files Browse the repository at this point in the history
Fix: Weight table update logic
  • Loading branch information
JH747 authored Dec 4, 2023
2 parents 001de62 + 054bfcd commit 453fdf6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
15 changes: 11 additions & 4 deletions frontend/app/src/main/java/com/example/speechbuddy/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.example.speechbuddy
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.MotionEvent
import android.view.inputmethod.InputMethodManager
import androidx.activity.compose.setContent
Expand All @@ -15,6 +16,7 @@ import com.example.speechbuddy.ui.SpeechBuddyTheme
import com.example.speechbuddy.worker.SeedDatabaseWorker
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import com.example.speechbuddy.utils.Constants.Companion.GUEST_ID

@AndroidEntryPoint
class HomeActivity : BaseActivity() {
Expand All @@ -23,10 +25,15 @@ class HomeActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)
// force the database worker to build a new db
// in order to check if the weight-db is empty or not and fill it
val request = OneTimeWorkRequestBuilder<SeedDatabaseWorker>().build()
WorkManager.getInstance(this).enqueue(request)
if(sessionManager.userId.value==GUEST_ID) {
// only activates if it is in guest mode.
// does not activate when logged in since db is overwritten on login
// force the database worker to build a new db
// in order to check if the weight-db is empty or not and fill it
Log.d("test", "home acrivity starting initialization of db")
val request = OneTimeWorkRequestBuilder<SeedDatabaseWorker>().build()
WorkManager.getInstance(this).enqueue(request)
}
setContent {
SpeechBuddyTheme(
settingsRepository = settingsRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ class WeightTableRepository @Inject constructor(
}
}

suspend fun replaceWeightTable(weightRowList: List<WeightRow>) {

val weightRowEntityList = mutableListOf<WeightRowEntity>()
for (weightRow in weightRowList) {
weightRowEntityList.add(weightRowMapper.mapFromDomainModel(weightRow))
fun replaceWeightTable(weightRowList: List<WeightRow>) {
CoroutineScope(Dispatchers.IO).launch {
val weightRowEntityList = mutableListOf<WeightRowEntity>()
for (weightRow in weightRowList) {
weightRowEntityList.add(weightRowMapper.mapFromDomainModel(weightRow))
}
weightRowDao.upsertAll(weightRowEntityList)
}
weightRowDao.upsertAll(weightRowEntityList)

}

suspend fun getBackupWeightTableRequest(): BackupWeightTableRequest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class MySymbolSettingsViewModel @Inject internal constructor(
private var getSymbolsJob: Job? = null

init {
symbolRepository.checkImages()
getSymbols()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,25 @@ class SeedDatabaseWorker(
val database = AppDatabase.getInstance(applicationContext)



if (!applicationContext.getDatabasePath("speechbuddy-db").exists()) {
Log.d("create-db", "no db->initializing db")
createWeightTable(database)
}
else{ // check if the weight table is filled with 0
val currentDb = database.weightRowDao().getAllWeightRows().first()
var cnt = 1
var cnt = 0
for (currentWeightRowEntity in currentDb) {
if (currentWeightRowEntity.weights.all{it == 0}){
cnt+=1 // count the rows with 0 weights
}
}
if (cnt == currentDb.size){ // if weight table is filled with 0
Log.d("create-db", "empty db->initializing db")
createWeightTable(database)
}
else{
Log.d("create-db", "db exists")
}
}


Expand Down

0 comments on commit 453fdf6

Please sign in to comment.