-
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
d9996c3
commit 1c12134
Showing
13 changed files
with
461 additions
and
69 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
part of '../neon_files.dart'; | ||
|
||
@immutable | ||
class FilesSync implements SyncImplementation<FilesSyncMapping, WebDavFile, FileSystemEntity> { | ||
const FilesSync(); | ||
|
||
@override | ||
String get 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 = NeonProvider.of<AccountsBloc>(context); | ||
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(); | ||
if (localPath == null) { | ||
return null; | ||
} | ||
if (!context.mounted) { | ||
return null; | ||
} | ||
|
||
return FilesSyncMapping( | ||
appId: AppIDs.files, | ||
accountId: account.id, | ||
remotePath: Uri(pathSegments: remotePath), | ||
localPath: Directory(localPath), | ||
journal: SyncJournal(), | ||
); | ||
} | ||
|
||
@override | ||
String getMappingDisplayTitle(final FilesSyncMapping mapping) => mapping.remotePath.toString(); | ||
|
||
@override | ||
String getMappingDisplaySubtitle(final FilesSyncMapping mapping) => mapping.localPath.path; | ||
|
||
@override | ||
String getMappingId(final FilesSyncMapping mapping) => | ||
'${Uri.encodeComponent(mapping.remotePath.toString())}-${Uri.encodeComponent(mapping.localPath.path)}'; | ||
|
||
@override | ||
Widget getConflictDetailsLocal(final BuildContext context, final FileSystemEntity object) { | ||
final stat = object.statSync(); | ||
return FilesFileTile( | ||
showFullPath: true, | ||
filesBloc: NeonProvider.of<FilesBloc>(context), | ||
details: FileDetails( | ||
path: object.path.split('/'), | ||
isDirectory: object is Directory, | ||
size: stat.size, | ||
etag: '', | ||
mimeType: '', | ||
lastModified: stat.modified, | ||
hasPreview: false, | ||
isFavorite: false, | ||
), | ||
); | ||
} | ||
|
||
@override | ||
Widget getConflictDetailsRemote(final BuildContext context, final WebDavFile object) => FilesFileTile( | ||
showFullPath: true, | ||
filesBloc: NeonProvider.of<FilesBloc>(context), | ||
details: FileDetails.fromWebDav( | ||
file: object, | ||
), | ||
); | ||
} |
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,62 @@ | ||
import 'dart:async'; | ||
import 'dart:io'; | ||
|
||
import 'package:flutter/foundation.dart'; | ||
import 'package:json_annotation/json_annotation.dart'; | ||
import 'package:neon/sync.dart'; | ||
import 'package:nextcloud/webdav.dart' as webdav; | ||
import 'package:watcher/watcher.dart'; | ||
|
||
part 'mapping.g.dart'; | ||
|
||
@JsonSerializable() | ||
class FilesSyncMapping implements SyncMapping<webdav.WebDavFile, FileSystemEntity> { | ||
FilesSyncMapping({ | ||
required this.accountId, | ||
required this.appId, | ||
required this.journal, | ||
required this.remotePath, | ||
required this.localPath, | ||
}); | ||
|
||
factory FilesSyncMapping.fromJson(final Map<String, dynamic> json) => _$FilesSyncMappingFromJson(json); | ||
Map<String, dynamic> toJson() => _$FilesSyncMappingToJson(this); | ||
|
||
@override | ||
final String accountId; | ||
|
||
@override | ||
final String appId; | ||
|
||
@override | ||
final SyncJournal journal; | ||
|
||
final Uri remotePath; | ||
|
||
@JsonKey( | ||
fromJson: _directoryFromJson, | ||
toJson: _directoryToJson, | ||
) | ||
final Directory localPath; | ||
|
||
static Directory _directoryFromJson(final String value) => Directory(value); | ||
static String _directoryToJson(final Directory value) => value.path; | ||
|
||
StreamSubscription<WatchEvent>? _subscription; | ||
|
||
@override | ||
void watch(final void Function() onUpdated) { | ||
debugPrint('Watching file changes: $localPath'); | ||
_subscription ??= DirectoryWatcher(localPath.path).events.listen( | ||
(final event) { | ||
debugPrint('Registered file change: ${event.path} ${event.type}'); | ||
onUpdated(); | ||
}, | ||
); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
unawaited(_subscription?.cancel()); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.