-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
209 additions
and
21 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
app/src/main/java/dev/dimension/flare/ui/component/status/FeedComponent.kt
This file contains 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,44 @@ | ||
package dev.dimension.flare.ui.component.status | ||
|
||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.aspectRatio | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalUriHandler | ||
import dev.dimension.flare.ui.component.NetworkImage | ||
import dev.dimension.flare.ui.model.UiTimeline | ||
|
||
@Composable | ||
internal fun FeedComponent( | ||
data: UiTimeline.ItemContent.Feed, | ||
modifier: Modifier = Modifier, | ||
) { | ||
val uriHandler = LocalUriHandler.current | ||
Column( | ||
modifier = | ||
modifier | ||
.clickable { | ||
uriHandler.openUri(data.url) | ||
}, | ||
) { | ||
data.image?.let { | ||
NetworkImage( | ||
model = it, | ||
contentDescription = data.title, | ||
modifier = | ||
Modifier | ||
.aspectRatio(16f / 9f), | ||
) | ||
} | ||
Text( | ||
text = data.title, | ||
style = MaterialTheme.typography.titleMedium, | ||
) | ||
data.description?.let { | ||
Text(text = it) | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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
15 changes: 15 additions & 0 deletions
15
shared/src/commonMain/kotlin/dev/dimension/flare/data/datasource/rss/RssDataSource.kt
This file contains 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,15 @@ | ||
package dev.dimension.flare.data.datasource.rss | ||
|
||
import androidx.paging.Pager | ||
import androidx.paging.PagingConfig | ||
|
||
internal object RssDataSource { | ||
fun fetch(url: String) = | ||
Pager( | ||
config = | ||
PagingConfig( | ||
pageSize = 20, | ||
), | ||
pagingSourceFactory = { RssTimelinePagingSource(url) }, | ||
).flow | ||
} |
28 changes: 28 additions & 0 deletions
28
...rc/commonMain/kotlin/dev/dimension/flare/data/datasource/rss/RssTimelineRemoteMediator.kt
This file contains 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,28 @@ | ||
package dev.dimension.flare.data.datasource.rss | ||
|
||
import androidx.paging.ExperimentalPagingApi | ||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import dev.dimension.flare.data.network.rss.RssService | ||
import dev.dimension.flare.ui.model.UiTimeline | ||
import dev.dimension.flare.ui.model.mapper.render | ||
|
||
@OptIn(ExperimentalPagingApi::class) | ||
internal class RssTimelinePagingSource( | ||
private val url: String, | ||
) : PagingSource<Int, UiTimeline>() { | ||
override fun getRefreshKey(state: PagingState<Int, UiTimeline>): Int? = null | ||
|
||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, UiTimeline> { | ||
return try { | ||
val response = RssService.fetch(url).render() | ||
return LoadResult.Page( | ||
data = response, | ||
prevKey = null, | ||
nextKey = null, | ||
) | ||
} catch (e: Exception) { | ||
LoadResult.Error(e) | ||
} | ||
} | ||
} |
This file contains 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 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 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
53 changes: 53 additions & 0 deletions
53
shared/src/commonMain/kotlin/dev/dimension/flare/ui/model/mapper/Rss.kt
This file contains 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,53 @@ | ||
package dev.dimension.flare.ui.model.mapper | ||
|
||
import com.fleeksoft.ksoup.Ksoup | ||
import dev.dimension.flare.data.network.rss.model.Feed | ||
import dev.dimension.flare.ui.model.UiTimeline | ||
import dev.dimension.flare.ui.render.toUi | ||
import kotlinx.datetime.Instant | ||
|
||
internal fun Feed.render(): List<UiTimeline> = | ||
when (this) { | ||
is Feed.Atom -> renderAtom() | ||
is Feed.Rss20 -> renderRss20() | ||
} | ||
|
||
private fun Feed.Atom.renderAtom(): List<UiTimeline> = | ||
this.entries.map { | ||
val descHtml = | ||
it.content?.value?.let { | ||
Ksoup.parse(it) | ||
} | ||
val img = descHtml?.select("img")?.firstOrNull()?.attr("src") ?: it.media?.thumbnail?.url | ||
UiTimeline( | ||
topMessage = null, | ||
content = | ||
UiTimeline.ItemContent.Feed( | ||
title = it.title.value, | ||
description = descHtml?.text(), | ||
url = it.links.first().href, | ||
image = img, | ||
createdAt = it.published?.let { input -> runCatching { Instant.parse(input) }.getOrNull() }?.toUi(), | ||
), | ||
) | ||
} | ||
|
||
private fun Feed.Rss20.renderRss20(): List<UiTimeline> = | ||
this.channel.items.map { | ||
val descHtml = | ||
it.description?.let { | ||
Ksoup.parse(it) | ||
} | ||
val img = descHtml?.select("img")?.firstOrNull() | ||
UiTimeline( | ||
topMessage = null, | ||
content = | ||
UiTimeline.ItemContent.Feed( | ||
title = it.title, | ||
description = descHtml?.text(), | ||
url = it.link, | ||
image = img?.attr("src"), | ||
createdAt = it.pubDate?.let { input -> runCatching { Instant.parse(input) }.getOrNull() }?.toUi(), | ||
), | ||
) | ||
} |
This file contains 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
Oops, something went wrong.