-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Suggest and track frequently used emoji reactions
Change-Id: Ia5500017459bbd254f069ba0adb69aecfd3e67f8
- Loading branch information
1 parent
01eefc9
commit 2287bb6
Showing
18 changed files
with
320 additions
and
48 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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
3 changes: 3 additions & 0 deletions
3
...kotlin/io/element/android/features/messages/impl/actionlist/ScActionListViewExtensions.kt
This file contains 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,3 @@ | ||
package io.element.android.features.messages.impl.actionlist | ||
|
||
internal const val EMOJI_COUNT_QUICK_PICKER = 6 |
53 changes: 53 additions & 0 deletions
53
...src/main/kotlin/io/element/android/features/messages/impl/emojis/RecentEmojiDataSource.kt
This file contains 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,53 @@ | ||
package io.element.android.features.messages.impl.emojis | ||
|
||
import chat.schildi.matrixsdk.ACCOUNT_DATA_RECENT_EMOJI | ||
import chat.schildi.matrixsdk.RecentEmojiItem | ||
import chat.schildi.matrixsdk.RecentEmojiSerializer | ||
import chat.schildi.matrixsdk.isValidRecentEmoji | ||
import chat.schildi.matrixsdk.recordSelection | ||
import io.element.android.libraries.matrix.api.MatrixClient | ||
import kotlinx.collections.immutable.ImmutableList | ||
import kotlinx.collections.immutable.persistentListOf | ||
import kotlinx.collections.immutable.toImmutableList | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.launch | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
|
||
class RecentEmojiDataSource @Inject constructor( | ||
private val client: MatrixClient, | ||
) { | ||
|
||
private suspend fun getCurrentRecentEmojis(): List<RecentEmojiItem> { | ||
val result = client.getAccountData(ACCOUNT_DATA_RECENT_EMOJI)?.let { RecentEmojiSerializer.deserializeContent(it) } | ||
if (result?.isFailure != false) { | ||
Timber.w("Failed to load recent emojis: ${result?.exceptionOrNull()}") | ||
return emptyList() | ||
} | ||
return result.getOrNull().orEmpty() | ||
} | ||
|
||
suspend fun getRecentEmojisSorted(): List<String> { | ||
return getCurrentRecentEmojis().filter { isValidRecentEmoji(it.emoji) }.sortedByDescending { it.recurrences ?: 1L }.map { it.emoji } | ||
} | ||
|
||
suspend fun recordEmoji(emoji: String) { | ||
if (!isValidRecentEmoji(emoji)) { | ||
return | ||
} | ||
val data = getCurrentRecentEmojis().toMutableList() | ||
data.recordSelection(emoji) | ||
client.setAccountData(ACCOUNT_DATA_RECENT_EMOJI, RecentEmojiSerializer.serializeContent(data)) | ||
} | ||
|
||
private val _recentEmojis = MutableStateFlow<ImmutableList<String>>(persistentListOf()) | ||
val recentEmojis: StateFlow<ImmutableList<String>> = _recentEmojis | ||
|
||
fun refresh(coroutineScope: CoroutineScope) { | ||
coroutineScope.launch { | ||
_recentEmojis.emit(getRecentEmojisSorted().toImmutableList()) | ||
} | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.