diff --git a/presentation/src/main/java/io/tuttut/presentation/ui/screen/main/diaryDetail/DiaryDetailScreen.kt b/presentation/src/main/java/io/tuttut/presentation/ui/screen/main/diaryDetail/DiaryDetailScreen.kt index b74abdb..2a2979f 100644 --- a/presentation/src/main/java/io/tuttut/presentation/ui/screen/main/diaryDetail/DiaryDetailScreen.kt +++ b/presentation/src/main/java/io/tuttut/presentation/ui/screen/main/diaryDetail/DiaryDetailScreen.kt @@ -47,6 +47,7 @@ import io.tuttut.presentation.ui.component.ReportBottomSheet import io.tuttut.presentation.ui.component.TutTutImage import io.tuttut.presentation.ui.component.TutTutLoadingScreen import io.tuttut.presentation.ui.component.TutTutTopBar +import io.tuttut.presentation.ui.state.IEditTextState import io.tuttut.presentation.util.clickableWithOutRipple import io.tuttut.presentation.util.getRelativeTime import io.tuttut.presentation.util.withScreenPadding @@ -62,33 +63,31 @@ fun DiaryDetailRoute( viewModel: DiaryDetailViewModel = hiltViewModel() ) { val uiState by viewModel.uiState.collectAsStateWithLifecycle() - val typedComment by viewModel.typedComment.collectAsStateWithLifecycle() val keyboardController = LocalSoftwareKeyboardController.current val focusManager = LocalFocusManager.current DiaryDetailScreen( modifier = modifier, uiState = uiState, - typedComment = typedComment, + commentState = viewModel.commentState, memberMap = viewModel.memberMap, - typeComment = viewModel::typeComment, onSend = { viewModel.onSend({ keyboardController?.hide() }, onShowSnackBar) }, onEdit = { viewModel.onEdit(moveEditDiary) }, - onDelete = { viewModel.showDeleteSheet = true }, - onReport = { viewModel.showReportSheet = true }, + onDelete = viewModel.deleteSheetState::show, + onReport = viewModel.reportSheetState::show, onDeleteComment = { viewModel.onDeleteComment(it, { focusManager.clearFocus() }, onShowSnackBar) }, onBack = onBack ) NegativeBottomSheet( - showSheet = viewModel.showDeleteSheet, + showSheet = viewModel.deleteSheetState.showSheet, scope = scope, onButton = { viewModel.onDelete(onBack, onShowSnackBar) }, - onDismissRequest = { viewModel.showDeleteSheet = false } + onDismissRequest = viewModel.deleteSheetState::dismiss ) ReportBottomSheet( - showSheet = viewModel.showReportSheet, + showSheet = viewModel.reportSheetState.showSheet, scope = scope, onSelectReportReason = { viewModel.onReport(it, onShowSnackBar) }, - onDismissRequest = { viewModel.showReportSheet = false } + onDismissRequest = viewModel.reportSheetState::dismiss ) BackHandler(onBack = onBack) } @@ -97,9 +96,8 @@ fun DiaryDetailRoute( internal fun DiaryDetailScreen( modifier: Modifier, uiState: DiaryDetailUiState, - typedComment: String, + commentState: IEditTextState, memberMap: HashMap, - typeComment: (String) -> Unit, onDeleteComment: (String) -> Unit, onSend: () -> Unit, onEdit: () -> Unit, @@ -173,9 +171,9 @@ internal fun DiaryDetailScreen( } } CommentArea( - typedComment = typedComment, + typedComment = commentState.typedText, user = uiState.currentUser, - typeComment = typeComment, + typeComment = commentState::typeText, onSend = onSend ) } diff --git a/presentation/src/main/java/io/tuttut/presentation/ui/screen/main/diaryDetail/DiaryDetailViewModel.kt b/presentation/src/main/java/io/tuttut/presentation/ui/screen/main/diaryDetail/DiaryDetailViewModel.kt index 5fb9d6c..62c754b 100644 --- a/presentation/src/main/java/io/tuttut/presentation/ui/screen/main/diaryDetail/DiaryDetailViewModel.kt +++ b/presentation/src/main/java/io/tuttut/presentation/ui/screen/main/diaryDetail/DiaryDetailViewModel.kt @@ -1,8 +1,5 @@ package io.tuttut.presentation.ui.screen.main.diaryDetail -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.setValue import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel import io.tuttut.data.model.dto.Comment @@ -15,9 +12,10 @@ import io.tuttut.data.repository.storage.StorageRepository import io.tuttut.presentation.base.BaseViewModel import io.tuttut.presentation.model.DiaryModel import io.tuttut.presentation.model.PreferenceUtil +import io.tuttut.presentation.ui.state.BottomSheetState +import io.tuttut.presentation.ui.state.EditTextState import io.tuttut.presentation.util.getCurrentDateTime import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.combine @@ -51,31 +49,23 @@ class DiaryDetailViewModel @Inject constructor( initialValue = DiaryDetailUiState.Loading ) - var showDeleteSheet by mutableStateOf(false) - var showReportSheet by mutableStateOf(false) - - private val _typedComment = MutableStateFlow("") - val typedComment: StateFlow = _typedComment - - fun typeComment(text: String) { - if (text.length < 200) { - _typedComment.value = text - } - } + val commentState = EditTextState(maxLength = 200) + val deleteSheetState = BottomSheetState() + val reportSheetState = BottomSheetState() fun onSend(hideKeyBoard: () -> Unit, onShowSnackBar: suspend (String, String?) -> Boolean) { - if (typedComment.value.trim().isEmpty()) return + if (commentState.typedText.trim().isEmpty()) return viewModelScope.launch { hideKeyBoard() val comment = Comment( authorId = pref.userId, created = getCurrentDateTime(), - content = typedComment.value.trim() + content = commentState.typedText.trim() ) commentRepo.addDiaryComment(pref.gardenId, diary.id, comment).collect { when(it) { is Result.Error -> onShowSnackBar("댓글 추가에 실패했어요", null) - is Result.Success -> _typedComment.value = "" + is Result.Success -> commentState.resetText() else -> {} } } @@ -108,7 +98,7 @@ class DiaryDetailViewModel @Inject constructor( fun onReport(reason: String, onShowSnackBar: suspend (String, String?) -> Boolean) { viewModelScope.launch { - showReportSheet = false + reportSheetState.dismiss() onShowSnackBar("${reason}로 신고했어요", null) } } diff --git a/presentation/src/main/java/io/tuttut/presentation/ui/state/BottomSheetState.kt b/presentation/src/main/java/io/tuttut/presentation/ui/state/BottomSheetState.kt new file mode 100644 index 0000000..050fa54 --- /dev/null +++ b/presentation/src/main/java/io/tuttut/presentation/ui/state/BottomSheetState.kt @@ -0,0 +1,23 @@ +package io.tuttut.presentation.ui.state + +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.setValue + +interface IBottomSheetState { + var showSheet: Boolean + fun show() + fun dismiss() +} + +class BottomSheetState : IBottomSheetState { + override var showSheet by mutableStateOf(false) + + override fun show() { + showSheet = true + } + + override fun dismiss() { + showSheet = false + } +} \ No newline at end of file diff --git a/presentation/src/main/java/io/tuttut/presentation/ui/state/DialogState.kt b/presentation/src/main/java/io/tuttut/presentation/ui/state/DialogState.kt index f17b325..2cce38c 100644 --- a/presentation/src/main/java/io/tuttut/presentation/ui/state/DialogState.kt +++ b/presentation/src/main/java/io/tuttut/presentation/ui/state/DialogState.kt @@ -13,7 +13,7 @@ interface IDialogState { open class DialogState( initState: Boolean = false -): IDialogState { +) : IDialogState { override var isOpen by mutableStateOf(initState) override fun show() {