nc-photos/app/lib/event/event.dart

160 lines
3.3 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';
2022-05-05 20:34:30 +02:00
import 'package:nc_photos/entity/local_file.dart';
2021-10-06 22:32:36 +02:00
import 'package:nc_photos/entity/share.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) {
2022-03-27 19:55:56 +02:00
_log.warning("[begin] Already listening");
2021-04-10 06:28:12 +02:00
return;
}
_subscription = _stream.listen(_listener);
}
void end() {
if (_subscription == null) {
2022-03-27 19:55:56 +02:00
_log.warning("[end] Already not listening");
2021-04-10 06:28:12 +02:00
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 AccountPrefUpdatedEvent {
const AccountPrefUpdatedEvent(this.pref, this.key, this.value);
final AccountPref pref;
final PrefKey key;
final dynamic value;
}
2021-04-10 06:28:12 +02:00
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;
2022-01-25 11:08:13 +01:00
static const propFavorite = 0x08;
static const propImageLocation = 0x10;
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;
}
class ShareCreatedEvent {
const ShareCreatedEvent(this.account, this.share);
final Account account;
final Share share;
}
2021-10-06 22:32:36 +02:00
class ShareRemovedEvent {
const ShareRemovedEvent(this.account, this.share);
final Account account;
final Share share;
}
2022-01-25 11:08:13 +01:00
class FavoriteResyncedEvent {
const FavoriteResyncedEvent(this.account);
2022-01-25 11:08:13 +01:00
final Account account;
}
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 {}
enum MetadataTaskState {
/// No work is being done
idle,
/// Processing images
prcoessing,
/// Paused on data network
waitingForWifi,
2022-04-09 05:23:37 +02:00
/// Paused on low battery
lowBattery,
}
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;
}
2022-05-05 20:34:30 +02:00
class LocalFileDeletedEvent {
const LocalFileDeletedEvent(this.files);
final List<LocalFile> files;
}
2021-05-28 19:15:09 +02:00
extension FilePropertyUpdatedEventExtension on FilePropertyUpdatedEvent {
bool hasAnyProperties(List<int> properties) =>
properties.any((p) => this.properties & p != 0);
}