feat(chess): add dismiss/clear-all for finished games in lobby
Persist dismissed game IDs locally (SharedPreferences on Android, java.util.prefs on Desktop) so completed games can be permanently hidden from the chess lobby. Adds per-game dismiss button and "Clear all" action when 2+ finished games exist. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
+38
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.commons.chess
|
||||
|
||||
/**
|
||||
* Persists dismissed chess game IDs locally per user.
|
||||
* Uses expect/actual for platform-specific storage.
|
||||
*/
|
||||
expect class ChessDismissedGamesStorage private constructor() {
|
||||
companion object {
|
||||
fun create(context: Any? = null): ChessDismissedGamesStorage
|
||||
}
|
||||
|
||||
fun load(userPubkey: String): Set<String>
|
||||
|
||||
fun save(
|
||||
userPubkey: String,
|
||||
ids: Set<String>,
|
||||
)
|
||||
}
|
||||
+14
@@ -27,8 +27,12 @@ import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Close
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedButton
|
||||
import androidx.compose.material3.Text
|
||||
@@ -289,6 +293,7 @@ fun CompletedGameCard(
|
||||
isDraw: Boolean,
|
||||
moveCount: Int,
|
||||
onClick: (() -> Unit)? = null,
|
||||
onDismiss: (() -> Unit)? = null,
|
||||
modifier: Modifier = Modifier,
|
||||
avatar: @Composable (() -> Unit)? = null,
|
||||
) {
|
||||
@@ -340,6 +345,15 @@ fun CompletedGameCard(
|
||||
color = resultColor,
|
||||
fontWeight = FontWeight.Bold,
|
||||
)
|
||||
if (onDismiss != null) {
|
||||
IconButton(onClick = onDismiss) {
|
||||
Icon(
|
||||
Icons.Default.Close,
|
||||
contentDescription = "Dismiss",
|
||||
tint = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+20
@@ -125,9 +125,15 @@ class ChessLobbyLogic(
|
||||
private val metadataProvider: IUserMetadataProvider,
|
||||
private val scope: CoroutineScope,
|
||||
pollingConfig: ChessPollingConfig = ChessPollingDefaults.android,
|
||||
private val dismissedStorage: ChessDismissedGamesStorage? = null,
|
||||
) {
|
||||
val state = ChessLobbyState(userPubkey, scope)
|
||||
|
||||
private val dismissedGameIds: MutableSet<String> =
|
||||
java.util.Collections.synchronizedSet(
|
||||
dismissedStorage?.load(userPubkey)?.toMutableSet() ?: mutableSetOf(),
|
||||
)
|
||||
|
||||
// Track when games were last loaded to prevent duplicate fetches
|
||||
// (e.g., discoverUserGames loads a game, then polling immediately re-fetches it)
|
||||
private val recentlyLoadedGames = java.util.concurrent.ConcurrentHashMap<String, Long>()
|
||||
@@ -869,6 +875,7 @@ class ChessLobbyLogic(
|
||||
|
||||
for (startEventId in newGameIds) {
|
||||
if (startEventId in completedGameIds) continue
|
||||
if (startEventId in dismissedGameIds) continue
|
||||
|
||||
val events = fetcher.fetchGameEvents(startEventId)
|
||||
val result = ChessGameLoader.loadGame(events, userPubkey)
|
||||
@@ -942,6 +949,19 @@ class ChessLobbyLogic(
|
||||
return null
|
||||
}
|
||||
|
||||
fun dismissCompletedGame(gameId: String) {
|
||||
state.removeCompletedGame(gameId)
|
||||
dismissedGameIds.add(gameId)
|
||||
dismissedStorage?.save(userPubkey, HashSet(dismissedGameIds))
|
||||
}
|
||||
|
||||
fun dismissAllCompletedGames() {
|
||||
val allIds = state.completedGames.value.map { it.gameId }
|
||||
state.clearCompletedGames()
|
||||
dismissedGameIds.addAll(allIds)
|
||||
dismissedStorage?.save(userPubkey, HashSet(dismissedGameIds))
|
||||
}
|
||||
|
||||
/**
|
||||
* Dismiss a finished game from the active/spectating list and move it to completed.
|
||||
* Called when the user clicks "Continue" on the game end overlay, or automatically
|
||||
|
||||
+10
@@ -442,6 +442,16 @@ class ChessLobbyState(
|
||||
}
|
||||
}
|
||||
|
||||
fun removeCompletedGame(gameId: String) {
|
||||
_completedGames.update { current ->
|
||||
current.filter { it.gameId != gameId }
|
||||
}
|
||||
}
|
||||
|
||||
fun clearCompletedGames() {
|
||||
_completedGames.value = emptyList()
|
||||
}
|
||||
|
||||
fun addSpectatingGame(
|
||||
gameId: String,
|
||||
state: LiveChessGameState,
|
||||
|
||||
Reference in New Issue
Block a user