Avatar working - WIP
This commit is contained in:
parent
71ca64644a
commit
13be8f3fac
6 changed files with 53 additions and 10 deletions
|
|
@ -65,9 +65,13 @@ class MatrixClient internal constructor(
|
||||||
sessionStore.reset()
|
sessionStore.reset()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun userId(): String = client.userId()
|
||||||
fun username(): String = client.displayName()
|
fun username(): String = client.displayName()
|
||||||
fun avatarUrl(): String = client.avatarUrl()
|
fun avatarUrl(): String = client.avatarUrl()
|
||||||
|
|
||||||
|
fun loadMedia(source: MediaSource) = client.getMediaContent(source)
|
||||||
|
fun loadMedia2(mxcUrl: String) = client.getMediaContent(mediaSourceFromUrl(mxcUrl))
|
||||||
|
|
||||||
interface SlidingSyncListener {
|
interface SlidingSyncListener {
|
||||||
fun onSyncUpdate(summary: UpdateSummary, rooms: List<Room>)
|
fun onSyncUpdate(summary: UpdateSummary, rooms: List<Room>)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,4 +3,5 @@ package io.element.android.x.ui.screen.roomlist
|
||||||
data class MatrixUser(
|
data class MatrixUser(
|
||||||
val username: String? = null,
|
val username: String? = null,
|
||||||
val avatarUrl: String? = null,
|
val avatarUrl: String? = null,
|
||||||
|
val avatarData: List<UByte>? = null,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package io.element.android.x.ui.screen.roomlist
|
package io.element.android.x.ui.screen.roomlist
|
||||||
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.util.Log
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.activity.ComponentActivity
|
import androidx.activity.ComponentActivity
|
||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
|
|
@ -23,6 +24,7 @@ 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.ui.theme.ElementXTheme
|
import io.element.android.x.ui.theme.ElementXTheme
|
||||||
|
import io.element.android.x.ui.theme.components.Avatar
|
||||||
import org.matrix.rustcomponents.sdk.Room
|
import org.matrix.rustcomponents.sdk.Room
|
||||||
|
|
||||||
class RoomListActivity : ComponentActivity() {
|
class RoomListActivity : ComponentActivity() {
|
||||||
|
|
@ -107,14 +109,7 @@ class RoomListActivity : ComponentActivity() {
|
||||||
Row(
|
Row(
|
||||||
modifier = Modifier.fillMaxWidth()
|
modifier = Modifier.fillMaxWidth()
|
||||||
) {
|
) {
|
||||||
Image(
|
Avatar(data = matrixUser.avatarData)
|
||||||
painter = rememberAsyncImagePainter(matrixUser.avatarUrl),
|
|
||||||
contentDescription = null,
|
|
||||||
modifier = Modifier
|
|
||||||
.size(48.dp)
|
|
||||||
.clip(CircleShape)
|
|
||||||
.border(1.5.dp, MaterialTheme.colorScheme.primary, CircleShape)
|
|
||||||
)
|
|
||||||
Spacer(modifier = Modifier.width(8.dp))
|
Spacer(modifier = Modifier.width(8.dp))
|
||||||
Text("${matrixUser.username}")
|
Text("${matrixUser.username}")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ import kotlinx.coroutines.launch
|
||||||
import org.matrix.rustcomponents.sdk.Room
|
import org.matrix.rustcomponents.sdk.Room
|
||||||
import org.matrix.rustcomponents.sdk.StoppableSpawn
|
import org.matrix.rustcomponents.sdk.StoppableSpawn
|
||||||
import org.matrix.rustcomponents.sdk.UpdateSummary
|
import org.matrix.rustcomponents.sdk.UpdateSummary
|
||||||
|
import org.matrix.rustcomponents.sdk.mediaSourceFromUrl
|
||||||
|
|
||||||
class RoomListViewModel(initialState: RoomListViewState) :
|
class RoomListViewModel(initialState: RoomListViewState) :
|
||||||
MavericksViewModel<RoomListViewState>(initialState), MatrixClient.SlidingSyncListener {
|
MavericksViewModel<RoomListViewState>(initialState), MatrixClient.SlidingSyncListener {
|
||||||
|
|
@ -29,11 +30,15 @@ class RoomListViewModel(initialState: RoomListViewState) :
|
||||||
private fun handleInit() {
|
private fun handleInit() {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
val client = getClient()
|
val client = getClient()
|
||||||
|
val url = client.avatarUrl()
|
||||||
|
val mediaSource = mediaSourceFromUrl(url)
|
||||||
|
mediaSource.url()
|
||||||
setState {
|
setState {
|
||||||
copy(
|
copy(
|
||||||
user = MatrixUser(
|
user = MatrixUser(
|
||||||
tryOrNull { client.username() } ?: "Room list",
|
username = tryOrNull { client.username() } ?: "Room list",
|
||||||
tryOrNull { client.avatarUrl() } ?: "https://previews.123rf.com/images/lkeskinen/lkeskinen1802/lkeskinen180208322/95731150-exemple-de-tampon-%C3%A9tiquette-typographique-timbre-ou-ic%C3%B4ne.jpg",
|
avatarUrl = mediaSource.url(),
|
||||||
|
avatarData = client.loadMedia2(url)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -47,4 +47,6 @@ dependencies {
|
||||||
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"
|
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"
|
||||||
|
|
||||||
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1"
|
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1"
|
||||||
|
|
||||||
|
implementation 'io.coil-kt:coil-compose:2.2.1'
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,36 @@
|
||||||
|
package io.element.android.x.ui.theme.components
|
||||||
|
|
||||||
|
import android.util.Log
|
||||||
|
import androidx.compose.foundation.Image
|
||||||
|
import androidx.compose.foundation.border
|
||||||
|
import androidx.compose.foundation.layout.size
|
||||||
|
import androidx.compose.foundation.shape.CircleShape
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.draw.clip
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import coil.compose.rememberAsyncImagePainter
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO fallback Avatar
|
||||||
|
*/
|
||||||
|
@Composable
|
||||||
|
fun Avatar(
|
||||||
|
data: List<UByte>?,
|
||||||
|
size: Int = 48,
|
||||||
|
) {
|
||||||
|
Image(
|
||||||
|
painter = rememberAsyncImagePainter(
|
||||||
|
model = data?.toUByteArray()?.toByteArray(),
|
||||||
|
onError = {
|
||||||
|
Log.e("TAG", "Error $it\n${it.result}", it.result.throwable)
|
||||||
|
}),
|
||||||
|
contentDescription = null,
|
||||||
|
modifier = Modifier
|
||||||
|
.size(size.dp)
|
||||||
|
.clip(CircleShape)
|
||||||
|
.border(1.5.dp, MaterialTheme.colorScheme.primary, CircleShape)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue