Skip to content

Commit

Permalink
Merge branch 'ddddddd' into feature/#310-representation-rule-ui-prese…
Browse files Browse the repository at this point in the history
…ntation
  • Loading branch information
murjune committed Sep 2, 2023
2 parents 3d93c1c + a57c596 commit 9b82d8e
Show file tree
Hide file tree
Showing 53 changed files with 1,232 additions and 582 deletions.
11 changes: 11 additions & 0 deletions app/src/main/java/hous/release/android/di/ReducerModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import dagger.hilt.InstallIn
import dagger.hilt.android.components.ActivityRetainedComponent
import hous.release.android.presentation.our_rules.event.AddRuleReducer
import hous.release.android.presentation.our_rules.event.MainRuleReducer
import hous.release.android.presentation.our_rules.event.UpdateRuleReducer
import hous.release.android.presentation.our_rules.viewmodel.AddRuleEvent
import hous.release.android.presentation.our_rules.viewmodel.AddRuleState
import hous.release.android.presentation.our_rules.viewmodel.MainRulesEvent
import hous.release.android.presentation.our_rules.viewmodel.MainRulesState
import hous.release.android.presentation.our_rules.viewmodel.UpdateRuleEvent
import hous.release.android.presentation.our_rules.viewmodel.UpdateRuleState
import hous.release.android.util.event.Reducer
import javax.inject.Qualifier

Expand All @@ -23,6 +26,10 @@ abstract class ReducerModule {
@AddRule
@Binds
abstract fun bindAddRuleReducer(addRuleReducer: AddRuleReducer): Reducer<AddRuleState, AddRuleEvent>

@UpdateRule
@Binds
abstract fun bindUpdateRuleReducer(updateRuleReducer: UpdateRuleReducer): Reducer<UpdateRuleState, UpdateRuleEvent>
}

@Retention(AnnotationRetention.BINARY)
Expand All @@ -32,3 +39,7 @@ annotation class MainRules
@Retention(AnnotationRetention.BINARY)
@Qualifier
annotation class AddRule

