nc-photos/lib/event/event.dart

117 lines
2.6 KiB
Dart
Raw Normal View History

2021-04-10 06:28:12 +02:00
import 'dart:async';
import 'package:event_bus/event_bus.dart';
import 'package:kiwi/kiwi.dart';
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/entity/album.dart';
import 'package:nc_photos/entity/file.dart';
2021-10-06 22:32:36 +02:00
import 'package:nc_photos/entity/share.dart';
import 'package:nc_photos/metadata_task_manager.dart';
2021-09-11 14:03:40 +02:00
import 'package:nc_photos/pref.dart';
2021-04-10 06:28:12 +02:00
class AppEventListener<T> {
AppEventListener(this._listener);
void begin() {
if (_subscription != null) {
_log.warning("[beginListenEvent] Already listening");
return;
}
_subscription = _stream.listen(_listener);
}
void end() {
if (_subscription == null) {
_log.warning("[endListenEvent] Already not listening");
return;
}
2021-07-23 22:05:57 +02:00
_subscription?.cancel();
2021-04-10 06:28:12 +02:00
_subscription = null;
}
final void Function(T) _listener;
final _stream = KiwiContainer().resolve<EventBus>().on<T>();
2021-07-23 22:05:57 +02:00
StreamSubscription<T>? _subscription;
2021-04-10 06:28:12 +02:00
final _log = Logger("event.event.AppEventListener<${T.runtimeType}>");
}
class AlbumCreatedEvent {
AlbumCreatedEvent(this.account, this.album);
final Account account;
final Album album;
}
class AlbumUpdatedEvent {
AlbumUpdatedEvent(this.account, this.album);
final Account account;
final Album album;
}
2021-05-28 19:15:09 +02:00
class FilePropertyUpdatedEvent {
FilePropertyUpdatedEvent(this.account, this.file, this.properties);
2021-04-10 06:28:12 +02:00
final Account account;
final File file;
2021-05-28 19:15:09 +02:00
final int properties;
// Bit masks for properties field
static const propMetadata = 0x01;
2021-05-28 20:45:00 +02:00
static const propIsArchived = 0x02;
2021-06-21 12:39:17 +02:00
static const propOverrideDateTime = 0x04;
2021-04-10 06:28:12 +02:00
}
class FileRemovedEvent {
FileRemovedEvent(this.account, this.file);
final Account account;
final File file;
}
2021-04-17 11:04:46 +02:00
2021-08-01 22:46:16 +02:00
class FileTrashbinRestoredEvent {
FileTrashbinRestoredEvent(this.account, this.file);
final Account account;
final File file;
}
2021-08-13 12:39:17 +02:00
class FileMovedEvent {
FileMovedEvent(this.account, this.file, this.destination);
final Account account;
final File file;
final String destination;
}
2021-10-06 22:32:36 +02:00
class ShareRemovedEvent {
const ShareRemovedEvent(this.account, this.share);
final Account account;
final Share share;
}
2021-04-17 11:04:46 +02:00
class ThemeChangedEvent {}
2021-05-28 19:15:09 +02:00
2021-06-23 10:15:25 +02:00
class LanguageChangedEvent {}
class MetadataTaskStateChangedEvent {
const MetadataTaskStateChangedEvent(this.state);
final MetadataTaskState state;
}
2021-09-11 14:03:40 +02:00
class PrefUpdatedEvent {
PrefUpdatedEvent(this.key, this.value);
final PrefKey key;
final dynamic value;
}
2021-05-28 19:15:09 +02:00
extension FilePropertyUpdatedEventExtension on FilePropertyUpdatedEvent {
bool hasAnyProperties(List<int> properties) =>
properties.any((p) => this.properties & p != 0);
}