Skip to content

Commit

Permalink
Merge pull request #86 from snuhcs-course/feat/reset-weight-table
Browse files Browse the repository at this point in the history
Feat/Initialize weight table after logout/exit guest/delete account
  • Loading branch information
89645321 authored Dec 1, 2023
2 parents a4f6246 + 08907ca commit 4fba5aa
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import android.view.inputmethod.InputMethodManager
import androidx.activity.compose.setContent
import androidx.annotation.RequiresApi
import androidx.lifecycle.lifecycleScope
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import com.example.speechbuddy.compose.SpeechBuddyHome
import com.example.speechbuddy.ui.SpeechBuddyTheme
import com.example.speechbuddy.worker.SeedDatabaseWorker
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

Expand All @@ -20,7 +23,10 @@ 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)
setContent {
SpeechBuddyTheme(
settingsRepository = settingsRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.google.gson.stream.JsonReader
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.withContext

class SeedDatabaseWorker(
Expand All @@ -24,27 +25,24 @@ class SeedDatabaseWorker(
override suspend fun doWork(): Result = withContext(Dispatchers.IO) {
try {
val database = AppDatabase.getInstance(applicationContext)

val weightRows = mutableListOf<WeightRowEntity>()

applicationContext.assets.open("weight_table.txt").use { inputStream ->
val inputList: MutableList<List<Int>> = ArrayList()
inputStream.bufferedReader().useLines { lines ->
lines.forEach { line ->
inputList.add(
line.split(",").mapNotNull { it.trim().toIntOrNull() })



if (!applicationContext.getDatabasePath("speechbuddy-db").exists()) {
createWeightTable(database)
}
else{ // check if the weight table is filled with 0
val currentDb = database.weightRowDao().getAllWeightRows().first()
var cnt = 1
for (currentWeightRowEntity in currentDb) {
if (currentWeightRowEntity.weights.all{it == 0}){
cnt+=1 // count the rows with 0 weights
}
}
var id = 1
for (weight in inputList) {
val weightRowEntity = WeightRowEntity(id++, weight)
weightRows.add(weightRowEntity)
if (cnt == currentDb.size){ // if weight table is filled with 0
createWeightTable(database)
}
}

database.weightRowDao().upsertAll(weightRows)

Result.success()


applicationContext.assets.open(SYMBOL_DATA_FILENAME).use { inputStream ->
Expand Down Expand Up @@ -76,4 +74,27 @@ class SeedDatabaseWorker(
}
}

private suspend fun createWeightTable(database: AppDatabase){
val weightRows = mutableListOf<WeightRowEntity>()

applicationContext.assets.open("weight_table.txt").use { inputStream ->
val inputList: MutableList<List<Int>> = ArrayList()
inputStream.bufferedReader().useLines { lines ->
lines.forEach { line ->
inputList.add(
line.split(",").mapNotNull { it.trim().toIntOrNull() })
}
}
var id = 1
for (weight in inputList) {
val weightRowEntity = WeightRowEntity(id++, weight)
weightRows.add(weightRowEntity)
}
}

database.weightRowDao().upsertAll(weightRows)

Result.success()
}

}

0 comments on commit 4fba5aa

Please sign in to comment.