From d38c2c12d549e4dbed39729d358d37247f97ff07 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Wed, 5 Feb 2020 12:38:36 +1000 Subject: [PATCH] autosync media every ~15 minutes --- qt/aqt/main.py | 16 ++++++++++++---- qt/aqt/mediasync.py | 12 +++++++++++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/qt/aqt/main.py b/qt/aqt/main.py index 0e5dc111d..69a0f5158 100644 --- a/qt/aqt/main.py +++ b/qt/aqt/main.py @@ -134,7 +134,7 @@ class AnkiQt(QMainWindow): self.setupSignals() self.setupAutoUpdate() self.setupHooks() - self.setupRefreshTimer() + self.setup_timers() self.updateTitleBar() # screens self.setupDeckBrowser() @@ -1170,12 +1170,14 @@ Difference to correct time: %s.""" showWarning(warn) self.app.closeAllWindows() - # Count refreshing + # Timers ########################################################################## - def setupRefreshTimer(self) -> None: - # every 10 minutes + def setup_timers(self) -> None: + # refresh decks every 10 minutes self.progress.timer(10 * 60 * 1000, self.onRefreshTimer, True) + # check media sync every 5 minutes + self.progress.timer(5 * 60 * 1000, self.on_autosync_timer, True) def onRefreshTimer(self): if self.state == "deckBrowser": @@ -1183,6 +1185,12 @@ Difference to correct time: %s.""" elif self.state == "overview": self.overview.refresh() + def on_autosync_timer(self): + elap = self.media_syncer.seconds_since_last_sync() + # autosync if 15 minutes have elapsed since last sync + if elap > 15 * 60: + self.maybe_auto_sync_media() + # Permanent libanki hooks ########################################################################## diff --git a/qt/aqt/mediasync.py b/qt/aqt/mediasync.py index a28a5a4b4..e4a01d9bc 100644 --- a/qt/aqt/mediasync.py +++ b/qt/aqt/mediasync.py @@ -7,7 +7,7 @@ import time from concurrent.futures import Future from copy import copy from dataclasses import dataclass -from typing import Callable, List, Optional, Union +from typing import List, Optional, Union import aqt from anki import hooks @@ -213,6 +213,16 @@ class MediaSyncer: diag: MediaSyncDialog = aqt.dialogs.open("sync_log", self.mw, self, True) diag.exec_() + def seconds_since_last_sync(self) -> int: + if self.is_syncing(): + return 0 + + if self._log: + last = self._log[-1].time + else: + last = 0 + return intTime() - last + class MediaSyncDialog(QDialog): silentlyClose = True