2021-04-10 12:28:12 +08: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-06 02:34:30 +08:00
|
|
|
import 'package:nc_photos/entity/local_file.dart';
|
2021-10-07 04:32:36 +08:00
|
|
|
import 'package:nc_photos/entity/share.dart';
|
2021-09-11 20:03:40 +08:00
|
|
|
import 'package:nc_photos/pref.dart';
|
2021-04-10 12:28:12 +08:00
|
|
|
|
|
|
|
class AppEventListener<T> {
|
|
|
|
AppEventListener(this._listener);
|
|
|
|
|
|
|
|
void begin() {
|
|
|
|
if (_subscription != null) {
|
2022-03-28 01:55:56 +08:00
|
|
|
_log.warning("[begin] Already listening");
|
2021-04-10 12:28:12 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
_subscription = _stream.listen(_listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
void end() {
|
|
|
|
if (_subscription == null) {
|
2022-03-28 01:55:56 +08:00
|
|
|
_log.warning("[end] Already not listening");
|
2021-04-10 12:28:12 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-07-24 04:05:57 +08:00
|
|
|
_subscription?.cancel();
|
2021-04-10 12:28:12 +08:00
|
|
|
_subscription = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
final void Function(T) _listener;
|
|
|
|
final _stream = KiwiContainer().resolve<EventBus>().on<T>();
|
2021-07-24 04:05:57 +08:00
|
|
|
StreamSubscription<T>? _subscription;
|
2021-04-10 12:28:12 +08:00
|
|
|
|
|
|
|
final _log = Logger("event.event.AppEventListener<${T.runtimeType}>");
|
|
|
|
}
|
|
|
|
|
2021-12-06 00:14:24 +08:00
|
|
|
class AccountPrefUpdatedEvent {
|
|
|
|
const AccountPrefUpdatedEvent(this.pref, this.key, this.value);
|
|
|
|
|
|
|
|
final AccountPref pref;
|
|
|
|
final PrefKey key;
|
|
|
|
final dynamic value;
|
|
|
|
}
|
|
|
|
|
2021-04-10 12:28:12 +08: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-29 01:15:09 +08:00
|
|
|
class FilePropertyUpdatedEvent {
|
|
|
|
FilePropertyUpdatedEvent(this.account, this.file, this.properties);
|
2021-04-10 12:28:12 +08:00
|
|
|
|
|
|
|
final Account account;
|
|
|
|
final File file;
|
2021-05-29 01:15:09 +08:00
|
|
|
final int properties;
|
|
|
|
|
|
|
|
// Bit masks for properties field
|
|
|
|
static const propMetadata = 0x01;
|
2021-05-29 02:45:00 +08:00
|
|
|
static const propIsArchived = 0x02;
|
2021-06-21 18:39:17 +08:00
|
|
|
static const propOverrideDateTime = 0x04;
|
2022-01-25 18:08:13 +08:00
|
|
|
static const propFavorite = 0x08;
|
2022-08-27 23:31:58 +08:00
|
|
|
static const propImageLocation = 0x10;
|
2021-04-10 12:28:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class FileRemovedEvent {
|
|
|
|
FileRemovedEvent(this.account, this.file);
|
|
|
|
|
|
|
|
final Account account;
|
|
|
|
final File file;
|
|
|
|
}
|
2021-04-17 17:04:46 +08:00
|
|
|
|
2021-08-02 04:46:16 +08:00
|
|
|
class FileTrashbinRestoredEvent {
|
|
|
|
FileTrashbinRestoredEvent(this.account, this.file);
|
|
|
|
|
|
|
|
final Account account;
|
|
|
|
final File file;
|
|
|
|
}
|
|
|
|
|
2021-08-13 18:39:17 +08:00
|
|
|
class FileMovedEvent {
|
|
|
|
FileMovedEvent(this.account, this.file, this.destination);
|
|
|
|
|
|
|
|
final Account account;
|
|
|
|
final File file;
|
|
|
|
final String destination;
|
|
|
|
}
|
|
|
|
|
2021-10-17 17:45:56 +08:00
|
|
|
class ShareCreatedEvent {
|
|
|
|
const ShareCreatedEvent(this.account, this.share);
|
|
|
|
|
|
|
|
final Account account;
|
|
|
|
final Share share;
|
|
|
|
}
|
|
|
|
|
2021-10-07 04:32:36 +08:00
|
|
|
class ShareRemovedEvent {
|
|
|
|
const ShareRemovedEvent(this.account, this.share);
|
|
|
|
|
|
|
|
final Account account;
|
|
|
|
final Share share;
|
|
|
|
}
|
|
|
|
|
2022-01-25 18:08:13 +08:00
|
|
|
class FavoriteResyncedEvent {
|
2022-07-28 03:00:42 +08:00
|
|
|
const FavoriteResyncedEvent(this.account);
|
2022-01-25 18:08:13 +08:00
|
|
|
|
|
|
|
final Account account;
|
|
|
|
}
|
|
|
|
|
2021-04-17 17:04:46 +08:00
|
|
|
class ThemeChangedEvent {}
|
2021-05-29 01:15:09 +08:00
|
|
|
|
2021-06-23 16:15:25 +08:00
|
|
|
class LanguageChangedEvent {}
|
|
|
|
|
2022-04-02 00:52:56 +08:00
|
|
|
enum MetadataTaskState {
|
|
|
|
/// No work is being done
|
|
|
|
idle,
|
|
|
|
|
|
|
|
/// Processing images
|
|
|
|
prcoessing,
|
|
|
|
|
|
|
|
/// Paused on data network
|
|
|
|
waitingForWifi,
|
2022-04-09 11:23:37 +08:00
|
|
|
|
|
|
|
/// Paused on low battery
|
|
|
|
lowBattery,
|
2022-04-02 00:52:56 +08:00
|
|
|
}
|
|
|
|
|
2021-08-16 01:06:00 +08:00
|
|
|
class MetadataTaskStateChangedEvent {
|
|
|
|
const MetadataTaskStateChangedEvent(this.state);
|
|
|
|
|
|
|
|
final MetadataTaskState state;
|
|
|
|
}
|
|
|
|
|
2021-09-11 20:03:40 +08:00
|
|
|
class PrefUpdatedEvent {
|
|
|
|
PrefUpdatedEvent(this.key, this.value);
|
|
|
|
|
|
|
|
final PrefKey key;
|
|
|
|
final dynamic value;
|
|
|
|
}
|
|
|
|
|
2022-05-06 02:34:30 +08:00
|
|
|
class LocalFileDeletedEvent {
|
|
|
|
const LocalFileDeletedEvent(this.files);
|
|
|
|
|
|
|
|
final List<LocalFile> files;
|
|
|
|
}
|
|
|
|
|
2021-05-29 01:15:09 +08:00
|
|
|
extension FilePropertyUpdatedEventExtension on FilePropertyUpdatedEvent {
|
|
|
|
bool hasAnyProperties(List<int> properties) =>
|
|
|
|
properties.any((p) => this.properties & p != 0);
|
|
|
|
}
|