Files
cutdex/lib/features/media/background_removal_controller.dart
T
lingjhf 06f1c26681
Gitea CI / Assemble and validate CI artifacts (push) Successful in 1m47s
Gitea CI / Check code and tests (push) Successful in 7m15s
Gitea CI / Build Android APK (push) Successful in 4m6s
Gitea CI / Build Windows x64 app (push) Successful in 3m34s
Gitea CI / Audit and package Windows x64 (push) Successful in 1m29s
Gitea CI / Gitea CI passed (push) Successful in 1s
Gitea Release / Prepare draft Release (push) Failing after 8s
Gitea Release / Upload Android to draft Release (push) Skipped
Gitea Release / Upload Windows to draft Release (push) Skipped
Gitea Release / Verify and publish Release (push) Skipped
Move editor UI into the app and release verified Core dependencies (#14)
2026-10-11 19:59:40 +00:00

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);
}
}
}