Add a module for the Matrix SDK with the aar of the Rust SDK

This commit is contained in:
Benoit Marty 2022-10-11 14:05:39 +02:00
parent 4219281691
commit 1238047b08
14 changed files with 131 additions and 34 deletions

View file

@ -37,6 +37,7 @@ android {
dependencies {
implementation project(":libraries:ui:theme")
implementation project(":libraries:sdk:matrix")
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.5.1'

View file

@ -1,6 +1,7 @@
package io.element.android.x.ui.screen.login
sealed interface LoginActions {
data class SetHomeserver(val homeserver: String) : LoginActions
data class SetLogin(val login: String) : LoginActions
data class SetPassword(val password: String) : LoginActions
object Submit : LoginActions

View file

@ -36,6 +36,11 @@ class LoginActivity : ComponentActivity() {
modifier = Modifier.fillMaxSize()
) {
val state = viewModel.state.collectAsState().value
VectorTextField(
value = state.homeserver,
onValueChange = {
viewModel.handle(LoginActions.SetHomeserver(it))
})
VectorTextField(
value = state.login,
onValueChange = {

View file

@ -1,37 +1,72 @@
package io.element.android.x.ui.screen.login
import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import io.element.android.x.sdk.matrix.MatrixInstance
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
class LoginViewModel : ViewModel() {
private val matrix = MatrixInstance.getInstance()
private val _state = MutableStateFlow(LoginViewState())
val state = _state.asStateFlow()
init {
observeState()
}
private fun observeState() {
// TODO Update submitEnabled when other state members are updated.
}
fun handle(action: LoginActions) {
when (action) {
is LoginActions.SetHomeserver -> handleSetHomeserver(action)
is LoginActions.SetLogin -> handleSetName(action)
is LoginActions.SetPassword -> handleSetPassword(action)
LoginActions.Submit -> handleSubmit()
}
}
private fun handleSetHomeserver(action: LoginActions.SetHomeserver) {
_state.value = _state.value.copy(
homeserver = action.homeserver,
submitEnabled = _state.value.login.isNotEmpty() &&
_state.value.password.isNotEmpty() &&
action.homeserver.isNotEmpty()
)
}
private fun handleSubmit() {
// TODO
viewModelScope.launch {
val currentState = state.value
try {
matrix.login(currentState.homeserver, currentState.login, currentState.password)
} catch (throwable: Throwable) {
Log.e("Error", "Cannot login", throwable)
}
}
}
private fun handleSetPassword(action: LoginActions.SetPassword) {
_state.value = _state.value.copy(
password = action.password,
submitEnabled = _state.value.login.isNotEmpty() && action.password.isNotEmpty()
submitEnabled = _state.value.login.isNotEmpty() &&
_state.value.homeserver.isNotEmpty() &&
action.password.isNotEmpty()
)
}
private fun handleSetName(action: LoginActions.SetLogin) {
_state.value = _state.value.copy(
login = action.login,
submitEnabled = action.login.isNotEmpty() && _state.value.password.isNotEmpty()
submitEnabled = action.login.isNotEmpty() &&
_state.value.homeserver.isNotEmpty() &&
_state.value.password.isNotEmpty()
)
}
}

View file

@ -1,6 +1,7 @@
package io.element.android.x.ui.screen.login
data class LoginViewState(
val homeserver: String = "matrix.org",
val login: String = "",
val password: String = "",
val submitEnabled: Boolean = false,