Skip to content

Commit

Permalink
Merge pull request #221 from mash-up-kr/feature/refactoring/filter
Browse files Browse the repository at this point in the history
[#128] 필터 칩 고를때, filter 거는게 아닌 api 새로 호출
  • Loading branch information
Ahn-seokjoo authored Sep 20, 2024
2 parents 9e05ca0 + c2792e9 commit 5ecc63b
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.mashup.dorabangs.data.datasource.remote.api

import com.mashup.dorabangs.data.model.classification.AIClassificationAIPostListResponseModel
import com.mashup.dorabangs.domain.model.AIClassificationFolders
import com.mashup.dorabangs.domain.model.AIClassificationPosts

interface AIClassificationRemoteDataSource {

Expand All @@ -23,7 +22,7 @@ interface AIClassificationRemoteDataSource {
page: Int? = null,
limit: Int? = null,
order: String? = null,
): AIClassificationPosts
): AIClassificationAIPostListResponseModel

suspend fun deletePostFromAIClassification(
postId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.mashup.dorabangs.data.model.classification.AIClassificationAIPostList
import com.mashup.dorabangs.data.model.toDomain
import com.mashup.dorabangs.data.network.service.AIClassificationService
import com.mashup.dorabangs.domain.model.AIClassificationFolders
import com.mashup.dorabangs.domain.model.AIClassificationPosts
import javax.inject.Inject

class AIClassificationRemoteDataSourceImpl @Inject constructor(
Expand Down Expand Up @@ -38,13 +37,13 @@ class AIClassificationRemoteDataSourceImpl @Inject constructor(
page: Int?,
limit: Int?,
order: String?,
): AIClassificationPosts =
): AIClassificationAIPostListResponseModel =
service.getAIClassificationPostsByFolder(
folderId = folderId,
page = page,
limit = limit,
order = order,
).toDomain()
)

override suspend fun deletePostFromAIClassification(postId: String) =
service.deletePostFromAIClassification(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.mashup.dorabangs.data.network.service

import com.mashup.dorabangs.data.model.AIClassificationFoldersResponseModel
import com.mashup.dorabangs.data.model.AIClassificationPostsResponseModel
import com.mashup.dorabangs.data.model.AiClassificationMoveSinglePostRequestModel
import com.mashup.dorabangs.data.model.classification.AIClassificationAIPostListResponseModel
import retrofit2.http.Body
Expand Down Expand Up @@ -40,7 +39,7 @@ interface AIClassificationService {
@Query("page") page: Int? = null,
@Query("limit") limit: Int? = null,
@Query("order") order: String? = null,
): AIClassificationPostsResponseModel
): AIClassificationAIPostListResponseModel

@DELETE("classification/posts/{postId}")
suspend fun deletePostFromAIClassification(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.mashup.dorabangs.data.datasource.remote.api.AIClassificationRemoteDat
import com.mashup.dorabangs.data.model.classification.toPagingDomain
import com.mashup.dorabangs.data.utils.doraPager
import com.mashup.dorabangs.domain.model.AIClassificationFolders
import com.mashup.dorabangs.domain.model.AIClassificationPosts
import com.mashup.dorabangs.domain.model.DoraSampleResponse
import com.mashup.dorabangs.domain.model.classification.AIClassificationFeedPost
import com.mashup.dorabangs.domain.repository.AIClassificationRepository
Expand Down Expand Up @@ -44,16 +43,19 @@ class AIClassificationRepositoryImpl @Inject constructor(

override suspend fun getAIClassificationPostsByFolder(
folderId: String,
page: Int?,
limit: Int?,
order: String?,
): AIClassificationPosts =
remoteDataSource.getAIClassificationPostsByFolder(
folderId = folderId,
page = page,
limit = limit,
order = order,
)
): Flow<PagingData<AIClassificationFeedPost>> =
doraPager(
apiExecutor = { page ->
remoteDataSource.getAIClassificationPostsByFolder(
folderId = folderId,
page = page,
limit = limit,
order = order,
).toPagingDomain()
},
).flow

override suspend fun deletePostFromAIClassification(postId: String): DoraSampleResponse {
return kotlin.runCatching {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.mashup.dorabangs.domain.repository

import androidx.paging.PagingData
import com.mashup.dorabangs.domain.model.AIClassificationFolders
import com.mashup.dorabangs.domain.model.AIClassificationPosts
import com.mashup.dorabangs.domain.model.DoraSampleResponse
import com.mashup.dorabangs.domain.model.classification.AIClassificationFeedPost
import kotlinx.coroutines.flow.Flow
Expand All @@ -22,10 +21,9 @@ interface AIClassificationRepository {

suspend fun getAIClassificationPostsByFolder(
folderId: String,
page: Int? = null,
limit: Int? = null,
order: String? = null,
): AIClassificationPosts
): Flow<PagingData<AIClassificationFeedPost>>

suspend fun deletePostFromAIClassification(
postId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ class GetAIClassificationPostsByFolderUseCase @Inject constructor(

suspend operator fun invoke(
folderId: String,
page: Int? = null,
limit: Int? = null,
order: Sort? = null,
) =
aiClassificationRepository.getAIClassificationPostsByFolder(
folderId = folderId,
page = page,
limit = limit,
order = order?.query,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,22 @@ fun ClassificationListScreen(
pagingList[idx]?.let { item ->
when (item) {
is FeedUiModel.DoraChipUiModel -> {
if (state.selectedFolder == item.title || state.selectedFolder == "전체") {
ClassificationFolderMove(
selectedFolder = item.title,
onClickAllItemMoveButton = { onClickAllItemMoveButton(item.folderId) },
count = item.postCount,
)
}
ClassificationFolderMove(
selectedFolder = item.title,
onClickAllItemMoveButton = { onClickAllItemMoveButton(item.folderId) },
count = item.postCount,
)
}

is FeedUiModel.FeedCardUiModel -> {
if (state.selectedFolder == item.category || state.selectedFolder == "전체") {
ClassificationCardItem(
idx = idx,
lastIndex = pagingList.itemCount - 1,
cardItem = item,
onClickDeleteButton = onClickDeleteButton,
onClickMoveButton = onClickMoveButton,
onClickCardItem = onClickCardItem,
)
}
ClassificationCardItem(
idx = idx,
lastIndex = pagingList.itemCount - 1,
cardItem = item,
onClickDeleteButton = onClickDeleteButton,
onClickMoveButton = onClickMoveButton,
onClickCardItem = onClickCardItem,
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.mashup.dorabangs.core.designsystem.component.chips.FeedUiModel

data class ClassificationState(
val chipState: ChipState = ChipState(),
val selectedFolder: String = "전체",
val isLoading: Boolean = false,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import com.mashup.dorabangs.core.coroutine.doraLaunch
import com.mashup.dorabangs.core.designsystem.component.chips.FeedUiModel
import com.mashup.dorabangs.domain.model.AIClassificationFolders
import com.mashup.dorabangs.domain.model.PostInfo
import com.mashup.dorabangs.domain.model.Sort
import com.mashup.dorabangs.domain.model.classification.AIClassificationFeedPost
import com.mashup.dorabangs.domain.usecase.aiclassification.DeletePostFromAIClassificationUseCase
import com.mashup.dorabangs.domain.usecase.aiclassification.GetAIClassificationFolderListUseCase
import com.mashup.dorabangs.domain.usecase.aiclassification.GetAIClassificationPostsByFolderUseCase
import com.mashup.dorabangs.domain.usecase.aiclassification.GetAIClassificationPostsUseCase
import com.mashup.dorabangs.domain.usecase.aiclassification.MoveAllPostsToRecommendedFolderUseCase
import com.mashup.dorabangs.domain.usecase.aiclassification.MoveSinglePostToRecommendedFolderUseCase
Expand All @@ -33,6 +35,7 @@ import javax.inject.Inject
class ClassificationViewModel @Inject constructor(
private val getAIClassificationFolderListUseCase: GetAIClassificationFolderListUseCase,
private val getAIClassificationPostsUseCase: GetAIClassificationPostsUseCase,
private val getAIClassificationFolderListByIdUseCase: GetAIClassificationPostsByFolderUseCase,
private val deletePostUseCase: DeletePostFromAIClassificationUseCase,
private val moveSinglePostUseCase: MoveSinglePostToRecommendedFolderUseCase,
private val moveAllPostUseCase: MoveAllPostsToRecommendedFolderUseCase,
Expand Down Expand Up @@ -109,12 +112,51 @@ class ClassificationViewModel @Inject constructor(
}
}

fun changeCategory(idx: Int) = intent {
reduce {
state.copy(
chipState = state.chipState.copy(currentIndex = idx),
selectedFolder = state.chipState.chipList.getOrNull(idx)?.title ?: "전체",
)
fun changeCategory(idx: Int) = viewModelScope.doraLaunch {
intent {
val selectedFolderItem = state.chipState.chipList.getOrNull(idx)
val selectedFolderId = selectedFolderItem?.id.orEmpty()
val selectedFolderName = selectedFolderItem?.title.orEmpty()

getAIClassificationFolderListByIdUseCase.invoke(
folderId = selectedFolderId,
limit = LIMIT,
order = Sort.DESC,
).map { pagedData ->
// chip create
pagedData.map {
val category =
state.chipState.chipList.firstOrNull { chip -> chip.id == it.folderId }?.title.orEmpty()
it.toUiModel(category)
}
}.map {
// add seperator
it.insertSeparators { before, after ->
after?.let {
if (before?.category != after.category) {
FeedUiModel.DoraChipUiModel(
id = selectedFolderId,
mergedTitle = "",
title = selectedFolderName,
postCount = selectedFolderItem?.postCount ?: 0,
folderId = after.folderId,
icon = null,
)
} else {
null
}
}
}
}.cachedIn(viewModelScope)
.let {
_paging.value = it.firstOrNull() ?: PagingData.empty()
}

reduce {
state.copy(
chipState = state.chipState.copy(currentIndex = idx),
)
}
}
}

Expand All @@ -124,9 +166,9 @@ class ClassificationViewModel @Inject constructor(
state.copy(isLoading = true)
}
}
val allMove = moveAllPostUseCase.invoke(suggestionFolderId = folderId)
val allMoveItem = moveAllPostUseCase.invoke(suggestionFolderId = folderId)

if (allMove.isSuccess) {
if (allMoveItem.isSuccess) {
getInitialData()
}
}.invokeOnCompletion {
Expand All @@ -136,45 +178,96 @@ class ClassificationViewModel @Inject constructor(
}

fun moveSelectedItem(cardItem: FeedUiModel.FeedCardUiModel) = viewModelScope.doraLaunch {
val move = moveSinglePostUseCase.invoke(
postId = cardItem.postId,
suggestionFolderId = cardItem.folderId,
)
intent {
reduce {
state.copy(
isLoading = true,
)
}
val moveItem = moveSinglePostUseCase.invoke(
postId = cardItem.postId,
suggestionFolderId = cardItem.folderId,
)

if (moveItem.isSuccess) {
val (chips, chipList) = updateChipList()
val findCategory = chips.list.find { it.folderId == cardItem.folderId }

if (move.isSuccess) {
val (chips, chipList) = updateChipList()
updateListScreenWithSingleItem(cardItem)
if (findCategory != null) {
updateListScreenWithSingleItem(cardItem)
updateSeparator(chipList)
} else {
getInitialData()
}

intent {
reduce {
state.copy(
chipState = ChipState(
chipState = state.chipState.copy(
totalCount = chips.totalCounts,
chipList = chipList,
),
)
}
}
}
}.invokeOnCompletion {
intent {
reduce {
state.copy(
isLoading = false,
)
}
}
}

fun deleteSelectedItem(cardItem: FeedUiModel.FeedCardUiModel) = viewModelScope.doraLaunch {
val delete = deletePostUseCase.invoke(cardItem.postId)
if (delete.isSuccess) {
val (chips, chipList) = updateChipList()
updateListScreenWithSingleItem(cardItem)
intent {
reduce {
state.copy(
isLoading = true,
)
}
val deleteItem = deletePostUseCase.invoke(cardItem.postId)
if (deleteItem.isSuccess) {
val (chips, chipList) = updateChipList()
val findCategory = chips.list.find { it.folderId == cardItem.folderId }

if (findCategory != null) {
updateListScreenWithSingleItem(cardItem)
updateSeparator(chipList)
} else {
getInitialData()
}

intent {
reduce {
state.copy(
chipState = ChipState(
chipState = state.chipState.copy(
totalCount = chips.totalCounts,
chipList = chipList,
),
)
}
}
}
}.invokeOnCompletion {
intent {
reduce {
state.copy(
isLoading = false,
)
}
}
}

private fun updateSeparator(chipList: List<FeedUiModel.DoraChipUiModel>) {
_paging.value = _paging.value.map {
if (it is FeedUiModel.DoraChipUiModel) {
val updateItem = chipList.find { chip -> chip.id == it.folderId }
it.copy(postCount = updateItem?.postCount ?: it.postCount)
} else {
it
}
}
}

private suspend fun updateListScreenWithSingleItem(cardItem: FeedUiModel.FeedCardUiModel) {
Expand Down

0 comments on commit 5ecc63b

Please sign in to comment.