Skip to content

Commit

Permalink
refactor: TestScreen의 동작을 MyPage로 옮김
Browse files Browse the repository at this point in the history
  • Loading branch information
yangsooplus committed Jan 2, 2024
1 parent e21b8a0 commit 8928f59
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 132 deletions.
1 change: 0 additions & 1 deletion data/src/main/java/com/susu/data/di/NetworkModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ object NetworkModule {
@AuthOkHttpClient
fun provideAuthOkHttpClient(
loggingInterceptor: HttpLoggingInterceptor,
json: Json,
): OkHttpClient = OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.build()
Expand Down
16 changes: 16 additions & 0 deletions domain/src/main/java/com/susu/domain/usecase/LogoutUseCase.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.susu.domain.usecase

import com.susu.core.common.runCatchingIgnoreCancelled
import com.susu.domain.repository.LoginRepository
import com.susu.domain.repository.TokenRepository
import javax.inject.Inject

class LogoutUseCase @Inject constructor(
private val loginRepository: LoginRepository,
private val tokenRepository: TokenRepository,
) {
suspend operator fun invoke() = runCatchingIgnoreCancelled {
loginRepository.logout()
tokenRepository.deleteTokens()
}
}
16 changes: 16 additions & 0 deletions domain/src/main/java/com/susu/domain/usecase/WithdrawUseCase.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.susu.domain.usecase

import com.susu.core.common.runCatchingIgnoreCancelled
import com.susu.domain.repository.LoginRepository
import com.susu.domain.repository.TokenRepository
import javax.inject.Inject

class WithdrawUseCase @Inject constructor(
private val loginRepository: LoginRepository,
private val tokenRepository: TokenRepository,
) {
suspend operator fun invoke() = runCatchingIgnoreCancelled {
loginRepository.withdraw()
tokenRepository.deleteTokens()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.susu.feature.loginsignup

import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable

@Composable
fun VoteScreen(
navigateToLogin: () -> Unit
) {
Button(onClick = navigateToLogin) {
Text("대충 회원가입 투표")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,41 @@ import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavOptions
import androidx.navigation.compose.composable
import androidx.navigation.navigation
import com.susu.feature.loginsignup.VoteScreen
import com.susu.feature.loginsignup.login.LoginScreen
import com.susu.feature.loginsignup.signup.SignUpScreen
import com.susu.feature.loginsignup.test.TestScreen

@Suppress("unused")
fun NavController.navigateLoginSignup(navOptions: NavOptions) {
navigate(LoginSignupRoute.Parent.route, navOptions)
}

fun NavGraphBuilder.loginSignupNavGraph(navController: NavController) {
navigation(startDestination = LoginSignupRoute.Parent.Login.route, route = LoginSignupRoute.Parent.route) {
composable(route = LoginSignupRoute.Parent.Login.route) {
LoginScreen(
navigateToReceived = { navController.navigate(LoginSignupRoute.Parent.Test.route) },
navigateToSignUp = { navController.navigate(LoginSignupRoute.Parent.SignUp.route) },
)
}
composable(route = LoginSignupRoute.Parent.SignUp.route) {
SignUpScreen(
navigateToReceived = { navController.navigate(LoginSignupRoute.Parent.Test.route) },
)
}
composable(route = LoginSignupRoute.Parent.Test.route) {
TestScreen(
navigateToLogin = { navController.popBackStack() },
)
}
fun NavGraphBuilder.loginSignupNavGraph(
navController: NavController,
navigateToReceived: () -> Unit,
) {
composable(route = LoginSignupRoute.Parent.Vote.route) {
VoteScreen(
navigateToLogin = { navController.navigate(LoginSignupRoute.Parent.Login.route) },
)
}
composable(route = LoginSignupRoute.Parent.Login.route) {
LoginScreen(
navigateToReceived = navigateToReceived,
navigateToSignUp = { navController.navigate(LoginSignupRoute.Parent.SignUp.route) },
)
}
composable(route = LoginSignupRoute.Parent.SignUp.route) {
SignUpScreen(
navigateToReceived = navigateToReceived,
)
}
}

sealed class LoginSignupRoute(val route: String) {
data object Parent : LoginSignupRoute("login-signup") {
data object Vote : LoginSignupRoute("vote")
data object Login : LoginSignupRoute("login")
data object SignUp : LoginSignupRoute("signup")
data object Test : LoginSignupRoute("test")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object KakaoLoginHelper {
loginWithKakaoAccount(
onSuccess = onSuccess,
onFailed = onFailed,
context = context
context = context,
)
}
},
Expand Down Expand Up @@ -87,21 +87,4 @@ object KakaoLoginHelper {
}
}
}

// 기능 테스트를 위함.
fun logout() = runCatching {
UserApiClient.instance.logout { error ->
if (error != null) {
throw error
}
}
}

fun unlink() = runCatching {
UserApiClient.instance.unlink { error ->
if (error != null) {
throw error
}
}
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.susu.feature.mypage

import com.susu.core.ui.base.SideEffect
import com.susu.core.ui.base.UiState

interface MyPageContract {
sealed class MyPageEffect : SideEffect {
data object NavigateToLogin : MyPageEffect()
data class ShowToast(val msg: String) : MyPageEffect()
}

object MyPageState : UiState
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,52 @@
package com.susu.feature.mypage

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.susu.core.designsystem.theme.SusuTheme

@Composable
fun MyPageScreen(
padding: PaddingValues,
viewModel: MyPageViewModel = hiltViewModel(),
navigateToLogin: () -> Unit,
) {
Text(
modifier = Modifier.padding(padding),
text = "마이 페이지",
)
LaunchedEffect(key1 = viewModel.sideEffect) {
viewModel.sideEffect.collect { sideEffect ->
when (sideEffect) {
MyPageContract.MyPageEffect.NavigateToLogin -> navigateToLogin()
is MyPageContract.MyPageEffect.ShowToast -> {
//TODO: UI 작업 시 에러 메세지 표시
navigateToLogin()
}
}
}
}

Column {
Button(onClick = viewModel::logout) {
Text(text = "로그아웃")
}
Button(
onClick = viewModel::withdraw,
) {
Text(text = "탈퇴")
}
}
}

@Preview
@Composable
fun MyPageScreenPreview() {
SusuTheme {
MyPageScreen(padding = PaddingValues(0.dp))
MyPageScreen(padding = PaddingValues(0.dp)) {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.susu.feature.mypage

import androidx.lifecycle.viewModelScope
import com.kakao.sdk.user.UserApiClient
import com.susu.core.ui.base.BaseViewModel
import com.susu.domain.usecase.LogoutUseCase
import com.susu.domain.usecase.WithdrawUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class MyPageViewModel @Inject constructor(
private val logoutUseCase: LogoutUseCase,
private val withdrawUseCase: WithdrawUseCase,
) : BaseViewModel<MyPageContract.MyPageState, MyPageContract.MyPageEffect>(MyPageContract.MyPageState) {

fun logout() {
UserApiClient.instance.logout { error ->
if (error != null) {
postSideEffect(MyPageContract.MyPageEffect.ShowToast(error.message ?: "에러 발생했지만 토큰은 삭제됨"))
} else {
viewModelScope.launch {
logoutUseCase().onSuccess {
postSideEffect(MyPageContract.MyPageEffect.NavigateToLogin)
}
}
}
}
}

fun withdraw() {
UserApiClient.instance.unlink { error ->
if (error != null) {
postSideEffect(MyPageContract.MyPageEffect.ShowToast(error.message ?: "에러 발생했지만 토큰은 삭제됨"))
} else {
viewModelScope.launch {
withdrawUseCase().onSuccess {
postSideEffect(MyPageContract.MyPageEffect.NavigateToLogin)
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ fun NavController.navigateMyPage(navOptions: NavOptions) {

fun NavGraphBuilder.myPageNavGraph(
padding: PaddingValues,
navigateToLogin: () -> Unit
) {
composable(route = MyPageRoute.route) {
MyPageScreen(padding)
MyPageScreen(padding, navigateToLogin = navigateToLogin)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ class MainActivity : ComponentActivity() {
),
),
startDestination = when (uiState.initialRoute) {
InitialRoute.SIGNUP_VOTE -> LoginSignupRoute.Parent.route
InitialRoute.LOGIN -> LoginSignupRoute.Parent.route
InitialRoute.SIGNUP_VOTE -> LoginSignupRoute.Parent.Vote.route
InitialRoute.LOGIN -> LoginSignupRoute.Parent.Login.route
InitialRoute.RECEIVED -> ReceivedRoute.route
InitialRoute.NONE -> LoginSignupRoute.Parent.route
},
Expand Down
Loading

0 comments on commit 8928f59

Please sign in to comment.