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
137 lines
4.2 KiB
Dart
137 lines
4.2 KiB
Dart
import 'package:cutdex_core/editor.dart';
|
|
import 'package:cutdex_core/timeline/model.dart';
|
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
|
|
import '../../workspace/timeline_session.dart';
|
|
|
|
part 'subtitle_task_actions.g.dart';
|
|
part 'subtitle_task_actions.freezed.dart';
|
|
|
|
enum SubtitleActionKind { original, translation, cancel, retry }
|
|
|
|
@freezed
|
|
abstract class SubtitleActionFeedback with _$SubtitleActionFeedback {
|
|
const factory SubtitleActionFeedback({
|
|
required SubtitleActionKind kind,
|
|
int? cueCount,
|
|
}) = _SubtitleActionFeedback;
|
|
}
|
|
|
|
@freezed
|
|
abstract class SubtitleActionException
|
|
with _$SubtitleActionException
|
|
implements Exception {
|
|
const factory SubtitleActionException(SubtitleActionKind kind, Object cause) =
|
|
_SubtitleActionException;
|
|
}
|
|
|
|
/// One operation state is shared by the compact card and its details dialog.
|
|
@riverpod
|
|
class SubtitleTaskActions extends _$SubtitleTaskActions {
|
|
@override
|
|
AsyncValue<SubtitleActionFeedback?> build(
|
|
EditorTimelineSession session,
|
|
String jobId,
|
|
) => const AsyncData(null);
|
|
|
|
Future<List<ElementDto>> candidates(
|
|
SubtitleBackgroundTask job, {
|
|
bool translated = false,
|
|
}) async {
|
|
final recognition = job.recognition;
|
|
return session.persistence
|
|
.timeline(job.sequence)
|
|
.exportDto()
|
|
.elements
|
|
.where((element) {
|
|
final binding = element.payloadRef == null
|
|
? null
|
|
: session.mediaBinding(element.payloadRef!);
|
|
return (translated
|
|
? binding?.media.uri == recognition.source
|
|
: binding?.media.id == recognition.command.mediaId) &&
|
|
binding?.audioTrackIndex == recognition.command.audioStream;
|
|
})
|
|
.toList();
|
|
}
|
|
|
|
Future<void> apply(
|
|
SubtitleBackgroundTask job,
|
|
String clipId, {
|
|
required String expectedVersion,
|
|
bool translated = false,
|
|
}) => _run(
|
|
translated ? SubtitleActionKind.translation : SubtitleActionKind.original,
|
|
() async {
|
|
final recognition = job.recognition;
|
|
final outcome = translated
|
|
? await session.executeCommand(
|
|
job.sequence,
|
|
ApplySubtitleTranslation(
|
|
source: recognition.source!.toFilePath(),
|
|
audioStream: recognition.command.audioStream,
|
|
sourceStartMs: recognition.sourceStartMs,
|
|
taskId: job.translation!.id,
|
|
clipId: clipId,
|
|
expectedVersion: expectedVersion,
|
|
),
|
|
)
|
|
: await session.executeCommand(
|
|
job.sequence,
|
|
ApplyTranscription(
|
|
taskId: recognition.id,
|
|
clipId: clipId,
|
|
expectedVersion: expectedVersion,
|
|
),
|
|
);
|
|
final ids = (outcome.data['captionIds'] as List).cast<String>();
|
|
final view = session.view(job.sequence);
|
|
final handle = ids.isEmpty
|
|
? null
|
|
: view.version.kernelVersion.query().elementById(
|
|
ElementId(ids.first),
|
|
);
|
|
final component = handle == null
|
|
? null
|
|
: session.captionComponentFor(job.sequence, handle);
|
|
if (component != null) view.setSelection([component]);
|
|
return outcome.data['cueCount'] as int;
|
|
},
|
|
);
|
|
|
|
Future<void> cancel(SubtitleBackgroundTask job) =>
|
|
_run(SubtitleActionKind.cancel, () async {
|
|
await session.subtitleWorkflows.cancel(job);
|
|
return null;
|
|
});
|
|
Future<void> retry(SubtitleBackgroundTask job) =>
|
|
_run(SubtitleActionKind.retry, () async {
|
|
await session.subtitleWorkflows.retry(job);
|
|
return null;
|
|
});
|
|
|
|
Future<void> _run(
|
|
SubtitleActionKind kind,
|
|
Future<int?> Function() action,
|
|
) async {
|
|
if (state.isLoading) return;
|
|
final lease = ref.keepAlive();
|
|
final release = session.retainViews();
|
|
state = const AsyncLoading();
|
|
try {
|
|
final count = await action();
|
|
if (ref.mounted) {
|
|
state = AsyncData(SubtitleActionFeedback(kind: kind, cueCount: count));
|
|
}
|
|
} catch (error, stack) {
|
|
if (ref.mounted) {
|
|
state = AsyncError(SubtitleActionException(kind, error), stack);
|
|
}
|
|
} finally {
|
|
release();
|
|
lease.close();
|
|
}
|
|
}
|
|
}
|