Skip to content

Commit

Permalink
[FEATURE] #105 : Event 삭제 로직 장착 및 EventEdit으로 이동 시 Date 파라미터 안받도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
tgyuuAn committed Jan 17, 2024
1 parent ead644a commit bb3da67
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,13 @@ import com.wap.designsystem.component.WappSubTopBar
import com.wap.wapp.core.commmon.extensions.toSupportingText
import com.wap.wapp.feature.management.event.R
import com.wap.wapp.feature.management.event.registration.EventRegistrationContent
import com.wap.wapp.feature.management.event.registration.EventRegistrationEvent
import com.wap.wapp.feature.management.event.registration.EventRegistrationState
import kotlinx.coroutines.flow.collectLatest
import java.time.LocalDate
import java.time.LocalTime

@Composable
internal fun EventEditRoute(
date: String,
eventId: String,
navigateToManagement: () -> Unit,
viewModel: EventEditViewModel = hiltViewModel(),
Expand Down Expand Up @@ -72,17 +70,17 @@ internal fun EventEditRoute(

viewModel.eventEditEvent.collectLatest {
when (it) {
is EventRegistrationEvent.Failure -> {
is EventEditViewModel.EventEditEvent.Failure ->
snackBarHostState.showSnackbar(it.error.toSupportingText())
}

is EventRegistrationEvent.ValidationError -> {
is EventEditViewModel.EventEditEvent.ValidationError ->
snackBarHostState.showSnackbar(it.message)
}

is EventRegistrationEvent.Success -> {
is EventEditViewModel.EventEditEvent.EditSuccess ->
navigateToManagement()

is EventEditViewModel.EventEditEvent.DeleteSuccess ->
navigateToManagement()
}
}
}
}
Expand All @@ -107,6 +105,7 @@ internal fun EventEditRoute(
onNextButtonClicked = onNextButtonClicked,
onEditButtonClicked = onRegisterButtonClicked,
onBackButtonClicked = navigateToManagement,
deleteEvent = viewModel::deleteEvent,
)
}

