Browse Source

Give the id to AvatarData to be able to compute initial properly.

jme/20-permission-management
Benoit Marty 2 years ago committed by Benoit Marty
parent
commit
443076f636
  1. 3
      features/messages/src/main/kotlin/io/element/android/features/messages/MessagesPresenter.kt
  2. 23
      features/messages/src/main/kotlin/io/element/android/features/messages/MessagesView.kt
  3. 3
      features/messages/src/main/kotlin/io/element/android/features/messages/timeline/TimelineItemsFactory.kt
  4. 4
      features/messages/src/main/kotlin/io/element/android/features/messages/timeline/TimelineView.kt
  5. 2
      features/messages/src/test/kotlin/io/element/android/features/messages/MessagesPresenterTest.kt
  6. 2
      features/messages/src/test/kotlin/io/element/android/features/messages/actionlist/ActionListPresenterTest.kt
  7. 14
      features/preferences/src/main/kotlin/io/element/android/features/preferences/root/PreferencesRootView.kt
  8. 13
      features/roomlist/src/main/kotlin/io/element/android/features/roomlist/RoomListPresenter.kt
  9. 2
      features/roomlist/src/main/kotlin/io/element/android/features/roomlist/RoomListView.kt
  10. 2
      features/roomlist/src/main/kotlin/io/element/android/features/roomlist/components/RoomListTopBar.kt
  11. 2
      features/roomlist/src/main/kotlin/io/element/android/features/roomlist/model/RoomListRoomSummary.kt
  12. 2
      features/roomlist/src/main/kotlin/io/element/android/features/roomlist/model/RoomListRoomSummaryPlaceholders.kt
  13. 4
      features/roomlist/src/main/kotlin/io/element/android/features/roomlist/model/stubbed.kt
  14. 2
      features/roomlist/src/test/kotlin/io/element/android/features/roomlist/RoomListPresenterTests.kt
  15. 13
      libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/components/avatar/Avatar.kt
  16. 23
      libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/components/avatar/AvatarData.kt
  17. 3
      libraries/matrixui/src/main/kotlin/io/element/android/libraries/matrix/ui/MatrixItemHelper.kt
  18. 18
      libraries/matrixui/src/main/kotlin/io/element/android/libraries/matrix/ui/components/MatrixUserPreviewParameterProvider.kt
  19. 2
      libraries/matrixui/src/main/kotlin/io/element/android/libraries/matrix/ui/model/MatrixUser.kt

3
features/messages/src/main/kotlin/io/element/android/features/messages/MessagesPresenter.kt

