Gitea CI / Check code and tests (pull_request) Failing after 8s
Gitea CI / Build Android APK (pull_request) Skipped
Gitea CI / Build Windows x64 app (pull_request) Skipped
Gitea CI / Audit and package Windows x64 (pull_request) Skipped
Gitea CI / Assemble and validate CI artifacts (pull_request) Skipped
Gitea CI / Gitea CI passed (pull_request) Failing after 1s
131 lines
4.3 KiB
Dart
131 lines
4.3 KiB
Dart
import 'package:cutdex/diagnostics/app_diagnostics.dart';
|
|
import 'package:cutdex/errors/app_failure.dart';
|
|
|
|
import 'package:cutdex_core/editor.dart';
|
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
|
|
import '../../integrations/runtime/processing_configuration.dart';
|
|
import '../../integrations/runtime/processing_resource_repository.dart';
|
|
import '../../workspace/timeline_session.dart';
|
|
import 'processing_targets.dart';
|
|
|
|
part 'background_removal_controller.g.dart';
|
|
part 'background_removal_controller.freezed.dart';
|
|
|
|
enum BackgroundRemovalFailure { download, start, rejected }
|
|
|
|
@freezed
|
|
sealed class BackgroundRemovalState with _$BackgroundRemovalState {
|
|
const factory BackgroundRemovalState.idle() = RemovalIdle;
|
|
const factory BackgroundRemovalState.downloading() = RemovalDownloading;
|
|
const factory BackgroundRemovalState.starting() = RemovalStarting;
|
|
const factory BackgroundRemovalState.started() = RemovalStarted;
|
|
const factory BackgroundRemovalState.cancelled() = RemovalCancelled;
|
|
const factory BackgroundRemovalState.failed(
|
|
BackgroundRemovalFailure failure, {
|
|
String? message,
|
|
}) = RemovalFailed;
|
|
}
|
|
|
|
@riverpod
|
|
class BackgroundRemovalController extends _$BackgroundRemovalController {
|
|
bool _cancelled = false;
|
|
ProcessingResourceRepository? _repository;
|
|
|
|
@override
|
|
BackgroundRemovalState build(
|
|
EditorTimelineSession session,
|
|
BackgroundRemovalClipTarget target,
|
|
) {
|
|
ref.onDispose(() {
|
|
_cancelled = true;
|
|
});
|
|
return const BackgroundRemovalState.idle();
|
|
}
|
|
|
|
Future<void> start(
|
|
ProcessingResourceRepository repository, {
|
|
bool download = false,
|
|
}) async {
|
|
if (state is RemovalDownloading || state is RemovalStarting) return;
|
|
final lease = ref.keepAlive();
|
|
final release = session.retainViews();
|
|
_repository = repository;
|
|
_cancelled = false;
|
|
var installed = !download;
|
|
state = download
|
|
? const BackgroundRemovalState.downloading()
|
|
: const BackgroundRemovalState.starting();
|
|
try {
|
|
if (download) {
|
|
await repository.ensureAvailable(
|
|
ProcessingCapability.backgroundRemoval,
|
|
);
|
|
installed = true;
|
|
}
|
|
if (_cancelled || !ref.mounted || !session.acceptsEdits) return;
|
|
state = const BackgroundRemovalState.starting();
|
|
final processing = ProcessingConfiguration.configured(
|
|
diagnostics: AppDiagnostics.report,
|
|
resourcesRoot: repository.root.parent.path,
|
|
);
|
|
final model = processing?.backgroundRemovalModel;
|
|
if (processing == null || model == null) {
|
|
throw AppException(AppErrorCode.startBackgroundRemovalFailed);
|
|
}
|
|
session.backgroundRemovals.configure(
|
|
backend: processing.backend.execute,
|
|
model: model,
|
|
);
|
|
await session.backgroundRemovals.start(
|
|
sequence: target.sequence,
|
|
clipId: target.clipId,
|
|
mediaId: target.mediaId,
|
|
name: target.name,
|
|
backend: processing.backend.execute,
|
|
model: model,
|
|
);
|
|
if (ref.mounted) state = const BackgroundRemovalState.started();
|
|
} on ProcessingResourceCancelledException {
|
|
if (ref.mounted) state = const BackgroundRemovalState.cancelled();
|
|
} on CommandRejected catch (error) {
|
|
if (ref.mounted) {
|
|
state = BackgroundRemovalState.failed(
|
|
BackgroundRemovalFailure.rejected,
|
|
message: error.message,
|
|
);
|
|
}
|
|
} catch (_) {
|
|
if (ref.mounted) {
|
|
state = BackgroundRemovalState.failed(
|
|
installed
|
|
? BackgroundRemovalFailure.start
|
|
: BackgroundRemovalFailure.download,
|
|
);
|
|
}
|
|
} finally {
|
|
if (ref.mounted &&
|
|
(state is RemovalDownloading || state is RemovalStarting)) {
|
|
state = const BackgroundRemovalState.cancelled();
|
|
}
|
|
_repository = null;
|
|
release();
|
|
lease.close();
|
|
}
|
|
}
|
|
|
|
void cancel(ProcessingResourceRepository repository) {
|
|
_cancelled = true;
|
|
(_repository ?? repository).cancel(ProcessingCapability.backgroundRemoval);
|
|
}
|
|
|
|
void togglePause(ProcessingResourceRepository repository) {
|
|
if (repository.downloadPausedFor(ProcessingCapability.backgroundRemoval)) {
|
|
repository.resumeDownload(ProcessingCapability.backgroundRemoval);
|
|
} else {
|
|
repository.pauseDownload(ProcessingCapability.backgroundRemoval);
|
|
}
|
|
}
|
|
}
|