Skip to content

Commit

Permalink
add dm datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
Tlaster committed Oct 21, 2024
1 parent fb7540a commit 08fd68c
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 6 deletions.
18 changes: 15 additions & 3 deletions app/src/main/java/dev/dimension/flare/ui/screen/dm/DMListScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.compose.material3.adaptive.navigation.rememberListDetailPaneScaf
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
Expand Down Expand Up @@ -53,10 +54,12 @@ import dev.dimension.flare.ui.component.status.ListComponent
import dev.dimension.flare.ui.model.UiDMList
import dev.dimension.flare.ui.model.localizedShortTime
import dev.dimension.flare.ui.presenter.dm.DMListPresenter
import dev.dimension.flare.ui.presenter.dm.DMListState
import dev.dimension.flare.ui.presenter.invoke
import dev.dimension.flare.ui.screen.list.ItemPlaceHolder
import dev.dimension.flare.ui.theme.MediumAlpha
import dev.dimension.flare.ui.theme.screenHorizontalPadding
import kotlinx.coroutines.launch
import kotlinx.parcelize.Parcelize

@OptIn(ExperimentalMaterial3AdaptiveApi::class)
Expand Down Expand Up @@ -263,7 +266,16 @@ private fun DMListScreen(
@Composable
private fun presenter(accountType: AccountType) =
run {
remember(accountType) {
DMListPresenter(accountType)
}.invoke()
val scope = rememberCoroutineScope()
val state =
remember(accountType) {
DMListPresenter(accountType)
}.invoke()
object : DMListState by state {
fun refresh() {
scope.launch {
state.refreshSuspend()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.dimension.flare.data.datasource.microblog

import androidx.paging.PagingData
import dev.dimension.flare.ui.model.UiDMItem
import dev.dimension.flare.ui.model.UiDMList
import kotlinx.coroutines.flow.Flow

internal interface DirectMessageDataSource {
fun directMessageList(): Flow<PagingData<UiDMList>>

fun directMessageConversation(id: String): Flow<PagingData<UiDMItem>>

fun sendDirectMessage(
id: String,
message: String,
)

fun getDirectMessageConversationInfo(id: String): Flow<UiDMList>
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package dev.dimension.flare.ui.presenter.dm

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.paging.compose.collectAsLazyPagingItems
import dev.dimension.flare.common.PagingState
import dev.dimension.flare.common.toPagingState
import dev.dimension.flare.data.datasource.microblog.DirectMessageDataSource
import dev.dimension.flare.data.repository.accountServiceProvider
import dev.dimension.flare.model.AccountType
import dev.dimension.flare.ui.model.UiDMItem
import dev.dimension.flare.ui.model.UiState
import dev.dimension.flare.ui.model.UiUserV2
import dev.dimension.flare.ui.model.collectAsUiState
import dev.dimension.flare.ui.model.flatMap
import dev.dimension.flare.ui.model.map
import dev.dimension.flare.ui.model.onSuccess
import dev.dimension.flare.ui.presenter.PresenterBase

class DMConversationPresenter(
Expand All @@ -14,7 +23,37 @@ class DMConversationPresenter(
) : PresenterBase<DMConversationState>() {
@Composable
override fun body(): DMConversationState {
TODO()
val serviceState = accountServiceProvider(accountType = accountType)
val items =
serviceState
.map { service ->
require(service is DirectMessageDataSource)
remember(service, id) {
service.directMessageConversation(id)
}.collectAsLazyPagingItems()
}.toPagingState()
val user =
serviceState
.flatMap { service ->
require(service is DirectMessageDataSource)
remember(service, id) {
service.getDirectMessageConversationInfo(id)
}.collectAsUiState().value
}.map {
it.user
}
return object : DMConversationState {
override val items = items

override val user = user

override fun send(message: String) {
serviceState.onSuccess {
require(it is DirectMessageDataSource)
it.sendDirectMessage(id, message)
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,49 @@
package dev.dimension.flare.ui.presenter.dm

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.paging.compose.collectAsLazyPagingItems
import dev.dimension.flare.common.PagingState
import dev.dimension.flare.common.onSuccess
import dev.dimension.flare.common.toPagingState
import dev.dimension.flare.data.datasource.microblog.DirectMessageDataSource
import dev.dimension.flare.data.repository.accountServiceProvider
import dev.dimension.flare.model.AccountType
import dev.dimension.flare.ui.model.UiDMList
import dev.dimension.flare.ui.model.map
import dev.dimension.flare.ui.presenter.PresenterBase

class DMListPresenter(
private val accountType: AccountType,
) : PresenterBase<DMListState>() {
@Composable
override fun body(): DMListState {
TODO()
val serviceState = accountServiceProvider(accountType = accountType)
val items =
serviceState
.map { service ->
require(service is DirectMessageDataSource)
remember(service) {
service.directMessageList()
}.collectAsLazyPagingItems()
}.toPagingState()
return object : DMListState {
override val items = items

override val isRefreshing = false

override suspend fun refreshSuspend() {
items.onSuccess {
refreshSuspend()
}
}
}
}
}

interface DMListState {
val items: PagingState<UiDMList>
val isRefreshing: Boolean

fun refresh()
suspend fun refreshSuspend()
}

0 comments on commit 08fd68c

Please sign in to comment.