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.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
##########################################################################

View File

@ -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