Skip to content

Commit

Permalink
[feature/#960] datapicker error (#961)
Browse files Browse the repository at this point in the history
* [feature/#960] 데이터 피커 오류 수정 및 스템프 삭제 오류 수정

* [feature/#960] apply spotlessApply
  • Loading branch information
chattymin authored Nov 10, 2024
1 parent 8bcf39a commit 9af0b90
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ package org.sopt.official.stamp.feature.mission.detail
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -44,6 +43,7 @@ import org.sopt.official.domain.soptamp.repository.StampRepository
import org.sopt.official.stamp.designsystem.component.toolbar.ToolbarIconType
import retrofit2.HttpException
import timber.log.Timber
import javax.inject.Inject

data class PostUiState(
val id: Int = -1,
Expand Down Expand Up @@ -88,7 +88,6 @@ class MissionDetailViewModel @Inject constructor(
val isSubmitEnabled = combine(content, imageModel, isMe) { content, image, isMe ->
content.isNotEmpty() && !image.isEmpty() && isMe
}
val isCompleted = uiState.map { it.isCompleted }
val toolbarIconType = uiState.map { it.toolbarIconType }
val isEditable = toolbarIconType.map {
it != ToolbarIconType.WRITE
Expand Down Expand Up @@ -210,97 +209,99 @@ class MissionDetailViewModel @Inject constructor(
}

private suspend fun handleSubmit() {
viewModelScope.launch {
val currentState = uiState.value
val (id, imageUri, content, date) = currentState
uiState.update {
it.copy(isError = false, error = null, isLoading = true)
}
val currentState = uiState.value
val (id, imageUri, content, date) = currentState
uiState.update {
it.copy(isError = false, error = null, isLoading = true)
}

val image = when (imageUri) {
ImageModel.Empty -> {
"ERROR"
}
val image = when (imageUri) {
ImageModel.Empty -> {
"ERROR"
}

is ImageModel.Local -> {
imageUri.uri[0]
}
is ImageModel.Local -> {
imageUri.uri[0]
}

is ImageModel.Remote -> {
imageUri.url[0]
}
is ImageModel.Remote -> {
imageUri.url[0]
}
}

if (imageUri is ImageModel.Remote) {
modifyMission(id, image, content, date)
} else {
imageUploaderRepository.getImageUploadURL().onSuccess { S3URL ->
val preSignedURL = S3URL.preSignedURL
val imageURL = S3URL.imageURL
if (imageUri is ImageModel.Remote) {
modifyMission(id, image, content, date)
} else {
imageUploaderRepository.getImageUploadURL().onSuccess { S3URL ->
val preSignedURL = S3URL.preSignedURL
val imageURL = S3URL.imageURL

runCatching {
imageUploaderRepository.uploadImage(
preSignedURL = preSignedURL,
imageUri = image
)
}.onFailure {
Timber.e(it.toString())
}
runCatching {
imageUploaderRepository.uploadImage(
preSignedURL = preSignedURL,
imageUri = image
)
}.onFailure {
Timber.e(it.toString())
}

if (uiState.value.isCompleted) {
modifyMission(id, imageURL, content, date)
} else {
stampRepository.completeMission(
Stamp(
missionId = id,
image = imageURL,
contents = content,
activityDate = date
)
).onSuccess {
uiState.update {
it.copy(isLoading = false, isSuccess = true)
}
}.onFailure { error ->
uiState.update {
it.copy(isLoading = false, isError = true, error = error, isSuccess = false)
}
}
}
}.onFailure { error ->
uiState.update {
it.copy(isLoading = false, isError = true, error = error, isSuccess = false)
}
if (uiState.value.isCompleted) {
modifyMission(id, imageURL, content, date)
} else {
completeMission(id, imageURL, content, date)
}
}.onFailure { error ->
uiState.update {
it.copy(isLoading = false, isError = true, error = error, isSuccess = false)
}
}
}
}

private suspend fun modifyMission(id: Int, image: String, content: String, date: String) = stampRepository.modifyMission(
Stamp(
missionId = id,
image = image,
contents = content,
activityDate = date
)
).onSuccess {
uiState.update {
it.copy(isLoading = false, isSuccess = true)
private suspend fun modifyMission(id: Int, image: String, content: String, date: String) {
stampRepository.modifyMission(
Stamp(
missionId = id,
image = image,
contents = content,
activityDate = date
)
).onSuccess {
uiState.update {
it.copy(isLoading = false, isSuccess = true)
}
}.onFailure { error ->
uiState.update {
it.copy(isLoading = false, isError = true, error = error, isSuccess = false)
}
}
}.onFailure { error ->
uiState.update {
it.copy(isLoading = false, isError = true, error = error, isSuccess = false)
}

private suspend fun completeMission(id: Int, image: String, content: String, date: String) {
stampRepository.completeMission(
Stamp(
missionId = id,
image = image,
contents = content,
activityDate = date
)
).onSuccess {
uiState.update {
it.copy(isLoading = false, isSuccess = true)
}
}.onFailure { error ->
uiState.update {
it.copy(isLoading = false, isError = true, error = error, isSuccess = false)
}
}
}

fun onDelete() {
viewModelScope.launch {
val currentState = uiState.value
val (id) = currentState
uiState.update {
it.copy(isError = false, error = null, isLoading = true)
}
stampRepository.deleteMission(id)
stampRepository.deleteMission(uiState.value.stampId)
.onSuccess {
uiState.update {
it.copy(isLoading = false, isDeleteSuccess = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList
import okhttp3.internal.immutableListOf
Expand All @@ -78,6 +75,9 @@ import org.sopt.official.stamp.designsystem.style.SoptTheme
import org.sopt.official.stamp.designsystem.style.White
import org.sopt.official.stamp.feature.ranking.getLevelTextColor
import org.sopt.official.stamp.util.DefaultPreview
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale

@Composable
fun DatePicker(
Expand Down Expand Up @@ -149,14 +149,15 @@ fun DataPickerBottomSheet(onSelected: (String) -> Unit, onDismissRequest: () ->

val formatter = SimpleDateFormat("yyyy.MM.dd", Locale.KOREA)
var chosenYear by remember { mutableIntStateOf(currentYear) }
var chosenMonth by remember { mutableIntStateOf(currentMonth) }
var chosenMonth by remember { mutableIntStateOf(currentMonth + 1) }
var chosenDay by remember { mutableIntStateOf(currentDay) }

val isValidDate by remember { derivedStateOf { calculateValidDate(chosenYear, chosenMonth, chosenDay) } }

ModalBottomSheet(
onDismissRequest = onDismissRequest,
sheetState = sheetState
sheetState = sheetState,
containerColor = White
) {
DatePickerUI(
isValidDate = isValidDate,
Expand Down Expand Up @@ -245,16 +246,16 @@ fun DateSelectionSection(
) {
DateItemsPicker(
isValidDate = isValidDate,
max = currentYear - 1950,
items = years.toImmutableList(),
max = YEAR_INDEX,
items = years,
firstIndex = (chosenYear - START_YEAR),
onItemSelected = onYearChosen
)
Spacer(modifier = Modifier.width(10.dp))
DateItemsPicker(
isValidDate = isValidDate,
max = currentMonth,
items = monthsNumber.toImmutableList(),
items = monthsNumber,
firstIndex = chosenMonth,
onItemSelected = onMonthChosen
)
Expand All @@ -263,10 +264,10 @@ fun DateSelectionSection(
isValidDate = isValidDate,
max = currentDay - 1,
items = when {
(chosenYear % 4 == 0) && chosenMonth == 2 -> days29.toImmutableList()
chosenMonth == 2 -> days28.toImmutableList()
days30Months.contains(chosenMonth) -> days30.toImmutableList()
else -> days31.toImmutableList()
(chosenYear % 4 == 0) && chosenMonth == 2 -> days29
chosenMonth == 2 -> days28
days30Months.contains(chosenMonth) -> days30
else -> days31
},
firstIndex = chosenDay - 1,
onItemSelected = onDayChosen
Expand All @@ -281,25 +282,27 @@ fun DateItemsPicker(
items: ImmutableList<String>,
firstIndex: Int,
onItemSelected: (String) -> Unit,
modifier: Modifier = Modifier
modifier: Modifier = Modifier,
) {
val listState = rememberLazyListState(firstIndex)
val currentValue = remember { mutableStateOf("") }

LaunchedEffect(!listState.isScrollInProgress, isValidDate) {
if (isValidDate) {
onItemSelected(currentValue.value)
listState.animateScrollToItem(index = listState.firstVisibleItemIndex)
} else {
if (max < currentValue.value.toInt()) {
if (currentValue.value.isNotEmpty()) {
onItemSelected(currentValue.value)
listState.animateScrollToItem(index = listState.firstVisibleItemIndex)
}
} else { // 오늘 이후의 날짜 선택시
if (currentValue.value.isNotEmpty() && max < currentValue.value.toInt()) {
listState.animateScrollToItem(index = max)
onItemSelected(currentValue.value)
}
}
}

Box(
modifier = Modifier.height(108.dp),
modifier = modifier.height(108.dp),
contentAlignment = Alignment.Center
) {
Column(
Expand All @@ -316,7 +319,7 @@ fun DateItemsPicker(
)
}
LazyColumn(
modifier = modifier,
modifier = Modifier,
horizontalAlignment = Alignment.CenterHorizontally,
state = listState,
) {
Expand Down Expand Up @@ -348,14 +351,15 @@ private val currentYear = Calendar.getInstance().get(Calendar.YEAR)
private val currentMonth = Calendar.getInstance().get(Calendar.MONTH)
private val currentDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH)

private const val START_YEAR = 1950
private const val END_YEAR = 2100
private const val YEAR_INDEX = 50 // 현재 년도의 +- 50년까지 Date Picker에 표시
private val START_YEAR = currentYear - YEAR_INDEX
private val END_YEAR = currentYear + YEAR_INDEX
private val years = (listOf("") + (START_YEAR..END_YEAR).map { it.toString() } + listOf("")).toImmutableList()
private val monthsNumber = (listOf("") + (1..12).map { it.toString() } + listOf("")).toImmutableList()
private val days28 = (listOf("") + (1..28).map { it.toString() } + listOf("")).toImmutableList()
private val days29 = (listOf("") + (1..29).map { it.toString() } + listOf("")).toImmutableList()
private val days30 = (listOf("") + (1..30).map { it.toString() } + listOf("")).toImmutableList() // 4,6,9,11
private val days31 = (listOf("") + (1..31).map { it.toString() } + listOf("")).toImmutableList() // 1,3,5,7,8,10.12
private val days28 = (listOf("") + (1..28).map { it.toString() } + listOf("")).toImmutableList() // 해당 월 : 2
private val days29 = (listOf("") + (1..29).map { it.toString() } + listOf("")).toImmutableList() // 해당 월 : 윤년 2월
private val days30 = (listOf("") + (1..30).map { it.toString() } + listOf("")).toImmutableList() // 해당 월 : 4,6,9,11
private val days31 = (listOf("") + (1..31).map { it.toString() } + listOf("")).toImmutableList() // 해당 월 : 1,3,5,7,8,10.12
private val days30Months = immutableListOf(4, 6, 9, 11)

@DefaultPreview
Expand Down

0 comments on commit 9af0b90

Please sign in to comment.