Files
cutdex/lib/features/media/background_removal_controller.dart
T
lingjhf 27e77fff39 feat: initial Cutdex application
Add the Flutter video editing application with project management, media
import, timeline editing, preview, AI assistance, diagnostics and settings.

Include shared UI and native host packages, platform runners, application
tests and release tooling.

Provide usage and contribution guides, the MIT license and third-party
notices. Exclude local credentials, build outputs and validation data.
2026-10-05 23:36:23 +08:00

131 lines
4.3 KiB
Dart

import 'package:cutdex/diagnostics/app_diagnostics.dart';
import 'package:cutdex/errors/app_failure.dart';
import 'package:cutdex_sdk/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);
}
}
}