Skip to content

Commit

Permalink
Merge pull request #53 from snuhcs-course/feat/settings
Browse files Browse the repository at this point in the history
Feat/settings
  • Loading branch information
paul2126 authored Nov 23, 2023
2 parents bd8b588 + b0bc2c9 commit a155369
Show file tree
Hide file tree
Showing 33 changed files with 1,050 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package com.example.speechbuddy

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

abstract class BaseActivity : AppCompatActivity() {

@Inject
lateinit var sessionManager: SessionManager

@Inject
lateinit var settingsRepository: SettingsRepository

}
44 changes: 41 additions & 3 deletions frontend/app/src/main/java/com/example/speechbuddy/HomeActivity.kt
Original file line number Diff line number Diff line change
@@ -1,32 +1,55 @@
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.activity.viewModels
import androidx.annotation.RequiresApi
import androidx.core.view.WindowCompat
import androidx.lifecycle.Observer
import com.example.speechbuddy.compose.SpeechBuddyHome
import com.example.speechbuddy.ui.SpeechBuddyTheme
import com.example.speechbuddy.viewmodel.DisplaySettingsViewModel
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
class HomeActivity : BaseActivity() {

private val displaySettingsViewModel: DisplaySettingsViewModel by viewModels()

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

super.onCreate(savedInstanceState)
// Displaying edge-to-edge
WindowCompat.setDecorFitsSystemWindows(window, false)

val isBeingReloadedForDarkModeChange = intent.getBooleanExtra("isBeingReloadedForDarkModeChange", false)

setContent {
SpeechBuddyTheme {
SpeechBuddyHome()
SpeechBuddyTheme(
darkTheme = getDarkMode()
) {
SpeechBuddyHome(getInitialPage(), isBeingReloadedForDarkModeChange)
}
}

subscribeObservers()

val previousDarkMode = getDarkMode()

val darkModeObserver = Observer<Boolean?> { darkMode ->
if (darkMode != previousDarkMode) {
recreateHomeActivity()
}
}

settingsRepository.darkModeLiveData.observeForever(darkModeObserver)

}

private fun subscribeObservers() {
Expand All @@ -41,6 +64,21 @@ class HomeActivity : BaseActivity() {
finish()
}

private fun recreateHomeActivity() {
val intent = Intent(this, HomeActivity::class.java)
intent.putExtra("isBeingReloadedForDarkModeChange", true)
startActivity(intent)
finish()
}

private fun getDarkMode(): Boolean {
return displaySettingsViewModel.getDarkMode()
}

private fun getInitialPage(): Boolean {
return displaySettingsViewModel.getInitialPage()
}

// hides keyboard
override fun dispatchTouchEvent(event: MotionEvent): Boolean {
if (event.action == MotionEvent.ACTION_DOWN) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.speechbuddy.compose

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.height
Expand Down Expand Up @@ -36,9 +38,13 @@ data class BottomNavItem(
val iconResId: Int
)

@RequiresApi(Build.VERSION_CODES.O)
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SpeechBuddyHome() {
fun SpeechBuddyHome(
initialPage: Boolean,
isBeingReloadedForDarkModeChange: Boolean
) {
val navController = rememberNavController()
val navItems = listOf(
BottomNavItem(
Expand Down Expand Up @@ -77,7 +83,9 @@ fun SpeechBuddyHome() {
) { paddingValues ->
SpeechBuddyHomeNavHost(
navController = navController,
bottomPaddingValues = paddingValues
bottomPaddingValues = paddingValues,
initialPage = initialPage,
isBeingReloadedForDarkModeChange = isBeingReloadedForDarkModeChange
)
}
}
Expand Down Expand Up @@ -117,12 +125,24 @@ private fun BottomNavigationBar(
}
}

@RequiresApi(Build.VERSION_CODES.O)
@Composable
private fun SpeechBuddyHomeNavHost(
navController: NavHostController,
bottomPaddingValues: PaddingValues
bottomPaddingValues: PaddingValues,
initialPage: Boolean,
isBeingReloadedForDarkModeChange: Boolean
) {
NavHost(navController = navController, startDestination = "symbol_selection") {
val startDestination = if (isBeingReloadedForDarkModeChange) {
"settings"
} else if (initialPage) {
"symbol_selection"
} else {
"text_to_speech"
}


NavHost(navController = navController, startDestination = startDestination) {
composable("symbol_selection") {
SymbolSelectionScreen(
bottomPaddingValues = bottomPaddingValues
Expand All @@ -140,7 +160,8 @@ private fun SpeechBuddyHomeNavHost(
}
composable("settings") {
SettingsScreen(
bottomPaddingValues = bottomPaddingValues
bottomPaddingValues = bottomPaddingValues,
isBeingReloadedForDarkModeChange = isBeingReloadedForDarkModeChange
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
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
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
Expand All @@ -26,6 +32,7 @@ import com.example.speechbuddy.compose.utils.TopAppBarUi
import com.example.speechbuddy.ui.models.AccountSettingsAlert
import com.example.speechbuddy.viewmodel.AccountSettingsViewModel

@RequiresApi(Build.VERSION_CODES.O)
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AccountSettings(
Expand Down Expand Up @@ -92,7 +99,7 @@ fun AccountSettings(
) {
ButtonUi(
text = stringResource(id = R.string.logout),
onClick = { viewModel.showAlert(AccountSettingsAlert.LOGOUT) }
onClick = { viewModel.showAlert(AccountSettingsAlert.BACKUP) }
)

ButtonUi(
Expand All @@ -108,6 +115,36 @@ fun AccountSettings(

uiState.alert?.let { alert ->
when (alert) {
AccountSettingsAlert.BACKUP -> {
AlertDialogUi(
title = stringResource(id = R.string.logout),
text = stringResource(id = R.string.logout_backup),
dismissButtonText = stringResource(id = R.string.logout),
confirmButtonText = stringResource(id = R.string.backup),
onDismiss = { viewModel.showAlert(AccountSettingsAlert.LOGOUT) },
onConfirm = { viewModel.backup() },
onDismissRequest = { viewModel.hideAlert() }
)
}

AccountSettingsAlert.LOADING -> {
CircularProgressIndicator(
modifier = Modifier
.fillMaxSize()
.wrapContentSize()
)
}

AccountSettingsAlert.BACKUP_SUCCESS -> {
Toast.makeText(
LocalContext.current,
stringResource(id = R.string.backup_success),
Toast.LENGTH_SHORT
).show()
viewModel.showAlert(AccountSettingsAlert.LOGOUT)
}


AccountSettingsAlert.LOGOUT -> {
AlertDialogUi(
title = stringResource(id = R.string.logout),
Expand Down Expand Up @@ -144,14 +181,12 @@ fun AccountSettings(
}

AccountSettingsAlert.CONNECTION -> {
AlertDialogUi(
title = stringResource(id = R.string.no_connection),
text = stringResource(id = R.string.no_connection_warning),
dismissButtonText = stringResource(id = R.string.cancel),
confirmButtonText = stringResource(id = R.string.confirm),
onDismiss = { viewModel.hideAlert() },
onConfirm = { viewModel.hideAlert() }
)
viewModel.hideAlert()
Toast.makeText(
LocalContext.current,
stringResource(id = R.string.connection_error),
Toast.LENGTH_SHORT
).show()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
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 @@ -8,23 +11,29 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Switch
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.example.speechbuddy.R
import com.example.speechbuddy.compose.utils.ButtonUi
import com.example.speechbuddy.compose.utils.TopAppBarUi
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)
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun BackupSettings(
Expand All @@ -34,6 +43,7 @@ fun BackupSettings(
viewModel: BackupSettingsViewModel = hiltViewModel()
) {
val uiState by viewModel.uiState.collectAsState()
val loading by viewModel.loading.observeAsState()

Surface(
modifier = modifier.fillMaxSize()
Expand Down Expand Up @@ -89,4 +99,36 @@ fun BackupSettings(
}
}
}

uiState.alert.let { alert ->
when (alert) {
BackupSettingsAlert.SUCCESS -> {
viewModel.toastDisplayed()
Toast.makeText(
LocalContext.current,
stringResource(id = R.string.backup_success),
Toast.LENGTH_SHORT
).show()
}

BackupSettingsAlert.CONNECTION -> {
viewModel.toastDisplayed()
Toast.makeText(
LocalContext.current,
stringResource(id = R.string.connection_error),
Toast.LENGTH_SHORT
).show()
}

else -> {}
}
}

if (loading == true) {
CircularProgressIndicator(
modifier = Modifier
.fillMaxSize()
.wrapContentSize()
)
}
}
Loading

0 comments on commit a155369

Please sign in to comment.