Migrate Preferences to new architecture

This commit is contained in:
ganfra 2023-01-09 19:27:28 +01:00
parent 9e211b5e04
commit ae273bd4ea
26 changed files with 399 additions and 174 deletions

View file

@ -0,0 +1,5 @@
package io.element.android.x.features.logout
sealed interface LogoutPreferenceEvents {
object Logout: LogoutPreferenceEvents
}

View file

@ -0,0 +1,41 @@
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 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> {
@Composable
override fun present(events: Flow<LogoutPreferenceEvents>): LogoutPreferenceState {
val logoutAction: MutableState<Async<Unit>> = remember {
mutableStateOf(Async.Uninitialized)
}
LaunchedEffect(Unit) {
events.collect { event ->
when (event) {
LogoutPreferenceEvents.Logout -> logout(logoutAction)
}
}
}
return LogoutPreferenceState(
logoutAction = logoutAction.value
)
}
private fun CoroutineScope.logout(logoutAction: MutableState<Async<Unit>>) = launch {
suspend {
matrixClient.logout()
}.execute(logoutAction)
}
}

View file

@ -19,15 +19,11 @@ 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.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import com.airbnb.mvrx.Loading
import com.airbnb.mvrx.Success
import com.airbnb.mvrx.compose.collectAsState
import com.airbnb.mvrx.compose.mavericksViewModel
import io.element.android.x.architecture.Async
import io.element.android.x.designsystem.ElementXTheme
import io.element.android.x.designsystem.components.ProgressDialog
import io.element.android.x.designsystem.components.dialogs.ConfirmationDialog
@ -36,12 +32,12 @@ import io.element.android.x.designsystem.components.preferences.PreferenceText
import io.element.android.x.element.resources.R as ElementR
@Composable
fun LogoutPreference(
viewModel: LogoutViewModel = mavericksViewModel(),
onSuccessLogout: () -> Unit = { },
fun LogoutPreferenceView(
state: LogoutPreferenceState,
onLogoutClicked: () -> Unit = {},
onSuccessLogout: () -> Unit = {},
) {
val state: LogoutViewState by viewModel.collectAsState()
if (state.logoutAction is Success) {
if (state.logoutAction is Async.Success) {
onSuccessLogout()
return
}
@ -65,7 +61,7 @@ fun LogoutPreference(
},
onSubmitClicked = {
openDialog.value = false
viewModel.logout()
onLogoutClicked()
},
onDismiss = {
openDialog.value = false
@ -73,7 +69,7 @@ fun LogoutPreference(
)
}
if (state.logoutAction is Loading) {
if (state.logoutAction is Async.Loading) {
ProgressDialog(text = "Login out...")
}
}
@ -95,6 +91,6 @@ fun LogoutPreferenceContent(
@Preview
fun LogoutContentPreview() {
ElementXTheme(darkTheme = false) {
LogoutPreference()
LogoutPreferenceView(LogoutPreferenceState())
}
}

View file

@ -16,10 +16,8 @@
package io.element.android.x.features.logout
import com.airbnb.mvrx.Async
import com.airbnb.mvrx.MavericksState
import com.airbnb.mvrx.Uninitialized
import io.element.android.x.architecture.Async
data class LogoutViewState(
val logoutAction: Async<Unit> = Uninitialized,
) : MavericksState
data class LogoutPreferenceState(
val logoutAction: Async<Unit> = Async.Uninitialized,
)

View file

@ -1,46 +0,0 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.x.features.logout
import com.airbnb.mvrx.MavericksViewModel
import com.airbnb.mvrx.MavericksViewModelFactory
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import io.element.android.x.anvilannotations.ContributesViewModel
import io.element.android.x.architecture.viewmodel.daggerMavericksViewModelFactory
import io.element.android.x.di.SessionScope
import io.element.android.x.matrix.MatrixClient
import kotlinx.coroutines.launch
@ContributesViewModel(SessionScope::class)
class LogoutViewModel @AssistedInject constructor(
private val client: MatrixClient,
@Assisted initialState: LogoutViewState
) : MavericksViewModel<LogoutViewState>(initialState) {
companion object : MavericksViewModelFactory<LogoutViewModel, LogoutViewState> by daggerMavericksViewModelFactory()
fun logout() {
viewModelScope.launch {
suspend {
client.logout()
}.execute {
copy(logoutAction = it)
}
}
}
}