Skip to content

Commit

Permalink
Extract number of pinned feeds as separate observable in feeds presenter
Browse files Browse the repository at this point in the history
  • Loading branch information
msasikanth committed Dec 23, 2023
1 parent 56451ac commit d9d6d06
Showing 1 changed file with 19 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
Expand Down Expand Up @@ -167,11 +166,18 @@ class FeedsPresenter(
}

private fun init() {
observeNumberOfPinnedFeeds()
observeShowUnreadCountPreference()
observeFeedsForCollapsedSheet()
observeFeedsForExpandedSheet()
}

private fun observeNumberOfPinnedFeeds() {
rssRepository.numberOfPinnedFeeds().distinctUntilChanged().onEach { numberOfPinnedFeeds ->
_state.update { it.copy(numberOfPinnedFeeds = numberOfPinnedFeeds) }
}
}

@OptIn(FlowPreview::class)
private fun observeFeedsForExpandedSheet() {
val searchQueryFlow = snapshotFlow { searchQuery }.debounce(500.milliseconds)
Expand Down Expand Up @@ -210,29 +216,21 @@ class FeedsPresenter(
.combine(settingsRepository.postsType) { selectedFeed, postsType ->
selectedFeed to postsType
}
.flatMapLatest { (selectedFeed, postsType) ->
rssRepository.numberOfPinnedFeeds().distinctUntilChanged().map { numberOfPinnedFeeds ->
val postsAfter = postsAfterInstantFromPostsType(postsType)
.mapLatest { (selectedFeed, postsType) ->
val postsAfter = postsAfterInstantFromPostsType(postsType)

val feeds =
createPager(config = createPagingConfig(pageSize = 20)) {
rssRepository.allFeeds(postsAfter = postsAfter)
}
.flow
.cachedIn(coroutineScope)
val feeds =
createPager(config = createPagingConfig(pageSize = 20)) {
rssRepository.allFeeds(postsAfter = postsAfter)
}
.flow
.cachedIn(coroutineScope)

Triple(selectedFeed, feeds, numberOfPinnedFeeds)
}
selectedFeed to feeds
}
.distinctUntilChanged()
.onEach { (selectedFeed, feeds, numberOfPinnedFeeds) ->
_state.update {
it.copy(
feeds = feeds,
numberOfPinnedFeeds = numberOfPinnedFeeds,
selectedFeed = selectedFeed
)
}
.onEach { (selectedFeed, feeds) ->
_state.update { it.copy(feeds = feeds, selectedFeed = selectedFeed) }
}
.launchIn(coroutineScope)
}
Expand Down

0 comments on commit d9d6d06

Please sign in to comment.