Fix bunch of issues around login/logout and introduces messages feature
This commit is contained in:
parent
b5e4ef9637
commit
d04d847521
19 changed files with 314 additions and 89 deletions
|
|
@ -1,10 +1,6 @@
|
|||
package io.element.android.x.features.login
|
||||
|
||||
import android.util.Log
|
||||
import com.airbnb.mvrx.Fail
|
||||
import com.airbnb.mvrx.Loading
|
||||
import com.airbnb.mvrx.MavericksViewModel
|
||||
import com.airbnb.mvrx.Success
|
||||
import io.element.android.x.matrix.MatrixInstance
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
|
|
@ -24,13 +20,11 @@ class LoginViewModel(initialState: LoginViewState) :
|
|||
|
||||
private fun handleSubmit() = withState { state ->
|
||||
viewModelScope.launch {
|
||||
setState { copy(isLoggedIn = Loading()) }
|
||||
try {
|
||||
suspend {
|
||||
matrix.login(state.homeserver, state.login, state.password)
|
||||
setState { copy(isLoggedIn = Success(Unit)) }
|
||||
} catch (throwable: Throwable) {
|
||||
Log.e("Error", "Cannot login", throwable)
|
||||
setState { copy(isLoggedIn = Fail(throwable)) }
|
||||
Unit
|
||||
}.execute {
|
||||
copy(isLoggedIn = it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,41 +0,0 @@
|
|||
plugins {
|
||||
id 'com.android.library'
|
||||
id 'org.jetbrains.kotlin.android'
|
||||
}
|
||||
|
||||
android {
|
||||
namespace 'io.element.android.x.features.messages'
|
||||
compileSdk 32
|
||||
|
||||
defaultConfig {
|
||||
minSdk 24
|
||||
targetSdk 32
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
consumerProguardFiles "consumer-rules.pro"
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
kotlinOptions {
|
||||
jvmTarget = '1.8'
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
||||
implementation 'androidx.core:core-ktx:1.7.0'
|
||||
implementation 'androidx.appcompat:appcompat:1.4.1'
|
||||
implementation 'com.google.android.material:material:1.5.0'
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
|
||||
}
|
||||
19
features/messages/build.gradle.kts
Normal file
19
features/messages/build.gradle.kts
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
plugins {
|
||||
id("io.element.android-compose")
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "io.element.android.x.features.messages"
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(project(":libraries:core"))
|
||||
implementation(project(":libraries:matrix"))
|
||||
implementation(project(":libraries:designsystem"))
|
||||
implementation(libs.mavericks.compose)
|
||||
implementation(libs.timber)
|
||||
implementation(libs.datetime)
|
||||
testImplementation("junit:junit:4.13.2")
|
||||
androidTestImplementation("androidx.test.ext:junit:1.1.3")
|
||||
androidTestImplementation("androidx.test.espresso:espresso-core:3.4.0")
|
||||
}
|
||||
2
features/messages/proguard-rules.pro
vendored
2
features/messages/proguard-rules.pro
vendored
|
|
@ -1,6 +1,6 @@
|
|||
# Add project specific ProGuard rules here.
|
||||
# You can control the set of applied configuration files using the
|
||||
# proguardFiles setting in build.gradle.
|
||||
# proguardFiles setting in build.gradle.kts.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
|
|
|||
|
|
@ -0,0 +1,44 @@
|
|||
@file:OptIn(ExperimentalMaterial3Api::class)
|
||||
|
||||
package io.element.android.x.features.messages
|
||||
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.input.nestedscroll.nestedScroll
|
||||
import com.airbnb.mvrx.compose.collectAsState
|
||||
import com.airbnb.mvrx.compose.mavericksViewModel
|
||||
import io.element.android.x.core.data.LogCompositions
|
||||
import io.element.android.x.features.messages.model.MessagesViewState
|
||||
|
||||
@Composable
|
||||
fun MessagesScreen(roomId: String) {
|
||||
val viewModel: MessagesViewModel = mavericksViewModel(argsFactory = { roomId })
|
||||
LogCompositions(tag = "MessagesScreen", msg = "Root")
|
||||
val roomTitle by viewModel.collectAsState(prop1 = MessagesViewState::roomTitle)
|
||||
MessagesContent(roomTitle)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun MessagesContent(roomTitle: String) {
|
||||
val appBarState = rememberTopAppBarState()
|
||||
val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior(appBarState)
|
||||
LogCompositions(tag = "RoomListScreen", msg = "Content")
|
||||
Scaffold(
|
||||
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
|
||||
topBar = {
|
||||
TopAppBar(
|
||||
title = { Text(text = roomTitle) }
|
||||
)
|
||||
},
|
||||
content = { padding ->
|
||||
Box(modifier = Modifier.padding(padding))
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
package io.element.android.x.features.messages
|
||||
|
||||
import com.airbnb.mvrx.Fail
|
||||
import com.airbnb.mvrx.Loading
|
||||
import com.airbnb.mvrx.MavericksViewModel
|
||||
import com.airbnb.mvrx.Success
|
||||
import io.element.android.x.core.data.parallelMap
|
||||
import io.element.android.x.designsystem.components.avatar.AvatarData
|
||||
import io.element.android.x.designsystem.components.avatar.AvatarSize
|
||||
import io.element.android.x.features.messages.model.MessagesViewState
|
||||
import io.element.android.x.matrix.MatrixClient
|
||||
import io.element.android.x.matrix.MatrixInstance
|
||||
import io.element.android.x.matrix.room.RoomSummary
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.flowOn
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.launch
|
||||
import org.matrix.rustcomponents.sdk.mediaSourceFromUrl
|
||||
|
||||
class MessagesViewModel(initialState: MessagesViewState) :
|
||||
MavericksViewModel<MessagesViewState>(initialState) {
|
||||
|
||||
private val matrix = MatrixInstance.getInstance()
|
||||
|
||||
init {
|
||||
handleInit()
|
||||
}
|
||||
|
||||
private fun handleInit() {
|
||||
viewModelScope.launch {
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun loadAvatarData(
|
||||
client: MatrixClient,
|
||||
name: String,
|
||||
url: String?,
|
||||
size: AvatarSize = AvatarSize.MEDIUM
|
||||
): AvatarData {
|
||||
val mediaContent = url?.let {
|
||||
val mediaSource = mediaSourceFromUrl(it)
|
||||
client.loadMediaThumbnailForSource(mediaSource, size.value.toLong(), size.value.toLong())
|
||||
}
|
||||
return mediaContent?.fold(
|
||||
{ it },
|
||||
{ null }
|
||||
).let { model ->
|
||||
AvatarData(name.first().uppercase(), model, size)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun getClient(): MatrixClient {
|
||||
return matrix.matrixClient().first().get()
|
||||
}
|
||||
|
||||
override fun onCleared() {
|
||||
super.onCleared()
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
package io.element.android.x.features.messages.model
|
||||
|
||||
import com.airbnb.mvrx.MavericksState
|
||||
import io.element.android.x.designsystem.components.avatar.AvatarData
|
||||
import io.element.android.x.matrix.core.RoomId
|
||||
|
||||
data class MessagesViewState(
|
||||
val roomId: String,
|
||||
val roomTitle: String = "",
|
||||
val roomAvatar: AvatarData? = null
|
||||
) : MavericksState {
|
||||
|
||||
@Suppress("unused")
|
||||
constructor(roomId: String) : this(roomId = roomId, roomTitle = "", roomAvatar = null)
|
||||
|
||||
}
|
||||
|
|
@ -14,11 +14,13 @@ import androidx.compose.runtime.getValue
|
|||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.input.nestedscroll.nestedScroll
|
||||
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.core.data.LogCompositions
|
||||
import io.element.android.x.designsystem.ElementXTheme
|
||||
import io.element.android.x.designsystem.components.ProgressDialog
|
||||
import io.element.android.x.designsystem.components.avatar.AvatarData
|
||||
import io.element.android.x.features.roomlist.components.RoomItem
|
||||
import io.element.android.x.features.roomlist.components.RoomListTopBar
|
||||
|
|
@ -46,7 +48,8 @@ fun RoomListScreen(
|
|||
roomSummaries = roomSummaries().orEmpty(),
|
||||
matrixUser = matrixUser(),
|
||||
onRoomClicked = onRoomClicked,
|
||||
onLogoutClicked = viewModel::logout
|
||||
onLogoutClicked = viewModel::logout,
|
||||
isLoginOut = logoutAction is Loading
|
||||
)
|
||||
}
|
||||
|
||||
|
|
@ -56,6 +59,7 @@ fun RoomListContent(
|
|||
matrixUser: MatrixUser?,
|
||||
onRoomClicked: (RoomId) -> Unit,
|
||||
onLogoutClicked: () -> Unit,
|
||||
isLoginOut: Boolean,
|
||||
) {
|
||||
val appBarState = rememberTopAppBarState()
|
||||
val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior(appBarState)
|
||||
|
|
@ -75,6 +79,9 @@ fun RoomListContent(
|
|||
}
|
||||
}
|
||||
)
|
||||
if (isLoginOut) {
|
||||
ProgressDialog("Login out...")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -86,7 +93,8 @@ private fun PreviewableRoomListContent() {
|
|||
roomSummaries = stubbedRoomSummaries(),
|
||||
matrixUser = MatrixUser("User#1", avatarData = AvatarData("U")),
|
||||
onRoomClicked = {},
|
||||
onLogoutClicked = {}
|
||||
onLogoutClicked = {},
|
||||
isLoginOut = false
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -99,7 +107,8 @@ private fun PreviewableDarkRoomListContent() {
|
|||
roomSummaries = stubbedRoomSummaries(),
|
||||
matrixUser = MatrixUser("User#1", avatarData = AvatarData("U")),
|
||||
onRoomClicked = {},
|
||||
onLogoutClicked = {}
|
||||
onLogoutClicked = {},
|
||||
isLoginOut = true
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,6 @@
|
|||
package io.element.android.x.features.roomlist
|
||||
|
||||
import com.airbnb.mvrx.Fail
|
||||
import com.airbnb.mvrx.Loading
|
||||
import com.airbnb.mvrx.MavericksViewModel
|
||||
import com.airbnb.mvrx.Success
|
||||
import io.element.android.x.core.data.parallelMap
|
||||
import io.element.android.x.designsystem.components.avatar.AvatarData
|
||||
import io.element.android.x.designsystem.components.avatar.AvatarSize
|
||||
|
|
@ -14,6 +11,8 @@ import io.element.android.x.matrix.MatrixClient
|
|||
import io.element.android.x.matrix.MatrixInstance
|
||||
import io.element.android.x.matrix.room.RoomSummary
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.flowOn
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.launch
|
||||
|
|
@ -47,7 +46,12 @@ class RoomListViewModel(initialState: RoomListViewState) :
|
|||
val userAvatarUrl = client.loadUserAvatarURLString().getOrNull()
|
||||
val userDisplayName = client.loadUserDisplayName().getOrNull()
|
||||
val avatarData =
|
||||
loadAvatarData(client, userDisplayName ?: client.userId().value, userAvatarUrl, AvatarSize.SMALL)
|
||||
loadAvatarData(
|
||||
client,
|
||||
userDisplayName ?: client.userId().value,
|
||||
userAvatarUrl,
|
||||
AvatarSize.SMALL
|
||||
)
|
||||
MatrixUser(
|
||||
username = userDisplayName ?: client.userId().value,
|
||||
avatarUrl = userAvatarUrl,
|
||||
|
|
@ -101,7 +105,11 @@ class RoomListViewModel(initialState: RoomListViewState) :
|
|||
): AvatarData {
|
||||
val mediaContent = url?.let {
|
||||
val mediaSource = mediaSourceFromUrl(it)
|
||||
client.loadMediaThumbnailForSource(mediaSource, size.value.toLong(), size.value.toLong())
|
||||
client.loadMediaThumbnailForSource(
|
||||
mediaSource,
|
||||
size.value.toLong(),
|
||||
size.value.toLong()
|
||||
)
|
||||
}
|
||||
return mediaContent?.fold(
|
||||
{ it },
|
||||
|
|
@ -113,18 +121,17 @@ class RoomListViewModel(initialState: RoomListViewState) :
|
|||
|
||||
private fun handleLogout() {
|
||||
viewModelScope.launch {
|
||||
setState { copy(logoutAction = Loading()) }
|
||||
try {
|
||||
suspend {
|
||||
delay(2000)
|
||||
getClient().logout()
|
||||
setState { copy(logoutAction = Success(Unit)) }
|
||||
} catch (throwable: Throwable) {
|
||||
setState { copy(logoutAction = Fail(throwable)) }
|
||||
}.execute {
|
||||
copy(logoutAction = it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun getClient(): MatrixClient {
|
||||
return matrix.restoreSession()!!
|
||||
return matrix.matrixClient().first().get()
|
||||
}
|
||||
|
||||
override fun onCleared() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue