Skip to content

Commit

Permalink
🔀 Merge pull request #68 from snuhcs-course/fix/darkmode
Browse files Browse the repository at this point in the history
fix/darkmode
  • Loading branch information
yjeong-k authored Nov 26, 2023
2 parents b0b0867 + add775a commit bf7d66c
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import androidx.activity.compose.setContent
import androidx.activity.viewModels
import com.example.speechbuddy.compose.SpeechBuddyAuth
import com.example.speechbuddy.ui.SpeechBuddyTheme
import com.example.speechbuddy.viewmodel.DisplaySettingsViewModel
import com.example.speechbuddy.viewmodel.LoginViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class AuthActivity : BaseActivity() {

private val loginViewModel: LoginViewModel by viewModels()
private val displaySettingsViewModel: DisplaySettingsViewModel by viewModels()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -23,7 +25,10 @@ class AuthActivity : BaseActivity() {
checkPreviousAuthUser()

setContent {
SpeechBuddyTheme {
SpeechBuddyTheme(
settingsRepository = settingsRepository,
initialDarkMode = getInitialDarkMode()
) {
SpeechBuddyAuth()
}
}
Expand All @@ -41,6 +46,10 @@ class AuthActivity : BaseActivity() {
finish()
}

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

private fun checkPreviousAuthUser() {
loginViewModel.checkPreviousUser()
}
Expand Down
32 changes: 7 additions & 25 deletions frontend/app/src/main/java/com/example/speechbuddy/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.annotation.RequiresApi
import androidx.compose.runtime.mutableStateOf
import androidx.core.view.WindowCompat
import androidx.lifecycle.Observer
import com.example.speechbuddy.compose.SpeechBuddyHome
Expand All @@ -26,28 +27,16 @@ class HomeActivity : BaseActivity() {

super.onCreate(savedInstanceState)

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

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

subscribeObservers()

val previousDarkMode = getDarkMode()

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

settingsRepository.darkModeLiveData.observeForever(darkModeObserver)

}

private fun subscribeObservers() {
Expand All @@ -62,20 +51,13 @@ class HomeActivity : BaseActivity() {
finish()
}

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

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

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

// hides keyboard
override fun dispatchTouchEvent(event: MotionEvent): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ data class BottomNavItem(
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SpeechBuddyHome(
initialPage: Boolean,
isBeingReloadedForDarkModeChange: Boolean
initialPage: Boolean
) {
val navController = rememberNavController()
val navItems = listOf(
Expand Down Expand Up @@ -93,8 +92,7 @@ fun SpeechBuddyHome(
bottomPaddingValues = paddingValues,
initialPage = initialPage,
showBottomNavBar = { bottomNavBarState.value = true },
hideBottomNavBar = { bottomNavBarState.value = false },
isBeingReloadedForDarkModeChange = isBeingReloadedForDarkModeChange
hideBottomNavBar = { bottomNavBarState.value = false }
)
}
}
Expand Down Expand Up @@ -151,12 +149,9 @@ private fun SpeechBuddyHomeNavHost(
initialPage: Boolean,
showBottomNavBar: () -> Unit,
hideBottomNavBar: () -> Unit,
isBeingReloadedForDarkModeChange: Boolean,
) {
val startDestination =
if (isBeingReloadedForDarkModeChange) {
"settings"
} else if (initialPage) {
if (initialPage) {
"symbol_selection"
} else {
"text_to_speech"
Expand All @@ -182,8 +177,7 @@ private fun SpeechBuddyHomeNavHost(
}
composable("settings") {
SettingsScreen(
bottomPaddingValues = bottomPaddingValues,
isBeingReloadedForDarkModeChange = isBeingReloadedForDarkModeChange
bottomPaddingValues = bottomPaddingValues
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,4 @@ fun LandingScreen(
}
}
}
}

@Preview
@Composable
private fun LandingScreenPreview() {
SpeechBuddyTheme {
LandingScreen(onLoginClick = {})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,4 @@ fun SettingsRowText(
style = MaterialTheme.typography.bodyMedium
)
}
}

@Preview(showBackground = true)
@Composable
fun SettingsRowPreview() {
SpeechBuddyTheme {
SettingsRow(
label = "이메일",
content = {
SettingsRowText(text = "[email protected]")
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,24 @@ import androidx.navigation.compose.rememberNavController
@RequiresApi(Build.VERSION_CODES.O)
@Composable
fun SettingsScreen(
bottomPaddingValues: PaddingValues,
isBeingReloadedForDarkModeChange: Boolean
bottomPaddingValues: PaddingValues
) {
val navController = rememberNavController()
SettingsScreenNavHost(
navController = navController,
bottomPaddingValues = bottomPaddingValues,
isBeingReloadedForDarkModeChange = isBeingReloadedForDarkModeChange
bottomPaddingValues = bottomPaddingValues
)
}

@RequiresApi(Build.VERSION_CODES.O)
@Composable
private fun SettingsScreenNavHost(
navController: NavHostController,
bottomPaddingValues: PaddingValues,
isBeingReloadedForDarkModeChange: Boolean
bottomPaddingValues: PaddingValues
) {
val flag = remember{ mutableStateOf(false) }
val startDestination = if (isBeingReloadedForDarkModeChange && !flag.value) "display" else "main"
if (isBeingReloadedForDarkModeChange && !flag.value) {
flag.value = true
}
val navigateToMain = { navController.navigate("main") }

NavHost(navController = navController, startDestination = startDestination) {
NavHost(navController = navController, startDestination = "main") {
composable("main") {
MainSettings(
navController = navController,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,4 @@ fun SymbolSearchTextField(
unfocusedBorderColor = MaterialTheme.colorScheme.surfaceVariant
)
)
}

@Preview(showBackground = true)
@Composable
fun SymbolSearchTextFieldPreview() {
SpeechBuddyTheme {
SymbolSearchTextField(
value = "검색어",
onValueChange = {}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,4 @@ fun AlertDialogUi(
},
containerColor = MaterialTheme.colorScheme.inverseOnSurface
)
}

@Preview
@Composable
fun AlertDialogUiPreview() {
SpeechBuddyTheme {
AlertDialogUi(
title = "title",
text = "text",
dismissButtonText = "dismiss",
confirmButtonText = "confirm",
onDismiss = {},
onConfirm = {}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,4 @@ fun ButtonUi(
Text(text = text, style = MaterialTheme.typography.titleMedium)
}
}
}

@Preview
@Composable
fun ButtonUiPreview() {
SpeechBuddyTheme {
ButtonUi(
text = "커스텀 버튼",
onClick = {},
isError = true,
level = ButtonLevel.SECONDARY
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,28 +98,4 @@ private fun getTextFieldColors(isValid: Boolean): TextFieldColors {
focusedSupportingTextColor = MaterialTheme.colorScheme.tertiary,
unfocusedSupportingTextColor = MaterialTheme.colorScheme.outline
)
}

@Preview(showBackground = true)
@Composable
fun TextFieldUiPreview() {
SpeechBuddyTheme {
TextFieldUi(
value = "[email protected]",
onValueChange = {},
label = { Text("이메일") },
supportingButton = {
ButtonUi(
text = "인증번호 발송",
onClick = {},
isError = true,
level = ButtonLevel.TERTIARY
)
},
supportingText = { Text("잘못된 이메일 주소입니다.") },
isError = true,
isValid = false,
isHidden = false,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,4 @@ fun TitleUi(
)
}
}
}

@Preview(showBackground = true)
@Composable
fun TitleUiPreview() {
SpeechBuddyTheme {
TitleUi(
title = stringResource(id = R.string.signup),
description = stringResource(id = R.string.signup_description)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,4 @@ fun TopAppBarUi(
titleContentColor = MaterialTheme.colorScheme.onSecondaryContainer
)
)
}

@Preview
@Composable
fun TopAppBarUiPreview() {
SpeechBuddyTheme {
TopAppBarUi(title = "title", onBackClick = {})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,11 @@ class SettingsRepository @Inject constructor(
private val settingsRemoteSource: SettingsRemoteSource,
private val converters: Converters,
) {

private val _darkModeLiveData = MutableLiveData<Boolean?>()
val darkModeLiveData: LiveData<Boolean?>
get() = _darkModeLiveData

suspend fun setDarkMode(value: Boolean) {
CoroutineScope(Dispatchers.Main).launch {
if (_darkModeLiveData.value != value) {
_darkModeLiveData.value = value
}
if (value) {
settingsPrefManager.saveDarkMode(true)
} else {
settingsPrefManager.saveDarkMode(false)
}
settingsPrefManager.saveDarkMode(value)
}
Expand All @@ -72,6 +67,12 @@ class SettingsRepository @Inject constructor(
}
}

fun getDarkModeForChange(): Flow<Boolean> {
return settingsPrefManager.settingsPreferencesFlow.map { settingsPreferences ->
settingsPreferences.darkMode
}
}

fun getInitialPage(): Flow<Resource<Boolean>> {
return settingsPrefManager.settingsPreferencesFlow.map { settingsPreferences ->
Resource.success(settingsPreferences.initialPage)
Expand Down
Loading

0 comments on commit bf7d66c

Please sign in to comment.