feat: sync rules for downloads

close #867
This commit is contained in:
edde746
2026-04-16 12:52:48 +02:00
parent c369f2c3d7
commit 360741674d
47 changed files with 2546 additions and 154 deletions
+69 -2
View File
@@ -12,12 +12,12 @@ import '../utils/global_key_utils.dart';
part 'app_database.g.dart';
// Simplified database with API cache for offline support
@DriftDatabase(tables: [DownloadedMedia, DownloadQueue, ApiCache, OfflineWatchProgress])
@DriftDatabase(tables: [DownloadedMedia, DownloadQueue, ApiCache, OfflineWatchProgress, SyncRules])
class AppDatabase extends _$AppDatabase {
AppDatabase() : super(_openConnection());
@override
int get schemaVersion => 9; // Added mediaIndex column to DownloadedMedia
int get schemaVersion => 11;
@override
MigrationStrategy get migration {
@@ -46,6 +46,18 @@ class AppDatabase extends _$AppDatabase {
appLogger.w('mediaIndex column may already exist: $e');
}
}
if (from < 10) {
appLogger.i('Adding SyncRules table (v10 migration)');
await m.createTable(syncRules);
}
if (from < 11) {
appLogger.i('Adding enabled column to SyncRules (v11 migration)');
try {
await m.addColumn(syncRules, syncRules.enabled);
} catch (e) {
appLogger.w('enabled column may already exist: $e');
}
}
},
);
}
@@ -201,6 +213,61 @@ class AppDatabase extends _$AppDatabase {
return delete(offlineWatchProgress).go();
}
// ============================================================
// Sync Rules Operations
// ============================================================
Future<List<SyncRuleItem>> getSyncRules() {
return select(syncRules).get();
}
Future<SyncRuleItem?> getSyncRule(String globalKey) {
return (select(syncRules)..where((t) => t.globalKey.equals(globalKey))).getSingleOrNull();
}
Future<void> insertSyncRule({
required String serverId,
required String ratingKey,
required String globalKey,
required String targetType,
required int episodeCount,
int mediaIndex = 0,
}) async {
await into(syncRules).insertOnConflictUpdate(
SyncRulesCompanion.insert(
serverId: serverId,
ratingKey: ratingKey,
globalKey: globalKey,
targetType: targetType,
episodeCount: episodeCount,
createdAt: DateTime.now().millisecondsSinceEpoch,
mediaIndex: Value(mediaIndex),
),
);
}
Future<void> updateSyncRuleCount(String globalKey, int episodeCount) async {
await (update(syncRules)..where((t) => t.globalKey.equals(globalKey))).write(
SyncRulesCompanion(episodeCount: Value(episodeCount)),
);
}
Future<void> updateSyncRuleEnabled(String globalKey, bool enabled) async {
await (update(syncRules)..where((t) => t.globalKey.equals(globalKey))).write(
SyncRulesCompanion(enabled: Value(enabled)),
);
}
Future<void> updateSyncRuleLastExecuted(String globalKey) async {
await (update(syncRules)..where((t) => t.globalKey.equals(globalKey))).write(
SyncRulesCompanion(lastExecutedAt: Value(DateTime.now().millisecondsSinceEpoch)),
);
}
Future<void> deleteSyncRule(String globalKey) async {
await (delete(syncRules)..where((t) => t.globalKey.equals(globalKey))).go();
}
// ============================================================
// Downloaded Media Queries for Watch State Sync
// ============================================================