-
Notifications
You must be signed in to change notification settings - Fork 265
Add shortcut suggestions for rooms, remove then when leaving #5180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
e9c76fa
Report shortcut usage for outgoing messages
networkException 1dca47e
Simplify how to get the other user in a DM room
jmartinesp cfb8f81
Add initial avatar icons to shortcuts
jmartinesp 016e8fe
Remove room shortcuts when they're no longer joined
jmartinesp 9c3f642
Update screenshots
ElementBot 3f346b2
Fix lint issues
jmartinesp ab37093
Use bold typeface for the initials avatar instead
jmartinesp e0431b1
Update screenshots
ElementBot 517492c
Add tests for `DefaultNotificationConversationService`
jmartinesp 69c1346
Try using API 33 for the new tests.
jmartinesp 3ed0016
Improve the `InitialsAvatarBitmapGenerator` preview
jmartinesp 37172f0
Remove `threadId` from `NotificationConversationService.onSendMessage…
jmartinesp 7ebbf67
Add observers for the pin code and session logout states
jmartinesp e62f669
Use default avatar size to request it from Coil
jmartinesp 462031a
Wrap all calls to `ShortcutManagerCompat` with `runCatchingExceptions…
jmartinesp 3fea706
Remove duplicated docs, which are now outdated
jmartinesp 7272df9
Update screenshots
ElementBot cc1c54a
Make `DefaultNotificationConversationService` a singleton
jmartinesp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
...main/kotlin/io/element/android/libraries/matrix/ui/media/InitialsAvatarBitmapGenerator.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
* Copyright 2025 New Vector Ltd. | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial | ||
* Please see LICENSE files in the repository root for full details. | ||
*/ | ||
|
||
package io.element.android.libraries.matrix.ui.media | ||
|
||
import android.graphics.Canvas | ||
import android.graphics.Paint | ||
import android.graphics.Rect | ||
import android.graphics.Typeface | ||
import androidx.compose.foundation.Image | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.asImageBitmap | ||
import androidx.compose.ui.graphics.toArgb | ||
import androidx.compose.ui.unit.dp | ||
import androidx.core.graphics.createBitmap | ||
import coil3.Bitmap | ||
import io.element.android.compound.theme.AvatarColors | ||
import io.element.android.compound.theme.ElementTheme | ||
import io.element.android.compound.tokens.generated.SemanticColors | ||
import io.element.android.compound.tokens.generated.compoundColorsDark | ||
import io.element.android.compound.tokens.generated.compoundColorsLight | ||
import io.element.android.libraries.designsystem.components.avatar.AvatarData | ||
import io.element.android.libraries.designsystem.components.avatar.AvatarSize | ||
import io.element.android.libraries.designsystem.preview.ElementPreview | ||
import io.element.android.libraries.designsystem.preview.PreviewsDayNight | ||
import io.element.android.libraries.designsystem.theme.components.Text | ||
|
||
/** | ||
* Generates a bitmap for an initials avatar based on the provided [AvatarData]. | ||
*/ | ||
class InitialsAvatarBitmapGenerator( | ||
useDarkTheme: Boolean = false, | ||
private val fontSizePercentage: Float = 0.5f, | ||
) { | ||
private val compoundColors: SemanticColors = if (useDarkTheme) { | ||
compoundColorsDark | ||
} else { | ||
compoundColorsLight | ||
} | ||
|
||
// List of predefined avatar colors to use for initials avatars, in light mode | ||
private val allAvatarColors: List<AvatarColors> = listOf( | ||
AvatarColors( | ||
background = compoundColors.bgDecorative1, | ||
foreground = compoundColors.textDecorative1, | ||
), | ||
AvatarColors( | ||
background = compoundColors.bgDecorative2, | ||
foreground = compoundColors.textDecorative2, | ||
), | ||
AvatarColors( | ||
background = compoundColors.bgDecorative3, | ||
foreground = compoundColors.textDecorative3, | ||
), | ||
AvatarColors( | ||
background = compoundColors.bgDecorative4, | ||
foreground = compoundColors.textDecorative4, | ||
), | ||
AvatarColors( | ||
background = compoundColors.bgDecorative5, | ||
foreground = compoundColors.textDecorative5, | ||
), | ||
AvatarColors( | ||
background = compoundColors.bgDecorative6, | ||
foreground = compoundColors.textDecorative6, | ||
), | ||
) | ||
|
||
/** | ||
* Generates a bitmap for an avatar with no URL, using the initials from the [AvatarData]. | ||
* @param size The size of the bitmap to generate, in pixels. | ||
* @param avatarData The [AvatarData] containing the initials and other information. | ||
*/ | ||
fun generateBitmap(size: Int, avatarData: AvatarData): Bitmap? { | ||
if (avatarData.url != null) { | ||
// This generator is only for initials avatars, not for avatars with URLs | ||
return null | ||
} | ||
|
||
// Get the color pair to use for the initials avatar | ||
val avatarColors = allAvatarColors[avatarData.id.sumOf { it.code } % allAvatarColors.size] | ||
|
||
val bitmap = createBitmap(size, size) | ||
Canvas(bitmap).run { | ||
drawColor(avatarColors.background.toArgb()) | ||
val letter = avatarData.initialLetter | ||
|
||
val textPaint = Paint().apply { | ||
color = avatarColors.foreground.toArgb() | ||
textSize = size * fontSizePercentage // Adjust text size relative to the avatar size | ||
isAntiAlias = true | ||
textAlign = Paint.Align.CENTER | ||
typeface = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD) | ||
} | ||
val bounds = Rect() | ||
textPaint.getTextBounds(letter, 0, letter.length, bounds) | ||
drawText( | ||
letter, | ||
size / 2f, | ||
size.toFloat() / 2 - (textPaint.descent() + textPaint.ascent()) / 2, | ||
textPaint | ||
) | ||
} | ||
|
||
return bitmap | ||
} | ||
} | ||
|
||
@Composable | ||
@PreviewsDayNight | ||
internal fun InitialsAvatarBitmapGeneratorPreview() = ElementPreview { | ||
Column( | ||
verticalArrangement = Arrangement.spacedBy(4.dp) | ||
) { | ||
repeat(6) { index -> | ||
val avatarData = remember { AvatarData(id = index.toString(), name = Char('0'.code + index).toString(), size = AvatarSize.IncomingCall) } | ||
val isLightTheme = ElementTheme.isLightTheme | ||
val bitmap = remember(isLightTheme) { | ||
val generator = InitialsAvatarBitmapGenerator(useDarkTheme = !isLightTheme) | ||
generator.generateBitmap(512, avatarData)?.asImageBitmap() | ||
} | ||
|
||
bitmap?.let { | ||
Image(bitmap = it, contentDescription = null, modifier = Modifier.size(48.dp)) | ||
} ?: Text("No avatar generated") | ||
} | ||
} | ||
} | ||
bmarty marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not really convinced of adding it here, but the alternative would be to trigger it when the room list updates which is way too often IMO... maybe there's a better middle ground.