Skip to content

Commit

Permalink
add rss sources presenter
Browse files Browse the repository at this point in the history
  • Loading branch information
Tlaster committed Jan 23, 2025
1 parent d621aa0 commit 6e37c80
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,6 @@ public fun createSampleStatus(user: UiUserV2): UiTimeline =
aboveTextContent = null,
onClicked = {},
onMediaClicked = { _, _ -> },
platformType = user.platformType,
),
platformType = user.platformType,
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ import kotlinx.collections.immutable.ImmutableList
public data class UiTimeline internal constructor(
val topMessage: TopMessage?,
val content: ItemContent?,
val platformType: PlatformType,
) {
val itemKey: String
get() =
buildString {
append(platformType.name)
if (topMessage != null) {
append("withTopMessage")
append(topMessage.itemKey)
Expand Down Expand Up @@ -55,7 +53,18 @@ public data class UiTimeline internal constructor(
public sealed interface ItemContent {
public val itemKey: String

public data class Feed internal constructor(
val title: String,
val description: String,
val url: String,
val image: String?,
) : ItemContent {
override val itemKey: String
get() = "Feed_$url"
}

public data class Status internal constructor(
val platformType: PlatformType,
val images: ImmutableList<UiMedia>,
val sensitive: Boolean,
val contentWarning: UiRichText?,
Expand All @@ -76,6 +85,7 @@ public data class UiTimeline internal constructor(
override val itemKey: String
get() =
buildString {
append(platformType.name)
append("Status")
append(statusKey)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ internal fun FeedViewPostReasonUnion.render(
)
},
content = data?.renderStatus(accountKey, event),
platformType = PlatformType.Bluesky,
)
}

Expand Down Expand Up @@ -243,7 +242,6 @@ internal fun StatusContent.BlueskyNotification.renderBlueskyNotification(
UiTimeline(
topMessage = topMessage,
content = content,
platformType = PlatformType.Bluesky,
)
}
is StatusContent.BlueskyNotification.Post ->
Expand Down Expand Up @@ -281,7 +279,6 @@ internal fun StatusContent.BlueskyNotification.renderBlueskyNotification(
return UiTimeline(
topMessage = topMessage,
content = content,
platformType = PlatformType.Bluesky,
)
}
}
Expand Down Expand Up @@ -319,7 +316,6 @@ internal fun PostView.render(
) = UiTimeline(
topMessage = null,
content = renderStatus(accountKey, event),
platformType = PlatformType.Bluesky,
)

internal fun PostView.renderStatus(
Expand All @@ -335,6 +331,7 @@ internal fun PostView.renderStatus(
)

return UiTimeline.ItemContent.Status(
platformType = PlatformType.Bluesky,
user = user,
images = findMedias(this),
card = findCard(this),
Expand Down Expand Up @@ -843,6 +840,7 @@ private fun render(
),
)
},
platformType = PlatformType.Bluesky,
onMediaClicked = { media, index ->
launcher.launch(
AppDeepLink.StatusMedia(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ internal fun Notification.render(

else -> status ?: UiTimeline.ItemContent.User(user)
},
platformType = PlatformType.Mastodon,
)
}

Expand Down Expand Up @@ -171,7 +170,6 @@ internal fun Status.render(
return UiTimeline(
topMessage = topMessage,
content = actualStatus.renderStatus(host, accountKey, event),
platformType = PlatformType.Mastodon,
)
}

Expand Down Expand Up @@ -354,6 +352,7 @@ private fun Status.renderStatus(
),
)
},
platformType = PlatformType.Mastodon,
onMediaClicked = { media, index ->
launcher.launch(
AppDeepLink.StatusMedia(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ internal fun Notification.render(
else ->
status ?: user?.let { UiTimeline.ItemContent.User(it) }
},
platformType = PlatformType.Misskey,
)
}

Expand Down Expand Up @@ -276,7 +275,6 @@ internal fun Note.render(
return UiTimeline(
topMessage = topMessage,
content = actualStatus.renderStatus(accountKey, event),
platformType = PlatformType.Misskey,
)
}

Expand Down Expand Up @@ -484,6 +482,7 @@ internal fun Note.renderStatus(
),
)
},
platformType = PlatformType.Misskey,
onMediaClicked = { media, index ->
launcher.launch(
AppDeepLink.StatusMedia(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ internal fun Status.render(
)
},
content = renderStatus(accountKey, event),
platformType = PlatformType.VVo,
)
}

Expand Down Expand Up @@ -235,6 +234,7 @@ internal fun Status.renderStatus(
),
)
},
platformType = PlatformType.VVo,
onMediaClicked = { media, index ->
launcher.launch(
AppDeepLink.StatusMedia(
Expand Down Expand Up @@ -322,7 +322,6 @@ internal fun Comment.render(
UiTimeline(
topMessage = null,
content = renderStatus(accountKey, event),
platformType = PlatformType.VVo,
)

internal fun Comment.renderStatus(
Expand Down Expand Up @@ -444,6 +443,7 @@ internal fun Comment.renderStatus(
),
)
},
platformType = PlatformType.VVo,
onMediaClicked = { media, index ->
launcher.launch(
AppDeepLink.StatusMedia(
Expand Down Expand Up @@ -488,7 +488,6 @@ internal fun Attitude.render(
statusKey = MicroBlogKey(id.toString(), vvoHost),
),
content = content,
platformType = PlatformType.VVo,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ internal fun TopLevel.renderNotifications(
statusKey = MicroBlogKey(id = notification.id.orEmpty(), host = accountKey.host),
),
content = itemContent,
platformType = PlatformType.xQt,
)
} else if (mentionTweet != null) {
val tweet = globalObjects?.tweets?.get(mentionTweet.id) ?: return@mapNotNull null
Expand Down Expand Up @@ -192,7 +191,6 @@ internal fun TopLevel.renderNotifications(
statusKey = MicroBlogKey(id = notification?.id.orEmpty(), host = accountKey.host),
),
content = data,
platformType = PlatformType.xQt,
)
} else {
null
Expand Down Expand Up @@ -230,7 +228,6 @@ internal fun Tweet.render(
return UiTimeline(
content = actualTweet,
topMessage = topMessage,
platformType = PlatformType.xQt,
)
}

Expand Down Expand Up @@ -515,6 +512,7 @@ internal fun Tweet.renderStatus(
onClicked = {
launcher.launch(AppDeepLink.StatusDetail(accountKey = accountKey, statusKey = statusKey))
},
platformType = PlatformType.xQt,
onMediaClicked = { media, index ->
launcher.launch(
AppDeepLink.StatusMedia(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public class ComposePresenter(
}
val replyState =
statusState?.map {
if (it.platformType == PlatformType.VVo) {
if (it.content is UiTimeline.ItemContent.Status && it.content.platformType == PlatformType.VVo) {
it.copy(
content = (it.content as? UiTimeline.ItemContent.Status)?.quote?.firstOrNull() ?: it.content,
content = it.content.quote.firstOrNull() ?: it.content,
)
} else {
it
Expand All @@ -80,7 +80,7 @@ public class ComposePresenter(
statusState?.mapNotNull {
val content = it.content
if (content is UiTimeline.ItemContent.Status) {
when (it.platformType) {
when (content.platformType) {
PlatformType.VVo -> {
if (content.quote.any() && status is ComposeStatus.Quote) {
InitialText(
Expand Down Expand Up @@ -108,9 +108,12 @@ public class ComposePresenter(
accounts.flatMap { data ->
accountState
.flatMap { current ->
statusState?.map {
current to listOf(it.platformType)
} ?: UiState.Success(current to PlatformType.entries.toList())
statusState
?.mapNotNull {
it.content as? UiTimeline.ItemContent.Status
}?.map {
current to listOf(it.platformType)
} ?: UiState.Success(current to PlatformType.entries.toList())
}.map { (current, platforms) ->
data
.sortedBy {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package dev.dimension.flare.ui.presenter.home.rss

import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import dev.dimension.flare.data.network.rss.Rss
import dev.dimension.flare.ui.model.UiState
import dev.dimension.flare.ui.model.collectAsUiState
import dev.dimension.flare.ui.model.flatMap
import dev.dimension.flare.ui.presenter.PresenterBase
import kotlinx.coroutines.flow.map

public class CheckRssSourcePresenter : PresenterBase<CheckRssSourcePresenter.State>() {
public interface State {
public val isValid: UiState<Boolean>

public fun setUrl(value: String)
}

@Composable
override fun body(): State {
var url by remember { mutableStateOf("") }
val isValid =
remember {
snapshotFlow { url }
.map {
runCatching {
Rss.fetch(it)
}.fold(
onSuccess = {
UiState.Success(true) as UiState<Boolean>
},
onFailure = {
UiState.Error(it)
},
)
}
}.collectAsUiState().value.flatMap { it }

return object : State {
override val isValid = isValid

override fun setUrl(value: String) {
url = value
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.compose.runtime.rememberCoroutineScope
import dev.dimension.flare.common.PagingState
import dev.dimension.flare.common.collectPagingState
import dev.dimension.flare.data.database.app.AppDatabase
import dev.dimension.flare.data.database.app.model.DbRssSources
import dev.dimension.flare.ui.model.UiRssSource
import dev.dimension.flare.ui.model.mapper.render
import dev.dimension.flare.ui.presenter.PresenterBase
Expand All @@ -20,7 +21,7 @@ public class RssSourcesPresenter :
KoinComponent {
private val appDatabase by inject<AppDatabase>()

public interface State {
public interface State : CheckRssSourcePresenter.State {
public val sources: PagingState<UiRssSource>

public fun add(
Expand All @@ -41,13 +42,17 @@ public class RssSourcesPresenter :
}
}
}.collectPagingState()
return object : State {
val checkRssSourcePresenterState = remember { CheckRssSourcePresenter() }.body()
return object : State, CheckRssSourcePresenter.State by checkRssSourcePresenterState {
override val sources: PagingState<UiRssSource> = sources

override fun add(
url: String,
title: String,
) {
scope.launch {
appDatabase.rssSourceDao().insert(DbRssSources(url = url, title = title, lastUpdate = 0))
}
}

override fun delete(url: String) {
Expand Down

0 comments on commit 6e37c80

Please sign in to comment.