Skip to content

Commit

Permalink
#29 refactor: 일지 상세 edittext, bottomSheet 상태 객체로 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
SeungWoo-Ahn committed May 9, 2024
1 parent b41a2a1 commit 2a3296f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -97,9 +96,8 @@ fun DiaryDetailRoute(
internal fun DiaryDetailScreen(
modifier: Modifier,
uiState: DiaryDetailUiState,
typedComment: String,
commentState: IEditTextState,
memberMap: HashMap<String, User>,
typeComment: (String) -> Unit,
onDeleteComment: (String) -> Unit,
onSend: () -> Unit,
onEdit: () -> Unit,
Expand Down Expand Up @@ -173,9 +171,9 @@ internal fun DiaryDetailScreen(
}
}
CommentArea(
typedComment = typedComment,
typedComment = commentState.typedText,
user = uiState.currentUser,
typeComment = typeComment,
typeComment = commentState::typeText,
onSend = onSend
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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<String> = _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 -> {}
}
}
Expand Down Expand Up @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface IDialogState {

open class DialogState(
initState: Boolean = false
): IDialogState {
) : IDialogState {
override var isOpen by mutableStateOf(initState)

override fun show() {
Expand Down

0 comments on commit 2a3296f

Please sign in to comment.