Skip to content

Commit

Permalink
add send message in profile
Browse files Browse the repository at this point in the history
  • Loading branch information
Tlaster committed Oct 28, 2024
1 parent d39ad98 commit 0d6feaf
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ internal fun DMScreenRoute(
AnimatedPane {
scaffoldNavigator.currentDestination?.content?.let { args ->
if (args.isUserKey) {
UserDMConversationScreen(
accountType = accountType,
userKey = MicroBlogKey.valueOf(args.key),
onBack = scaffoldNavigator::navigateBack,
navigationState = navigationState,
toProfile = {
navigator.navigate(ProfileRouteDestination(userKey = it, accountType = accountType))
},
)
} else {
DMConversationScreen(
accountType = accountType,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package dev.dimension.flare.ui.screen.dm

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import com.eygraber.compose.placeholder.material3.placeholder
import dev.dimension.flare.R
import dev.dimension.flare.model.AccountType
import dev.dimension.flare.model.MicroBlogKey
import dev.dimension.flare.molecule.producePresenter
import dev.dimension.flare.ui.component.BackButton
import dev.dimension.flare.ui.component.FlareScaffold
import dev.dimension.flare.ui.component.FlareTopAppBar
import dev.dimension.flare.ui.model.onError
import dev.dimension.flare.ui.model.onLoading
import dev.dimension.flare.ui.model.onSuccess
import dev.dimension.flare.ui.presenter.dm.UserDMConversationPresenter
import dev.dimension.flare.ui.presenter.invoke
import dev.dimension.flare.ui.screen.home.NavigationState

@OptIn(ExperimentalMaterial3Api::class)
@Composable
internal fun UserDMConversationScreen(
accountType: AccountType,
userKey: MicroBlogKey,
onBack: () -> Unit,
navigationState: NavigationState,
toProfile: (MicroBlogKey) -> Unit,
) {
val state by producePresenter(key = "UserDMConversationScreen${userKey}$accountType") {
presenter(
accountType = accountType,
userKey = userKey,
)
}
state.roomKey
.onSuccess {
DMConversationScreen(
accountType = accountType,
roomKey = it,
onBack = onBack,
navigationState = navigationState,
toProfile = toProfile,
)
}.onLoading {
FlareScaffold(
topBar = {
FlareTopAppBar(
title = {
Text(
"Loading...",
modifier =
Modifier
.placeholder(true),
)
},
navigationIcon = {
BackButton(onBack = onBack)
},
)
},
) {
Box(
modifier = Modifier.padding(it).fillMaxSize(),
contentAlignment = Alignment.Center,
) {
CircularProgressIndicator()
}
}
}.onError {
FlareScaffold(
topBar = {
FlareTopAppBar(
title = {
Text(
"Loading...",
modifier =
Modifier
.placeholder(true),
)
},
navigationIcon = {
BackButton(onBack = onBack)
},
)
},
) {
Box(
modifier = Modifier.padding(it).fillMaxSize(),
contentAlignment = Alignment.Center,
) {
Text(text = stringResource(R.string.dm_list_error))
}
}
}
}

@Composable
private fun presenter(
accountType: AccountType,
userKey: MicroBlogKey,
) = run {
remember(accountType, userKey) {
UserDMConversationPresenter(
accountType = accountType,
userKey = userKey,
)
}.invoke()
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import com.ramcosta.composedestinations.generated.destinations.ServiceSelectRout
import com.ramcosta.composedestinations.generated.destinations.SettingsRouteDestination
import com.ramcosta.composedestinations.generated.destinations.TimelineRouteDestination
import com.ramcosta.composedestinations.navigation.DependenciesContainerBuilder
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import com.ramcosta.composedestinations.navigation.dependency
import com.ramcosta.composedestinations.spec.Direction
import com.ramcosta.composedestinations.spec.NavHostGraphSpec
Expand Down Expand Up @@ -143,6 +144,7 @@ import soup.compose.material.motion.animation.rememberSlideDistance

data class RootNavController(
val navController: NavController,
val navigator: DestinationsNavigator = navController.toDestinationsNavigator(),
)

@OptIn(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package dev.dimension.flare.ui.presenter.dm

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
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.model.MicroBlogKey
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

class UserDMConversationPresenter(
private val accountType: AccountType,
private val userKey: MicroBlogKey,
) : PresenterBase<UserDMConversationPresenter.State>() {
interface State {
val roomKey: UiState<MicroBlogKey>
}

@Composable
override fun body(): State {
val serviceState = accountServiceProvider(accountType = accountType)
val roomKey =
serviceState.flatMap {
require(it is DirectMessageDataSource)
remember {
it.createDirectMessageRoom(userKey)
}.collectAsUiState().value.flatMap { it }
}
return object : State {
override val roomKey = roomKey
}
}
}

0 comments on commit 0d6feaf

Please sign in to comment.