Skip to content

Commit

Permalink
[1.240.*] Pre-release merge (#755)
Browse files Browse the repository at this point in the history
  • Loading branch information
tramline-github[bot] authored Oct 1, 2024
2 parents 5e5f107 + f35dcf3 commit 27af428
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.datetime.Instant
Expand Down Expand Up @@ -322,15 +311,10 @@ class HomePresenter(
activeSource = activeSource,
postsType = postsType,
loadingState = HomeLoadingState.Loading,
featuredPosts = null,
posts = null
)
}
}
.flatMapLatest {
val postsType = _state.value.postsType
val activeSource = _state.value.activeSource

.flatMapLatest { (activeSource, postsType) ->
val unreadOnly =
when (postsType) {
PostsType.ALL,
Expand All @@ -351,26 +335,17 @@ class HomePresenter(
}
}

rssRepository
.featuredPosts(
selectedFeedId = activeSource?.id,
loadFeaturedPostsItems(
activeSource = activeSource,
unreadOnly = unreadOnly,
after = postsAfter
postsAfter = postsAfter
)
.map { featuredPosts ->
featuredPosts.map { postWithMetadata ->
FeaturedPostItem(
postWithMetadata = postWithMetadata,
seedColor = null,
)
}
}
.onEach { featuredPosts ->
_state.update { it.copy(featuredPosts = featuredPosts.toImmutableList()) }
}
.distinctUntilChangedBy { it.map { featuredPost -> featuredPost.postWithMetadata.id } }
.map { featuredPosts -> NTuple4(activeSource, postsAfter, unreadOnly, featuredPosts) }
}
.distinctUntilChanged()
.onEach { (activeSource, postsAfter, unreadOnly, featuredPosts) ->
val posts =
createPager(config = createPagingConfig(pageSize = 20, enablePlaceholders = true)) {
Expand Down Expand Up @@ -402,6 +377,30 @@ class HomePresenter(
.launchIn(coroutineScope)
}

private fun loadFeaturedPostsItems(
activeSource: Source?,
unreadOnly: Boolean?,
postsAfter: Instant
) =
rssRepository
.featuredPosts(
selectedFeedId = activeSource?.id,
unreadOnly = unreadOnly,
after = postsAfter
)
.map { featuredPosts ->
featuredPosts.map { postWithMetadata ->
val seedColor =
withContext(dispatchersProvider.default) {
seedColorExtractor.cached(postWithMetadata.imageUrl)
}
FeaturedPostItem(
postWithMetadata = postWithMetadata,
seedColor = seedColor,
)
}
}

private fun feedsSheetStateChanged(feedsSheetState: SheetValue) {
_state.update {
// Clear search query once feeds sheet is collapsed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package dev.sasikanth.rss.reader.ui

import androidx.collection.LruCache
import androidx.collection.lruCache
import androidx.compose.ui.graphics.ImageBitmap
import coil3.ImageLoader
import coil3.PlatformContext
Expand All @@ -33,10 +35,15 @@ class SeedColorExtractor(
private val imageLoader: Lazy<ImageLoader>,
private val platformContext: Lazy<PlatformContext>,
) {
private val lruCache: LruCache<String, Int> = lruCache(maxSize = 100)

@OptIn(ExperimentalCoilApi::class)
suspend fun calculateSeedColor(url: String?): Int? {
if (url.isNullOrBlank()) return null

val cache = lruCache[url]
if (cache != null) return cache

val bitmap: ImageBitmap? = suspendCancellableCoroutine { cont ->
val request =
ImageRequest.Builder(platformContext.value)
Expand All @@ -53,7 +60,16 @@ class SeedColorExtractor(
imageLoader.value.enqueue(request)
}

return bitmap?.seedColor()
return bitmap?.seedColor().also { seedColor ->
if (seedColor != null) {
lruCache.put(url, seedColor)
}
}
}

fun cached(url: String?): Int? {
if (url.isNullOrBlank()) return null
return lruCache[url]
}

private fun ImageBitmap.seedColor(): Int {
Expand Down

0 comments on commit 27af428

Please sign in to comment.