Skip to content

Commit

Permalink
[chore] 리뷰 반영
Browse files Browse the repository at this point in the history
  • Loading branch information
fbghgus123 committed Sep 27, 2024
1 parent a31ebb2 commit 7a164d4
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import androidx.compose.ui.Modifier
fun DoraInfinityLazyColumn(
modifier: Modifier = Modifier,
scrollState: LazyListState = rememberLazyListState(),
isLoadAvail: Boolean = true,
isLoadAvailable: Boolean = true,
onReachedBottom: () -> Unit = {},
content: LazyListScope.() -> Unit = {},
) {
Expand All @@ -28,7 +28,7 @@ fun DoraInfinityLazyColumn(
}

LaunchedEffect(reachedBottom) {
if (reachedBottom && isLoadAvail) {
if (reachedBottom && isLoadAvailable) {
onReachedBottom.invoke()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fun HomeRoute(
val copiedUrlSnackBarHostState by remember { mutableStateOf(SnackbarHostState()) }
val toastSnackBarHostState by remember { mutableStateOf(SnackbarHostState()) }

loadScrollCache(
LoadScrollCache(
state = state,
scrollState = scrollState,
scrollCache = viewModel.scrollCache,
Expand Down Expand Up @@ -223,8 +223,8 @@ fun BoxScope.HomeSideEffectUI(
isBtnEnable = state.selectedFolderId != state.changeFolderId,
folderList = state.folderList.toSelectBottomSheetModel(
state.changeFolderId.ifEmpty {
state.selectedFolderId.apply {
updateSelectFolderId(this, "")
state.selectedFolderId.also {
updateSelectFolderId(it, "")
}
},
),
Expand Down Expand Up @@ -262,7 +262,7 @@ fun BoxScope.HomeSideEffectUI(
}

@Composable
fun loadScrollCache(
fun LoadScrollCache(
state: HomeState,
scrollState: LazyListState,
scrollCache: Map<Int, Int>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ fun HomeScreen(
.fillMaxSize()
.haze(hazeState),
scrollState = scrollState,
isLoadAvail = state.isScrollLoading.not(),
isLoadAvailable = state.isScrollLoading.not(),
onReachedBottom = onReachedBottom,
) {
item {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import com.mashup.dorabangs.core.designsystem.R
import com.mashup.dorabangs.core.designsystem.component.chips.FeedUiModel.DoraChipUiModel
import com.mashup.dorabangs.core.designsystem.component.chips.FeedUiModel.FeedCardUiModel
import com.mashup.dorabangs.core.designsystem.component.toast.ToastStyle
import com.mashup.dorabangs.domain.model.Folder
import com.mashup.dorabangs.domain.model.FolderList
import com.mashup.dorabangs.domain.model.FolderType
import com.mashup.dorabangs.domain.model.Link
import com.mashup.dorabangs.domain.model.NewFolderNameList
import com.mashup.dorabangs.domain.model.Post
import com.mashup.dorabangs.domain.model.PostInfo
import com.mashup.dorabangs.domain.model.Sort
import com.mashup.dorabangs.domain.usecase.aiclassification.GetAIClassificationCountUseCase
Expand Down Expand Up @@ -65,9 +67,7 @@ class HomeViewModel @Inject constructor(
override val container = container<HomeState, HomeSideEffect>(HomeState())

val scrollCache = ConcurrentHashMap<Int, Int>()
private val postDataManager = PostDataManager { key, pagingInfo ->
getPosts(key, pagingInfo)
}
private val postDataManager = PostDataManager()

init {
viewModelScope.doraLaunch {
Expand Down Expand Up @@ -281,7 +281,7 @@ class HomeViewModel @Inject constructor(
reduce { state.copy(isLoading = true, postList = emptyList()) }

val cacheKey = cacheKey ?: getCacheKey(state)
val postList = postDataManager.fetchPostData(cacheKey, state.folderList)
val postList = fetchPostData(cacheKey, state.folderList)

reduce { state.copy(postList = postList) }
}.invokeOnCompletion {
Expand All @@ -297,12 +297,12 @@ class HomeViewModel @Inject constructor(
return@intent
}

intent {
reduce { state.copy(isScrollLoading = true) }
}
reduce { state.copy(isScrollLoading = true) }

val cacheKey = getCacheKey(state)
loadPostList(cacheKey)

reduce { state.copy(isScrollLoading = false) }
}
}

Expand All @@ -311,8 +311,8 @@ class HomeViewModel @Inject constructor(
if (postDataManager.getHasNext(cacheKey).not()) {
return@intent
}
val newPostList = postDataManager.fetchRemotePostData(cacheKey, state.folderList)
reduce { state.copy(isScrollLoading = false, postList = state.postList + newPostList) }
val newPostList = fetchRemotePostData(cacheKey, state.folderList)
reduce { state.copy(postList = state.postList + newPostList) }
}.invokeOnCompletion {
intent {
reduce { state.copy(isLoading = false) }
Expand Down Expand Up @@ -550,4 +550,26 @@ class HomeViewModel @Inject constructor(
reduce { state.copy(folderList = folderList) }
}
}

private suspend fun fetchPostData(key: String, folderList: List<Folder>) =
postDataManager.getCachedPostData(key) ?: fetchRemotePostData(key, folderList)

private suspend fun fetchRemotePostData(key: String, folderList: List<Folder>): List<FeedCardUiModel> {
val pagingInfo = postDataManager.getPagingInfo(key)
val posts = getPosts(key, pagingInfo)
val feedCardList = posts.items.toUIModel(folderList)

postDataManager.apply {
updateNextPagingInfo(key, pagingInfo, posts.metaData.hasNext)
cacheFeedCardList(key, feedCardList)
}

return feedCardList
}

private fun List<Post>.toUIModel(folderList: List<Folder>) = this.map { post ->
val category =
folderList.firstOrNull { folder -> folder.id == post.folderId }?.name.orEmpty()
post.toUiModel(category)
}
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,18 @@
package com.mashup.dorabangs.feature.home

import com.mashup.dorabangs.core.designsystem.component.chips.FeedUiModel.FeedCardUiModel
import com.mashup.dorabangs.domain.model.Folder
import com.mashup.dorabangs.domain.model.Post
import com.mashup.dorabangs.domain.model.Posts
import com.mashup.dorabangs.feature.model.PagingInfo
import com.mashup.dorabangs.feature.model.toUiModel
import java.util.concurrent.ConcurrentHashMap

class PostDataManager(
private val getPost: suspend (String, PagingInfo) -> Posts,
) {
class PostDataManager {
private val pagingInfoCache = ConcurrentHashMap<String, PagingInfo>()
private val postIdCache = ConcurrentHashMap<String, List<String>>()
private val postDataCache = ConcurrentHashMap<String, FeedCardUiModel>()

suspend fun fetchPostData(key: String, folderList: List<Folder>): List<FeedCardUiModel> {
return if (postIdCache[key].isNullOrEmpty()) {
fetchRemotePostData(key, folderList)
} else {
postIdCache[key]
?.mapNotNull { postId -> postDataCache[postId] }
.orEmpty()
}
}
fun getCachedPostData(key: String) =
postIdCache[key]
?.mapNotNull { postId -> postDataCache[postId] }

suspend fun fetchRemotePostData(key: String, folderList: List<Folder>): List<FeedCardUiModel> {
val pagingInfo = getPagingInfo(key)
return getPost(key, pagingInfo)
.updateNextPagingInfo(key)
.items
.toUIModel(folderList)
.caching(key)
}

fun getPostIdList(key: String) = postIdCache[key].orEmpty()

Expand All @@ -57,31 +37,23 @@ class PostDataManager(
pagingInfoCache.remove(key)
}

private fun getPagingInfo(key: String): PagingInfo {
fun getPagingInfo(key: String): PagingInfo {
return pagingInfoCache[key] ?: PagingInfo.getDefault(key).apply {
pagingInfoCache[key] = this
}
}

private fun Posts.updateNextPagingInfo(key: String): Posts {
val pagingInfo = getPagingInfo(key)
fun updateNextPagingInfo(key: String, pagingInfo: PagingInfo, hasNext: Boolean) {
pagingInfoCache[key] = pagingInfo.copy(
nextPage = pagingInfo.nextPage + 1,
hasNext = this.metaData.hasNext,
hasNext = hasNext,
)
return this
}

private fun List<FeedCardUiModel>.caching(key: String): List<FeedCardUiModel> {
this.forEach { post ->
fun cacheFeedCardList(key: String, data: List<FeedCardUiModel>) {
data.forEach { post ->
postDataCache[post.postId] = post
}
postIdCache[key] = (postIdCache[key] ?: emptyList()) + this.map { it.postId }
return this
}

private fun List<Post>.toUIModel(folderList: List<Folder>) = this.map { post ->
val category = folderList.firstOrNull { folder -> folder.id == post.folderId }?.name.orEmpty()
post.toUiModel(category)
postIdCache[key] = (postIdCache[key] ?: emptyList()) + data.map { it.postId }
}
}

0 comments on commit 7a164d4

Please sign in to comment.