From e8deec7b276c7795f8e831b748f5201d0f9073f5 Mon Sep 17 00:00:00 2001 From: vrubelroman Date: Fri, 23 Oct 2026 23:08:40 +0300 Subject: [PATCH] Downloaded games newest first on the desktop too; the games list counts a download as it goes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Desktop: in a database from Lichess and Chess.com the «№» order is the time the games were played, the most recent first (as on the phone); a click turns it around. - chess-core GameEntry::played_at: the shared key (UTCDate+UTCTime, else Date). - Android: over a downloaded database's games list the number of games grows with its download (it stood still until the end), as on the list of databases. Co-Authored-By: Claude Opus 5.5 --- .../chesspenguin/ui/GamesScreen.kt | 4 ++ apps/desktop/src/library.rs | 53 +++++++++++++++++-- crates/chess-core/src/pgn/scan.rs | 9 ++++ crates/mobile-api/src/lib.rs | 11 ++-- 4 files changed, 67 insertions(+), 10 deletions(-) diff --git a/androidApp/app/src/main/java/io/github/vrubelroman/chesspenguin/ui/GamesScreen.kt b/androidApp/app/src/main/java/io/github/vrubelroman/chesspenguin/ui/GamesScreen.kt index 78f05c2..44105f7 100644 --- a/androidApp/app/src/main/java/io/github/vrubelroman/chesspenguin/ui/GamesScreen.kt +++ b/androidApp/app/src/main/java/io/github/vrubelroman/chesspenguin/ui/GamesScreen.kt @@ -157,6 +157,10 @@ fun GamesScreen(db: String, onBack: () -> Unit, onGame: (place: Int, query: Stri Text( if (query.isNotEmpty() && found != null) { stringResource(R.string.found_of, groupDigits(found), groupDigits(n)) + } else if (download?.state?.running == true) { + // The games come as the download goes, as on the list of + // databases; the list itself is read again when it ends. + stringResource(R.string.online_downloading, groupDigits((entry?.games ?: n) + download.state.added.toInt())) } else { pluralStringResource(R.plurals.games_count, n, groupDigits(n)) }, diff --git a/apps/desktop/src/library.rs b/apps/desktop/src/library.rs index afd6d3b..de715c0 100644 --- a/apps/desktop/src/library.rs +++ b/apps/desktop/src/library.rs @@ -153,7 +153,7 @@ impl SmallBoard { let pgn = db.as_pgn()?; let candidates: Option> = (!text.is_empty()).then(|| { let mut v: Vec = - filtered_sorted(pgn.entries(), &text, (SortKey::Index, true)) + filtered_sorted(pgn.entries(), &text, (SortKey::Index, true), false) .into_iter() .map(|i| i as u32) .collect(); @@ -1013,7 +1013,9 @@ impl Library { let key = (path.clone(), self.search.clone(), self.sort.0, self.sort.1); if self.view_key.as_ref() != Some(&key) { - self.view = filtered_sorted(pgn.entries(), &self.search, self.sort); + // Games from Lichess and Chess.com: by default the most recent first. + let newest = crate::online::is_online(&path); + self.view = filtered_sorted(pgn.entries(), &self.search, self.sort, newest); self.view_key = Some(key); } // With a position on the board: only the games that reach it. @@ -1320,7 +1322,15 @@ pub fn display_date(raw: &str) -> String { } } -fn filtered_sorted(entries: &[GameEntry], search: &str, (key, asc): (SortKey, bool)) -> Vec { +/// The games matching `search`, in the order of the column `key`. In a downloaded +/// database (`newest`) the "№" order is the time the games were played, the most recent +/// first: the sites' games are appended to the file oldest first, one site after another. +fn filtered_sorted( + entries: &[GameEntry], + search: &str, + (key, asc): (SortKey, bool), + newest: bool, +) -> Vec { let needle = search.trim().to_lowercase(); let mut v: Vec = entries .iter() @@ -1360,6 +1370,12 @@ fn filtered_sorted(entries: &[GameEntry], search: &str, (key, asc): (SortKey, bo _ => 0, } }; + if newest && key == SortKey::Index { + let mut keyed: Vec<(String, usize)> = + v.into_iter().map(|i| (entries[i].played_at(), i)).collect(); + keyed.sort_by(|x, y| if asc { y.cmp(x) } else { x.cmp(y) }); + return keyed.into_iter().map(|(_, i)| i).collect(); + } v.sort_by(|&a, &b| { let (ea, eb) = (&entries[a], &entries[b]); let ord = text(ea) @@ -1386,3 +1402,34 @@ fn import_error(e: &ImportError) -> String { e => e.to_string(), } } + +#[cfg(test)] +mod tests { + use super::*; + use chess_core::pgn::scan_games; + use std::sync::atomic::AtomicBool; + + fn entries(pgn: &str) -> Vec { + scan_games( + pgn.as_bytes(), + chess_core::TextEncoding::Utf8, + &mut |_| {}, + &AtomicBool::new(false), + ) + .unwrap() + } + + #[test] + fn downloaded_games_come_newest_first() { + // As a download writes them: one site's games oldest first, then the other's. + let e = entries(concat!( + "[Site \"lichess\"][UTCDate \"2026.01.01\"][UTCTime \"10:00:00\"] 1. e4 *\n", + "[Site \"lichess\"][UTCDate \"2026.03.01\"][UTCTime \"10:00:00\"] 1. d4 *\n", + "[Site \"chess.com\"][UTCDate \"2026.02.01\"][UTCTime \"10:00:00\"] 1. c4 *\n", + )); + assert_eq!(filtered_sorted(&e, "", (SortKey::Index, true), true), vec![1, 2, 0]); + assert_eq!(filtered_sorted(&e, "", (SortKey::Index, false), true), vec![0, 2, 1]); + // Any other database keeps the order of its file. + assert_eq!(filtered_sorted(&e, "", (SortKey::Index, true), false), vec![0, 1, 2]); + } +} diff --git a/crates/chess-core/src/pgn/scan.rs b/crates/chess-core/src/pgn/scan.rs index 11f9c1e..25e9777 100644 --- a/crates/chess-core/src/pgn/scan.rs +++ b/crates/chess-core/src/pgn/scan.rs @@ -69,6 +69,15 @@ impl GameEntry { pub fn eco(&self) -> Option<&str> { self.tag("ECO") } + /// When the game was played, as text that sorts by time: `UTCDate` and `UTCTime` + /// when both are there (Lichess, Chess.com), else `Date`. + pub fn played_at(&self) -> String { + match (self.tag("UTCDate"), self.tag("UTCTime")) { + (Some(d), Some(t)) => format!("{d} {t}"), + _ => self.date().unwrap_or("").to_owned(), + } + } + pub fn ply_count(&self) -> Option { self.tag("PlyCount").and_then(|v| v.trim().parse().ok()) } diff --git a/crates/mobile-api/src/lib.rs b/crates/mobile-api/src/lib.rs index b6f7c8e..8f7fe58 100644 --- a/crates/mobile-api/src/lib.rs +++ b/crates/mobile-api/src/lib.rs @@ -336,13 +336,10 @@ impl Database { }; let entries = pgn.entries(); let when = |i: u32| { - let Some(e) = entries.get(i as usize) else { - return String::new(); - }; - match (e.tag("UTCDate"), e.tag("UTCTime")) { - (Some(d), Some(t)) => format!("{d} {t}"), - _ => e.tag("Date").unwrap_or("").to_owned(), - } + entries + .get(i as usize) + .map(|e| e.played_at()) + .unwrap_or_default() }; let mut keyed: Vec<(String, u32)> = indices.into_iter().map(|i| (when(i), i)).collect(); keyed.sort_by(|a, b| b.cmp(a));