nc-photos/lib/metadata_task_manager.dart

110 lines
2.8 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';
2021-04-10 06:28:12 +02:00
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/api/api_util.dart' as api_util;
import 'package:nc_photos/entity/file.dart';
2021-05-24 09:09:25 +02:00
import 'package:nc_photos/entity/file/data_source.dart';
import 'package:nc_photos/event/event.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/pref.dart';
import 'package:nc_photos/use_case/update_missing_metadata.dart';
/// Task to update metadata for missing files
class MetadataTask {
MetadataTask(this.account);
@override
toString() {
return "$runtimeType {"
"account: $account, "
"}";
}
Future<void> call() async {
try {
final fileRepo = FileRepo(FileCachedDataSource());
for (final r in account.roots) {
final op = UpdateMissingMetadata(fileRepo);
await for (final _ in op(account,
File(path: "${api_util.getWebdavRootUrlRelative(account)}/$r"))) {
2021-10-27 22:40:54 +02:00
if (!Pref().isEnableExifOr()) {
_log.info("[call] EXIF disabled, task ending immaturely");
op.stop();
return;
}
2021-04-10 06:28:12 +02:00
}
}
} finally {
KiwiContainer()
.resolve<EventBus>()
2021-09-15 08:58:06 +02:00
.fire(const MetadataTaskStateChangedEvent(MetadataTaskState.idle));
2021-04-10 06:28:12 +02:00
}
}
final Account account;
static final _log = Logger("metadata_task_manager.MetadataTask");
}
/// Manage metadata tasks to run concurrently
class MetadataTaskManager {
factory MetadataTaskManager() {
2021-09-15 08:58:06 +02:00
_inst ??= MetadataTaskManager._();
return _inst!;
}
MetadataTaskManager._() {
_stateChangedListener.begin();
2021-04-10 06:28:12 +02:00
_handleStream();
}
/// Add a task to the queue
void addTask(MetadataTask task) {
_log.info("[addTask] New task added: $task");
_streamController.add(task);
}
MetadataTaskState get state => _currentState;
void _onMetadataTaskStateChanged(MetadataTaskStateChangedEvent ev) {
if (ev.state != _currentState) {
_currentState = ev.state;
}
}
2021-04-10 06:28:12 +02:00
void _handleStream() async {
await for (final task in _streamController.stream) {
2021-10-27 22:40:54 +02:00
if (Pref().isEnableExifOr()) {
2021-04-10 06:28:12 +02:00
_log.info("[_doTask] Executing task: $task");
await task();
} else {
_log.info("[_doTask] Ignoring task: $task");
}
}
}
final _streamController = StreamController<MetadataTask>.broadcast();
var _currentState = MetadataTaskState.idle;
late final _stateChangedListener =
AppEventListener<MetadataTaskStateChangedEvent>(
_onMetadataTaskStateChanged);
2021-04-10 06:28:12 +02:00
static final _log = Logger("metadata_task_manager.MetadataTaskManager");
static MetadataTaskManager? _inst;
}
enum MetadataTaskState {
/// No work is being done
idle,
/// Processing images
prcoessing,
/// Paused on data network
waitingForWifi,
2021-04-10 06:28:12 +02:00
}