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.
This commit is contained in:
commit
27e77fff39
718 files changed
+183958
No files matched your search
@@ -0,0 +1,252 @@
|
||||
import 'package:cutdex/errors/app_failure.dart';
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:cutdex_sdk/project.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:cutdex_sdk/runtime.dart';
|
||||
import 'package:cutdex_sdk/timeline/model.dart';
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
import '../../workspace/external_media_import.dart';
|
||||
import '../../workspace/media_import.dart';
|
||||
import '../../workspace/prepared_media_metadata.dart';
|
||||
import '../../workspace/projects.dart';
|
||||
import '../../workspace/preview_target.dart';
|
||||
import '../../workspace/timeline_session.dart';
|
||||
|
||||
part 'timeline_drop_preparation.g.dart';
|
||||
part 'timeline_drop_preparation.freezed.dart';
|
||||
|
||||
@freezed
|
||||
sealed class TimelineDropSource with _$TimelineDropSource {
|
||||
const factory TimelineDropSource.file(ExternalMediaFile file) = DroppedFile;
|
||||
const factory TimelineDropSource.asset(AssetDocument asset) = DroppedAsset;
|
||||
}
|
||||
|
||||
@freezed
|
||||
abstract class PreparedTimelineMedia with _$PreparedTimelineMedia {
|
||||
const factory PreparedTimelineMedia({
|
||||
required TimelineDropSource source,
|
||||
required MediaReference media,
|
||||
required PreparedMediaMetadata metadata,
|
||||
}) = _PreparedTimelineMedia;
|
||||
}
|
||||
|
||||
@freezed
|
||||
abstract class TimelineDropState with _$TimelineDropState {
|
||||
const factory TimelineDropState({
|
||||
@Default(AsyncData<PreparedTimelineMedia?>(null))
|
||||
AsyncValue<PreparedTimelineMedia?> preparation,
|
||||
@Default(AsyncData<void>(null)) AsyncValue<void> commit,
|
||||
}) = _TimelineDropState;
|
||||
}
|
||||
|
||||
/// Probing and importing share the captured project, independent of pointer movement.
|
||||
@riverpod
|
||||
class TimelineDropPreparation extends _$TimelineDropPreparation {
|
||||
int _revision = 0;
|
||||
Future<void>? _pending;
|
||||
final _operations = <int, Set<Future<void>>>{};
|
||||
@override
|
||||
TimelineDropState build(
|
||||
String projectId,
|
||||
EditorTimelineSession session,
|
||||
SequenceId sequence,
|
||||
) => const TimelineDropState();
|
||||
void reset() {
|
||||
final revision = ++_revision;
|
||||
scheduleMicrotask(() {
|
||||
if (ref.mounted && revision == _revision) {
|
||||
state = const TimelineDropState();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
bool get _active {
|
||||
if (!ref.mounted) return false;
|
||||
final workspace = ref.read(projectsProvider).value;
|
||||
return session.acceptsEdits &&
|
||||
workspace?.activeProject?.id == projectId &&
|
||||
workspace?.timeline?.id == session.document.id;
|
||||
}
|
||||
|
||||
Future<void> waitForPreparation() => _pending ?? Future<void>.value();
|
||||
|
||||
/// Capture this drag's work before reset changes the current revision.
|
||||
/// Errors remain visible to callers, while native access can always be released.
|
||||
Future<void> waitForCompletion() =>
|
||||
Future.wait(_operations[_revision]?.toList() ?? <Future<void>>[])
|
||||
.then((_) {});
|
||||
|
||||
void _track(Future<void> work, int revision) {
|
||||
final operations = _operations.putIfAbsent(revision, () => {});
|
||||
final settled = work.then<void>(
|
||||
(_) {},
|
||||
onError: (Object _, StackTrace _) {},
|
||||
);
|
||||
operations.add(settled);
|
||||
unawaited(
|
||||
settled.then((_) {
|
||||
operations.remove(settled);
|
||||
if (operations.isEmpty) _operations.remove(revision);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> probe(TimelineDropSource source) {
|
||||
final revision = ++_revision;
|
||||
state = const TimelineDropState(preparation: AsyncLoading());
|
||||
final lease = ref.keepAlive();
|
||||
final work = _probe(source, revision);
|
||||
_pending = work;
|
||||
final completion = work.whenComplete(() {
|
||||
if (identical(_pending, work)) _pending = null;
|
||||
lease.close();
|
||||
});
|
||||
_track(completion, revision);
|
||||
return completion;
|
||||
}
|
||||
|
||||
Future<void> _probe(TimelineDropSource source, int revision) async {
|
||||
final runtime = ref.read(runtimeConnectionProvider);
|
||||
try {
|
||||
final image =
|
||||
source is DroppedAsset && source.asset.metadata['kind'] == 'image';
|
||||
Future<PreparedTimelineMedia> load(MediaReference media) async {
|
||||
final metadata = image
|
||||
? PreparedMediaMetadata.image()
|
||||
: PreparedMediaMetadata.fromInfo(await runtime.probe(media));
|
||||
return PreparedTimelineMedia(
|
||||
source: source,
|
||||
media: media,
|
||||
metadata: metadata,
|
||||
);
|
||||
}
|
||||
|
||||
final value = switch (source) {
|
||||
DroppedAsset(:final asset) =>
|
||||
await ref
|
||||
.read(projectsProvider.notifier)
|
||||
.withAssetMedia(projectId, asset, load),
|
||||
DroppedFile(:final file) => await load(
|
||||
MediaReference(
|
||||
id: file.localPath!,
|
||||
uri: Uri.file(
|
||||
file.localPath!,
|
||||
windows:
|
||||
!kIsWeb && defaultTargetPlatform == TargetPlatform.windows,
|
||||
),
|
||||
),
|
||||
),
|
||||
};
|
||||
if (ref.mounted && revision == _revision && _active) {
|
||||
state = state.copyWith(preparation: AsyncData(value));
|
||||
}
|
||||
} catch (error, stack) {
|
||||
if (ref.mounted && revision == _revision) {
|
||||
state = state.copyWith(preparation: AsyncError(error, stack));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> commit(
|
||||
Future<void> Function(AssetDocument asset, MediaReference media) insert, {
|
||||
required bool Function() isCurrent,
|
||||
void Function()? validatePlacement,
|
||||
}) {
|
||||
if (state.commit.isLoading) return Future<void>.value();
|
||||
final revision = _revision;
|
||||
final work = _commit(
|
||||
insert,
|
||||
revision: revision,
|
||||
isCurrent: isCurrent,
|
||||
validatePlacement: validatePlacement,
|
||||
);
|
||||
_track(work, revision);
|
||||
return work;
|
||||
}
|
||||
|
||||
Future<void> _commit(
|
||||
Future<void> Function(AssetDocument asset, MediaReference media) insert, {
|
||||
required int revision,
|
||||
required bool Function() isCurrent,
|
||||
void Function()? validatePlacement,
|
||||
}) async {
|
||||
final lease = ref.keepAlive();
|
||||
final release = session.retainViews();
|
||||
state = state.copyWith(commit: const AsyncLoading());
|
||||
try {
|
||||
await waitForPreparation();
|
||||
if (!ref.mounted || revision != _revision || !_active || !isCurrent()) {
|
||||
return;
|
||||
}
|
||||
final preparation = state.preparation;
|
||||
if (preparation.hasError) {
|
||||
Error.throwWithStackTrace(
|
||||
preparation.error!,
|
||||
preparation.stackTrace ?? StackTrace.current,
|
||||
);
|
||||
}
|
||||
final prepared = preparation.value;
|
||||
if (prepared == null) {
|
||||
throw AppException(AppErrorCode.mediaReadFailed);
|
||||
}
|
||||
validatePlacement?.call();
|
||||
final AssetDocument? asset;
|
||||
switch (prepared.source) {
|
||||
case DroppedAsset(asset: final asAsset):
|
||||
asset = asAsset;
|
||||
case DroppedFile(:final file):
|
||||
final imported = await ref
|
||||
.read(externalMediaImportProvider(projectId).notifier)
|
||||
.importFiles([file]);
|
||||
asset = imported?.selected.firstOrNull;
|
||||
if (asset == null) {
|
||||
throw AppException.fromFailure(
|
||||
imported?.result?.failures.firstOrNull?.failure ??
|
||||
const AppFailure(AppErrorCode.mediaImportFailed),
|
||||
);
|
||||
}
|
||||
}
|
||||
if (!ref.mounted || revision != _revision || !_active || !isCurrent()) {
|
||||
return;
|
||||
}
|
||||
await ref.read(projectsProvider.notifier).withAssetMedia(
|
||||
projectId,
|
||||
asset,
|
||||
(media) async {
|
||||
if (!ref.mounted ||
|
||||
revision != _revision ||
|
||||
!_active ||
|
||||
!isCurrent()) {
|
||||
return;
|
||||
}
|
||||
await insert(asset!, media);
|
||||
},
|
||||
);
|
||||
if (ref.mounted && revision == _revision && _active && isCurrent()) {
|
||||
ref
|
||||
.read(
|
||||
previewNavigationProvider(
|
||||
projectId,
|
||||
session.document.id,
|
||||
).notifier,
|
||||
)
|
||||
.showTimeline();
|
||||
}
|
||||
} catch (error, stack) {
|
||||
if (ref.mounted && revision == _revision) {
|
||||
state = state.copyWith(commit: AsyncError(error, stack));
|
||||
}
|
||||
rethrow;
|
||||
} finally {
|
||||
if (ref.mounted && revision == _revision && state.commit.isLoading) {
|
||||
state = state.copyWith(commit: const AsyncData(null));
|
||||
}
|
||||
release();
|
||||
lease.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user