Expand All @@ -132,6 +131,7 @@ internal fun EventEditScreen(
onNextButtonClicked: () -> Unit,
onEditButtonClicked: () -> Unit,
onBackButtonClicked: () -> Unit,
deleteEvent: () -> Unit,
) {
var showStartDatePicker by remember { mutableStateOf(false) }
var showStartTimePicker by remember { mutableStateOf(false) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.wap.wapp.core.commmon.util.DateUtil
import com.wap.wapp.core.domain.usecase.event.DeleteEventUseCase
import com.wap.wapp.core.domain.usecase.event.GetEventUseCase
import com.wap.wapp.core.domain.usecase.event.UpdateEventUseCase
import com.wap.wapp.feature.management.event.registration.EventRegistrationEvent
import com.wap.wapp.feature.management.event.registration.EventRegistrationState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
Expand All @@ -28,7 +27,7 @@ class EventEditViewModel @Inject constructor(
MutableStateFlow(EventRegistrationState.EVENT_DETAILS)
val currentEditState = _currentEditState.asStateFlow()

private val _eventEditEvent: MutableSharedFlow<EventRegistrationEvent> =
private val _eventEditEvent: MutableSharedFlow<EventEditEvent> =
MutableSharedFlow()
val eventEditEvent = _eventEditEvent.asSharedFlow()

Expand Down Expand Up @@ -59,15 +58,25 @@ class EventEditViewModel @Inject constructor(

private val _eventId: MutableStateFlow<String> = MutableStateFlow("")

fun setEventTitle(eventTitle: String) { _eventTitle.value = eventTitle }
fun setEventTitle(eventTitle: String) {
_eventTitle.value = eventTitle
}

fun setEventContent(eventContent: String) { _eventContent.value = eventContent }
fun setEventContent(eventContent: String) {
_eventContent.value = eventContent
}

fun setEventLocation(eventLocation: String) { _eventLocation.value = eventLocation }
fun setEventLocation(eventLocation: String) {
_eventLocation.value = eventLocation
}

fun setEventStartDate(eventDate: LocalDate) { _eventStartDate.value = eventDate }
fun setEventStartDate(eventDate: LocalDate) {
_eventStartDate.value = eventDate
}

fun setEventStartTime(eventTime: LocalTime) { _eventStartTime.value = eventTime }
fun setEventStartTime(eventTime: LocalTime) {
_eventStartTime.value = eventTime
}

fun setEventEndDate(eventDate: LocalDate) {
if (!isValidEndDate(eventDate)) {
Expand Down Expand Up @@ -116,13 +125,21 @@ class EventEditViewModel @Inject constructor(
eventEndTime = _eventEndTime.value,
eventId = _eventId.value,
).onSuccess {
_eventEditEvent.emit(EventRegistrationEvent.Success)
_eventEditEvent.emit(EventEditEvent.EditSuccess)
}.onFailure { throwable ->
_eventEditEvent.emit(EventRegistrationEvent.Failure(throwable))
_eventEditEvent.emit(EventEditEvent.Failure(throwable))
}
}
}

fun deleteEvent() = viewModelScope.launch {
deleteEventUseCase(_eventId.value).onSuccess {
_eventEditEvent.emit(EventEditEvent.DeleteSuccess)
}.onFailure { throwable ->
_eventEditEvent.emit(EventEditEvent.Failure(throwable))
}
}

private fun isValidEndTime(eventTime: LocalTime): Boolean =
_eventEndDate.value == _eventStartDate.value && eventTime > _eventStartTime.value

Expand All @@ -148,11 +165,13 @@ class EventEditViewModel @Inject constructor(
.onFailure { emitValidationErrorMessage("이벤트를 불러오는데 실패하였습니다.") }
}

private fun emitValidationErrorMessage(message: String) {
viewModelScope.launch {
_eventEditEvent.emit(
EventRegistrationEvent.ValidationError(message),
)
}
private fun emitValidationErrorMessage(message: String) =
viewModelScope.launch { _eventEditEvent.emit(EventEditEvent.ValidationError(message)) }

sealed class EventEditEvent {
data class ValidationError(val message: String) : EventEditEvent()
data class Failure(val error: Throwable) : EventEditEvent()
data object EditSuccess : EventEditEvent()
data object DeleteSuccess : EventEditEvent()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@ import com.wap.wapp.feature.management.event.edit.EventEditRoute
import com.wap.wapp.feature.management.event.registration.EventRegistrationRoute

const val eventRegistrationNavigationRoute = "event_registration_route"
const val eventEditNavigationRoute = "event/edit/{date}/{eventId}"
const val eventEditNavigationRoute = "event/edit/{eventId}"

fun NavController.navigateToEventRegistration(navOptions: NavOptions? = navOptions {}) {
this.navigate(eventRegistrationNavigationRoute, navOptions)
}

fun NavController.navigateToEventEdit(
date: String,
eventId: String,
navOptions: NavOptions? = navOptions {},
) {
this.navigate("event/edit/$date/$eventId", navOptions)
this.navigate("event/edit/$eventId", navOptions)
}

fun NavGraphBuilder.managementEventNavGraph(
Expand All @@ -36,15 +35,10 @@ fun NavGraphBuilder.managementEventNavGraph(

composable(
route = eventEditNavigationRoute,
arguments = listOf(
navArgument("date") { type = NavType.StringType },
navArgument("eventId") { type = NavType.StringType },
),
arguments = listOf(navArgument("eventId") { type = NavType.StringType }),
) { navBackStackEntry ->
val date = navBackStackEntry.arguments?.getString("date") ?: ""
val eventId = navBackStackEntry.arguments?.getString("eventId") ?: ""
EventEditRoute(
date = date,
eventId = eventId,
navigateToManagement = navigateToManagement,
)
Expand Down

0 comments on commit bb3da67

Please sign in to comment.