Fix native events not propagated to controllers

This commit is contained in:
Ming Ming 2024-01-18 01:19:48 +08:00
parent f97476e4f0
commit 7bb2f2452a
4 changed files with 67 additions and 4 deletions

View file

@ -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;
}

View file

@ -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<T> {
NativeEventListener(this.listener);
@ -31,7 +32,7 @@ class NativeEventListener<T> {
static final _mappedStream =
MessageRelay.stream.whereType<Message>().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<int>());
}
Message toEvent() => Message(
_id,
id,
jsonEncode({
"fileIds": fileIds,
}),
);
static const _id = "FileExifUpdatedEvent";
static const id = "FileExifUpdatedEvent";
final List<int> fileIds;
}

View file

@ -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<Message>().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;
}

View file

@ -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");
}