feature: Re-implemented syncing

This commit is contained in:
PartyDonut 2025-07-27 10:54:29 +02:00
parent c5c7f71b84
commit 86ff355e21
51 changed files with 3067 additions and 1147 deletions

View file

@ -9,7 +9,6 @@ import 'package:fladder/models/item_base_model.dart';
import 'package:fladder/models/items/episode_model.dart';
import 'package:fladder/models/items/item_shared_models.dart';
import 'package:fladder/models/items/photos_model.dart';
import 'package:fladder/models/syncing/sync_item.dart';
import 'package:fladder/providers/sync_provider.dart';
import 'package:fladder/providers/user_provider.dart';
import 'package:fladder/screens/collections/add_to_collection.dart';
@ -110,8 +109,8 @@ extension ItemBaseModelExtensions on ItemBaseModel {
)) &&
syncAble &&
(canDownload ?? false);
final syncedItem = ref.read(syncProvider.notifier).getSyncedItem(this);
final downloadUrl = ref.read(userProvider.notifier).createDownloadUrl(this);
final syncedItemFuture = ref.read(syncProvider.notifier).getSyncedItem(this);
return [
if (!exclude.contains(ItemActions.play))
if (playAble)
@ -235,22 +234,39 @@ extension ItemBaseModelExtensions on ItemBaseModel {
),
if (!exclude.contains(ItemActions.download) && downloadEnabled) ...[
if (!kIsWeb)
if (syncedItem == null)
ItemActionButton(
icon: const Icon(IconsaxPlusLinear.arrow_down_2),
label: Text(context.localized.sync),
action: () => ref.read(syncProvider.notifier).addSyncItem(context, this),
)
else
ItemActionButton(
icon: IgnorePointer(child: SyncButton(item: this, syncedItem: syncedItem)),
action: () => syncedItem.status == SyncStatus.complete
? ref.read(syncProvider.notifier).deleteFullSyncFiles(syncedItem, null)
: ref.read(syncProvider.notifier).syncFile(syncedItem, false),
label: Text(
syncedItem.status == SyncStatus.complete ? context.localized.delete : context.localized.sync,
),
)
ItemActionButton(
icon: FutureBuilder(
future: syncedItemFuture,
builder: (context, snapshot) {
final syncedItem = snapshot.data;
if (syncedItem != null) {
return IgnorePointer(child: SyncButton(item: this, syncedItem: syncedItem));
}
return const Icon(IconsaxPlusLinear.arrow_down_2);
},
),
label: FutureBuilder(
future: syncedItemFuture,
builder: (context, snapshot) {
final syncedItem = snapshot.data;
if (syncedItem != null) {
return Text(
context.localized.syncDetails,
);
}
return Text(context.localized.sync);
},
),
action: () async {
final syncedItem = await syncedItemFuture;
if (syncedItem != null) {
await showSyncItemDetails(context, syncedItem, ref);
} else {
await ref.read(syncProvider.notifier).addSyncItem(context, this);
}
context.refreshData();
},
)
else if (downloadUrl != null) ...[
ItemActionButton(
icon: const Icon(IconsaxPlusLinear.document_download),

View file

@ -0,0 +1,80 @@
import 'dart:convert';
import 'dart:developer';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:drift/drift.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:isar/isar.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
import 'package:fladder/models/syncing/database_item.dart';
import 'package:fladder/models/syncing/i_synced_item.dart';
import 'package:fladder/models/syncing/sync_item.dart';
Future<void> isarMigration(Ref ref, AppDatabase db, String savePath) async {
if (kIsWeb) return;
//Return if the database is already migrated
final isNotEmtpy = await db.select(db.databaseItems).get().then((value) => value.isNotEmpty);
if (isNotEmtpy) {
log('Isar database is not empty, skipping migration');
return;
}
//Open isar database
final applicationDirectory = await getApplicationDocumentsDirectory();
final isarPath = Directory(path.joinAll([applicationDirectory.path, 'Fladder', 'Database']));
await isarPath.create(recursive: true);
final isar = Isar.open(
schemas: [ISyncedItemSchema],
directory: isarPath.path,
);
//Fetch all synced items from the old database
List<SyncedItem> items = isar.iSyncedItems
.where()
.findAll()
.map((e) => SyncedItem.fromIsar(e, path.joinAll([savePath, e.userId ?? "Unkown User"])))
.toList();
//Clear any missing paths
items = items.where((e) => e.path != null ? Directory(e.path!).existsSync() : false).toList();
//Convert to drift database items
final driftItems = items.map(
(item) => DatabaseItemsCompanion(
id: Value(item.id),
parentId: Value(item.parentId),
syncing: Value(item.syncing),
userId: Value(item.userId),
path: Value(item.path),
fileSize: Value(item.fileSize),
sortName: Value(item.sortName),
videoFileName: Value(item.videoFileName),
trickPlayModel: Value(item.fTrickPlayModel != null ? jsonEncode(item.fTrickPlayModel?.toJson()) : null),
mediaSegments: Value(item.mediaSegments != null ? jsonEncode(item.mediaSegments?.toJson()) : null),
images: Value(item.fImages != null ? jsonEncode(item.fImages?.toJson()) : null),
chapters: Value(jsonEncode(item.fChapters.map((e) => e.toJson()).toList())),
subtitles: Value(jsonEncode(item.subtitles.map((e) => e.toJson()).toList())),
userData: Value(item.userData != null ? jsonEncode(item.userData?.toJson()) : null),
),
);
await db.batch((batch) {
batch.insertAll(
db.databaseItems,
driftItems,
mode: InsertMode.insertOrReplace,
);
});
//Delete database file
final baseFolder = Directory(path.join(applicationDirectory.path, 'Fladder'));
if (await baseFolder.exists()) {
log('Deleting old Fladder base folder: ${baseFolder.path}');
// await baseFolder.delete(recursive: true);
}
}