Skip to content

Commit

Permalink
[PC-253] AuthNavigationGraph Route 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
tgyuuAn committed Jan 4, 2025
1 parent 01fc43d commit 4aa470a
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 41 deletions.
103 changes: 65 additions & 38 deletions feature/auth/src/main/java/com/puzzle/auth/graph/main/AuthScreen.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
package com.puzzle.auth.graph.main

import android.util.Log
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.sp
import com.airbnb.mvrx.compose.collectAsState
import com.airbnb.mvrx.compose.mavericksViewModel
import com.kakao.sdk.user.UserApiClient
import com.puzzle.auth.graph.main.contract.AuthIntent.Navigate
import com.puzzle.auth.graph.main.contract.AuthState
import com.puzzle.designsystem.foundation.PieceTheme
import com.puzzle.navigation.AuthGraph
import com.puzzle.navigation.AuthGraphDest
import com.puzzle.navigation.NavigationEvent

@Composable
fun AuthRoute(
Expand All @@ -14,47 +29,59 @@ fun AuthRoute(
val context = LocalContext.current

AuthScreen(

state = state,
loginKakao = {
UserApiClient.instance.loginWithKakaoAccount(context) { token, error ->
if (error != null) {
Log.e("test", "로그인 실패", error)
} else if (token != null) {
Log.i("test", "로그인 성공 ${token.accessToken}")
}
}
},
navigate = { viewModel.onIntent(Navigate(it)) },
)
}

@Composable
fun AuthScreen() {
// Column(
// modifier = Modifier
// .fillMaxSize()
// .clickable {
// viewModel.navigationHelper.navigate(
// NavigationEvent.NavigateTo(
// route = MatchingGraph,
// popUpTo = AuthGraph,
// )
// )
// },
// ) {
// Text(
// text = "카카오 로그인",
// fontSize = 30.sp,
// modifier = Modifier.clickable {
// UserApiClient.instance.loginWithKakaoAccount(context) { token, error ->
// if (error != null) {
// Log.e("test", "로그인 실패", error)
// } else if (token != null) {
// Log.i("test", "로그인 성공 ${token.accessToken}")
// }
// }
// }
// )
//
// Text(
// text = "AuthRoute",
// fontSize = 30.sp,
// )
// }
fun AuthScreen(
state: AuthState,
loginKakao: () -> Unit,
navigate: (NavigationEvent) -> Unit,
) {
Column(
modifier = Modifier
.fillMaxSize()
.clickable {
navigate(
NavigationEvent.NavigateTo(
route = AuthGraphDest.AuthRegistrationRoute,
popUpTo = AuthGraph,
)
)
},
) {
Text(
text = "카카오 로그인",
fontSize = 30.sp,
modifier = Modifier.clickable { loginKakao() },
)

Text(
text = "AuthRoute",
fontSize = 30.sp,
)
}
}

//@Preview
//@Composable
//fun PreviewAuthScreen() {
//
//}
@Preview
@Composable
fun PreviewAuthScreen() {
PieceTheme {
AuthScreen(
state = AuthState(),
loginKakao = {},
navigate = {},
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,50 @@ import com.airbnb.mvrx.MavericksViewModel
import com.airbnb.mvrx.MavericksViewModelFactory
import com.airbnb.mvrx.hilt.AssistedViewModelFactory
import com.airbnb.mvrx.hilt.hiltMavericksViewModelFactory
import com.puzzle.auth.graph.main.contract.AuthIntent
import com.puzzle.auth.graph.main.contract.AuthSideEffect
import com.puzzle.auth.graph.main.contract.AuthState
import com.puzzle.navigation.NavigationHelper
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.Channel.Factory.BUFFERED
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch

class AuthViewModel @AssistedInject constructor(
@Assisted initialState: AuthState,
private val navigationHelper: NavigationHelper,
) : MavericksViewModel<AuthState>(initialState) {
private val intents = Channel<AuthIntent>(BUFFERED)

private val _sideEffect = Channel<AuthSideEffect>(BUFFERED)
val sideEffect = _sideEffect.receiveAsFlow()

init {
intents.receiveAsFlow()
.onEach(::processIntent)
.launchIn(viewModelScope)
}

internal fun onIntent(intent: AuthIntent) = viewModelScope.launch {
intents.send(intent)
}

private fun processIntent(intent: AuthIntent) {
when (intent) {
is AuthIntent.Navigate -> navigationHelper.navigate(intent.navigationEvent)
}
}

private fun handleSideEffect(sideEffect: AuthSideEffect) {
when (sideEffect) {
else -> Unit
}
}

@AssistedFactory
interface Factory : AssistedViewModelFactory<AuthViewModel, AuthState> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package com.puzzle.auth.graph.main.contract

class AuthIntent
import com.puzzle.navigation.NavigationEvent

sealed class AuthIntent {
data class Navigate(val navigationEvent: NavigationEvent) : AuthIntent()
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.puzzle.auth.graph.main.contract

class AuthSideEffect
sealed class AuthSideEffect
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.airbnb.mvrx.compose.collectAsState
import com.airbnb.mvrx.compose.mavericksViewModel

@Composable
internal fun AuthRegistrationScreen(
internal fun AuthRegistrationRoute(
viewModel: AuthRegistrationViewModel = mavericksViewModel()
) {
val state by viewModel.collectAsState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import androidx.navigation.navigation
import com.puzzle.auth.graph.main.AuthRoute
import com.puzzle.auth.graph.registration.AuthRegistrationRoute
import com.puzzle.auth.graph.verification.AuthVerificationRoute
import com.puzzle.navigation.AuthGraph
import com.puzzle.navigation.AuthGraphDest

Expand All @@ -12,5 +14,13 @@ fun NavGraphBuilder.authNavGraph() {
composable<AuthGraphDest.AuthRoute> {
AuthRoute()
}

composable<AuthGraphDest.AuthRegistrationRoute> {
AuthRegistrationRoute()
}

composable<AuthGraphDest.AuthVerificationRoute> {
AuthVerificationRoute()
}
}
}

0 comments on commit 4aa470a

Please sign in to comment.