Skip to content

Commit

Permalink
feat: 게시글 삭제시, 리스트 반영 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
rhkrwngud445 committed May 21, 2024
1 parent 609b892 commit c2b5e5c
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.withpeace.withpeace.feature.mypage.navigation.myPageNavGraph
import com.withpeace.withpeace.feature.postdetail.navigation.POST_DETAIL_ROUTE_WITH_ARGUMENT
import com.withpeace.withpeace.feature.postdetail.navigation.navigateToPostDetail
import com.withpeace.withpeace.feature.postdetail.navigation.postDetailGraph
import com.withpeace.withpeace.feature.postlist.navigation.POST_LIST_DELETED_POST_ID_ARGUMENT
import com.withpeace.withpeace.feature.postlist.navigation.POST_LIST_ROUTE
import com.withpeace.withpeace.feature.postlist.navigation.postListGraph
import com.withpeace.withpeace.feature.registerpost.navigation.IMAGE_LIST_ARGUMENT
Expand Down Expand Up @@ -193,6 +194,12 @@ fun WithpeaceNavHost(
onAuthExpired = {
onAuthExpired(onShowSnackBar, navController)
},
onDeleteSuccess = {
navController.previousBackStackEntry?.savedStateHandle?.apply {
set(POST_LIST_DELETED_POST_ID_ARGUMENT, it)
}
navController.popBackStack()
}
)
postListGraph(
onShowSnackBar = onShowSnackBar,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ fun PostDetailRoute(
onClickBackButton: () -> Unit,
onClickEditButton: (RegisterPostUiModel) -> Unit,
onAuthExpired: () -> Unit,
onDeleteSuccess: (Long) -> Unit,
) {
val postUiState by viewModel.postUiState.collectAsStateWithLifecycle()
val isLoading by viewModel.isLoading.collectAsStateWithLifecycle()
Expand Down Expand Up @@ -98,9 +99,9 @@ fun PostDetailRoute(

PostDetailUiEvent.UnAuthorized -> onAuthExpired()

PostDetailUiEvent.DeleteSuccess -> {
is PostDetailUiEvent.DeleteSuccess -> {
onShowSnackBar("게시글이 삭제되었습니다.")
onClickBackButton()
onDeleteSuccess(it.postId)
}

PostDetailUiEvent.RegisterCommentFailByNetwork -> onShowSnackBar("댓글 등록에 실패했습니다. 네트워크를 확인해주세요")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.withpeace.withpeace.feature.postdetail
sealed interface PostDetailUiEvent {
data object DeleteFailByNetworkError : PostDetailUiEvent
data object UnAuthorized : PostDetailUiEvent
data object DeleteSuccess : PostDetailUiEvent
data class DeleteSuccess(val postId: Long) : PostDetailUiEvent
data object RegisterCommentFailByNetwork : PostDetailUiEvent
data object RegisterCommentSuccess : PostDetailUiEvent
data object ReportPostSuccess : PostDetailUiEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class PostDetailViewModel @Inject constructor(
}
},
).onEach {
_postUiEvent.send(PostDetailUiEvent.DeleteSuccess)
_postUiEvent.send(PostDetailUiEvent.DeleteSuccess(postId))
}.launchIn(viewModelScope)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ fun NavGraphBuilder.postDetailGraph(
onClickBackButton: () -> Unit,
onClickEditButton: (RegisterPostUiModel) -> Unit,
onAuthExpired: () -> Unit,
onDeleteSuccess: (Long) -> Unit,
) {
composable(
route = POST_DETAIL_ROUTE_WITH_ARGUMENT,
Expand All @@ -43,7 +44,8 @@ fun NavGraphBuilder.postDetailGraph(
onShowSnackBar = onShowSnackBar,
onClickBackButton = onClickBackButton,
onClickEditButton = onClickEditButton,
onAuthExpired = onAuthExpired
onAuthExpired = onAuthExpired,
onDeleteSuccess = onDeleteSuccess,
)
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
package com.withpeace.withpeace.feature.postlist

import android.util.Log
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.PagingData
import androidx.paging.cachedIn
import androidx.paging.filter
import androidx.paging.map
import com.withpeace.withpeace.core.domain.model.error.ClientError
import com.withpeace.withpeace.core.domain.usecase.GetPostsUseCase
import com.withpeace.withpeace.core.ui.post.PostTopicUiModel
import com.withpeace.withpeace.core.ui.post.PostUiModel
import com.withpeace.withpeace.core.ui.post.toDomain
import com.withpeace.withpeace.core.ui.post.toPostUiModel
import com.withpeace.withpeace.feature.postlist.navigation.POST_LIST_DELETED_POST_ID_ARGUMENT
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import javax.inject.Inject
Expand All @@ -33,8 +40,13 @@ class PostListViewModel @Inject constructor(
val currentTopic = _currentTopic.asStateFlow()

private val _postListPagingFlow = MutableStateFlow(PagingData.empty<PostUiModel>())
val postListPagingFlow = _postListPagingFlow.asStateFlow()
private val deletedIdsFlow = MutableStateFlow(setOf<Long>())

val postListPagingFlow = combine(_postListPagingFlow, deletedIdsFlow) { paging, deletedPostId ->
paging.filter {
deletedPostId.contains(it.postId).not()
}
}.cachedIn(viewModelScope)
init {
fetchPostList(currentTopic.value)
}
Expand All @@ -58,12 +70,16 @@ class PostListViewModel @Inject constructor(
throw IllegalStateException() // LoadStateError를 내보내기 위함
},
).map {
it.map { it.toPostUiModel() }
it.map { post -> post.toPostUiModel() }
}.cachedIn(viewModelScope).firstOrNull() ?: PagingData.empty()
}
}
}

fun updateDeletedId(id: Long) {
deletedIdsFlow.update { it + id }
}

companion object {
private const val PAGE_SIZE = 7
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.withpeace.withpeace.feature.postlist.navigation

import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavOptions
import androidx.navigation.compose.composable
import com.withpeace.withpeace.feature.postlist.PostListRoute
import com.withpeace.withpeace.feature.postlist.PostListViewModel

const val POST_LIST_ROUTE = "post_list_rotue"
const val POST_LIST_ROUTE = "post_list_route"
const val POST_LIST_DELETED_POST_ID_ARGUMENT = "post_list_deleted_post_id"

fun NavController.navigateToPostList(navOptions: NavOptions? = null) =
navigate(POST_LIST_ROUTE, navOptions)
Expand All @@ -17,10 +20,16 @@ fun NavGraphBuilder.postListGraph(
onAuthExpired: () -> Unit,
) {
composable(POST_LIST_ROUTE) {
val deletedId = it.savedStateHandle.get<Long>(POST_LIST_DELETED_POST_ID_ARGUMENT)
val viewModel: PostListViewModel = hiltViewModel()
deletedId?.let { id ->
viewModel.updateDeletedId(id)
}
PostListRoute(
viewModel = viewModel,
onShowSnackBar = onShowSnackBar,
navigateToDetail = navigateToPostDetail,
onAuthExpired = onAuthExpired
onAuthExpired = onAuthExpired,
)
}
}

0 comments on commit c2b5e5c

Please sign in to comment.