You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.6 KiB
63 lines
1.6 KiB
import 'dart:async'; |
|
|
|
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:nextcloud/webdav.dart'; |
|
import 'package:universal_io/io.dart'; |
|
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 PathUri 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()); |
|
} |
|
}
|
|
|