autosync media every ~15 minutes

This commit is contained in:
Damien Elmes 2020-02-05 12:38:36 +10:00
parent 12d009e503
commit d38c2c12d5
2 changed files with 23 additions and 5 deletions

View File

@ -134,7 +134,7 @@ class AnkiQt(QMainWindow):
self.setupSignals() self.setupSignals()
self.setupAutoUpdate() self.setupAutoUpdate()
self.setupHooks() self.setupHooks()
self.setupRefreshTimer() self.setup_timers()
self.updateTitleBar() self.updateTitleBar()
# screens # screens
self.setupDeckBrowser() self.setupDeckBrowser()
@ -1170,12 +1170,14 @@ Difference to correct time: %s."""
showWarning(warn) showWarning(warn)
self.app.closeAllWindows() self.app.closeAllWindows()
# Count refreshing # Timers
########################################################################## ##########################################################################
def setupRefreshTimer(self) -> None: def setup_timers(self) -> None:
# every 10 minutes # refresh decks every 10 minutes
self.progress.timer(10 * 60 * 1000, self.onRefreshTimer, True) 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): def onRefreshTimer(self):
if self.state == "deckBrowser": if self.state == "deckBrowser":
@ -1183,6 +1185,12 @@ Difference to correct time: %s."""
elif self.state == "overview": elif self.state == "overview":
self.overview.refresh() 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 # Permanent libanki hooks
########################################################################## ##########################################################################

View File

@ -7,7 +7,7 @@ import time
from concurrent.futures import Future from concurrent.futures import Future
from copy import copy from copy import copy
from dataclasses import dataclass from dataclasses import dataclass
from typing import Callable, List, Optional, Union from typing import List, Optional, Union
import aqt import aqt
from anki import hooks from anki import hooks
@ -213,6 +213,16 @@ class MediaSyncer:
diag: MediaSyncDialog = aqt.dialogs.open("sync_log", self.mw, self, True) diag: MediaSyncDialog = aqt.dialogs.open("sync_log", self.mw, self, True)
diag.exec_() 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): class MediaSyncDialog(QDialog):
silentlyClose = True silentlyClose = True