Skip to content

Commit

Permalink
Merge pull request #97 from YAPP-Github/feature/ledger-add-type
Browse files Browse the repository at this point in the history
Feature/ledger add type
  • Loading branch information
jinukeu authored Jan 26, 2024
2 parents a4a7955 + 0d458de commit 35c53ac
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ class LedgerRepositoryImpl @Inject constructor(
override suspend fun deleteLedger(id: Int) = ledgerService.deleteLedgerList(
listOf(id),
).getOrThrow()

override suspend fun getCreateLedgerConfig(): List<Int> = ledgerService.getCreateLedgerConfig().getOrThrow().onlyStartAtCategoryIds
}
4 changes: 4 additions & 0 deletions data/src/main/java/com/susu/data/remote/api/LedgerService.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.susu.data.remote.api

import com.susu.data.remote.model.request.LedgerRequest
import com.susu.data.remote.model.response.CreateLedgerConfigResponse
import com.susu.data.remote.model.response.LedgerListResponse
import com.susu.data.remote.model.response.LedgerResponse
import com.susu.data.remote.retrofit.ApiResult
Expand Down Expand Up @@ -37,4 +38,7 @@ interface LedgerService {

@DELETE("ledgers")
suspend fun deleteLedgerList(@Query("ids") idList: List<Int>): ApiResult<Unit>

@GET("ledgers/configs/create-ledger")
suspend fun getCreateLedgerConfig(): ApiResult<CreateLedgerConfigResponse>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.susu.data.remote.model.response

import kotlinx.serialization.Serializable

@Serializable
data class CreateLedgerConfigResponse(
val onlyStartAtCategoryIds: List<Int>,
)
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ interface LedgerRepository {
suspend fun deleteLedger(
id: Int,
)

suspend fun getCreateLedgerConfig(): List<Int>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.susu.domain.usecase.ledger

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

typealias OnlyStartAtCategoryIdList = List<Int>

class GetCreateLedgerConfigUseCase @Inject constructor(
private val ledgerRepository: LedgerRepository,
) {
suspend operator fun invoke(): Result<OnlyStartAtCategoryIdList> = runCatchingIgnoreCancelled {
ledgerRepository.getCreateLedgerConfig()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ fun LedgerAddRoute(
}
}

var dateContentCategoryName: String by remember {
mutableStateOf("")
var dateContentCategory: Category? by remember {
mutableStateOf(null)
}

var dateContentName: String by remember {
Expand All @@ -69,14 +69,14 @@ fun LedgerAddRoute(
onClickNextButton = viewModel::goToNextStep,
updateParentSelectedCategory = { category ->
viewModel.updateSelectedCategory(category)
dateContentCategoryName = category?.customCategory ?: category?.name ?: ""
dateContentCategory = category
},
updateParentName = { name ->
viewModel.updateName(name)
dateContentName = name
},
dateContentName = dateContentName,
dateContentCategoryName = dateContentCategoryName,
dateContentCategory = dateContentCategory,
updateParentDate = { startAt, endAt ->
viewModel.updateDate(startAt, endAt)
},
Expand All @@ -90,7 +90,7 @@ fun LedgerAddScreen(
onClickNextButton: () -> Unit = {},
updateParentSelectedCategory: (Category?) -> Unit = {},
updateParentName: (String) -> Unit = {},
dateContentCategoryName: String = "",
dateContentCategory: Category? = Category(),
dateContentName: String = "",
updateParentDate: (LocalDateTime?, LocalDateTime?) -> Unit = { _, _ -> },
) {
Expand Down Expand Up @@ -129,7 +129,7 @@ fun LedgerAddScreen(

LedgerAddStep.DATE -> DateContentRoute(
name = dateContentName,
categoryName = dateContentCategoryName,
category = dateContentCategory,
updateParentDate = updateParentDate,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@ import androidx.compose.foundation.layout.size
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.susu.core.designsystem.component.bottomsheet.datepicker.SusuLimitDatePickerBottomSheet
import com.susu.core.designsystem.component.button.GhostButtonColor
import com.susu.core.designsystem.component.button.SmallButtonStyle
import com.susu.core.designsystem.component.button.SusuGhostButton
import com.susu.core.designsystem.theme.Gray60
import com.susu.core.designsystem.theme.SusuTheme
import com.susu.core.model.Category
import com.susu.core.ui.extension.collectWithLifecycle
import com.susu.core.ui.util.AnnotatedText
import com.susu.core.ui.util.currentDate
Expand All @@ -29,7 +34,7 @@ import java.time.LocalDateTime
fun DateContentRoute(
viewModel: DateViewModel = hiltViewModel(),
name: String,
categoryName: String,
category: Category?,
updateParentDate: (LocalDateTime?, LocalDateTime?) -> Unit,
) {
val uiState = viewModel.uiState.collectAsStateWithLifecycle().value
Expand All @@ -44,7 +49,8 @@ fun DateContentRoute(
}

LaunchedEffect(key1 = Unit) {
viewModel.updateNameAndCategory(name, categoryName)
viewModel.setScreenType(category ?: Category())
viewModel.updateNameAndCategory(name, category?.customCategory ?: category?.name ?: "")
}

DateContent(
Expand All @@ -55,6 +61,7 @@ fun DateContentRoute(
onEndDateItemSelected = viewModel::updateEndDate,
onClickEndDateText = viewModel::showEndDateBottomSheet,
onDismissEndDateBottomSheet = viewModel::hideEndDateBottomSheet,
onClickSetDateButton = viewModel::toggleShowOnlyStartAt,
)
}

Expand All @@ -68,6 +75,7 @@ fun DateContent(
onEndDateItemSelected: (Int, Int, Int) -> Unit = { _, _, _ -> },
onClickEndDateText: () -> Unit = {},
onDismissEndDateBottomSheet: () -> Unit = {},
onClickSetDateButton: () -> Unit = {},
) {
Column(
modifier = Modifier
Expand All @@ -93,19 +101,39 @@ fun DateContent(
year = uiState.startAt?.year,
month = uiState.startAt?.monthValue,
day = uiState.startAt?.dayOfMonth,
suffix = stringResource(R.string.ledger_add_screen_from),
suffix = if (uiState.showOnlyStartAt.not()) stringResource(R.string.ledger_add_screen_from) else "",
onClick = onClickStartDateText,
)

Spacer(modifier = Modifier.size(SusuTheme.spacing.spacing_xxs))
if (uiState.showOnlyStartAt.not()) {
Spacer(modifier = Modifier.size(SusuTheme.spacing.spacing_xxs))

SelectDateRow(
year = uiState.endAt?.year,
month = uiState.endAt?.monthValue,
day = uiState.endAt?.dayOfMonth,
suffix = stringResource(R.string.ledger_add_screen_until),
onClick = onClickEndDateText,
SelectDateRow(
year = uiState.endAt?.year,
month = uiState.endAt?.monthValue,
day = uiState.endAt?.dayOfMonth,
suffix = stringResource(R.string.ledger_add_screen_until),
onClick = onClickEndDateText,
)
}

Spacer(modifier = Modifier.weight(1f))

SusuGhostButton(
modifier = Modifier.align(Alignment.CenterHorizontally),
color = GhostButtonColor.Orange,
style = SmallButtonStyle.height40,
text = if (uiState.showOnlyStartAt) {
stringResource(R.string.date_content_show_endat)
} else {
stringResource(
R.string.date_content_set_only_startat,
)
},
onClick = onClickSetDateButton,
)

Spacer(modifier = Modifier.size(SusuTheme.spacing.spacing_xxl))
}

if (uiState.showStartDateBottomSheet) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ data class DateState(
val endAt: LocalDateTime? = null,
val showStartDateBottomSheet: Boolean = false,
val showEndDateBottomSheet: Boolean = false,
val showOnlyStartAt: Boolean = false,
) : UiState

sealed interface DateSideEffect : SideEffect {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
package com.susu.feature.received.ledgeradd.content.date

import androidx.lifecycle.viewModelScope
import com.susu.core.model.Category
import com.susu.core.ui.base.BaseViewModel
import com.susu.core.ui.util.getSafeLocalDateTime
import com.susu.domain.usecase.ledger.GetCreateLedgerConfigUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import java.time.LocalDateTime
import javax.inject.Inject

@HiltViewModel
class DateViewModel @Inject constructor() : BaseViewModel<DateState, DateSideEffect>(
class DateViewModel @Inject constructor(
private val getCreateLedgerConfigUseCase: GetCreateLedgerConfigUseCase,
) : BaseViewModel<DateState, DateSideEffect>(
DateState(),
) {
private var category = Category()

fun setScreenType(category: Category) = viewModelScope.launch {
if (this@DateViewModel.category == category) return@launch

getCreateLedgerConfigUseCase()
.onSuccess { onlyStartAtCategoryIdList ->
intent { copy(showOnlyStartAt = category.id in onlyStartAtCategoryIdList) }
}
}

fun updateNameAndCategory(name: String, categoryName: String) = intent {
copy(
name = name,
Expand All @@ -19,9 +36,11 @@ class DateViewModel @Inject constructor() : BaseViewModel<DateState, DateSideEff

fun updateStartDate(year: Int, month: Int, day: Int) = intent {
val toUpdateStartAt = LocalDateTime.of(year, month, day, 0, 0)
postSideEffect(DateSideEffect.UpdateParentDate(toUpdateStartAt, endAt))
val toUpdateEndAt = if (showOnlyStartAt) toUpdateStartAt else endAt
postSideEffect(DateSideEffect.UpdateParentDate(toUpdateStartAt, toUpdateEndAt))
copy(
startAt = toUpdateStartAt,
endAt = toUpdateEndAt,
)
}

Expand All @@ -33,12 +52,23 @@ class DateViewModel @Inject constructor() : BaseViewModel<DateState, DateSideEff
)
}

fun updateParentDate() = with(currentState) {
postSideEffect(DateSideEffect.UpdateParentDate(startAt, endAt))
}

fun showStartDateBottomSheet() = intent { copy(showStartDateBottomSheet = true) }
fun hideStartDateBottomSheet() = intent { copy(showStartDateBottomSheet = false) }
fun showEndDateBottomSheet() = intent { copy(showEndDateBottomSheet = true) }
fun hideEndDateBottomSheet() = intent { copy(showEndDateBottomSheet = false) }
fun toggleShowOnlyStartAt() = intent {
if (showOnlyStartAt) {
postSideEffect(DateSideEffect.UpdateParentDate(startAt, null))
copy(
showOnlyStartAt = false,
endAt = null,
)
} else {
postSideEffect(DateSideEffect.UpdateParentDate(startAt, startAt))
copy(
showOnlyStartAt = true,
endAt = startAt,
)
}
}
}
2 changes: 2 additions & 0 deletions feature/received/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@
<string name="received_envelope_edit_screen_present_placeholder">한끼 식사</string>
<string name="received_envelope_edit_screen_phone_number_placeholder">01012345678</string>
<string name="received_envelope_edit_screen_memo_placeholder">입력해주세요</string>
<string name="date_content_show_endat">종료일 추가</string>
<string name="date_content_set_only_startat">시작일만 지정</string>
</resources>

0 comments on commit 35c53ac

Please sign in to comment.