fix(downloads): profile-scoped ownership and watch-sync integrity

This commit is contained in:
edde746
2026-07-02 11:41:25 +02:00
parent 44be03d39c
commit 2b7142bdcd
8 changed files with 186 additions and 31 deletions
+8 -4
View File
@@ -39,7 +39,10 @@ class SyncRuleResult {
class SyncRuleExecutor {
final AppDatabase _database;
bool _isExecuting = false;
DateTime? _lastFullRunAt;
/// Per profile: one profile's background pass must not consume another
/// profile's cooldown window after a switch.
final Map<String, DateTime> _lastFullRunAtByProfile = {};
static const Duration _cooldownWifi = Duration(minutes: 30);
static const Duration _cooldownCellular = Duration(hours: 3);
@@ -85,11 +88,12 @@ class SyncRuleExecutor {
return [];
}
if (!force && _lastFullRunAt != null) {
final lastFullRunAt = _lastFullRunAtByProfile[profileId];
if (!force && lastFullRunAt != null) {
final hasWifi =
connectivity.contains(ConnectivityResult.wifi) || connectivity.contains(ConnectivityResult.ethernet);
final cooldown = hasWifi ? _cooldownWifi : _cooldownCellular;
final elapsed = DateTime.now().difference(_lastFullRunAt!);
final elapsed = DateTime.now().difference(lastFullRunAt);
if (elapsed < cooldown) {
appLogger.d(
'Sync rules cooldown active (${elapsed.inMinutes}m < ${cooldown.inMinutes}m, hasWifi=$hasWifi) — skipping',
@@ -124,7 +128,7 @@ class SyncRuleExecutor {
}
}
_lastFullRunAt = DateTime.now();
_lastFullRunAtByProfile[profileId] = DateTime.now();
return results;
} finally {
_isExecuting = false;