diff --git a/frontend/app/src/main/java/com/example/speechbuddy/AuthActivity.kt b/frontend/app/src/main/java/com/example/speechbuddy/AuthActivity.kt index 26270b29..5545d0da 100644 --- a/frontend/app/src/main/java/com/example/speechbuddy/AuthActivity.kt +++ b/frontend/app/src/main/java/com/example/speechbuddy/AuthActivity.kt @@ -1,12 +1,10 @@ package com.example.speechbuddy import android.content.Intent -import android.os.Build import android.os.Bundle import android.view.MotionEvent 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 @@ -25,7 +23,6 @@ import java.time.LocalDate @AndroidEntryPoint class AuthActivity : BaseActivity() { - @RequiresApi(Build.VERSION_CODES.O) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -49,7 +46,6 @@ class AuthActivity : BaseActivity() { } } - @RequiresApi(Build.VERSION_CODES.O) private fun autoBackup() { setContent { SpeechBuddyTheme( @@ -78,7 +74,6 @@ class AuthActivity : BaseActivity() { return darkMode } - @RequiresApi(Build.VERSION_CODES.O) private suspend fun isBackupNecessary(): Boolean { val autoBackup = settingsRepository.getAutoBackup().first().data if (autoBackup == null || autoBackup == false) return false @@ -87,23 +82,21 @@ class AuthActivity : BaseActivity() { return true } - @RequiresApi(Build.VERSION_CODES.O) private fun checkPreviousAuthUser() { lifecycleScope.launch { - authRepository.checkPreviousUser().collect { - if (it.data != null) { - val userId = it.data.first - val authToken = it.data.second - val setAuthTokenJob = sessionManager.setAuthToken(authToken) - setAuthTokenJob.join() - - if (userId != GUEST_ID && sessionManager.isLogin.value != true && isBackupNecessary()) - autoBackup() - sessionManager.setUserId(userId) - } else { - val request = OneTimeWorkRequestBuilder().build() - WorkManager.getInstance(applicationContext).enqueue(request) - } + val resource = authRepository.checkPreviousUser().first() + if (resource.data != null) { + val userId = resource.data.first + val authToken = resource.data.second + val setAuthTokenJob = sessionManager.setAuthToken(authToken) + setAuthTokenJob.join() + + if (userId != GUEST_ID && sessionManager.isLogin.value != true && isBackupNecessary()) + autoBackup() + sessionManager.setUserId(userId) + } else { + val request = OneTimeWorkRequestBuilder().build() + WorkManager.getInstance(applicationContext).enqueue(request) } } } @@ -121,7 +114,6 @@ class AuthActivity : BaseActivity() { return super.dispatchTouchEvent(event) } - @RequiresApi(Build.VERSION_CODES.O) private fun displayBackup() { CoroutineScope(Dispatchers.IO).launch { settingsRepository.displayBackup().collect { result -> @@ -139,7 +131,6 @@ class AuthActivity : BaseActivity() { } } - @RequiresApi(Build.VERSION_CODES.O) private fun symbolListBackup() { CoroutineScope(Dispatchers.IO).launch { settingsRepository.symbolListBackup().collect { result -> @@ -157,7 +148,6 @@ class AuthActivity : BaseActivity() { } } - @RequiresApi(Build.VERSION_CODES.O) private fun favoriteSymbolBackup() { CoroutineScope(Dispatchers.IO).launch { settingsRepository.favoriteSymbolBackup().collect { result -> @@ -175,7 +165,6 @@ class AuthActivity : BaseActivity() { } } - @RequiresApi(Build.VERSION_CODES.O) private fun weightTableBackup() { CoroutineScope(Dispatchers.IO).launch { settingsRepository.weightTableBackup().collect { result -> diff --git a/frontend/app/src/main/java/com/example/speechbuddy/compose/symbolselection/SymbolSelectionScreen.kt b/frontend/app/src/main/java/com/example/speechbuddy/compose/symbolselection/SymbolSelectionScreen.kt index d747d18a..ac0c2b0a 100644 --- a/frontend/app/src/main/java/com/example/speechbuddy/compose/symbolselection/SymbolSelectionScreen.kt +++ b/frontend/app/src/main/java/com/example/speechbuddy/compose/symbolselection/SymbolSelectionScreen.kt @@ -103,7 +103,7 @@ fun SymbolSelectionScreen( symbol = entry, onSelect = { coroutineScope.launch { - val id = viewModel.selectSymbol(entry) + viewModel.selectSymbol(entry) lazyGridState.animateScrollToItem(0) } }, diff --git a/frontend/app/src/main/java/com/example/speechbuddy/data/remote/SettingsRemoteSource.kt b/frontend/app/src/main/java/com/example/speechbuddy/data/remote/SettingsRemoteSource.kt index 40034d23..95c74bf5 100644 --- a/frontend/app/src/main/java/com/example/speechbuddy/data/remote/SettingsRemoteSource.kt +++ b/frontend/app/src/main/java/com/example/speechbuddy/data/remote/SettingsRemoteSource.kt @@ -7,6 +7,7 @@ import com.example.speechbuddy.data.remote.requests.BackupWeightTableRequest import com.example.speechbuddy.service.BackupService import com.example.speechbuddy.utils.ResponseHandler import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.flow import retrofit2.Response import javax.inject.Inject @@ -17,41 +18,34 @@ class SettingsRemoteSource @Inject constructor( ) { suspend fun getDisplaySettings(authHeader: String): Flow> = flow { - try { - val result = backupService.getDisplaySettings(authHeader) - emit(result) - } catch (e: Exception) { - emit(responseHandler.getConnectionErrorResponse()) - } + val result = backupService.getDisplaySettings(authHeader) + emit(result) + }.catch { + emit(responseHandler.getConnectionErrorResponse()) } suspend fun getSymbolList(authHeader: String): Flow> = flow { - try { - val result = backupService.getSymbolList(authHeader) - emit(result) - } catch (e: Exception) { - emit(responseHandler.getConnectionErrorResponse()) - } + val result = backupService.getSymbolList(authHeader) + emit(result) + }.catch { + emit(responseHandler.getConnectionErrorResponse()) } suspend fun getFavoritesList(authHeader: String): Flow> = flow { - try { - val result = backupService.getFavoriteSymbolList(authHeader) - emit(result) - } catch (e: Exception) { - emit(responseHandler.getConnectionErrorResponse()) - } + val result = backupService.getFavoriteSymbolList(authHeader) + emit(result) + }.catch { + emit(responseHandler.getConnectionErrorResponse()) } + suspend fun getWeightTable(authHeader: String): Flow> = flow { - try { - val result = backupService.getWeightTable(authHeader) - emit(result) - } catch (e: Exception) { - emit(responseHandler.getConnectionErrorResponse()) - } + val result = backupService.getWeightTable(authHeader) + emit(result) + }.catch { + emit(responseHandler.getConnectionErrorResponse()) } } \ No newline at end of file diff --git a/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/BackupSettingsViewModel.kt b/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/BackupSettingsViewModel.kt index a17d45e0..578dc69a 100644 --- a/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/BackupSettingsViewModel.kt +++ b/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/BackupSettingsViewModel.kt @@ -98,7 +98,9 @@ class BackupSettingsViewModel @Inject internal constructor( ResponseCode.SUCCESS.value -> { symbolListBackup() } - + ResponseCode.BAD_REQUEST.value -> { + handleNoInternetConnection() + } ResponseCode.NO_INTERNET_CONNECTION.value -> { handleNoInternetConnection() } @@ -114,7 +116,9 @@ class BackupSettingsViewModel @Inject internal constructor( ResponseCode.SUCCESS.value -> { favoriteSymbolBackup() } - + ResponseCode.BAD_REQUEST.value -> { + handleNoInternetConnection() + } ResponseCode.NO_INTERNET_CONNECTION.value -> { handleNoInternetConnection() } @@ -132,7 +136,9 @@ class BackupSettingsViewModel @Inject internal constructor( ResponseCode.SUCCESS.value -> { weightTableBackup() } - + ResponseCode.BAD_REQUEST.value -> { + handleNoInternetConnection() + } ResponseCode.NO_INTERNET_CONNECTION.value -> { handleNoInternetConnection() } @@ -148,7 +154,9 @@ class BackupSettingsViewModel @Inject internal constructor( ResponseCode.SUCCESS.value -> { handleSuccess() } - + ResponseCode.BAD_REQUEST.value -> { + handleNoInternetConnection() + } ResponseCode.NO_INTERNET_CONNECTION.value -> { handleNoInternetConnection() } diff --git a/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt b/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt index ac2502ed..e221cfc1 100644 --- a/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt +++ b/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt @@ -94,9 +94,10 @@ class SymbolSelectionViewModel @Inject internal constructor( fun clearAll() { weightTableRepository.update(selectedSymbols) selectedSymbols = emptyList() + getEntries() } - fun selectSymbol(symbol: Symbol): Int { + fun selectSymbol(symbol: Symbol) { queryInput = "" val newSymbolItem = SymbolItem(id = selectedSymbols.size, symbol = symbol) @@ -109,8 +110,6 @@ class SymbolSelectionViewModel @Inject internal constructor( } provideSuggestion(symbol) - - return newSymbolItem.id } fun updateFavorite(symbol: Symbol, value: Boolean) { diff --git a/frontend/app/src/main/java/com/example/speechbuddy/worker/SeedDatabaseWorker.kt b/frontend/app/src/main/java/com/example/speechbuddy/worker/SeedDatabaseWorker.kt index d8a23093..7ba60779 100644 --- a/frontend/app/src/main/java/com/example/speechbuddy/worker/SeedDatabaseWorker.kt +++ b/frontend/app/src/main/java/com/example/speechbuddy/worker/SeedDatabaseWorker.kt @@ -14,7 +14,6 @@ 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( @@ -26,41 +25,26 @@ class SeedDatabaseWorker( try { val database = AppDatabase.getInstance(applicationContext) - val currentDb = database.weightRowDao().getAllWeightRows().first() - 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") - applicationContext.assets.open("weight_table.txt").use { inputStream -> - val weightRows = mutableListOf() + applicationContext.assets.open("weight_table.txt").use { inputStream -> + val weightRows = mutableListOf() - val inputList: MutableList> = 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) + val inputList: MutableList> = ArrayList() + inputStream.bufferedReader().useLines { lines -> + lines.forEach { line -> + inputList.add( + line.split(",").mapNotNull { it.trim().toIntOrNull() }) } - - database.weightRowDao().upsertAll(weightRows) - - Result.success() } - } - else{ - Log.d("create-db", "db exists") - } + var id = 1 + for (weight in inputList) { + val weightRowEntity = WeightRowEntity(id++, weight) + weightRows.add(weightRowEntity) + } + database.weightRowDao().upsertAll(weightRows) + Result.success() + } applicationContext.assets.open(SYMBOL_DATA_FILENAME).use { inputStream -> JsonReader(inputStream.reader()).use { jsonReader ->