Online databases update on their own; release 2.0.0
Some checks failed
CI / check (push) Waiting to run
CI / windows-build (push) Waiting to run
CI / macos-build (push) Waiting to run
Release / version (push) Has been cancelled
Release / macos (push) Has been cancelled
Release / android (push) Has been cancelled
Release / linux (push) Has been cancelled
Release / flatpak (push) Has been cancelled
Release / windows (push) Has been cancelled
Release / publish (push) Has been cancelled
Some checks failed
CI / check (push) Waiting to run
CI / windows-build (push) Waiting to run
CI / macos-build (push) Waiting to run
Release / version (push) Has been cancelled
Release / macos (push) Has been cancelled
Release / android (push) Has been cancelled
Release / linux (push) Has been cancelled
Release / flatpak (push) Has been cancelled
Release / windows (push) Has been cancelled
Release / publish (push) Has been cancelled
At the start and on coming back after ten minutes or more, every database downloaded from Lichess and Chess.com fetches the games played since, one after another (the sites ask for one export at a time), on the desktop and on Android. Only new games are told. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
8c04196d49
commit
e073592af9
9 changed files with 139 additions and 28 deletions
|
|
@ -111,6 +111,8 @@ class MainActivity : ComponentActivity() {
|
|||
val updates = (application as ChessPenguinApp).updates
|
||||
updates.foreground = true
|
||||
updates.checkIfDue()
|
||||
// The downloaded databases get their new games (at most every ten minutes).
|
||||
(application as ChessPenguinApp).downloads.updateAllQuietly()
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package io.github.vrubelroman.chesspenguin.data
|
|||
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
|
|
@ -22,8 +23,9 @@ import java.util.concurrent.ConcurrentHashMap
|
|||
/// database did not exist before it (its first download).
|
||||
data class DownloadView(val accounts: List<OnlineAccount>, val state: DownloadState, val fresh: Boolean = false)
|
||||
|
||||
/// A download that has ended, for the snackbar of the screen that is shown then.
|
||||
data class Finished(val id: String, val name: String, val view: DownloadView, val games: Int)
|
||||
/// A download that has ended, for the snackbar of the screen that is shown then;
|
||||
/// `quiet` when the app started it on its own (said only when it added games).
|
||||
data class Finished(val id: String, val name: String, val view: DownloadView, val games: Int, val quiet: Boolean = false)
|
||||
|
||||
/// The downloads of games from Lichess and Chess.com (android-ui.md §23). They belong to
|
||||
/// the app, not to a screen: leaving the screen does not stop them, and the list of
|
||||
|
|
@ -44,13 +46,15 @@ class OnlineDownloads(private val library: Library, private val scope: Coroutine
|
|||
|
||||
/// Starts fetching the new games of `accounts` into the database file `path` (put on
|
||||
/// the list at once); one download per database. Returns the database's entry.
|
||||
fun start(accounts: List<OnlineAccount>, path: String): DbEntry {
|
||||
fun start(accounts: List<OnlineAccount>, path: String): DbEntry = run(accounts, path, false).first
|
||||
|
||||
private fun run(accounts: List<OnlineAccount>, path: String, quiet: Boolean): Pair<DbEntry, Job?> {
|
||||
val fresh = !File(path).exists()
|
||||
val entry = library.addOnline(File(path))
|
||||
if (running.containsKey(entry.id)) return entry
|
||||
if (running.containsKey(entry.id)) return entry to null
|
||||
val d = OnlineDownload.start(accounts, path)
|
||||
running[entry.id] = d
|
||||
scope.launch(Dispatchers.IO) {
|
||||
val job = scope.launch(Dispatchers.IO) {
|
||||
while (true) {
|
||||
val s = d.state()
|
||||
_views.update { it + (entry.id to DownloadView(accounts, s, fresh)) }
|
||||
|
|
@ -64,9 +68,9 @@ class OnlineDownloads(private val library: Library, private val scope: Coroutine
|
|||
// No game at all (no standard chess there, or it failed at once): not a database.
|
||||
val name = library.entry(entry.id)?.name ?: entry.name
|
||||
if (games == 0) library.dropEmpty(entry.id) else library.downloaded(entry.id, games, info?.updated)
|
||||
_views.value[entry.id]?.let { _finished.tryEmit(Finished(entry.id, name, it, games)) }
|
||||
_views.value[entry.id]?.let { _finished.tryEmit(Finished(entry.id, name, it, games, quiet)) }
|
||||
}
|
||||
return entry
|
||||
return entry to job
|
||||
}
|
||||
|
||||
/// Fetches the games played since the last download of a database.
|
||||
|
|
@ -76,6 +80,26 @@ class OnlineDownloads(private val library: Library, private val scope: Coroutine
|
|||
start(info.accounts, path)
|
||||
}
|
||||
|
||||
/// Every downloaded database gets the games played since, one after another (the
|
||||
/// sites ask for one export at a time): at the app's start and when it comes back to
|
||||
/// the screen, at most every [AUTO_EVERY_MS].
|
||||
fun updateAllQuietly() {
|
||||
val now = System.currentTimeMillis()
|
||||
if (auto?.isActive == true || now - lastAuto < AUTO_EVERY_MS) return
|
||||
lastAuto = now
|
||||
auto = scope.launch(Dispatchers.IO) {
|
||||
for (e in library.entries.value.filter { it.source == Source.Online }) {
|
||||
if (isRunning(e.id)) continue
|
||||
val path = library.onlineFile(e).path
|
||||
val info = onlineDatabase(path) ?: continue
|
||||
run(info.accounts, path, true).second?.join()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var lastAuto = 0L
|
||||
private var auto: Job? = null
|
||||
|
||||
/// Stops a download; what it fetched stays.
|
||||
fun cancel(id: String) {
|
||||
running[id]?.cancel()
|
||||
|
|
@ -85,5 +109,6 @@ class OnlineDownloads(private val library: Library, private val scope: Coroutine
|
|||
|
||||
private companion object {
|
||||
const val POLL_MS = 400L
|
||||
const val AUTO_EVERY_MS = 10 * 60 * 1000L
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -815,6 +815,8 @@ private fun pastedName(db: Database, dated: String): String {
|
|||
fun finishedText(res: android.content.res.Resources, f: Finished): String? {
|
||||
val s = f.view.state
|
||||
val games = res.getQuantityString(R.plurals.games_count, f.games, groupDigits(f.games))
|
||||
// An update of its own speaks only of new games.
|
||||
if (f.quiet && (s.failure != null || s.added == 0UL)) return null
|
||||
s.failure?.let { return res.getString(R.string.online_snack_failed, f.name, problemShort(res, it)) }
|
||||
return when {
|
||||
s.cancelled || f.games == 0 -> null
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue