-
Notifications
You must be signed in to change notification settings - Fork 21
add FavIconPresenter to handle favicon retrieval and caching #1482
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -12,6 +12,7 @@ import io.ktor.client.statement.bodyAsText | |||
| import io.ktor.http.ContentType | ||||
| import io.ktor.http.Url | ||||
| import io.ktor.serialization.kotlinx.xml.xml | ||||
| import kotlinx.serialization.decodeFromString | ||||
| import nl.adaptivity.xmlutil.serialization.XML | ||||
|
|
||||
| internal object RssService { | ||||
|
|
@@ -50,55 +51,86 @@ internal object RssService { | |||
| ?: throw IllegalArgumentException("No RSS or Atom feeds found at the provided URL: $url") | ||||
|
|
||||
| suspend fun fetchIcon(url: String): String? { | ||||
| val webContent = | ||||
| tryRun { | ||||
| ktorClient( | ||||
| config = {}, | ||||
| ).get(url).bodyAsText() | ||||
| }.getOrNull() ?: return null | ||||
| val feed = | ||||
| tryRun { | ||||
| fetch(url) | ||||
| xml.decodeFromString<Feed>(webContent) | ||||
| }.getOrNull() | ||||
| val feedIcon = | ||||
| when (feed) { | ||||
| is Feed.Atom -> feed.icon | ||||
| is Feed.RDF -> null | ||||
| is Feed.Rss20 -> null | ||||
| else -> null | ||||
| null -> null | ||||
|
||||
| null -> null |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
shared/src/commonMain/kotlin/dev/dimension/flare/ui/presenter/home/FavIconPresenter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package dev.dimension.flare.ui.presenter.home | ||
|
|
||
| import androidx.compose.runtime.Composable | ||
| import dev.dimension.flare.common.Cacheable | ||
| import dev.dimension.flare.common.collectAsState | ||
| import dev.dimension.flare.data.database.cache.CacheDatabase | ||
| import dev.dimension.flare.data.database.cache.model.DbEmoji | ||
| import dev.dimension.flare.data.database.cache.model.EmojiContent | ||
| import dev.dimension.flare.data.network.rss.RssService | ||
| import dev.dimension.flare.ui.model.UiState | ||
| import dev.dimension.flare.ui.model.toUi | ||
| import dev.dimension.flare.ui.presenter.PresenterBase | ||
| import kotlinx.coroutines.flow.map | ||
| import kotlinx.coroutines.flow.mapNotNull | ||
| import org.koin.core.component.KoinComponent | ||
| import org.koin.core.component.inject | ||
|
|
||
| public class FavIconPresenter( | ||
| private val host: String, | ||
| ) : PresenterBase<UiState<String>>(), | ||
| KoinComponent { | ||
| private val dbKey by lazy { | ||
| "$host-favIcon" | ||
| } | ||
|
|
||
| private val database: CacheDatabase by inject() | ||
| private val flow by lazy { | ||
| Cacheable( | ||
| fetchSource = { | ||
| val favIcon = RssService.fetchIcon("https://$host") | ||
| if (favIcon == null) { | ||
| throw IllegalStateException("Favicon not found") | ||
| } else { | ||
| database.emojiDao().insert( | ||
| DbEmoji( | ||
| host = dbKey, | ||
| content = EmojiContent.FavIcon(favIcon), | ||
| ), | ||
| ) | ||
| } | ||
| }, | ||
| cacheSource = { | ||
| database | ||
| .emojiDao() | ||
| .get(dbKey) | ||
| .mapNotNull { | ||
| it?.content as? EmojiContent.FavIcon | ||
| }.map { | ||
| it.data | ||
| } | ||
| }, | ||
| ) | ||
| } | ||
|
|
||
| @Composable | ||
| override fun body(): UiState<String> = flow.collectAsState().toUi() | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Inconsistent use of
tryRunvsrunCatching. The fileRss.ktwas updated to use standard KotlinrunCatching, but this file still uses the customtryRunfunction. For consistency across the codebase, consider usingrunCatchinghere as well, similar to the changes inRss.kt.