Skip to content

Commit

Permalink
Lazily extract seed colors from featured posts
Browse files Browse the repository at this point in the history
This helps with speeding up the loading to display time of the loaded articles.
  • Loading branch information
msasikanth committed Sep 22, 2024
1 parent cffdf2c commit c0715e8
Showing 1 changed file with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -358,42 +358,46 @@ class HomePresenter(
after = postsAfter
)
.map { featuredPosts ->
featuredPosts
.map { postWithMetadata ->
val seedColor =
withContext(dispatchersProvider.default) {
seedColorExtractor.calculateSeedColor(postWithMetadata.imageUrl)
}

FeaturedPostItem(
postWithMetadata = postWithMetadata,
seedColor = seedColor,
)
}
.toImmutableList()
featuredPosts.map { postWithMetadata ->
FeaturedPostItem(
postWithMetadata = postWithMetadata,
seedColor = null,
)
}
}
.onEach { featuredPosts -> _state.update { it.copy(featuredPosts = featuredPosts) } }
.map { featuredPosts ->
val featuredPostsIds = featuredPosts.map { it.postWithMetadata.id }
NTuple4(activeSource, postsAfter, unreadOnly, featuredPostsIds)
.onEach { featuredPosts ->
_state.update { it.copy(featuredPosts = featuredPosts.toImmutableList()) }
}
.map { featuredPosts -> NTuple4(activeSource, postsAfter, unreadOnly, featuredPosts) }
}
.onEach { _state.update { it.copy(loadingState = HomeLoadingState.Idle) } }
.distinctUntilChanged()
.onEach { (activeSource, postsAfter, unreadOnly, featuredPostsIds) ->
.onEach { (activeSource, postsAfter, unreadOnly, featuredPosts) ->
val posts =
createPager(config = createPagingConfig(pageSize = 20, enablePlaceholders = true)) {
rssRepository.posts(
selectedFeedId = activeSource?.id,
featuredPostsIds = featuredPostsIds,
featuredPostsIds = featuredPosts.map { it.postWithMetadata.id },
unreadOnly = unreadOnly,
after = postsAfter,
)
}
.flow
.cachedIn(coroutineScope)

_state.update { it.copy(posts = posts) }
_state.update { it.copy(posts = posts, loadingState = HomeLoadingState.Idle) }
}
.onEach { (_, _, _, featuredPosts) ->
val featuredPostsWithSeedColor =
featuredPosts.map { featuredPost ->
val seedColor =
withContext(dispatchersProvider.default) {
seedColorExtractor.calculateSeedColor(featuredPost.postWithMetadata.imageUrl)
}

featuredPost.copy(seedColor = seedColor)
}

_state.update { it.copy(featuredPosts = featuredPostsWithSeedColor.toImmutableList()) }
}
.launchIn(coroutineScope)
}
Expand Down

0 comments on commit c0715e8

Please sign in to comment.