Skip to content

Commit

Permalink
refactor: 카테고리, 관계 추가 대응
Browse files Browse the repository at this point in the history
  • Loading branch information
yangsooplus committed Sep 10, 2024
1 parent dc9b2a1 commit e82f53b
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 54 deletions.
2 changes: 2 additions & 0 deletions core/model/src/main/java/com/susu/core/model/Category.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ data class Category(
val category: String = "",
val customCategory: String? = null,
val style: String = "",
val isCustom: Boolean = false,
val isActive: Boolean = true,
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ data class Relationship(
val relation: String = "",
val customRelation: String? = null,
val description: String? = null,
val isCustom: Boolean = false,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import com.susu.core.android.Dispatcher
import com.susu.core.android.SusuDispatchers
import com.susu.core.model.Category
import com.susu.data.local.dao.CategoryConfigDao
import com.susu.data.local.model.toEntity
import com.susu.data.local.model.toModel
import com.susu.data.remote.api.CategoryService
import com.susu.data.remote.model.response.toModel
import com.susu.domain.repository.CategoryConfigRepository
Expand All @@ -18,12 +16,16 @@ class CategoryConfigRepositoryImpl @Inject constructor(
private val api: CategoryService,
@Dispatcher(SusuDispatchers.IO) private val ioDispatcher: CoroutineDispatcher,
) : CategoryConfigRepository {
override suspend fun getCategoryConfig(): List<Category> = withContext(ioDispatcher) {
val localCategoryConfig = dao.getCategoryConfig().map { it.toModel() }
if (localCategoryConfig.isNotEmpty()) return@withContext localCategoryConfig

val remoteCategoryConfig = api.getCategoryConfig().getOrThrow().map { it.toModel() }
dao.insert(remoteCategoryConfig.map { it.toEntity() })
return@withContext remoteCategoryConfig
private var cache: List<Category>? = null
override suspend fun getCategoryConfig(): List<Category> = withContext(ioDispatcher) {
// TODO: category config 캐싱 로직 삭제, 메모리 캐싱만 - 추후 room db 마이그레이션?
return@withContext if (cache != null) {
cache!!
} else {
val categories = api.getCategoryConfig().getOrThrow().map { it.toModel() }.sortedBy { it.seq }
cache = categories
categories
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class VoteRepositoryImpl @Inject constructor(

override suspend fun getPopularVoteList(): List<Vote> = api.getPopularVoteList().getOrThrow().map { it.toModel() }

override suspend fun getPostCategoryConfig(): List<Category> = api.getPostCategoryConfig().getOrThrow().map { it.toModel() }
override suspend fun getPostCategoryConfig(): List<Category> = api.getPostCategoryConfig().getOrThrow().map { it.toModel() }.sortedBy { it.seq }

override suspend fun getVoteDetail(id: Long): Vote = api.getVoteDetail(id).getOrThrow().toModel()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ data class CategoryConfigResponse(
val seq: Int,
val name: String,
val style: String,
val isActive: Boolean,
val isCustom: Boolean,
val isMiscCategory: Boolean
)

internal fun CategoryConfigResponse.toModel() = Category(
id = id,
seq = seq,
name = name,
style = style,
isCustom = isCustom,
isActive = isActive
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ data class RelationShipListResponse(
data class RelationConfigShipResponse(
val id: Long,
val relation: String,
val isCustom: Boolean = false,
)

internal fun RelationShipListResponse.toModel() = relationships.map { it.toModel() }

internal fun RelationConfigShipResponse.toModel() = Relationship(
id = id,
relation = relation,
isCustom = isCustom
)
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class CategoryViewModel @Inject constructor(
.onSuccess {
intent {
copy(
categoryConfig = it.dropLast(1).toPersistentList(),
customCategory = it.last(),
categoryConfig = it.filter { !it.isCustom }.toPersistentList(),
customCategory = it.find { it.isCustom }!!,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ data class LedgerEditState(
val showEndDateBottomSheet: Boolean = false,
val showOnlyStartDate: Boolean = false,
) : UiState {
val isSelectedCustomCategory = selectedCategoryId == categoryConfigList.last().id
val isSelectedCustomCategory = categoryConfigList.find { it.id == selectedCategoryId }?.isCustom ?: false
val saveButtonEnabled = when {
name.isEmpty() -> false
endYear == null -> false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,16 @@ fun LedgerEditScreen(
verticalArrangement = Arrangement.spacedBy(SusuTheme.spacing.spacing_xxs),
horizontalArrangement = Arrangement.spacedBy(SusuTheme.spacing.spacing_xxs),
) {
uiState.categoryConfigList.dropLast(1).forEach { categoryConfig ->
SusuFilledButton(
isActive = categoryConfig.id == uiState.selectedCategoryId,
color = FilledButtonColor.Orange,
style = SmallButtonStyle.height32,
text = categoryConfig.name,
onClick = { onClickCategoryButton(categoryConfig.id) },
)
uiState.categoryConfigList.forEach { categoryConfig ->
if (categoryConfig.id != 5) {
SusuFilledButton(
isActive = categoryConfig.id == uiState.selectedCategoryId,
color = FilledButtonColor.Orange,
style = SmallButtonStyle.height32,
text = categoryConfig.name,
onClick = { onClickCategoryButton(categoryConfig.id) },
)
}
}

if (uiState.showCustomCategoryButton) {
Expand All @@ -199,12 +201,12 @@ fun LedgerEditScreen(
color = TextFieldButtonColor.Orange,
style = SmallTextFieldButtonStyle.height32,
text = uiState.customCategory,
isFocused = uiState.categoryConfigList.last().id == uiState.selectedCategoryId,
isFocused = uiState.isSelectedCustomCategory,
isSaved = uiState.isCustomCategoryChipSaved,
onClickClearIcon = onClickCustomCategoryClearIcon,
onClickCloseIcon = onClickCustomCategoryCloseIcon,
onClickFilledButton = onClickCustomCategoryInnerButton,
onClickButton = { onClickCategoryButton(uiState.categoryConfigList.last().id) },
onClickButton = { onClickCategoryButton(uiState.categoryConfigList.find { it.isCustom }?.id ?: 5) },
)
} else {
AddConditionButton(onClick = onClickAddConditionButton)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class LedgerEditViewModel @Inject constructor(
showCustomCategoryButton = true,
)
}
updateCategory(currentState.categoryConfigList.last().id)
updateCategory(currentState.categoryConfigList.find { it.isCustom }?.id ?: 5)
postSideEffect(LedgerEditSideEffect.FocusCustomCategory)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,9 @@ fun LedgerFilterScreen(
) {
Text(text = stringResource(R.string.ledger_filter_screen_event_category), style = SusuTheme.typography.title_xs)
Spacer(modifier = Modifier.size(SusuTheme.spacing.spacing_m))
Row(
FlowRow(
horizontalArrangement = Arrangement.spacedBy(SusuTheme.spacing.spacing_xxs),
verticalArrangement = Arrangement.spacedBy(SusuTheme.spacing.spacing_xxs),
) {
uiState.categoryConfig.forEach { category ->
SusuLinedButton(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kotlinx.collections.immutable.persistentListOf
data class CategoryState(
val selectedCategory: Category? = null,
val categoryConfig: PersistentList<Category> = persistentListOf(),
val customCategory: Category = Category(id = 5),
val customCategory: Category = Category(id = 5, isCustom = true),
val showTextFieldButton: Boolean = false,
val isSavedCustomCategory: Boolean = false,
) : UiState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class CategoryViewModel @Inject constructor(
getCategoryConfigUseCase().onSuccess {
intent {
copy(
categoryConfig = it.dropLast(1).toPersistentList(),
categoryConfig = it.filter { !it.isCustom }.toPersistentList(),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,28 +244,30 @@ fun SentEnvelopeEditScreen(
categoryText = stringResource(com.susu.core.ui.R.string.word_event),
categoryTextAlign = Alignment.Top,
) {
uiState.categoryConfig.dropLast(1).forEach { category ->
SusuFilledButton(
color = FilledButtonColor.Orange,
style = SmallButtonStyle.height32,
text = category.name,
isActive = category.id == uiState.categoryId,
onClick = { onSelectCategory(category) },
)
uiState.categoryConfig.forEach { category ->
if (category.isCustom.not()) {
SusuFilledButton(
color = FilledButtonColor.Orange,
style = SmallButtonStyle.height32,
text = category.name,
isActive = category.id == uiState.categoryId,
onClick = { onSelectCategory(category) },
)
}
}
if (uiState.showCustomCategory) {
SusuTextFieldWrapContentButton(
focusRequester = categoryFocusRequester,
color = TextFieldButtonColor.Orange,
style = SmallTextFieldButtonStyle.height32,
text = uiState.customCategory ?: "",
isFocused = uiState.categoryId == uiState.categoryConfig.last().id,
isFocused = uiState.categoryId == uiState.categoryConfig.find { it.isCustom }?.id,
isSaved = uiState.customCategorySaved,
onTextChange = onCustomCategoryUpdated,
onClickClearIcon = onCustomCategoryCleared,
onClickCloseIcon = onCloseCustomCategory,
onClickFilledButton = onClickCustomCategoryInnerButton,
onClickButton = { onSelectCategory(uiState.categoryConfig.last()) },
onClickButton = { onSelectCategory(uiState.categoryConfig.find { it.isCustom }!!) },
)
} else {
AddConditionButton(
Expand All @@ -291,28 +293,30 @@ fun SentEnvelopeEditScreen(
categoryText = stringResource(com.susu.core.ui.R.string.word_relationship),
categoryTextAlign = Alignment.Top,
) {
uiState.relationshipConfig.dropLast(1).forEach { relationship ->
SusuFilledButton(
color = FilledButtonColor.Orange,
style = SmallButtonStyle.height32,
text = relationship.relation,
isActive = relationship.id == uiState.relationshipId,
onClick = { onSelectRelationship(relationship) },
)
uiState.relationshipConfig.forEach { relationship ->
if (relationship.isCustom.not()) {
SusuFilledButton(
color = FilledButtonColor.Orange,
style = SmallButtonStyle.height32,
text = relationship.relation,
isActive = relationship.id == uiState.relationshipId,
onClick = { onSelectRelationship(relationship) },
)
}
}
if (uiState.showCustomRelationship) {
SusuTextFieldWrapContentButton(
focusRequester = relationshipFocusRequester,
style = SmallTextFieldButtonStyle.height32,
color = TextFieldButtonColor.Orange,
text = uiState.customRelationship ?: "",
isFocused = uiState.relationshipId == uiState.relationshipConfig.last().id,
isFocused = uiState.relationshipId == uiState.relationshipConfig.find { it.isCustom }?.id,
isSaved = uiState.customRelationshipSaved,
onTextChange = onCustomRelationshipUpdated,
onClickClearIcon = onCustomRelationshipCleared,
onClickCloseIcon = onCloseCustomRelationship,
onClickFilledButton = onClickCustomRelationshipInnerButton,
onClickButton = { onSelectRelationship(uiState.relationshipConfig.last()) },
onClickButton = { onSelectRelationship(uiState.relationshipConfig.find { it.isCustom }!!) },
)
} else {
AddConditionButton(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ class SentEnvelopeEditViewModel @Inject constructor(
phoneNumber = friend.phoneNumber.ifEmpty { null },
categoryId = category.id,
customCategory = category.customCategory,
showCustomCategory = category.id == categoryConfig.last().id,
customCategorySaved = category.id == categoryConfig.last().id,
showCustomRelationship = relationship.id == relationshipConfig.last().id,
customRelationshipSaved = relationship.id == relationshipConfig.last().id,
showCustomCategory = category.id == categoryConfig.find { it.isCustom }?.id,
customCategorySaved = category.id == categoryConfig.find { it.isCustom }?.id,
showCustomRelationship = relationship.id == relationshipConfig.find { it.isCustom }?.id,
customRelationshipSaved = relationship.id == relationshipConfig.find { it.isCustom }?.id,
)
}
}
Expand Down Expand Up @@ -197,7 +197,7 @@ class SentEnvelopeEditViewModel @Inject constructor(
intent {
copy(
showCustomCategory = true,
categoryId = categoryConfig.last().id,
categoryId = categoryConfig.find { it.isCustom }?.id ?: 5,
customCategorySaved = false,
)
}
Expand All @@ -212,7 +212,7 @@ class SentEnvelopeEditViewModel @Inject constructor(

fun hideCustomCategoryInput() {
intent { copy(showCustomCategory = false) }
if (currentState.categoryId == currentState.categoryConfig.last().id) {
if (currentState.categoryId == (currentState.categoryConfig.find { it.isCustom }?.id ?: 5)) {
intent { copy(categoryId = null) }
}
}
Expand All @@ -221,7 +221,7 @@ class SentEnvelopeEditViewModel @Inject constructor(
intent {
copy(
showCustomRelationship = true,
relationshipId = relationshipConfig.last().id,
relationshipId = relationshipConfig.find { it.isCustom }?.id ?: 5,
customRelationshipSaved = false,
)
}
Expand All @@ -236,7 +236,7 @@ class SentEnvelopeEditViewModel @Inject constructor(

fun hideCustomRelationshipInput() {
intent { copy(showCustomRelationship = false) }
if (currentState.relationshipId == currentState.relationshipConfig.last().id) {
if (currentState.relationshipId == (currentState.relationshipConfig.find { it.isCustom }?.id ?: 5)) {
intent { copy(relationshipId = null) }
}
}
Expand Down

0 comments on commit e82f53b

Please sign in to comment.