Change screen: propagate current homeserver value
This commit is contained in:
parent
b789955481
commit
08508772bf
7 changed files with 34 additions and 13 deletions
|
|
@ -30,7 +30,6 @@ fun OnBoardingScreenNavigation(navigator: DestinationsNavigator) {
|
||||||
@Composable
|
@Composable
|
||||||
fun LoginScreenNavigation(navigator: DestinationsNavigator) {
|
fun LoginScreenNavigation(navigator: DestinationsNavigator) {
|
||||||
LoginScreen(
|
LoginScreen(
|
||||||
homeserver = "matrix.org",
|
|
||||||
onChangeServer = {
|
onChangeServer = {
|
||||||
navigator.navigate(ChangeServerScreenNavigationDestination)
|
navigator.navigate(ChangeServerScreenNavigationDestination)
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -25,21 +25,21 @@ import com.airbnb.mvrx.Success
|
||||||
import com.airbnb.mvrx.compose.collectAsState
|
import com.airbnb.mvrx.compose.collectAsState
|
||||||
import com.airbnb.mvrx.compose.mavericksViewModel
|
import com.airbnb.mvrx.compose.mavericksViewModel
|
||||||
import io.element.android.x.designsystem.ElementXTheme
|
import io.element.android.x.designsystem.ElementXTheme
|
||||||
|
import timber.log.Timber
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun LoginScreen(
|
fun LoginScreen(
|
||||||
viewModel: LoginViewModel = mavericksViewModel(),
|
viewModel: LoginViewModel = mavericksViewModel(),
|
||||||
homeserver: String,
|
|
||||||
onChangeServer: () -> Unit = { },
|
onChangeServer: () -> Unit = { },
|
||||||
onLoginWithSuccess: () -> Unit = { },
|
onLoginWithSuccess: () -> Unit = { },
|
||||||
) {
|
) {
|
||||||
val state: LoginViewState by viewModel.collectAsState()
|
val state: LoginViewState by viewModel.collectAsState()
|
||||||
LaunchedEffect(key1 = Unit) {
|
LaunchedEffect(key1 = Unit) {
|
||||||
viewModel.homeserver = homeserver
|
Timber.d("resume")
|
||||||
|
viewModel.onResume()
|
||||||
}
|
}
|
||||||
LoginContent(
|
LoginContent(
|
||||||
state = state,
|
state = state,
|
||||||
homeserver = homeserver,
|
|
||||||
onChangeServer = onChangeServer,
|
onChangeServer = onChangeServer,
|
||||||
onLoginChanged = viewModel::onSetName,
|
onLoginChanged = viewModel::onSetName,
|
||||||
onPasswordChanged = viewModel::onSetPassword,
|
onPasswordChanged = viewModel::onSetPassword,
|
||||||
|
|
@ -53,7 +53,6 @@ fun LoginScreen(
|
||||||
@Composable
|
@Composable
|
||||||
fun LoginContent(
|
fun LoginContent(
|
||||||
state: LoginViewState,
|
state: LoginViewState,
|
||||||
homeserver: String = "",
|
|
||||||
onChangeServer: () -> Unit = {},
|
onChangeServer: () -> Unit = {},
|
||||||
onLoginChanged: (String) -> Unit = {},
|
onLoginChanged: (String) -> Unit = {},
|
||||||
onPasswordChanged: (String) -> Unit = {},
|
onPasswordChanged: (String) -> Unit = {},
|
||||||
|
|
@ -92,7 +91,7 @@ fun LoginContent(
|
||||||
modifier = Modifier.fillMaxWidth()
|
modifier = Modifier.fillMaxWidth()
|
||||||
) {
|
) {
|
||||||
OutlinedTextField(
|
OutlinedTextField(
|
||||||
value = homeserver,
|
value = state.homeserver,
|
||||||
modifier = Modifier.fillMaxWidth(),
|
modifier = Modifier.fillMaxWidth(),
|
||||||
onValueChange = { /* no op */ },
|
onValueChange = { /* no op */ },
|
||||||
enabled = false,
|
enabled = false,
|
||||||
|
|
@ -178,8 +177,9 @@ fun LoginContent(
|
||||||
private fun LoginContentPreview() {
|
private fun LoginContentPreview() {
|
||||||
ElementXTheme(darkTheme = false) {
|
ElementXTheme(darkTheme = false) {
|
||||||
LoginContent(
|
LoginContent(
|
||||||
state = LoginViewState(),
|
state = LoginViewState(
|
||||||
homeserver = "matrix.org",
|
homeserver = "matrix.org",
|
||||||
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,10 +9,17 @@ import kotlinx.coroutines.launch
|
||||||
class LoginViewModel(initialState: LoginViewState) :
|
class LoginViewModel(initialState: LoginViewState) :
|
||||||
MavericksViewModel<LoginViewState>(initialState) {
|
MavericksViewModel<LoginViewState>(initialState) {
|
||||||
|
|
||||||
lateinit var homeserver: String
|
|
||||||
|
|
||||||
private val matrix = MatrixInstance.getInstance()
|
private val matrix = MatrixInstance.getInstance()
|
||||||
|
|
||||||
|
fun onResume() {
|
||||||
|
val currentHomeserver = matrix.getHomeserverOrDefault()
|
||||||
|
setState {
|
||||||
|
copy(
|
||||||
|
homeserver = currentHomeserver
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun onSubmit() = withState { state ->
|
fun onSubmit() = withState { state ->
|
||||||
setState {
|
setState {
|
||||||
copy(isLoggedIn = Loading())
|
copy(isLoggedIn = Loading())
|
||||||
|
|
@ -20,8 +27,10 @@ class LoginViewModel(initialState: LoginViewState) :
|
||||||
|
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
suspend {
|
suspend {
|
||||||
// Ensure the server is passed to the Rust SDK
|
// Ensure the server is provided to the Rust SDK
|
||||||
matrix.setHomeserver(homeserver)
|
if (matrix.getHomeserver() == null) {
|
||||||
|
matrix.setHomeserver(state.homeserver)
|
||||||
|
}
|
||||||
matrix.login(state.login, state.password)
|
matrix.login(state.login, state.password)
|
||||||
matrix.activeClient().startSync()
|
matrix.activeClient().startSync()
|
||||||
}.execute {
|
}.execute {
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import com.airbnb.mvrx.MavericksState
|
||||||
import com.airbnb.mvrx.Uninitialized
|
import com.airbnb.mvrx.Uninitialized
|
||||||
|
|
||||||
data class LoginViewState(
|
data class LoginViewState(
|
||||||
|
val homeserver: String = "",
|
||||||
val login: String = "",
|
val login: String = "",
|
||||||
val password: String = "",
|
val password: String = "",
|
||||||
val isLoggedIn: Async<Unit> = Uninitialized,
|
val isLoggedIn: Async<Unit> = Uninitialized,
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,14 @@ class ChangeServerViewModel(initialState: ChangeServerViewState) :
|
||||||
|
|
||||||
private val matrix = MatrixInstance.getInstance()
|
private val matrix = MatrixInstance.getInstance()
|
||||||
|
|
||||||
|
init {
|
||||||
|
setState {
|
||||||
|
copy(
|
||||||
|
homeserver = matrix.getHomeserverOrDefault()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun setServer(server: String) {
|
fun setServer(server: String) {
|
||||||
setState {
|
setState {
|
||||||
copy(homeserver = server)
|
copy(homeserver = server)
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ import com.airbnb.mvrx.MavericksState
|
||||||
import com.airbnb.mvrx.Uninitialized
|
import com.airbnb.mvrx.Uninitialized
|
||||||
|
|
||||||
data class ChangeServerViewState(
|
data class ChangeServerViewState(
|
||||||
val homeserver: String = "matrix.org",
|
val homeserver: String = "",
|
||||||
val changeServerAction: Async<Unit> = Uninitialized,
|
val changeServerAction: Async<Unit> = Uninitialized,
|
||||||
) : MavericksState {
|
) : MavericksState {
|
||||||
val submitEnabled = homeserver.isNotEmpty() && changeServerAction !is Loading
|
val submitEnabled = homeserver.isNotEmpty() && changeServerAction !is Loading
|
||||||
|
|
|
||||||
|
|
@ -70,6 +70,10 @@ class Matrix(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getHomeserver(): String? = authService.homeserverDetails()?.url()
|
||||||
|
|
||||||
|
fun getHomeserverOrDefault(): String = getHomeserver() ?: "matrix.org"
|
||||||
|
|
||||||
suspend fun setHomeserver(homeserver: String) {
|
suspend fun setHomeserver(homeserver: String) {
|
||||||
withContext(coroutineDispatchers.io) {
|
withContext(coroutineDispatchers.io) {
|
||||||
authService.configureHomeserver(homeserver)
|
authService.configureHomeserver(homeserver)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue