@@ -33,6 +33,7 @@ import kotlinx.collections.immutable.persistentListOf
3333import kotlinx.collections.immutable.toImmutableList
3434import kotlinx.collections.immutable.toPersistentList
3535import kotlinx.coroutines.launch
36+ import java.time.LocalDateTime
3637
3738class BookDetailPresenter @AssistedInject constructor(
3839 @Assisted private val screen : BookDetailScreen ,
@@ -45,6 +46,13 @@ class BookDetailPresenter @AssistedInject constructor(
4546 private const val START_INDEX = 0
4647 }
4748
49+ private fun getRecordComparator (sortType : RecordSort ): Comparator <ReadingRecordModel > {
50+ return when (sortType) {
51+ RecordSort .PAGE_NUMBER_ASC -> compareBy { it.pageNumber }
52+ RecordSort .CREATED_DATE_DESC -> compareByDescending { LocalDateTime .parse(it.createdAt) }
53+ }
54+ }
55+
4856 @Composable
4957 override fun present (): BookDetailUiState {
5058 val scope = rememberCoroutineScope()
@@ -150,13 +158,7 @@ class BookDetailPresenter @AssistedInject constructor(
150158 readingRecords = if (startIndex == START_INDEX ) {
151159 result.content.toPersistentList()
152160 } else {
153- // 새로운 데이터를 기존 리스트와 합친 후 정렬
154- (readingRecords + result.content).sortedWith(
155- when (currentRecordSort) {
156- RecordSort .PAGE_NUMBER_ASC -> compareBy { it.pageNumber }
157- RecordSort .CREATED_DATE_DESC -> compareByDescending { it.createdAt }
158- },
159- ).toPersistentList()
161+ (readingRecords + result.content).toPersistentList()
160162 }
161163
162164 currentStartIndex = startIndex
@@ -225,12 +227,7 @@ class BookDetailPresenter @AssistedInject constructor(
225227
226228 is BookDetailUiEvent .OnRecordSortItemSelected -> {
227229 currentRecordSort = event.sortType
228- readingRecords = readingRecords.sortedWith(
229- when (event.sortType) {
230- RecordSort .PAGE_NUMBER_ASC -> compareBy { it.pageNumber }
231- RecordSort .CREATED_DATE_DESC -> compareByDescending { it.createdAt }
232- },
233- ).toPersistentList()
230+ readingRecords = readingRecords.sortedWith(getRecordComparator(event.sortType)).toPersistentList()
234231 isRecordSortBottomSheetVisible = false
235232 }
236233
0 commit comments