Skip to content

Commit

Permalink
✨ 날짜 선택 시 이벤트 생성 화면에 반영되도록 세팅
Browse files Browse the repository at this point in the history
  • Loading branch information
sooziini committed Sep 19, 2024
1 parent 651ee10 commit 33ede2f
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@ class EventCreationActivity : ComponentActivity() {
startDestination = EventCreationRoute.initRoute,
eventCreationState = state,
clearEventCreationState = eventCreationViewModel::clearEventCreationState,
updateDate = eventCreationViewModel::updateDate,
onGalleryButtonClicked = photoPicker::open,
onPictureDeleteButtonClicked = eventCreationViewModel::deletePicture,
onCompleteButtonClicked = { description ->
checkCreation(state.pictures, description)
},
onGalleryButtonClicked = photoPicker::open,
onPictureDeleteButtonClicked = eventCreationViewModel::deletePicture,
onBackButtonClicked = { finish() },
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package com.mashup.gabbangzip.sharedalbum.presentation.ui.eventcreation

import android.net.Uri
import com.mashup.gabbangzip.sharedalbum.presentation.utils.ImmutableList
import com.mashup.gabbangzip.sharedalbum.presentation.utils.LocalDateUtil

data class EventCreationState(
val date: String = LocalDateUtil.getNowDate(),
val date: Long? = null,
val pictures: ImmutableList<Uri?> = ImmutableList(emptyList()),
val eventCreationSuccess: Long? = null,
val isLoading: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.mashup.gabbangzip.sharedalbum.presentation.ui.eventcreation

import android.net.Uri
import android.util.Log
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.mashup.gabbangzip.sharedalbum.domain.usecase.event.CreateEventUseCase
import com.mashup.gabbangzip.sharedalbum.presentation.utils.ImmutableList
import com.mashup.gabbangzip.sharedalbum.presentation.utils.LocalDateUtil
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -16,7 +16,6 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import java.io.File
import java.time.LocalDateTime
import javax.inject.Inject

@HiltViewModel
Expand All @@ -33,16 +32,23 @@ class EventCreationViewModel @Inject constructor(
private val _eventFlow: MutableSharedFlow<EventCreationEvent> = MutableSharedFlow()
val eventFlow = _eventFlow.asSharedFlow()

fun updateDate(date: Long) {
viewModelScope.launch {
_uiState.update { it.copy(date = date) }
}
}

fun updatePictures(uriList: List<Uri?>) {
viewModelScope.launch {
val currentList = uiState.value.pictures
val uriListNotNull = uriList.filterNotNull()
val addedList = if (currentList.size + uriListNotNull.size > EventCreationActivity.PICTURES_MAX_COUNT) {
_eventFlow.emit(EventCreationEvent.OverflowImageError)
uriListNotNull.take(EventCreationActivity.PICTURES_MAX_COUNT - currentList.size)
} else {
uriListNotNull
}
val addedList =
if (currentList.size + uriListNotNull.size > EventCreationActivity.PICTURES_MAX_COUNT) {
_eventFlow.emit(EventCreationEvent.OverflowImageError)
uriListNotNull.take(EventCreationActivity.PICTURES_MAX_COUNT - currentList.size)
} else {
uriListNotNull
}
_uiState.update { it.copy(pictures = ImmutableList(currentList + addedList)) }
}
}
Expand Down Expand Up @@ -81,21 +87,21 @@ class EventCreationViewModel @Inject constructor(
createEventUseCase(
groupId = groupId,
description = description,
date = LocalDateTime.now().toString(),
date = uiState.value.date?.let {
LocalDateUtil.parseLongToLocalDateTime(it).toString()
} ?: "",
fileList = fileList,
).onSuccess {
Log.d(TAG, "이벤트 생성 성공")
_uiState.update { it.copy(eventCreationSuccess = groupId) }
updateLoadingState(isLoading = false)
}.onFailure {
Log.d(TAG, "이벤트 생성 실패")
showErrorSnackBar()
updateLoadingState(isLoading = false)
}
}
}

fun updateLoadingState(isLoading: Boolean) {
private fun updateLoadingState(isLoading: Boolean) {
_uiState.update { it.copy(isLoading = isLoading) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ import com.mashup.gabbangzip.sharedalbum.presentation.theme.PicTypography
import com.mashup.gabbangzip.sharedalbum.presentation.theme.SharedAlbumTheme
import com.mashup.gabbangzip.sharedalbum.presentation.ui.common.PicBackButtonTopBar
import com.mashup.gabbangzip.sharedalbum.presentation.ui.common.PicButton
import com.mashup.gabbangzip.sharedalbum.presentation.ui.common.PicDatePickerDialog
import com.mashup.gabbangzip.sharedalbum.presentation.ui.common.PicDatePickerField
import com.mashup.gabbangzip.sharedalbum.presentation.ui.common.PicDialog
import com.mashup.gabbangzip.sharedalbum.presentation.ui.common.PicGallery
import com.mashup.gabbangzip.sharedalbum.presentation.ui.common.PicLoadingIndicator
import com.mashup.gabbangzip.sharedalbum.presentation.ui.common.PicTextField
import com.mashup.gabbangzip.sharedalbum.presentation.ui.eventcreation.EventCreationActivity.Companion.PICTURES_MAX_COUNT
import com.mashup.gabbangzip.sharedalbum.presentation.ui.eventcreation.EventCreationState
import com.mashup.gabbangzip.sharedalbum.presentation.utils.LocalDateUtil
import com.mashup.gabbangzip.sharedalbum.presentation.utils.StableImage
import com.mashup.gabbangzip.sharedalbum.presentation.utils.hideKeyboardOnOutsideClicked
import com.mashup.gabbangzip.sharedalbum.presentation.utils.noRippleClickable
Expand All @@ -54,33 +56,50 @@ import com.mashup.gabbangzip.sharedalbum.presentation.utils.noRippleClickable
@Composable
fun EventCreationDetailScreen(
state: EventCreationState,
updateDate: (Long) -> Unit,
onCompleteButtonClicked: (String) -> Unit,
onGalleryButtonClicked: () -> Unit,
onPictureDeleteButtonClicked: (Int) -> Unit,
onDismissButtonClicked: () -> Unit,
) {
val focusManager = LocalFocusManager.current
var showDialog by remember { mutableStateOf(false) }

var showExitDialog by remember { mutableStateOf(false) }
var showDatePickerDialog by remember { mutableStateOf(false) }

var summary by remember { mutableStateOf("") }
val buttonEnabled by rememberUpdatedState(summary.isNotBlank() && state.pictures.size >= PICTURES_MAX_COUNT)
val buttonEnabled by rememberUpdatedState(
summary.isNotBlank() && state.date != null && state.pictures.size >= PICTURES_MAX_COUNT,
)

if (showDialog) {
if (showExitDialog) {
PicDialog(
titleText = stringResource(R.string.event_creation_dialog_title),
contentText = stringResource(R.string.event_creation_dialog_desc),
dismissText = stringResource(R.string.event_creation_dialog_dismiss),
confirmText = stringResource(R.string.event_creation_dialog_confirm),
onDismiss = {
showDialog = false
showExitDialog = false
onDismissButtonClicked()
},
onConfirm = { showDialog = false },
onConfirm = { showExitDialog = false },
onDismissRequest = {},
)
}

if (showDatePickerDialog) {
PicDatePickerDialog(
date = state.date,
onClickedDismiss = { showDatePickerDialog = false },
onClickedConfirm = {
updateDate(it)
showDatePickerDialog = false
},
)
}

BackHandler(true) {
showDialog = true
showExitDialog = true
}

Column(
Expand All @@ -93,7 +112,7 @@ fun EventCreationDetailScreen(
titleText = stringResource(id = R.string.event_creation),
backButtonClicked = {
focusManager.clearFocus()
showDialog = true
showExitDialog = true
},
)
Column(
Expand All @@ -117,8 +136,10 @@ fun EventCreationDetailScreen(
modifier = Modifier
.fillMaxWidth()
.padding(top = 16.dp, bottom = 24.dp),
date = state.date,
textColor = Gray60,
date = state.date?.let {
LocalDateUtil.format(LocalDateUtil.parseLongToLocalDateTime(it), "yy/MM/dd")
} ?: "",
onClicked = { showDatePickerDialog = true },
)
EventCreationTitle(stringResource(id = R.string.picture_select))
}
Expand Down Expand Up @@ -209,6 +230,7 @@ private fun EventCreationScreenPreview() {
SharedAlbumTheme {
EventCreationDetailScreen(
state = EventCreationState(),
updateDate = {},
onCompleteButtonClicked = {},
onGalleryButtonClicked = {},
onPictureDeleteButtonClicked = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fun NavController.navigateToEventCreationDetail() {

fun NavGraphBuilder.eventCreationDetailNavGraph(
eventCreationState: EventCreationState,
updateDate: (Long) -> Unit,
onCompleteButtonClicked: (String) -> Unit,
onGalleryButtonClicked: () -> Unit,
onPictureDeleteButtonClicked: (Int) -> Unit,
Expand All @@ -21,6 +22,7 @@ fun NavGraphBuilder.eventCreationDetailNavGraph(
composable(route = EventCreationRoute.DetailScreenRoute.route) {
EventCreationDetailScreen(
state = eventCreationState,
updateDate = updateDate,
onCompleteButtonClicked = onCompleteButtonClicked,
onGalleryButtonClicked = onGalleryButtonClicked,
onPictureDeleteButtonClicked = onPictureDeleteButtonClicked,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ fun EventCreationNavHost(
startDestination: String,
eventCreationState: EventCreationState,
clearEventCreationState: () -> Unit,
updateDate: (Long) -> Unit,
onCompleteButtonClicked: (String) -> Unit,
onGalleryButtonClicked: () -> Unit,
onPictureDeleteButtonClicked: (Int) -> Unit,
Expand All @@ -26,6 +27,7 @@ fun EventCreationNavHost(
) {
eventCreationDetailNavGraph(
eventCreationState = eventCreationState,
updateDate = updateDate,
onCompleteButtonClicked = onCompleteButtonClicked,
onGalleryButtonClicked = onGalleryButtonClicked,
onPictureDeleteButtonClicked = onPictureDeleteButtonClicked,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
package com.mashup.gabbangzip.sharedalbum.presentation.utils

import java.time.LocalDate
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.util.Locale

object LocalDateUtil {
fun getNowDate(): String {
val formatter = DateTimeFormatter.ofPattern("yy/MM/dd")
return LocalDate.now().format(formatter)
}

fun format(date: LocalDateTime, pattern: String = "yyyy.MM.dd"): String {
return date
.atZone(ZoneId.of("Asia/Seoul"))
.format(DateTimeFormatter.ofPattern(pattern, Locale.KOREAN))
}

fun getTimeMillis(date: LocalDateTime?): Long {
return date?.atZone(ZoneId.of("Asia/Seoul"))?.toInstant()?.toEpochMilli()
?: System.currentTimeMillis()
fun parseLongToLocalDateTime(date: Long): LocalDateTime {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneId.of("Asia/Seoul"))
}
}

0 comments on commit 33ede2f

Please sign in to comment.