@Retention(AnnotationRetention.BINARY)
@Qualifier
annotation class UpdateRule
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fun MainRuleContent(
onSearch: (String) -> Unit = {},
onOpenDetailRule: (Int) -> Unit = {},
onNavigateToAddRule: () -> Unit = {},
onNavigateToRepresentRule: () -> Unit = {},
onFinish: () -> Unit = {}
) {
val focusManager = LocalFocusManager.current
Expand All @@ -42,7 +43,8 @@ fun MainRuleContent(
.padding(
start = 16.dp,
end = 16.dp
).pointerInput(Unit) {
)
.pointerInput(Unit) {
detectTapGestures(
onTap = {
focusManager.clearFocus()
Expand All @@ -52,7 +54,8 @@ fun MainRuleContent(
horizontalAlignment = Alignment.CenterHorizontally
) {
MainRuleToolbar(
onBack = onFinish
onBack = onFinish,
onNavigateToRepresentRule = onNavigateToRepresentRule
)
Spacer(modifier = Modifier.padding(top = 4.dp))
HousTextField(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import hous.release.designsystem.theme.HousTheme

@Composable
fun MainRuleToolbar(
onClickSetting: () -> Unit = {},
onNavigateToRepresentRule: () -> Unit = {},
modifier: Modifier = Modifier,
title: String = "우리 집 Rules",
onBack: () -> Unit = { }
Expand Down Expand Up @@ -62,7 +62,7 @@ fun MainRuleToolbar(
)
}
)
MainRuleDropDownMenu(expanded, onDismiss)
MainRuleDropDownMenu(expanded, onDismiss, onNavigateToRepresentRule)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ fun BasicUpdateRuleScreen(
ruleName: String = "",
description: String = "",
photos: List<PhotoUiModel> = emptyList(),
isFocusOn: Boolean = false,
onDisposeFocus: () -> Unit = {},
addRule: () -> Unit = { },
changeName: (String) -> Unit = {},
changeDescription: (String) -> Unit = {},
Expand All @@ -49,7 +51,12 @@ fun BasicUpdateRuleScreen(
) {
val focusManager = LocalFocusManager.current
val (focusRequester) = FocusRequester.createRefs()
AutoFocusOnInitializeEffect { focusRequester.requestFocus() }
AutoFocusOnInitializeEffect {
if (isFocusOn) {
focusRequester.requestFocus()
onDisposeFocus()
}
}

Column(
modifier = Modifier.fillMaxSize().pointerInput(Unit) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package hous.release.android.presentation.our_rules.event

import hous.release.android.presentation.our_rules.model.PhotoUiModel
import hous.release.android.presentation.our_rules.viewmodel.UpdateRuleEvent
import hous.release.android.presentation.our_rules.viewmodel.UpdateRuleState
import hous.release.android.util.event.Reducer
import timber.log.Timber
import javax.inject.Inject

class UpdateRuleReducer @Inject constructor() : Reducer<UpdateRuleState, UpdateRuleEvent> {
override fun dispatch(state: UpdateRuleState, event: UpdateRuleEvent): UpdateRuleState {
return when (event) {
is UpdateRuleEvent.InitRule -> {
state.copy(
id = event.id,
name = event.name,
description = event.description,
photos = event.photos
)
}

is UpdateRuleEvent.ChangeDescription -> {
state.copy(description = event.description)
}

is UpdateRuleEvent.ChangeName -> {
state.copy(name = event.name)
}

is UpdateRuleEvent.LoadImage -> {
val newPhotos = event.photos.map { photo ->
PhotoUiModel.from(photo)
}
val updatedPhotos = state.photos + newPhotos
Timber.e("LoadImage: updatedPhotos: $updatedPhotos")
state.copy(
photos = updatedPhotos
)
}

is UpdateRuleEvent.DeleteImage -> {
val filteredPhotos = state.photos.filter { photo ->
photo != event.photo
}
state.copy(
photos = filteredPhotos
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import hous.release.android.R
import hous.release.android.presentation.our_rules.component.LoadingBar
import hous.release.android.presentation.our_rules.component.dialog.AddRuleLimitedDialog
import hous.release.android.presentation.our_rules.component.dialog.AddRuleOutDialog
import hous.release.android.presentation.our_rules.component.dialog.UpdateRuleOutDialog
import hous.release.android.presentation.our_rules.model.DetailRuleUiModel
import hous.release.android.presentation.our_rules.screen.AddRuleScreen
import hous.release.android.presentation.our_rules.screen.MainRuleScreen
Expand All @@ -32,6 +33,8 @@ import hous.release.android.presentation.our_rules.viewmodel.AddRuleSideEffect
import hous.release.android.presentation.our_rules.viewmodel.AddRuleViewModel
import hous.release.android.presentation.our_rules.viewmodel.MainRuleSideEffect
import hous.release.android.presentation.our_rules.viewmodel.MainRuleViewModel
import hous.release.android.presentation.our_rules.viewmodel.UpdateRuleSideEffect
import hous.release.android.presentation.our_rules.viewmodel.UpdateRuleViewModel
import hous.release.android.presentation.practice.findActivity
import hous.release.android.util.ToastMessageUtil
import hous.release.domain.entity.Photo
Expand Down Expand Up @@ -63,7 +66,7 @@ private fun NavGraphBuilder.mainRuleScreen(
val activity = LocalContext.current.findActivity()
val viewModel = hiltViewModel<MainRuleViewModel>()
val uiState = viewModel.uiState.collectAsStateWithLifecycle()

var isLoading by remember { mutableStateOf(false) }
var isShowLimitedAddRuleDialog by remember { mutableStateOf(false) }
LaunchedEffect(Unit) {
viewModel.sideEffect.collect { event ->
Expand All @@ -76,6 +79,11 @@ private fun NavGraphBuilder.mainRuleScreen(
isShowLimitedAddRuleDialog = true
}
}

is MainRuleSideEffect.LoadingBar -> {
Timber.d("LoadingBar: ${event.isLoading}")
isLoading = event.isLoading
}
}
}
}
Expand All @@ -84,6 +92,7 @@ private fun NavGraphBuilder.mainRuleScreen(
isShowLimitedAddRuleDialog = false
})
}
if (isLoading) LoadingBar()

MainRuleScreen(
detailRule = uiState.value.detailRule,
Expand All @@ -93,12 +102,15 @@ private fun NavGraphBuilder.mainRuleScreen(
onSearch = viewModel::searchRule,
onNavigateToAddRule = viewModel::canAddRule,
onNavigateToUpdateRule = navController::navigateUpdateRule,
onNavigateToRepresentRule = navController::navigateToRepresentRule,
onFinish = activity::finish,
refresh = viewModel::fetchMainRules
refresh = viewModel::fetchMainRules,
deleteRule = viewModel::deleteRule,
)
}
}

@OptIn(ExperimentalLifecycleComposeApi::class)
private fun NavGraphBuilder.updateRuleScreen(
navController: NavController
) {
Expand All @@ -108,9 +120,90 @@ private fun NavGraphBuilder.updateRuleScreen(
navController.previousBackStackEntry?.savedStateHandle?.get<DetailRuleUiModel>(
RulesScreens.DETAIL_RULE_KEY
)?.let { detailRule ->
val viewModel = hiltViewModel<UpdateRuleViewModel>()
val uiState = viewModel.uiState.collectAsStateWithLifecycle()
var isOutDialogShow by remember { mutableStateOf(false) }
var isLoading by remember { mutableStateOf(false) }
val context = LocalContext.current
val takePhotoFromAlbumLauncher = rememberLauncherForActivityResult(
ActivityResultContracts.PickMultipleVisualMedia(5)
) { uriList ->
if (uriList.isNotEmpty()) {
viewModel.loadImage(uriList.map { Photo.from(it.toString()) })
}
}
val onBack: () -> Unit = { navController.popBackStack() }
val onOpenGallery = {
takePhotoFromAlbumLauncher.launch(
PickVisualMediaRequest(
ActivityResultContracts.PickVisualMedia.ImageOnly
)
)
}
LaunchedEffect(Unit) {
viewModel.init(detailRule)
}
LaunchedEffect(Unit) {
viewModel.sideEffect.collect { event ->
when (event) {
is UpdateRuleSideEffect.IDLE -> Unit
is UpdateRuleSideEffect.DuplicateToast -> {
ToastMessageUtil.showToast(
context,
context.getString(R.string.our_rule_duplicate_rule)
)
}

is UpdateRuleSideEffect.ShowLimitImageToast -> {
ToastMessageUtil.showToast(
context,
context.getString(R.string.our_rule_limit_photo_count)
)
}

is UpdateRuleSideEffect.LoadingBar -> {
isLoading = event.isLoading
}

is UpdateRuleSideEffect.PopBackStack -> {
onBack()
}
}
}
}

val onBackPressed = {
if (uiState.value.name.isNotBlank()) {
isOutDialogShow = true
} else {
Timber.d("onBackPressed")
onBack()
}
}
BackHandler(viewModel.isChangeRuleContent(detailRule), onBackPressed)
if (isLoading) LoadingBar()

if (isOutDialogShow) {
UpdateRuleOutDialog(
onConfirm = {
isOutDialogShow = false
onBack()
},
onDismiss = {
isOutDialogShow = false
}
)
}
UpdateRuleScreen(
rule = detailRule,
onBack = navController::popBackStack
ruleName = uiState.value.name,
description = uiState.value.description,
photos = uiState.value.photos,
changeName = viewModel::changeName,
changeDescription = viewModel::changeDescription,
updateRule = viewModel::updateRule,
deletePhoto = viewModel::deleteImage,
onBack = onBackPressed,
onOpenGallery = onOpenGallery
)
} ?: run {
Timber.e("DetailRuleUiModel is null")
Expand Down Expand Up @@ -146,7 +239,6 @@ private fun NavGraphBuilder.addRuleScreen(onBack: () -> Unit) {
when (event) {
is AddRuleSideEffect.IDLE -> Unit
is AddRuleSideEffect.DuplicateToast -> {
isLoading = false
ToastMessageUtil.showToast(
context,
context.getString(R.string.our_rule_duplicate_rule)
Expand All @@ -165,12 +257,10 @@ private fun NavGraphBuilder.addRuleScreen(onBack: () -> Unit) {
}

is AddRuleSideEffect.PopBackStack -> {
isLoading = false
onBack()
}

is AddRuleSideEffect.ShowLimitRuleCountDialog -> {
isLoading = false
isShowLimitedDialog = true
}
}
Expand Down Expand Up @@ -220,7 +310,6 @@ private fun NavGraphBuilder.representativeRuleScreen(onBack: () -> Unit) {
}
}
// Navigation

fun NavController.navigateToAddRule() {
navigate(RulesScreens.Add.route)
}
Expand All @@ -232,3 +321,7 @@ fun NavController.navigateUpdateRule(detailRuleUiModel: DetailRuleUiModel) {
)
navigate(RulesScreens.Update.route)
}

fun NavController.navigateToRepresentRule() {
navigate(RulesScreens.Represent.route)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package hous.release.android.presentation.our_rules.screen

import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import hous.release.android.R
Expand All @@ -20,7 +24,11 @@ fun AddRuleScreen(
onOpenGallery: () -> Unit = {},
onBack: () -> Unit = {}
) {
var isFocus by remember { mutableStateOf(true) }
val onDisposeFocus = { isFocus = false }
BasicUpdateRuleScreen(
isFocusOn = isFocus,
onDisposeFocus = onDisposeFocus,
title = stringResource(id = R.string.our_rule_add_new_rule_title),
trailingTitle = stringResource(id = R.string.our_rule_add_new_rule),
ruleName = ruleName,
Expand Down
Loading

0 comments on commit 9b82d8e

Please sign in to comment.