Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/mz 179 ledger search #62

Merged
merged 5 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.susu.core.designsystem.R
import com.susu.core.designsystem.component.button.ClearIconButton
import com.susu.core.designsystem.theme.Gray100
import com.susu.core.designsystem.theme.Gray20
import com.susu.core.designsystem.theme.Gray60
Expand All @@ -40,6 +41,7 @@ fun SusuSearchBar(
modifier: Modifier = Modifier,
value: String,
onValueChange: (String) -> Unit = {},
onClickClearIcon: () -> Unit = {},
maxLines: Int = 1,
minLines: Int = 1,
textColor: Color = Gray100,
Expand Down Expand Up @@ -94,6 +96,13 @@ fun SusuSearchBar(
)
}
}

if (value.isNotEmpty()) {
ClearIconButton(
iconSize = 24.dp,
onClick = onClickClearIcon,
)
}
}
},
)
Expand All @@ -104,7 +113,7 @@ fun SusuSearchBar(
fun SusuSearchBarPreview() {
SusuTheme {
var text by remember {
mutableStateOf("")
mutableStateOf("zzzz")
}

SusuSearchBar(
Expand Down
8 changes: 8 additions & 0 deletions core/model/src/main/java/com/susu/core/model/Category.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.susu.core.model

data class Category(
val id: Int,
val seq: Int,
val category: String,
val customCategory: String? = null,
)
15 changes: 15 additions & 0 deletions core/model/src/main/java/com/susu/core/model/Ledger.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.susu.core.model

import androidx.compose.runtime.Stable
import java.time.LocalDateTime

@Stable
data class Ledger(
val id: Int,
val title: String,
val description: String,
val startAt: LocalDateTime,
val endAt: LocalDateTime,
val category: Category,
val totalAmounts: Int,
)
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 @@ -5,6 +5,7 @@
<string name="word_align_high_amount">금액 높은 순</string>
<string name="word_align_low_amount">금액 낮은 순</string>
<string name="word_recent_search">최근 검색</string>
<string name="word_search_result">검색 결과</string>
<string name="word_edit">편집</string>
<string name="word_save">저장</string>
<string name="word_delete">삭제</string>
Expand Down
7 changes: 7 additions & 0 deletions data/src/main/java/com/susu/data/data/di/RepositoryModule.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.susu.data.data.di

import com.susu.data.data.repository.LedgerRecentSearchRepositoryImpl
import com.susu.data.data.repository.LedgerRepositoryImpl
import com.susu.data.data.repository.LoginRepositoryImpl
import com.susu.data.data.repository.SignUpRepositoryImpl
import com.susu.data.data.repository.TermRepositoryImpl
import com.susu.data.data.repository.TokenRepositoryImpl
import com.susu.domain.repository.LedgerRecentSearchRepository
import com.susu.domain.repository.LedgerRepository
import com.susu.domain.repository.LoginRepository
import com.susu.domain.repository.SignUpRepository
import com.susu.domain.repository.TermRepository
Expand Down Expand Up @@ -43,4 +45,9 @@ abstract class RepositoryModule {
abstract fun bindTermRepository(
termRepositoryImpl: TermRepositoryImpl,
): TermRepository

@Binds
abstract fun bindLedgerRepository(
ledgerRepositoryImpl: LedgerRepositoryImpl,
): LedgerRepository
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.susu.data.data.repository

import com.susu.core.model.Ledger
import com.susu.data.remote.api.LedgerService
import com.susu.data.remote.model.response.toModel
import com.susu.domain.repository.LedgerRepository
import kotlinx.datetime.toKotlinLocalDateTime
import java.time.LocalDateTime
import javax.inject.Inject

class LedgerRepositoryImpl @Inject constructor(
private val ledgerService: LedgerService,
) : LedgerRepository {
override suspend fun getLedgerList(
title: String?,
categoryId: Long?,
fromStartAt: LocalDateTime,
toEndAt: LocalDateTime,
page: Int?,
sort: String?,
): List<Ledger> {
return ledgerService.getLedgerList(
title = title,
categoryId = categoryId,
fromStartAt = fromStartAt.toKotlinLocalDateTime(),
toEndAt = toEndAt.toKotlinLocalDateTime(),
page = page,
sort = sort,
).getOrThrow().toModel()
}
}
19 changes: 19 additions & 0 deletions data/src/main/java/com/susu/data/remote/api/LedgerService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.susu.data.remote.api

import com.susu.data.remote.model.response.LedgerListResponse
import com.susu.data.remote.retrofit.ApiResult
import kotlinx.datetime.LocalDateTime
import retrofit2.http.GET
import retrofit2.http.Query

interface LedgerService {
@GET("ledgers")
suspend fun getLedgerList(
@Query("title") title: String?,
@Query("categoryId") categoryId: Long?,
@Query("fromStartAt") fromStartAt: LocalDateTime,
@Query("toEndAt") toEndAt: LocalDateTime,
@Query("page") page: Int?,
@Query("sort") sort: String?,
): ApiResult<LedgerListResponse>
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.susu.data.remote.di

import com.susu.data.remote.api.LedgerService
import com.susu.data.remote.api.SignUpService
import com.susu.data.remote.api.TermService
import com.susu.data.remote.api.TokenService
Expand Down Expand Up @@ -39,4 +40,10 @@ object ApiServiceModule {
fun providesTermService(retrofit: Retrofit): TermService {
return retrofit.create(TermService::class.java)
}

@Singleton
@Provides
fun providesLedgerService(retrofit: Retrofit): LedgerService {
return retrofit.create(LedgerService::class.java)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.susu.data.remote.model.response

import com.susu.core.model.Category
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.toJavaLocalDateTime
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class LedgerListResponse(
@SerialName("data")
val ledgerList: List<Ledger>,
val page: Int,
val size: Int,
val totalPage: Int,
val totalCount: Int,
val sort: SortInfo,
)

@Serializable
data class Ledger(
@SerialName("ledger")
val info: LedgerInfo,
val category: CategoryInfo,
val totalAmounts: Int,
val totalCounts: Int,
)

@Serializable
data class LedgerInfo(
val id: Int,
val title: String,
val description: String,
val startAt: LocalDateTime,
val endAt: LocalDateTime,
)

@Serializable
data class CategoryInfo(
val id: Int,
val seq: Int,
val category: String,
val customCategory: String? = null,
)

@Serializable
data class SortInfo(
val empty: Boolean,
val unsorted: Boolean,
val sorted: Boolean,
)

internal fun LedgerListResponse.toModel() = this.ledgerList.map { ledger ->
with(ledger) {
com.susu.core.model.Ledger(
id = info.id,
title = info.title,
description = info.description,
startAt = info.startAt.toJavaLocalDateTime(),
endAt = info.endAt.toJavaLocalDateTime(),
category = category.toModel(),
totalAmounts = totalAmounts,
)
}
}

internal fun CategoryInfo.toModel() = Category(
id = id,
seq = seq,
category = category,
customCategory = customCategory,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.susu.domain.repository

import com.susu.core.model.Ledger
import java.time.LocalDateTime

interface LedgerRepository {
suspend fun getLedgerList(
title: String?,
categoryId: Long?,
fromStartAt: LocalDateTime,
toEndAt: LocalDateTime,
page: Int?,
sort: String?,
): List<Ledger>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.susu.domain.usecase.ledger

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

class GetLedgerListUseCase @Inject constructor(
private val ledgerRepository: LedgerRepository,
) {
suspend operator fun invoke(param: Param) = runCatchingIgnoreCancelled {
with(param) {
ledgerRepository.getLedgerList(
title = title,
categoryId = categoryId,
fromStartAt = fromStartAt,
toEndAt = toEndAt,
page = page,
sort = sort,
)
}
}

data class Param(
val title: String? = null,
val categoryId: Long? = null,
val fromStartAt: LocalDateTime = LocalDateTime.now().minusYears(10),
val toEndAt: LocalDateTime = LocalDateTime.now(),
val page: Int? = null,
val sort: String? = null,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ fun NavGraphBuilder.receivedNavGraph(
composable(route = ReceivedRoute.ledgerSearchRoute) {
LedgerSearchRoute(
popBackStack = popBackStack,
navigateLedgerDetail = navigateLedgerDetail,
)
}
composable(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package com.susu.feature.received.search

import com.susu.core.model.Ledger
import com.susu.core.ui.base.SideEffect
import com.susu.core.ui.base.UiState
import kotlinx.collections.immutable.PersistentList
import kotlinx.collections.immutable.persistentListOf

data class LedgerSearchState(
val searchKeyword: String = "",
val searchKeywordList: PersistentList<String> = persistentListOf(),
val recentSearchKeywordList: PersistentList<String> = persistentListOf(),
val ledgerList: PersistentList<Ledger> = persistentListOf(),
val isLoading: Boolean = false,
val showSearchResultEmpty: Boolean = false,
) : UiState

sealed interface LedgerSearchSideEffect : SideEffect {
data object PopBackStack : LedgerSearchSideEffect
data class NavigateLedgerDetail(val id: Int) : LedgerSearchSideEffect
}
Loading