Skip to content

Commit

Permalink
Merge pull request #86 from Nexters/feature/style-navigation
Browse files Browse the repository at this point in the history
[FEAT] 홈화면 내에 스타일로 프로필 생성하기 버튼을 통해, 스타일 입력화면내에 선택한 스타일이 체크되도록 구현
  • Loading branch information
easyhooon authored Feb 26, 2024
2 parents eefb483 + 06ef0a5 commit 4f8b364
Show file tree
Hide file tree
Showing 21 changed files with 161 additions and 106 deletions.
3 changes: 2 additions & 1 deletion core/common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
alias(libs.plugins.ilab.android.library)
alias(libs.plugins.ilab.android.library.compose)
alias(libs.plugins.ilab.android.hilt)
alias(libs.plugins.ilab.android.retrofit)
}

android {
Expand All @@ -16,7 +17,7 @@ dependencies {
libs.androidx.core,
libs.androidx.hilt.navigation.compose,

libs.timber,
libs.bundles.androidx.lifecycle,

)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.nexters.ilab.android.core.common

import retrofit2.HttpException
import timber.log.Timber
import java.net.SocketTimeoutException
import java.net.UnknownHostException

interface ErrorHandlerActions {
fun openServerErrorDialog()
fun openNetworkErrorDialog()
}

fun handleException(exception: Throwable, actions: ErrorHandlerActions) {
when (exception) {
is HttpException -> {
if (exception.code() == 500) {
actions.openServerErrorDialog()
} else {
Timber.e(exception)
}
}
is UnknownHostException -> {
actions.openNetworkErrorDialog()
}
is SocketTimeoutException -> {
actions.openServerErrorDialog()
}
else -> {
Timber.e(exception)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import com.nexters.ilab.android.core.data.response.UserInfoResponse
interface AuthDataSource {
suspend fun signIn()
suspend fun getUserInfo(uuid: Long): UserInfoResponse
suspend fun signOut()
suspend fun deleteAccount()
suspend fun signOut(uuid: Long)
suspend fun deleteAccount(uuid: Long)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class AuthDataSourceImpl @Inject constructor(
return service.getUserInfo(uuid)
}

override suspend fun signOut() {
service.signOut()
override suspend fun signOut(uuid: Long) {
service.signOut(uuid)
}

override suspend fun deleteAccount() {
service.deleteAccount()
override suspend fun deleteAccount(uuid: Long) {
service.deleteAccount(uuid)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ class AuthRepositoryImpl @Inject constructor(
}

override suspend fun signOut() = runSuspendCatching {
authDataSource.signOut()
val uuid = tokenDataSource.getUUID()
authDataSource.signOut(uuid)
}

override suspend fun deleteAccount() = runSuspendCatching {
authDataSource.deleteAccount()
val uuid = tokenDataSource.getUUID()
authDataSource.deleteAccount(uuid)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ interface AuthService {
@Path("uuid") uuid: Long,
): UserInfoResponse

@POST("users/logout")
suspend fun signOut()
@POST("users/logout/{uuid}")
suspend fun signOut(
@Path("uuid") uuid: Long,
)

@POST("users/delete")
suspend fun deleteAccount()
@POST("users/delete/{uuid}")
suspend fun deleteAccount(
@Path("uuid") uuid: Long,
)
}
1 change: 0 additions & 1 deletion feature/home/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

plugins {
alias(libs.plugins.ilab.android.feature)
alias(libs.plugins.ilab.android.retrofit)
}

android {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import com.nexters.ilab.android.core.designsystem.theme.Title1
import com.nexters.ilab.android.core.designsystem.theme.Title2
import com.nexters.ilab.android.core.domain.entity.ProfileEntity
import com.nexters.ilab.android.core.domain.entity.StyleEntity
import com.nexters.ilab.android.feature.home.viewmodel.HomeSideEffect
import com.nexters.ilab.android.feature.home.viewmodel.HomeState
import com.nexters.ilab.android.feature.home.viewmodel.HomeViewModel
import com.nexters.ilab.core.ui.ComponentPreview
Expand All @@ -70,21 +71,30 @@ import com.nexters.ilab.core.ui.component.PagerIndicator
import com.nexters.ilab.core.ui.component.ServerErrorDialog
import com.nexters.ilab.core.ui.component.TopAppBarNavigationType

@Suppress("unused")
@Composable
internal fun HomeRoute(
padding: PaddingValues,
onSettingClick: () -> Unit,
onGenerateImgBtnClick: () -> Unit,
onGenerateImgBtnClick: (String) -> Unit,
viewModel: HomeViewModel = hiltViewModel(),
) {
val uiState by viewModel.container.stateFlow.collectAsStateWithLifecycle()

LaunchedEffect(viewModel) {
viewModel.container.sideEffectFlow.collect { sideEffect ->
when (sideEffect) {
is HomeSideEffect.NavigateToUploadPhoto -> {
onGenerateImgBtnClick(sideEffect.selectedStyle)
}
}
}
}

HomeScreen(
uiState = uiState,
padding = padding,
onSettingClick = onSettingClick,
onGenerateImgBtnClick = onGenerateImgBtnClick,
onGenerateImgBtnClick = viewModel::onGenerateImgBtnClick,
openProfileImageDialog = viewModel::openProfileImageDialog,
dismissProfileImageDialog = viewModel::dismissProfileImageDialog,
getStyleList = viewModel::getStyleList,
Expand Down Expand Up @@ -205,7 +215,10 @@ internal fun HomeContent(
imageRatio = imageRatio,
startDp = startDp,
endDp = endDp,
openProfileImageDialog = { openProfileImageDialog(index) },
openProfileImageDialog = {
setSelectedStyleImage(index)
openProfileImageDialog(index)
},
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fun NavController.navigateToHome(navOptions: NavOptions) {
fun NavGraphBuilder.homeNavGraph(
padding: PaddingValues,
onSettingClick: () -> Unit,
onGenerateImgBtnClick: () -> Unit,
onGenerateImgBtnClick: (String) -> Unit,
) {
composable(route = HOME_ROUTE) {
HomeRoute(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package com.nexters.ilab.android.feature.home.viewmodel

interface HomeSideEffect
sealed interface HomeSideEffect {
data class NavigateToUploadPhoto(val selectedStyle: String) : HomeSideEffect
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ data class HomeState(
val isServerErrorDialogVisible: Boolean = false,
val isNetworkErrorDialogVisible: Boolean = false,
val selectedProfileEntity: ProfileEntity = ProfileEntity("", "", ""),
val selectedStyleEntity: StyleEntity = StyleEntity(0, "", ""),
val selectedStyle: StyleEntity = StyleEntity(0, "", ""),
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ package com.nexters.ilab.android.feature.home.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.nexters.ilab.android.core.common.ErrorHandlerActions
import com.nexters.ilab.android.core.common.handleException
import com.nexters.ilab.android.core.domain.entity.ProfileEntity
import com.nexters.ilab.android.core.domain.repository.StyleRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import org.orbitmvi.orbit.ContainerHost
import org.orbitmvi.orbit.syntax.simple.intent
import org.orbitmvi.orbit.syntax.simple.postSideEffect
import org.orbitmvi.orbit.syntax.simple.reduce
import org.orbitmvi.orbit.viewmodel.container
import retrofit2.HttpException
import timber.log.Timber
import java.net.UnknownHostException
import javax.inject.Inject

@HiltViewModel
class HomeViewModel @Inject constructor(
private val styleRepository: StyleRepository,
) : ViewModel(), ContainerHost<HomeState, HomeSideEffect> {
) : ViewModel(), ContainerHost<HomeState, HomeSideEffect>, ErrorHandlerActions {
override val container = container<HomeState, HomeSideEffect>(HomeState())

// for test
Expand Down Expand Up @@ -54,28 +54,13 @@ class HomeViewModel @Inject constructor(
}
styleRepository.getStyleList()
.onSuccess { styleImageList ->
Timber.d("$styleImageList")
val endIndex = if (styleImageList.size < 4) styleImageList.size - 1 else 3
reduce {
state.copy(styleImageList = styleImageList.shuffled().slice(0..endIndex))
}
}
.onFailure { exception ->
when (exception) {
is HttpException -> {
if (exception.code() == 500) {
openServerErrorDialog()
} else {
Timber.e(exception)
}
}
is UnknownHostException -> {
openNetworkErrorDialog()
}
else -> {
Timber.e(exception)
}
}
handleException(exception, this@HomeViewModel)
}
reduce {
state.copy(isLoading = false)
Expand All @@ -86,11 +71,23 @@ class HomeViewModel @Inject constructor(
fun setSelectedStyleImage(index: Int) = intent {
if (index in 0..<state.styleImageList.size) {
reduce {
state.copy(selectedStyleEntity = state.styleImageList[index])
state.copy(selectedStyle = state.styleImageList[index])
}
}
}

fun onGenerateImgBtnClick() = intent {
postSideEffect(
HomeSideEffect.NavigateToUploadPhoto(
if (state.selectedStyle.name.isEmpty()) {
state.styleImageList[0].name
} else {
state.selectedStyle.name
},
),
)
}

fun openProfileImageDialog(index: Int) = intent {
reduce {
state.copy(isProfileImageDialogVisible = true)
Expand All @@ -108,7 +105,7 @@ class HomeViewModel @Inject constructor(
}
}

private fun openServerErrorDialog() = intent {
override fun openServerErrorDialog() = intent {
reduce {
state.copy(isServerErrorDialogVisible = true)
}
Expand All @@ -120,7 +117,7 @@ class HomeViewModel @Inject constructor(
}
}

private fun openNetworkErrorDialog() = intent {
override fun openNetworkErrorDialog() = intent {
reduce {
state.copy(isNetworkErrorDialogVisible = true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import com.nexters.ilab.core.ui.component.LoadingIndicator
import kotlinx.coroutines.launch
import retrofit2.HttpException
import timber.log.Timber
import java.net.SocketTimeoutException
import java.net.UnknownHostException

@Composable
Expand All @@ -73,7 +74,7 @@ internal fun LoginRoute(
resource.getString(R.string.unknown_error_message)
}
}

is SocketTimeoutException -> resource.getString(R.string.server_error_message)
else -> resource.getString(R.string.unknown_error_message)
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ internal class MainNavController(
}
}

fun navigateToUploadPhoto(selectedStyle: String) {
navController.navigateToUploadPhoto(null, selectedStyle)
}

fun navigateToUploadCheck() {
navController.navigateToUploadCheck()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ internal fun MainScreen(
homeNavGraph(
padding = padding,
onSettingClick = navigator::navigateToSetting,
onGenerateImgBtnClick = {
navigator.navigate(MainTab.UPLOAD_PHOTO)
onGenerateImgBtnClick = { selectedStyle ->
navigator.navigateToUploadPhoto(selectedStyle)
},
)

Expand Down
1 change: 0 additions & 1 deletion feature/mypage/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

plugins {
alias(libs.plugins.ilab.android.feature)
alias(libs.plugins.ilab.android.retrofit)
}

android {
Expand Down
Loading

0 comments on commit 4f8b364

Please sign in to comment.