Use EventSink lambda in state instead of Flow in Presenter

This commit is contained in:
ganfra 2023-01-11 15:53:52 +01:00
parent 56e54bb172
commit 1a0c9df1da
43 changed files with 277 additions and 490 deletions

View file

@ -1,35 +1,36 @@
package io.element.android.x.features.logout
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import io.element.android.x.architecture.Async
import io.element.android.x.architecture.Presenter
import io.element.android.x.architecture.execute
import io.element.android.x.matrix.MatrixClient
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch
import javax.inject.Inject
class LogoutPreferencePresenter @Inject constructor(private val matrixClient: MatrixClient) : Presenter<LogoutPreferenceState, LogoutPreferenceEvents> {
class LogoutPreferencePresenter @Inject constructor(private val matrixClient: MatrixClient) : Presenter<LogoutPreferenceState> {
@Composable
override fun present(events: Flow<LogoutPreferenceEvents>): LogoutPreferenceState {
override fun present(): LogoutPreferenceState {
val localCoroutineScope = rememberCoroutineScope()
val logoutAction: MutableState<Async<Unit>> = remember {
mutableStateOf(Async.Uninitialized)
}
LaunchedEffect(Unit) {
events.collect { event ->
when (event) {
LogoutPreferenceEvents.Logout -> logout(logoutAction)
}
fun handleEvents(event: LogoutPreferenceEvents) {
when (event) {
LogoutPreferenceEvents.Logout -> localCoroutineScope.logout(logoutAction)
}
}
return LogoutPreferenceState(
logoutAction = logoutAction.value
logoutAction = logoutAction.value,
eventSink = ::handleEvents
)
}

View file

@ -19,6 +19,7 @@ package io.element.android.x.features.logout
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Logout
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.res.stringResource
@ -34,14 +35,15 @@ import io.element.android.x.element.resources.R as ElementR
@Composable
fun LogoutPreferenceView(
state: LogoutPreferenceState,
onLogoutClicked: () -> Unit = {},
onSuccessLogout: () -> Unit = {},
onSuccessLogout: () -> Unit = {}
) {
val eventSink = state.eventSink
if (state.logoutAction is Async.Success) {
onSuccessLogout()
LaunchedEffect(state.logoutAction) {
onSuccessLogout()
}
return
}
val openDialog = remember { mutableStateOf(false) }
LogoutPreferenceContent(
@ -61,7 +63,7 @@ fun LogoutPreferenceView(
},
onSubmitClicked = {
openDialog.value = false
onLogoutClicked()
eventSink(LogoutPreferenceEvents.Logout)
},
onDismiss = {
openDialog.value = false

View file

@ -20,4 +20,5 @@ import io.element.android.x.architecture.Async
data class LogoutPreferenceState(
val logoutAction: Async<Unit> = Async.Uninitialized,
val eventSink: (LogoutPreferenceEvents) -> Unit = {},
)