-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(neon_files): Implement file syncing
Signed-off-by: jld3103 <[email protected]>
- Loading branch information
1 parent
52e5142
commit f9a4f41
Showing
14 changed files
with
492 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
part of '../neon_files.dart'; | ||
|
||
class FilesSync implements SyncImplementation<FilesSyncMapping, WebDavFile, FileSystemEntity> { | ||
@override | ||
String appId = AppIDs.files; | ||
|
||
@override | ||
FilesSyncSources getSources(final Account account, final FilesSyncMapping mapping) => FilesSyncSources( | ||
account.client, | ||
mapping.remotePath, | ||
mapping.localPath, | ||
); | ||
|
||
@override | ||
Map<String, dynamic> serializeMapping(final FilesSyncMapping mapping) => mapping.toJson(); | ||
|
||
@override | ||
FilesSyncMapping deserializeMapping(final Map<String, dynamic> json) => FilesSyncMapping.fromJson(json); | ||
|
||
@override | ||
Future<FilesSyncMapping?> addMapping(final BuildContext context, final Account account) async { | ||
final accountsBloc = Provider.of<AccountsBloc>(context, listen: false); | ||
final appsBloc = accountsBloc.getAppsBlocFor(account); | ||
final filesBloc = appsBloc.getAppBlocByID(AppIDs.files)! as FilesBloc; | ||
final filesBrowserBloc = filesBloc.getNewFilesBrowserBloc(); | ||
|
||
final remotePath = await showDialog<List<String>?>( | ||
context: context, | ||
builder: (final context) => FilesChooseFolderDialog( | ||
bloc: filesBrowserBloc, | ||
filesBloc: filesBloc, | ||
originalPath: const [], | ||
), | ||
); | ||
filesBrowserBloc.dispose(); | ||
if (remotePath == null) { | ||
return null; | ||
} | ||
|
||
final localPath = await FileUtils.pickDirectory(); | ||
// ignore: use_build_context_synchronously | ||
if (localPath == null || !context.mounted) { | ||
return null; | ||
} | ||
|
||
return FilesSyncMapping( | ||
appId: AppIDs.files, | ||
accountId: account.id, | ||
remotePath: remotePath, | ||
localPath: localPath, | ||
journal: SyncJournal({}), | ||
); | ||
} | ||
|
||
@override | ||
List<Widget> buildConflictTiles( | ||
final BuildContext context, | ||
final Function(SyncConflictSolution) onSolution, | ||
final SyncConflictSolution? selectedSolution, | ||
final SyncMapping<WebDavFile, FileSystemEntity> mapping, | ||
final String id, | ||
final FileSystemEntity localObject, | ||
final WebDavFile remoteObject, | ||
) { | ||
final filesBloc = Provider.of<FilesBloc>(context, listen: false); | ||
final stat = localObject.statSync(); | ||
mapping as FilesSyncMapping; | ||
return [ | ||
FilesFileTile( | ||
showFullPath: true, | ||
filesBloc: filesBloc, | ||
details: FileDetails( | ||
path: '${mapping.localPath}/$id'.split('/'), | ||
isDirectory: localObject is Directory, | ||
size: stat.size, | ||
etag: '', | ||
mimeType: '', | ||
lastModified: stat.modified, | ||
hasPreview: false, | ||
isFavorite: false, | ||
), | ||
onTap: () { | ||
onSolution(SyncConflictSolution.overwriteA); | ||
}, | ||
trailing: Radio<SyncConflictSolution>( | ||
value: SyncConflictSolution.overwriteA, | ||
groupValue: selectedSolution, | ||
onChanged: (final solution) { | ||
onSolution(solution!); | ||
}, | ||
), | ||
), | ||
FilesFileTile( | ||
showFullPath: true, | ||
filesBloc: filesBloc, | ||
details: FileDetails.fromWebDav( | ||
file: remoteObject, | ||
), | ||
onTap: () { | ||
onSolution(SyncConflictSolution.overwriteB); | ||
}, | ||
trailing: Radio<SyncConflictSolution>( | ||
value: SyncConflictSolution.overwriteB, | ||
groupValue: selectedSolution, | ||
onChanged: (final solution) { | ||
onSolution(solution!); | ||
}, | ||
), | ||
), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import 'dart:async'; | ||
import 'dart:io'; | ||
|
||
import 'package:flutter/foundation.dart'; | ||
import 'package:json_annotation/json_annotation.dart'; | ||
import 'package:neon/nextcloud.dart'; | ||
import 'package:neon/sync.dart'; | ||
import 'package:watcher/watcher.dart'; | ||
|
||
part 'mapping.g.dart'; | ||
|
||
@JsonSerializable() | ||
class FilesSyncMapping extends SyncMapping<WebDavFile, FileSystemEntity> { | ||
FilesSyncMapping({ | ||
required super.accountId, | ||
required super.appId, | ||
required super.journal, | ||
required this.remotePath, | ||
required this.localPath, | ||
}); | ||
|
||
factory FilesSyncMapping.fromJson(final Map<String, dynamic> json) => _$FilesSyncMappingFromJson(json); | ||
Map<String, dynamic> toJson() => _$FilesSyncMappingToJson(this); | ||
|
||
final List<String> remotePath; | ||
|
||
final String localPath; | ||
|
||
@override | ||
late String title = remotePath.join('/'); | ||
|
||
@override | ||
late String subtitle = localPath; | ||
|
||
StreamSubscription<WatchEvent>? _subscription; | ||
|
||
@override | ||
void watch(final Function() onUpdated) { | ||
debugPrint('Watching file changes: $localPath'); | ||
_subscription ??= DirectoryWatcher(localPath).events.listen( | ||
(final event) { | ||
debugPrint('Registered file change: ${event.path} ${event.type}'); | ||
onUpdated(); | ||
}, | ||
); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
unawaited(_subscription?.cancel()); | ||
} | ||
} |
Oops, something went wrong.