diff --git a/app/lib/controller/account_controller.dart b/app/lib/controller/account_controller.dart index ecdcb1c2..9d761182 100644 --- a/app/lib/controller/account_controller.dart +++ b/app/lib/controller/account_controller.dart @@ -10,10 +10,12 @@ import 'package:nc_photos/controller/session_controller.dart'; import 'package:nc_photos/controller/sharings_controller.dart'; import 'package:nc_photos/controller/sync_controller.dart'; import 'package:nc_photos/di_container.dart'; +import 'package:nc_photos/event/native_event_relay.dart'; class AccountController { void setCurrentAccount(Account account) { _account = account; + _collectionsController?.dispose(); _collectionsController = null; _serverController?.dispose(); @@ -32,6 +34,9 @@ class AccountController { _placesController = null; _filesController?.dispose(); _filesController = null; + + _nativeEventRelay?.dispose(); + _nativeEventRelay = NativeEventRelay(filesController: filesController); } Account get account => _account!; @@ -87,6 +92,7 @@ class AccountController { ); Account? _account; + CollectionsController? _collectionsController; ServerController? _serverController; AccountPrefController? _accountPrefController; @@ -96,4 +102,6 @@ class AccountController { SharingsController? _sharingsController; PlacesController? _placesController; FilesController? _filesController; + + NativeEventRelay? _nativeEventRelay; } diff --git a/app/lib/event/native_event.dart b/app/lib/event/native_event.dart index 1406207e..9d3f2067 100644 --- a/app/lib/event/native_event.dart +++ b/app/lib/event/native_event.dart @@ -8,6 +8,7 @@ import 'package:np_platform_message_relay/np_platform_message_relay.dart'; part 'native_event.g.dart'; +@Deprecated("See AccountController.NativeEventRelay") class NativeEventListener { NativeEventListener(this.listener); @@ -31,7 +32,7 @@ class NativeEventListener { static final _mappedStream = MessageRelay.stream.whereType().map((ev) { switch (ev.event) { - case FileExifUpdatedEvent._id: + case FileExifUpdatedEvent.id: return FileExifUpdatedEvent.fromEvent(ev); default: @@ -51,20 +52,20 @@ class FileExifUpdatedEvent { const FileExifUpdatedEvent(this.fileIds); factory FileExifUpdatedEvent.fromEvent(Message ev) { - assert(ev.event == _id); + assert(ev.event == id); assert(ev.data != null); final dataJson = jsonDecode(ev.data!) as Map; return FileExifUpdatedEvent((dataJson["fileIds"] as List).cast()); } Message toEvent() => Message( - _id, + id, jsonEncode({ "fileIds": fileIds, }), ); - static const _id = "FileExifUpdatedEvent"; + static const id = "FileExifUpdatedEvent"; final List fileIds; } diff --git a/app/lib/event/native_event_relay.dart b/app/lib/event/native_event_relay.dart new file mode 100644 index 00000000..fe4997e4 --- /dev/null +++ b/app/lib/event/native_event_relay.dart @@ -0,0 +1,40 @@ +import 'dart:async'; + +import 'package:logging/logging.dart'; +import 'package:nc_photos/controller/files_controller.dart'; +import 'package:nc_photos/event/native_event.dart'; +import 'package:nc_photos/stream_extension.dart'; +import 'package:np_codegen/np_codegen.dart'; +import 'package:np_platform_message_relay/np_platform_message_relay.dart'; + +part 'native_event_relay.g.dart'; + +@npLog +class NativeEventRelay { + NativeEventRelay({ + required this.filesController, + }) { + _subscription = MessageRelay.stream.whereType().listen((event) { + switch (event.event) { + case FileExifUpdatedEvent.id: + _onFileExifUpdatedEvent(FileExifUpdatedEvent.fromEvent(event)); + break; + + default: + _log.severe('Unknown event: ${event.event}'); + break; + } + }); + } + + void dispose() { + _subscription?.cancel(); + } + + void _onFileExifUpdatedEvent(FileExifUpdatedEvent ev) { + filesController.applySyncResult(fileExifs: ev.fileIds); + } + + final FilesController filesController; + StreamSubscription? _subscription; +} diff --git a/app/lib/event/native_event_relay.g.dart b/app/lib/event/native_event_relay.g.dart new file mode 100644 index 00000000..d3c5aa4d --- /dev/null +++ b/app/lib/event/native_event_relay.g.dart @@ -0,0 +1,14 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'native_event_relay.dart'; + +// ************************************************************************** +// NpLogGenerator +// ************************************************************************** + +extension _$NativeEventRelayNpLog on NativeEventRelay { + // ignore: unused_element + Logger get _log => log; + + static final log = Logger("event.native_event_relay.NativeEventRelay"); +}