Skip to content

Commit

Permalink
#4 Flow 걷어내기~
Browse files Browse the repository at this point in the history
  • Loading branch information
comye1 committed Feb 23, 2022
1 parent 78900cb commit 536fbbb
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 34 deletions.
13 changes: 7 additions & 6 deletions app/src/main/java/com/comye1/cheggprep/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.VisibilityOff
import androidx.compose.runtime.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
Expand Down Expand Up @@ -88,8 +91,6 @@ class MainActivity : ComponentActivity() {
}
}

val firebaseAuth = moreViewModel.firebaseAuth.collectAsState()

val updateNeeded = remember {
mutableStateOf(false)
}
Expand All @@ -112,7 +113,7 @@ class MainActivity : ComponentActivity() {
}


if (firebaseAuth.value) { // firebaseAuth가 true일 때
if (moreViewModel.firebaseAuth.value) { // firebaseAuth가 true일 때
firebaseAuthWithGoogle(moreViewModel.token.value, moreViewModel::signIn)
moreViewModel.completeAuth() // 로그인 후 false로 변경
}
Expand All @@ -139,7 +140,7 @@ class MainActivity : ComponentActivity() {
delay(800L)
updateNeeded.value = false
}
}else {
} else {
HomeScreen(navController, homeViewModel)
}
}
Expand All @@ -155,7 +156,7 @@ class MainActivity : ComponentActivity() {
}
composable(Screen.More.route) {
showBottomBar(true)
MoreScreen(navController, moreViewModel)
MoreScreen(moreViewModel)
}
composable(
Screen.Deck.route + "/{key}",
Expand Down
11 changes: 3 additions & 8 deletions app/src/main/java/com/comye1/cheggprep/screens/MoreScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ fun Modifier.moreModifier(onClick: () -> Unit) = this

@ExperimentalMaterialApi
@Composable
fun MoreScreen(navController: NavHostController, viewModel: MoreViewModel) {

// 뷰모델에서 user 가져오기
val user = remember {
viewModel.user
}.collectAsState()
fun MoreScreen(viewModel: MoreViewModel) {

val context = LocalContext.current

Expand Down Expand Up @@ -72,9 +67,9 @@ fun MoreScreen(navController: NavHostController, viewModel: MoreViewModel) {
}
) {
Column {
if (user.value != null) {
if (viewModel.user.value != null) {
AccountSection(
name = user.value!!.displayName,
name = viewModel.user.value!!.displayName,
modifier = Modifier.padding(horizontal = 8.dp, vertical = 16.dp),
signText = "Sign out",
signFunction = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ fun SignInScreen(viewModel: MoreViewModel) {
var text by remember {
mutableStateOf<String?>(null)
}
val user by remember(viewModel) {
viewModel.user
}.collectAsState()

val signInRequestCode = 1
val authResultLauncher =
rememberLauncherForActivityResult(contract = AuthResultContract()) { task ->
Expand All @@ -51,9 +49,8 @@ fun SignInScreen(viewModel: MoreViewModel) {
authResultLauncher.launch(signInRequestCode)
// authResultLauncer launch : SignIn Intent 실행 및 결과 처리 (AuthResultContract)
})
user?.let {
viewModel.user.value?.also {
viewModel.toMainScreen() // Flow를 통해 user가 전달되면 MainScreen으로 이동
// TODO 새로 로드하기,,, 어떢하죠
}
}

Expand Down
28 changes: 13 additions & 15 deletions app/src/main/java/com/comye1/cheggprep/viewmodel/MoreViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,20 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.comye1.cheggprep.models.User
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch

class MoreViewModel : ViewModel() {

// SignIn
private val _user: MutableStateFlow<User?> = MutableStateFlow(null)
val user: StateFlow<User?> = _user
var user = mutableStateOf<User?>(null)
private set

private val userEventChannel = Channel<UserEvent>()
val userEvent = userEventChannel.receiveAsFlow()

fun signIn(email: String, displayName: String) {
_user.value = User(email, displayName)
user.value = User(email, displayName)
//
viewModelScope.launch {
userEventChannel.send(UserEvent.SignIn)
Expand All @@ -29,26 +27,26 @@ class MoreViewModel : ViewModel() {

// SignOut
fun signOut() {
_user.value = null
user.value = null
viewModelScope.launch {
userEventChannel.send(UserEvent.SignOut)
}
}

// MainActivity에서 firebaseAuth 호출하기
private val _firebaseAuth = MutableStateFlow(false)
val firebaseAuth = _firebaseAuth
var firebaseAuth = mutableStateOf(false)
private set

private val _token = MutableStateFlow("")
val token = _token
var token = mutableStateOf("")
private set

fun triggerAuth(idToken: String) { // firebaseAuthWithGoogle을 호출하기 위함
_token.value = idToken
_firebaseAuth.value = true
token.value = idToken
firebaseAuth.value = true
}

fun completeAuth() { // firebaseAuthWithGoogle 실행 후
_firebaseAuth.value = false
firebaseAuth.value = false
}

////////////////////////////////////
Expand All @@ -66,8 +64,8 @@ class MoreViewModel : ViewModel() {

companion object {
sealed class UserEvent {
object SignIn: UserEvent()
object SignOut: UserEvent()
object SignIn : UserEvent()
object SignOut : UserEvent()
}

}
Expand Down

0 comments on commit 536fbbb

Please sign in to comment.