@ -67,7 +67,8 @@ class MessagesPresenter @Inject constructor( @@ -67,7 +67,8 @@ class MessagesPresenter @Inject constructor(
LaunchedEffect(syncUpdateFlow) {
roomAvatar.value =
AvatarData(
name = room.bestName,
id = room.roomId.value,
name = room.name,
url = room.avatarUrl,
size = AvatarSize.SMALL
)

23
features/messages/src/main/kotlin/io/element/android/features/messages/MessagesView.kt

@ -21,18 +21,7 @@ @@ -21,18 +21,7 @@
package io.element.android.features.messages
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBars
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.*
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.ModalBottomSheetValue
import androidx.compose.material.icons.Icons
@ -68,11 +57,7 @@ import io.element.android.libraries.designsystem.components.avatar.Avatar @@ -68,11 +57,7 @@ import io.element.android.libraries.designsystem.components.avatar.Avatar
import io.element.android.libraries.designsystem.components.avatar.AvatarData
import io.element.android.libraries.designsystem.preview.ElementPreviewDark
import io.element.android.libraries.designsystem.preview.ElementPreviewLight
import io.element.android.libraries.designsystem.theme.components.Icon
import io.element.android.libraries.designsystem.theme.components.IconButton
import io.element.android.libraries.designsystem.theme.components.Scaffold
import io.element.android.libraries.designsystem.theme.components.Text
import io.element.android.libraries.designsystem.theme.components.TopAppBar
import io.element.android.libraries.designsystem.theme.components.*
import io.element.android.libraries.designsystem.utils.LogCompositions
import io.element.android.libraries.matrix.core.RoomId
import io.element.android.libraries.textcomposer.MessageComposerMode
@ -223,9 +208,9 @@ fun MessagesViewDarkPreview() = ElementPreviewDark { ContentToPreview() } @@ -223,9 +208,9 @@ fun MessagesViewDarkPreview() = ElementPreviewDark { ContentToPreview() }
private fun ContentToPreview() {
MessagesView(
MessagesState(
roomId = RoomId(""),
roomId = RoomId("!id"),
roomName = "Room name",
roomAvatar = AvatarData("Room name"),
roomAvatar = AvatarData("!id", "Room name"),
composerState = aMessageComposerState().copy(
text = StableCharSequence("Hello"),
isFullScreen = false,

3
features/messages/src/main/kotlin/io/element/android/features/messages/timeline/TimelineItemsFactory.kt

@ -156,7 +156,8 @@ class TimelineItemsFactory @Inject constructor( @@ -156,7 +156,8 @@ class TimelineItemsFactory @Inject constructor(
val senderDisplayName = room.userDisplayName(currentSender).getOrNull()
val senderAvatarUrl = room.userAvatarUrl(currentSender).getOrNull()
val senderAvatarData = AvatarData(
name = senderDisplayName ?: currentSender,
id = currentSender,
name = senderDisplayName,
url = senderAvatarUrl,
size = AvatarSize.SMALL
)

4
features/messages/src/main/kotlin/io/element/android/features/messages/timeline/TimelineView.kt

@ -418,8 +418,8 @@ internal fun createMessageEvent( @@ -418,8 +418,8 @@ internal fun createMessageEvent(
): TimelineItem.MessageEvent {
return TimelineItem.MessageEvent(
id = EventId(Math.random().toString()),
senderId = "senderId",
senderAvatar = AvatarData("sender"),
senderId = "@senderId",
senderAvatar = AvatarData("@senderId", "sender"),
content = content,
reactionsState = TimelineItemReactions(
persistentListOf(

2
features/messages/src/test/kotlin/io/element/android/features/messages/MessagesPresenterTest.kt

@ -170,7 +170,7 @@ private fun aMessageEvent( @@ -170,7 +170,7 @@ private fun aMessageEvent(
id = AN_EVENT_ID,
senderId = A_USER_ID.value,
senderDisplayName = A_USER_NAME,
senderAvatar = AvatarData(),
senderAvatar = AvatarData(A_USER_ID.value, A_USER_NAME),
content = content,
sentTime = "",
isMine = isMine,

2
features/messages/src/test/kotlin/io/element/android/features/messages/actionlist/ActionListPresenterTest.kt

@ -168,7 +168,7 @@ private fun aMessageEvent( @@ -168,7 +168,7 @@ private fun aMessageEvent(
id = AN_EVENT_ID,
senderId = A_USER_ID.value,
senderDisplayName = A_USER_NAME,
senderAvatar = AvatarData(),
senderAvatar = AvatarData(A_USER_ID.value, A_USER_NAME),
content = content,
sentTime = "",
isMine = isMine,

14
features/preferences/src/main/kotlin/io/element/android/features/preferences/root/PreferencesRootView.kt

@ -20,12 +20,16 @@ import androidx.compose.runtime.Composable @@ -20,12 +20,16 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import io.element.android.features.logout.LogoutPreferenceView
import io.element.android.features.preferences.user.UserPreferences
import io.element.android.features.rageshake.preferences.RageshakePreferencesView
import io.element.android.libraries.architecture.Async
import io.element.android.libraries.designsystem.components.preferences.PreferenceView
import io.element.android.libraries.designsystem.preview.ElementPreviewDark
import io.element.android.libraries.designsystem.preview.ElementPreviewLight
import io.element.android.libraries.matrix.ui.components.MatrixUserPreviewParameterProvider
import io.element.android.libraries.matrix.ui.model.MatrixUser
import io.element.android.libraries.ui.strings.R as StringR
@Composable
@ -55,13 +59,15 @@ fun PreferencesRootView( @@ -55,13 +59,15 @@ fun PreferencesRootView(
@Preview
@Composable
fun PreferencesRootViewLightPreview() = ElementPreviewLight { ContentToPreview() }
fun PreferencesRootViewLightPreview(@PreviewParameter(MatrixUserPreviewParameterProvider::class) matrixUser: MatrixUser) =
ElementPreviewLight { ContentToPreview(matrixUser) }
@Preview
@Composable
fun PreferencesRootViewDarkPreview() = ElementPreviewDark { ContentToPreview() }
fun PreferencesRootViewDarkPreview(@PreviewParameter(MatrixUserPreviewParameterProvider::class) matrixUser: MatrixUser) =
ElementPreviewDark { ContentToPreview(matrixUser) }
@Composable
private fun ContentToPreview() {
PreferencesRootView(aPreferencesRootState())
private fun ContentToPreview(matrixUser: MatrixUser) {
PreferencesRootView(aPreferencesRootState().copy(myUser = Async.Success(matrixUser)))
}

13
features/roomlist/src/main/kotlin/io/element/android/features/roomlist/RoomListPresenter.kt

@ -16,15 +16,8 @@ @@ -16,15 +16,8 @@
package io.element.android.features.roomlist
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import io.element.android.features.roomlist.model.RoomListEvents
import io.element.android.features.roomlist.model.RoomListRoomSummary
import io.element.android.features.roomlist.model.RoomListRoomSummaryPlaceholders
@ -107,7 +100,8 @@ class RoomListPresenter @Inject constructor( @@ -107,7 +100,8 @@ class RoomListPresenter @Inject constructor(
val userDisplayName = client.loadUserDisplayName().getOrNull()
val avatarData =
AvatarData(
name = userDisplayName ?: client.userId().value,
id = client.userId().value,
name = userDisplayName,
url = userAvatarUrl,
size = AvatarSize.SMALL
)
@ -136,6 +130,7 @@ class RoomListPresenter @Inject constructor( @@ -136,6 +130,7 @@ class RoomListPresenter @Inject constructor(
is RoomSummary.Empty -> RoomListRoomSummaryPlaceholders.create(roomSummary.identifier)
is RoomSummary.Filled -> {
val avatarData = AvatarData(
id = roomSummary.identifier(),
name = roomSummary.details.name,
url = roomSummary.details.avatarURLString
)

2
features/roomlist/src/main/kotlin/io/element/android/features/roomlist/RoomListView.kt

@ -168,7 +168,7 @@ fun RoomListViewDarkPreview() = ElementPreviewDark { ContentToPreview() } @@ -168,7 +168,7 @@ fun RoomListViewDarkPreview() = ElementPreviewDark { ContentToPreview() }
private fun ContentToPreview() {
RoomListView(
aRoomListState().copy(
matrixUser = MatrixUser(id = UserId("@id"), username = "User#1", avatarData = AvatarData("U")),
matrixUser = MatrixUser(id = UserId("@id"), username = "User#1", avatarData = AvatarData("@id", "U")),
roomList = stubbedRoomSummaries(),
filter = "filter",
)

2
features/roomlist/src/main/kotlin/io/element/android/features/roomlist/components/RoomListTopBar.kt

@ -239,7 +239,7 @@ fun DefaultRoomListTopBarDarkPreview() = ElementPreviewDark { DefaultRoomListTop @@ -239,7 +239,7 @@ fun DefaultRoomListTopBarDarkPreview() = ElementPreviewDark { DefaultRoomListTop
@Composable
private fun DefaultRoomListTopBarPreview() {
DefaultRoomListTopBar(
matrixUser = MatrixUser(UserId("id"), "Alice", AvatarData("Alice")),
matrixUser = MatrixUser(UserId("@id"), "Alice", AvatarData("@id", "Alice")),
scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior(rememberTopAppBarState()),
)
}

2
features/roomlist/src/main/kotlin/io/element/android/features/roomlist/model/RoomListRoomSummary.kt

@ -28,6 +28,6 @@ data class RoomListRoomSummary( @@ -28,6 +28,6 @@ data class RoomListRoomSummary(
val hasUnread: Boolean = false,
val timestamp: String? = null,
val lastMessage: CharSequence? = null,
val avatarData: AvatarData = AvatarData(),
val avatarData: AvatarData = AvatarData(id, name),
val isPlaceholder: Boolean = false,
)

2
features/roomlist/src/main/kotlin/io/element/android/features/roomlist/model/RoomListRoomSummaryPlaceholders.kt

@ -27,7 +27,7 @@ object RoomListRoomSummaryPlaceholders { @@ -27,7 +27,7 @@ object RoomListRoomSummaryPlaceholders {
name = "Short name",
timestamp = "hh:mm",
lastMessage = "Last message for placeholder",
avatarData = AvatarData("S")
avatarData = AvatarData(id, "S")
)
}

4
features/roomlist/src/main/kotlin/io/element/android/features/roomlist/model/stubbed.kt

@ -27,7 +27,7 @@ internal fun stubbedRoomSummaries(): ImmutableList<RoomListRoomSummary> { @@ -27,7 +27,7 @@ internal fun stubbedRoomSummaries(): ImmutableList<RoomListRoomSummary> {
hasUnread = true,
timestamp = "14:18",
lastMessage = "A very very very very long message which suites on two lines",
avatarData = AvatarData("R"),
avatarData = AvatarData("!id", "R"),
id = "roomId"
),
RoomListRoomSummary(
@ -35,7 +35,7 @@ internal fun stubbedRoomSummaries(): ImmutableList<RoomListRoomSummary> { @@ -35,7 +35,7 @@ internal fun stubbedRoomSummaries(): ImmutableList<RoomListRoomSummary> {
hasUnread = false,
timestamp = "14:16",
lastMessage = "A short message",
avatarData = AvatarData("Z"),
avatarData = AvatarData("!id", "Z"),
id = "roomId2"
),
RoomListRoomSummaryPlaceholders.create("roomId2")

2
features/roomlist/src/test/kotlin/io/element/android/features/roomlist/RoomListPresenterTests.kt

@ -217,6 +217,6 @@ private val aRoomListRoomSummary = RoomListRoomSummary( @@ -217,6 +217,6 @@ private val aRoomListRoomSummary = RoomListRoomSummary(
hasUnread = true,
timestamp = A_FORMATTED_DATE,
lastMessage = A_MESSAGE,
avatarData = AvatarData(name = A_ROOM_NAME),
avatarData = AvatarData(id = A_ROOM_ID.value, name = A_ROOM_NAME),
isPlaceholder = false,
)

13
libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/components/avatar/Avatar.kt

@ -29,6 +29,7 @@ import androidx.compose.ui.graphics.Brush @@ -29,6 +29,7 @@ import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.unit.sp
import coil.compose.AsyncImage
import io.element.android.libraries.designsystem.AvatarGradientEnd
@ -90,7 +91,7 @@ private fun InitialsAvatar( @@ -90,7 +91,7 @@ private fun InitialsAvatar(
) {
Text(
modifier = Modifier.align(Alignment.Center),
text = avatarData.name.first().uppercase(),
text = avatarData.getInitial(),
fontSize = (avatarData.size.value / 2).sp,
color = Color.White,
)
@ -99,13 +100,15 @@ private fun InitialsAvatar( @@ -99,13 +100,15 @@ private fun InitialsAvatar(
@Preview
@Composable
fun AvatarLightPreview() = ElementPreviewLight { ContentToPreview() }
fun AvatarLightPreview(@PreviewParameter(AvatarDataPreviewParameterProvider::class) avatarData: AvatarData) =
ElementPreviewLight { ContentToPreview(avatarData) }
@Preview
@Composable
fun AvatarDarkPreview() = ElementPreviewDark { ContentToPreview() }
fun AvatarDarkPreview(@PreviewParameter(AvatarDataPreviewParameterProvider::class) avatarData: AvatarData) =
ElementPreviewDark { ContentToPreview(avatarData) }
@Composable
private fun ContentToPreview() {
Avatar(AvatarData(name = "A"))
private fun ContentToPreview(avatarData: AvatarData) {
Avatar(avatarData)
}

23
libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/components/avatar/AvatarData.kt

@ -17,10 +17,31 @@ @@ -17,10 +17,31 @@
package io.element.android.libraries.designsystem.components.avatar
import androidx.compose.runtime.Immutable
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
@Immutable
data class AvatarData(
val name: String = "",
val id: String,
val name: String?,
val url: String? = null,
val size: AvatarSize = AvatarSize.MEDIUM
) {
fun getInitial(): String {
val firstChar = name?.firstOrNull() ?: id.getOrNull(1) ?: '?'
return firstChar.uppercase()
}
}
open class AvatarDataPreviewParameterProvider : PreviewParameterProvider<AvatarData> {
override val values: Sequence<AvatarData>
get() = sequenceOf(
anAvatarData(),
anAvatarData().copy(name = null),
)
}
fun anAvatarData() = AvatarData(
// Let's the id not start with a 'a'.
id = "@id_of_alice:server.org",
name = "Alice",
)

3
libraries/matrixui/src/main/kotlin/io/element/android/libraries/matrix/ui/MatrixItemHelper.kt

@ -38,7 +38,8 @@ class MatrixItemHelper @Inject constructor( @@ -38,7 +38,8 @@ class MatrixItemHelper @Inject constructor(
val userDisplayName = client.loadUserDisplayName().getOrNull()
val avatarData =
AvatarData(
userDisplayName ?: client.userId().value,
client.userId().value,
userDisplayName,
userAvatarUrl,
avatarSize
)

18
libraries/matrixui/src/main/kotlin/io/element/android/libraries/matrix/ui/components/MatrixUserPreviewParameterProvider.kt

@ -17,22 +17,24 @@ @@ -17,22 +17,24 @@
package io.element.android.libraries.matrix.ui.components
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import io.element.android.libraries.designsystem.components.avatar.AvatarData
import io.element.android.libraries.designsystem.components.avatar.anAvatarData
import io.element.android.libraries.matrix.core.UserId
import io.element.android.libraries.matrix.ui.model.MatrixUser
open class MatrixUserPreviewParameterProvider : PreviewParameterProvider<MatrixUser> {
override val values: Sequence<MatrixUser>
get() = sequenceOf(
aMatrixUser(),
MatrixUser(
id = UserId("@alice:server.org"),
username = "Alice",
avatarData = AvatarData("Alice")
),
MatrixUser(
id = UserId("@alice:server.org"),
id = UserId("@id_of_alice:server.org"),
username = null,
avatarData = AvatarData("Alice")
avatarData = anAvatarData().copy(name = null)
),
)
}
fun aMatrixUser() = MatrixUser(
id = UserId("@id_of_alice:server.org"),
username = "Alice",
avatarData = anAvatarData()
)

2
libraries/matrixui/src/main/kotlin/io/element/android/libraries/matrix/ui/model/MatrixUser.kt

@ -24,7 +24,7 @@ import io.element.android.libraries.matrix.core.UserId @@ -24,7 +24,7 @@ import io.element.android.libraries.matrix.core.UserId
data class MatrixUser(
val id: UserId,
val username: String? = null,
val avatarData: AvatarData = AvatarData(),
val avatarData: AvatarData = AvatarData(id.value, username),
)
fun MatrixUser.getBestName(): String {

Loading…
Cancel
Save