Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strategy 패턴 적용 #118

Merged
merged 12 commits into from
Dec 9, 2023
2 changes: 1 addition & 1 deletion frontend/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ android {

defaultConfig {
applicationId = "com.example.speechbuddy"
minSdk = 24
minSdk = 26
targetSdk = 34
versionCode = 1
versionName = "1.0"
Expand Down
93 changes: 46 additions & 47 deletions frontend/app/src/main/java/com/example/speechbuddy/AuthActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@ import android.os.Bundle
import android.view.MotionEvent
import android.view.inputmethod.InputMethodManager
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.annotation.RequiresApi
import androidx.lifecycle.lifecycleScope
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import com.example.speechbuddy.compose.SpeechBuddyAuth
import com.example.speechbuddy.ui.SpeechBuddyTheme
import com.example.speechbuddy.utils.Constants.Companion.GUEST_ID
import com.example.speechbuddy.utils.ResponseCode
import com.example.speechbuddy.viewmodel.LoginViewModel
import com.example.speechbuddy.worker.SeedDatabaseWorker
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import java.time.LocalDate

@AndroidEntryPoint
class AuthActivity : BaseActivity() {

private val loginViewModel: LoginViewModel by viewModels()

@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {

Expand All @@ -40,26 +43,12 @@ class AuthActivity : BaseActivity() {
}
}

@RequiresApi(Build.VERSION_CODES.O)
private fun subscribeObservers() {
sessionManager.isAuthorized.observe(this) { isAuthorized ->
if (isAuthorized &&
sessionManager.userId.value != GUEST &&
sessionManager.isLogin.value != true &&
getAutoBackup() &&
getLastBackupDate() != LocalDate.now().toString()
) {
autoBackup()
} else if (isAuthorized){
navHomeActivity()
}
if (isAuthorized) navHomeActivity()
}
}

companion object {
const val GUEST = -1
}

@RequiresApi(Build.VERSION_CODES.O)
private fun autoBackup() {
setContent {
Expand All @@ -83,34 +72,40 @@ class AuthActivity : BaseActivity() {
var darkMode = false
lifecycleScope.launch {
settingsRepository.getDarkMode().collect {
darkMode = it.data?: false
darkMode = it.data ?: false
}
}
return darkMode
}

private fun getAutoBackup(): Boolean {
var autoBackup = true
lifecycleScope.launch {
settingsRepository.getAutoBackup().collect {
autoBackup = it.data?: true
}
}
return autoBackup
@RequiresApi(Build.VERSION_CODES.O)
private suspend fun isBackupNecessary(): Boolean {
val autoBackup = settingsRepository.getAutoBackup().first().data
if (autoBackup == null || autoBackup == false) return false
val lastBackupDate = settingsRepository.getLastBackupDate().first().data
if (lastBackupDate == null || lastBackupDate == LocalDate.now().toString()) return false
return true
}

private fun getLastBackupDate(): String {
var lastBackupDate = ""
@RequiresApi(Build.VERSION_CODES.O)
private fun checkPreviousAuthUser() {
lifecycleScope.launch {
settingsRepository.getLastBackupDate().collect {
lastBackupDate = it.data?: ""
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<SeedDatabaseWorker>().build()
WorkManager.getInstance(applicationContext).enqueue(request)
}
}
}
return lastBackupDate
}

private fun checkPreviousAuthUser() {
loginViewModel.checkPreviousUser()
}

// hides keyboard
Expand All @@ -120,18 +115,20 @@ class AuthActivity : BaseActivity() {
if (v != null) {
v.clearFocus()
val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(v.getWindowToken(), 0)
imm.hideSoftInputFromWindow(v.windowToken, 0)
}
}
return super.dispatchTouchEvent(event)
}

@RequiresApi(Build.VERSION_CODES.O)
private fun displayBackup() {
lifecycleScope.launch {
CoroutineScope(Dispatchers.IO).launch {
settingsRepository.displayBackup().collect { result ->
when (result.code()) {
ResponseCode.SUCCESS.value -> { symbolListBackup() }
ResponseCode.SUCCESS.value -> {
symbolListBackup()
}

ResponseCode.NO_INTERNET_CONNECTION.value -> {
sessionManager.setIsLogin(false)
Expand All @@ -144,28 +141,30 @@ class AuthActivity : BaseActivity() {

@RequiresApi(Build.VERSION_CODES.O)
private fun symbolListBackup() {
lifecycleScope.launch {
CoroutineScope(Dispatchers.IO).launch {
settingsRepository.symbolListBackup().collect { result ->
when (result.code()) {
ResponseCode.SUCCESS.value -> { favoriteSymbolBackup() }
ResponseCode.SUCCESS.value -> {
favoriteSymbolBackup()
}

ResponseCode.NO_INTERNET_CONNECTION.value -> {
sessionManager.setIsLogin(false)
navHomeActivity()
}
}

}
}

}

@RequiresApi(Build.VERSION_CODES.O)
private fun favoriteSymbolBackup() {
lifecycleScope.launch {
CoroutineScope(Dispatchers.IO).launch {
settingsRepository.favoriteSymbolBackup().collect { result ->
when (result.code()) {
ResponseCode.SUCCESS.value -> { weightTableBackup() }
ResponseCode.SUCCESS.value -> {
weightTableBackup()
}

ResponseCode.NO_INTERNET_CONNECTION.value -> {
sessionManager.setIsLogin(false)
Expand All @@ -178,7 +177,7 @@ class AuthActivity : BaseActivity() {

@RequiresApi(Build.VERSION_CODES.O)
private fun weightTableBackup() {
lifecycleScope.launch {
CoroutineScope(Dispatchers.IO).launch {
settingsRepository.weightTableBackup().collect { result ->
when (result.code()) {
ResponseCode.SUCCESS.value -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package com.example.speechbuddy

import androidx.appcompat.app.AppCompatActivity
import com.example.speechbuddy.domain.SessionManager
import com.example.speechbuddy.repository.AuthRepository
import com.example.speechbuddy.repository.SettingsRepository
import com.example.speechbuddy.repository.UserRepository
import javax.inject.Inject

abstract class BaseActivity : AppCompatActivity() {
Expand All @@ -12,9 +12,9 @@ abstract class BaseActivity : AppCompatActivity() {
lateinit var sessionManager: SessionManager

@Inject
lateinit var settingsRepository: SettingsRepository
lateinit var authRepository: AuthRepository

@Inject
lateinit var userRepository: UserRepository
lateinit var settingsRepository: SettingsRepository

}
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
package com.example.speechbuddy

import android.app.Application
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import com.example.speechbuddy.worker.SeedDatabaseWorker
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class BaseApplication : Application(){

override fun onCreate() {
super.onCreate()

// create DB at the start of the app
val request = OneTimeWorkRequestBuilder<SeedDatabaseWorker>().build()
WorkManager.getInstance(this).enqueue(request)
}
}
class BaseApplication : Application()
28 changes: 6 additions & 22 deletions frontend/app/src/main/java/com/example/speechbuddy/HomeActivity.kt
Original file line number Diff line number Diff line change
@@ -1,38 +1,24 @@
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
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
import com.example.speechbuddy.utils.Constants.Companion.GUEST_ID

@AndroidEntryPoint
class HomeActivity : BaseActivity() {

@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
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)
}

subscribeObservers()

setContent {
SpeechBuddyTheme(
settingsRepository = settingsRepository,
Expand All @@ -41,13 +27,11 @@ class HomeActivity : BaseActivity() {
SpeechBuddyHome(getInitialPage())
}
}

subscribeObservers()
}

private fun subscribeObservers() {
sessionManager.isAuthorized.observe(this) { isAuthorized ->
if (!isAuthorized && !intent.getBooleanExtra("isTest", false)) navAuthActivity()
if (!isAuthorized) navAuthActivity()
}
}

Expand All @@ -61,7 +45,7 @@ class HomeActivity : BaseActivity() {
var initialPage = true
lifecycleScope.launch {
settingsRepository.getInitialPage().collect {
initialPage = it.data?: true
initialPage = it.data ?: true
}
}
return initialPage
Expand All @@ -71,7 +55,7 @@ class HomeActivity : BaseActivity() {
var darkMode = false
lifecycleScope.launch {
settingsRepository.getDarkMode().collect {
darkMode = it.data?: false
darkMode = it.data ?: false
}
}
return darkMode
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.example.speechbuddy.compose

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
Expand Down Expand Up @@ -54,7 +52,6 @@ data class BottomNavItem(
val iconResId: Int
)

@RequiresApi(Build.VERSION_CODES.O)
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SpeechBuddyHome(
Expand Down Expand Up @@ -224,7 +221,6 @@ private fun BottomNavigationBar(
}
}

@RequiresApi(Build.VERSION_CODES.O)
@Composable
private fun SpeechBuddyHomeNavHost(
navController: NavHostController,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.example.speechbuddy.compose.settings

import android.os.Build
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
Expand All @@ -29,7 +27,6 @@ import com.example.speechbuddy.compose.utils.TitleUi
import com.example.speechbuddy.ui.models.AccountSettingsAlert
import com.example.speechbuddy.viewmodel.AccountSettingsViewModel

@RequiresApi(Build.VERSION_CODES.O)
@Composable
fun AccountSettings(
modifier: Modifier = Modifier,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.example.speechbuddy.compose.settings

import android.os.Build
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
Expand Down Expand Up @@ -30,7 +28,6 @@ import com.example.speechbuddy.compose.utils.TitleUi
import com.example.speechbuddy.ui.models.BackupSettingsAlert
import com.example.speechbuddy.viewmodel.BackupSettingsViewModel

@RequiresApi(Build.VERSION_CODES.O)
@Composable
fun BackupSettings(
modifier: Modifier = Modifier,
Expand Down Expand Up @@ -71,7 +68,9 @@ fun BackupSettings(
Switch(
checked = uiState.isAutoBackupEnabled,
onCheckedChange = { viewModel.setAutoBackup(it) },
modifier = Modifier.heightIn(max = 32.dp).testTag("auto_backup"),
modifier = Modifier
.heightIn(max = 32.dp)
.testTag("auto_backup"),
enabled = uiState.buttonEnabled
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import androidx.compose.ui.unit.dp
import com.example.speechbuddy.R
import com.example.speechbuddy.compose.utils.TitleUi


@Composable
fun Copyright(
modifier: Modifier = Modifier,
Expand Down
Loading