Skip to content

Commit

Permalink
Merge pull request #79 from YAPP-Github/feature/MZ-182-ledger-add
Browse files Browse the repository at this point in the history
Feature/mz 182 ledger add
  • Loading branch information
jinukeu authored Jan 21, 2024
2 parents 5e2e1bc + a9a7261 commit 688ae13
Show file tree
Hide file tree
Showing 33 changed files with 860 additions and 210 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ fun SusuDatePickerBottomSheet(
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SusuLimitDatePickerBottomSheet(
criteriaYear: Int,
criteriaMonth: Int,
criteriaDay: Int,
initialCriteriaYear: Int? = null,
initialCriteriaMonth: Int? = null,
initialCriteriaDay: Int? = null,
initialYear: Int? = null,
initialMonth: Int? = null,
initialDay: Int? = null,
Expand All @@ -130,6 +130,12 @@ fun SusuLimitDatePickerBottomSheet(
onDismissRequest: (Int, Int, Int) -> Unit = { _, _, _ -> },
onItemSelected: (Int, Int, Int) -> Unit = { _, _, _ -> },
) {
val (criteriaYear, criteriaMonth, criteriaDay) = listOf(
initialCriteriaYear ?: 2030,
initialCriteriaMonth ?: 12,
initialCriteriaDay ?: 31,
)

var selectedYear by remember {
mutableIntStateOf(
when {
Expand Down Expand Up @@ -273,6 +279,7 @@ fun SusuLimitDatePickerBottomSheet(
)
} else {
selectedDay = criteriaDay
onItemSelected(selectedYear, selectedMonth, selectedDay)
Box(
modifier = Modifier
.width(100.dp)
Expand Down Expand Up @@ -348,9 +355,9 @@ private fun getLastDayInMonth(year: Int, month: Int): Int {
fun SusuLimitDatePickerBottomSheetPreview() {
SusuTheme {
SusuLimitDatePickerBottomSheet(
criteriaYear = 2024,
criteriaMonth = 2,
criteriaDay = 16,
initialCriteriaYear = 2024,
initialCriteriaMonth = 2,
initialCriteriaDay = 16,
initialYear = 2024,
initialMonth = 2,
initialDay = 20,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ fun BasicButton(
rightIcon: (@Composable () -> Unit)? = null,
iconSpacing: Dp = 0.dp,
isClickable: Boolean = true,
rippleEnabled: Boolean = true,
onClick: () -> Unit = {},
) {
Box(
Expand All @@ -52,6 +53,7 @@ fun BasicButton(
shape = shape,
)
.susuClickable(
rippleEnabled = rippleEnabled,
rippleColor = rippleColor,
runIf = isClickable,
onClick = onClick,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ fun SusuGhostButton(
rightIcon: (@Composable () -> Unit)? = null,
isActive: Boolean = true,
isClickable: Boolean = true,
rippleEnabled: Boolean = true,
onClick: () -> Unit = {},
) {
val (paddingValues, iconSpacing, textStyle) = style()
Expand All @@ -73,6 +74,7 @@ fun SusuGhostButton(
padding = paddingValues,
iconSpacing = iconSpacing,
isClickable = isClickable,
rippleEnabled = rippleEnabled,
onClick = onClick,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ fun SusuTextFieldFillMaxButton(
onClickCloseIcon: () -> Unit = {},
onClickFilledButton: () -> Unit = {},
onClickButton: (isFocused: Boolean) -> Unit = {},
focusRequester: FocusRequester = remember { FocusRequester() },
) {
val (backgroundColor, textColor) = with(color) {
when {
Expand All @@ -88,7 +89,8 @@ fun SusuTextFieldFillMaxButton(
BasicTextField(
modifier = modifier
.fillMaxWidth()
.susuClickable { onClickButton(isFocused) },
.susuClickable { onClickButton(isFocused) }
.focusRequester(focusRequester),
value = text,
onValueChange = onTextChange,
enabled = isSaved.not() && isFocused,
Expand Down
12 changes: 11 additions & 1 deletion core/ui/src/main/java/com/susu/core/ui/util/Date.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package com.susu.core.ui.util

import android.util.Log
import java.time.DateTimeException
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

val currentDate = LocalDateTime.now()
val currentDate: LocalDateTime = LocalDateTime.now()
val minDate: LocalDateTime = LocalDateTime.of(1930, 1, 1, 0, 0)

@Suppress("detekt:FunctionNaming")
fun LocalDateTime.to_yyyy_dot_MM_dot_dd(): String {
val formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd")
return this.format(formatter)
}

fun getSafeLocalDateTime(year: Int, month: Int, day: Int): LocalDateTime = try {
LocalDateTime.of(year, month, day, 0, 0)
} catch (e: DateTimeException) {
Log.e("DateTimeError", "Invalid date provided: $year-$month-$day", e)
LocalDateTime.of(year, month, 1, 0, 0)
}
1 change: 1 addition & 0 deletions core/ui/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<string name="word_next">다음</string>
<string name="word_done">완료</string>
<string name="word_apply_filter">필터 적용하기</string>
<string name="word_input_placeholder">입력해주세요</string>
<string name="sns_kakao">카카오톡</string>
<string name="sns_naver">네이버</string>
<string name="sns_google">구글</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class LedgerRepositoryImpl @Inject constructor(
sort = sort,
).getOrThrow().toModel()

override suspend fun createLedger(ledger: Ledger): Ledger = ledgerService.createLedger(
ledgerRequest = ledger.toData(),
).getOrThrow().toModel()

override suspend fun editLedger(ledger: Ledger): Ledger = ledgerService.editLedger(
id = ledger.id,
ledgerRequest = ledger.toData(),
Expand Down
6 changes: 6 additions & 0 deletions data/src/main/java/com/susu/data/remote/api/LedgerService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import retrofit2.http.Body
import retrofit2.http.DELETE
import retrofit2.http.GET
import retrofit2.http.PATCH
import retrofit2.http.POST
import retrofit2.http.Path
import retrofit2.http.Query

Expand All @@ -29,6 +30,11 @@ interface LedgerService {
@Body ledgerRequest: LedgerRequest,
): ApiResult<LedgerResponse>

@POST("ledgers")
suspend fun createLedger(
@Body ledgerRequest: LedgerRequest,
): ApiResult<LedgerResponse>

@DELETE("ledgers")
suspend fun deleteLedgerList(@Query("ids") idList: List<Int>): ApiResult<Unit>
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ interface LedgerRepository {
sort: String?,
): List<Ledger>

suspend fun createLedger(
ledger: Ledger,
): Ledger

suspend fun editLedger(
ledger: Ledger,
): Ledger
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.susu.domain.usecase.ledger

import com.susu.core.common.runCatchingIgnoreCancelled
import com.susu.core.model.Ledger
import com.susu.domain.repository.LedgerRepository
import javax.inject.Inject

class CreateLedgerUseCase @Inject constructor(
private val ledgerRepository: LedgerRepository,
) {
suspend operator fun invoke(ledger: Ledger) = runCatchingIgnoreCancelled {
ledgerRepository.createLedger(ledger)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.susu.feature.received.ledgeradd

import com.susu.core.ui.base.SideEffect
import com.susu.core.ui.base.UiState

data class LedgerAddState(
val currentStep: LedgerAddStep = LedgerAddStep.CATEGORY,
val buttonEnabled: Boolean = false,
val isLoading: Boolean = false,
) : UiState

enum class LedgerAddStep {
CATEGORY,
NAME,
DATE,
}

sealed interface LedgerAddSideEffect : SideEffect {
data object PopBackStack : LedgerAddSideEffect
data class PopBackStackWithLedger(val ledger: String) : LedgerAddSideEffect
data class HandleException(val throwable: Throwable, val retry: () -> Unit) : LedgerAddSideEffect
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.susu.feature.received.ledgeradd

import androidx.activity.compose.BackHandler
import androidx.compose.animation.AnimatedContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
Expand All @@ -16,51 +17,82 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.susu.core.designsystem.component.appbar.SusuProgressAppBar
import com.susu.core.designsystem.component.appbar.icon.BackIcon
import com.susu.core.designsystem.component.button.FilledButtonColor
import com.susu.core.designsystem.component.button.MediumButtonStyle
import com.susu.core.designsystem.component.button.SusuFilledButton
import com.susu.core.designsystem.component.screen.LoadingScreen
import com.susu.core.designsystem.theme.SusuTheme
import com.susu.core.model.Category
import com.susu.core.ui.R
import com.susu.core.ui.extension.collectWithLifecycle
import com.susu.core.ui.extension.susuDefaultAnimatedContentTransitionSpec
import com.susu.feature.received.ledgeradd.content.CategoryContent
import com.susu.feature.received.ledgeradd.content.DateContent
import com.susu.feature.received.ledgeradd.content.NameContent

enum class LedgerAddStep {
CATEGORY,
NAME,
DATE,
}
import com.susu.feature.received.ledgeradd.content.category.CategoryContentRoute
import com.susu.feature.received.ledgeradd.content.date.DateContentRoute
import com.susu.feature.received.ledgeradd.content.name.NameContentRoute
import java.time.LocalDateTime

@Composable
fun LedgerAddRoute(
viewModel: LedgerAddViewModel = hiltViewModel(),
popBackStack: () -> Unit,
popBackStackWithLedger: (String) -> Unit,
handleException: (Throwable, () -> Unit) -> Unit,
) {
var currentStep by remember {
mutableStateOf(LedgerAddStep.CATEGORY)
val uiState = viewModel.uiState.collectAsStateWithLifecycle().value
viewModel.sideEffect.collectWithLifecycle { sideEffect ->
when (sideEffect) {
LedgerAddSideEffect.PopBackStack -> popBackStack()
is LedgerAddSideEffect.HandleException -> handleException(sideEffect.throwable, sideEffect.retry)
is LedgerAddSideEffect.PopBackStackWithLedger -> popBackStackWithLedger(sideEffect.ledger)
}
}

var dateContentCategoryName: String by remember {
mutableStateOf("")
}

var dateContentName: String by remember {
mutableStateOf("")
}

BackHandler {
viewModel.goToPrevStep()
}

LedgerAddScreen(
currentStep = currentStep,
onClickBack = popBackStack,
onClickNextButton = {
// TODO 임시 코드 입니다.
currentStep = when (currentStep) {
LedgerAddStep.CATEGORY -> LedgerAddStep.NAME
LedgerAddStep.NAME -> LedgerAddStep.DATE
LedgerAddStep.DATE -> LedgerAddStep.DATE
}
uiState = uiState,
onClickBack = viewModel::goToPrevStep,
onClickNextButton = viewModel::goToNextStep,
updateParentSelectedCategory = { category ->
viewModel.updateSelectedCategory(category)
dateContentCategoryName = category?.customCategory ?: category?.name ?: ""
},
updateParentName = { name ->
viewModel.updateName(name)
dateContentName = name
},
dateContentName = dateContentName,
dateContentCategoryName = dateContentCategoryName,
updateParentDate = { startAt, endAt ->
viewModel.updateDate(startAt, endAt)
},
)
}

@Composable
fun LedgerAddScreen(
currentStep: LedgerAddStep = LedgerAddStep.CATEGORY,
uiState: LedgerAddState = LedgerAddState(),
onClickBack: () -> Unit = {},
onClickNextButton: () -> Unit = {},
updateParentSelectedCategory: (Category?) -> Unit = {},
updateParentName: (String) -> Unit = {},
dateContentCategoryName: String = "",
dateContentName: String = "",
updateParentDate: (LocalDateTime?, LocalDateTime?) -> Unit = { _, _ -> },
) {
Box(
modifier = Modifier
Expand All @@ -73,12 +105,12 @@ fun LedgerAddScreen(
BackIcon(onClickBack)
},
entireStep = LedgerAddStep.entries.size,
currentStep = currentStep.ordinal + 1,
currentStep = uiState.currentStep.ordinal + 1,
)

AnimatedContent(
modifier = Modifier.weight(1f),
targetState = currentStep,
targetState = uiState.currentStep,
label = "LedgerAddScreen",
transitionSpec = {
susuDefaultAnimatedContentTransitionSpec(
Expand All @@ -87,9 +119,19 @@ fun LedgerAddScreen(
},
) { targetState ->
when (targetState) {
LedgerAddStep.CATEGORY -> CategoryContent()
LedgerAddStep.NAME -> NameContent()
LedgerAddStep.DATE -> DateContent()
LedgerAddStep.CATEGORY -> CategoryContentRoute(
updateParentSelectedCategory = updateParentSelectedCategory,
)

LedgerAddStep.NAME -> NameContentRoute(
updateParentName = updateParentName,
)

LedgerAddStep.DATE -> DateContentRoute(
name = dateContentName,
categoryName = dateContentCategoryName,
updateParentDate = updateParentDate,
)
}
}

Expand All @@ -101,10 +143,16 @@ fun LedgerAddScreen(
color = FilledButtonColor.Black,
style = MediumButtonStyle.height60,
text = stringResource(id = R.string.word_save),
isClickable = uiState.buttonEnabled,
isActive = uiState.buttonEnabled,
onClick = onClickNextButton,
)
}
}

if (uiState.isLoading) {
LoadingScreen()
}
}

@Preview
Expand Down
Loading

0 comments on commit 688ae13

Please sign in to comment.