Skip to content

Commit

Permalink
refactor/#8: PreferenceUtil 대신 userRepository 사용
Browse files Browse the repository at this point in the history
  • Loading branch information
kangyein9892 committed Nov 14, 2024
1 parent 2b4fe9a commit 55f0f3f
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 59 deletions.
2 changes: 0 additions & 2 deletions app/src/main/java/org/sopt/and/WavveApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package org.sopt.and

import android.app.Application
import dagger.hilt.android.HiltAndroidApp
import org.sopt.and.presentation.util.PreferenceUtil

@HiltAndroidApp
class WavveApplication: Application() {

override fun onCreate() {
super.onCreate()
PreferenceUtil.init(this)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Modifier
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
Expand All @@ -22,20 +23,22 @@ import org.sopt.and.presentation.extension.getPassword
import org.sopt.and.presentation.extension.setIdPassword
import org.sopt.and.presentation.home.HomeScreen
import org.sopt.and.presentation.main.component.MainBottomBar
import org.sopt.and.presentation.main.viewmodel.MainViewModel
import org.sopt.and.presentation.my.MyScreen
import org.sopt.and.presentation.navigation.Route
import org.sopt.and.presentation.search.SearchScreen
import org.sopt.and.presentation.sign.signin.SignInScreen
import org.sopt.and.presentation.sign.signup.SignUpScreen
import org.sopt.and.presentation.ui.theme.FirstGrey
import org.sopt.and.presentation.util.PreferenceUtil

@Composable
fun MainScreen() {
fun MainScreen(
viewmodel: MainViewModel = hiltViewModel()
) {

val navController = rememberNavController()
val mainNavigator = remember(navController) { MainNavigator(navController) }
val startDestination = rememberUpdatedState(getStartDestination()).value
val startDestination = viewmodel.getStartDestination()

Scaffold(
bottomBar = {
Expand All @@ -62,15 +65,6 @@ fun MainScreen() {
}
}

@Composable
private fun getStartDestination(): Route {
return if(PreferenceUtil.id.isNotBlank() && PreferenceUtil.password.isNotBlank()){
Route.Home
} else {
Route.SignIn
}
}

@Composable
private fun MainNavHost(
navController: NavHostController,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.sopt.and.presentation.main.viewmodel

import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import org.sopt.and.domain.repository.UserRepository
import org.sopt.and.presentation.navigation.Route
import javax.inject.Inject

@HiltViewModel
class MainViewModel @Inject constructor(
private val userRepository: UserRepository
): ViewModel() {

fun getStartDestination(): Route {
return if(userRepository.getId().isNotEmpty()){
Route.Home
} else {
Route.SignIn
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import org.sopt.and.presentation.ui.theme.FirstGrey
import org.sopt.and.presentation.ui.theme.SecondGrey
import org.sopt.and.presentation.ui.theme.ThirdGrey
import org.sopt.and.presentation.ui.theme.White
import org.sopt.and.presentation.util.PreferenceUtil

@Composable
fun MyScreen(
Expand All @@ -65,14 +64,12 @@ fun MyScreen(
val context = rememberUpdatedState(LocalContext.current).value
val snackBarHostState = remember { SnackbarHostState() }

viewModel.updateId(PreferenceUtil.id)

LaunchedEffect(viewModel.intent, lifecycleOwner){
viewModel.intent.flowWithLifecycle(lifecycle = lifecycleOwner.lifecycle)
.collect{ intent ->
when(intent) {
MySideEffect.LogOut -> {
PreferenceUtil.clearIdPassword()
viewModel.clearIdPassword()
snackBarHostState.showSnackbar(
message = context.getString(R.string.my_logout_text)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import org.sopt.and.domain.repository.UserRepository
import org.sopt.and.presentation.my.model.MyState
import org.sopt.and.presentation.my.sideeffect.MySideEffect
import javax.inject.Inject

@HiltViewModel
class MyViewModel @Inject constructor(

private val userRepository: UserRepository
): ViewModel() {

private val _state = MutableStateFlow(MyState())
Expand All @@ -24,13 +25,21 @@ class MyViewModel @Inject constructor(
private val _intent = MutableSharedFlow<MySideEffect>()
val intent = _intent.asSharedFlow()

fun updateId(id: String) = _state.update {
it.copy(id = id)
init {
updateId()
}

fun updateId() = _state.update {
it.copy(id = userRepository.getId())
}

fun onLogOutButtonClick() = viewModelScope.launch {
_intent.emit(MySideEffect.LogOut)
}

fun clearIdPassword() = viewModelScope.launch {
userRepository.clearIdPassword()
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import org.sopt.and.presentation.ui.theme.SecondGrey
import org.sopt.and.presentation.ui.theme.ThirdGrey
import org.sopt.and.presentation.ui.theme.WavveColor
import org.sopt.and.presentation.ui.theme.White
import org.sopt.and.presentation.util.PreferenceUtil

@Composable
fun SignInScreen(
Expand All @@ -66,8 +65,7 @@ fun SignInScreen(
viewModel.intent.collect{ intent ->
when(intent) {
SignInSideEffect.SignIn -> {
PreferenceUtil.id = state.id
PreferenceUtil.password = state.password
viewModel.saveUser(state.id, state.password)
navigateToMy()
}
SignInSideEffect.SignUp -> navigateToSignUp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import org.sopt.and.domain.repository.UserRepository
import org.sopt.and.presentation.R
import org.sopt.and.presentation.sign.signin.model.SignInState
import org.sopt.and.presentation.sign.signin.sideeffect.SignInSideEffect
import javax.inject.Inject

@HiltViewModel
class SignInViewModel @Inject constructor(

private val userRepository: UserRepository
): ViewModel() {

private var _state = MutableStateFlow(SignInState())
Expand All @@ -25,6 +26,10 @@ class SignInViewModel @Inject constructor(
private var _intent = MutableSharedFlow<SignInSideEffect>()
val intent = _intent.asSharedFlow()

fun saveUser(id: String, password: String){
userRepository.saveUser(id, password)
}

fun updateId(id: String) = _state.update {
it.copy(id = id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ object KeyUtil {
const val ID = "id"
const val PASSWORD = "password"
const val DEFAULT_STRING = ""
const val PREF_NAME = "wavve_prefs"
}

This file was deleted.

0 comments on commit 55f0f3f

Please sign in to comment.