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
253 lines
8.0 KiB
Dart
253 lines
8.0 KiB
Dart
import 'package:cutdex/errors/app_failure.dart';
|
|
|
|
import 'dart:async';
|
|
|
|
import 'package:cutdex_core/project.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:cutdex_native_flutter/cutdex_native_flutter.dart';
|
|
import 'package:cutdex_core/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();
|
|
}
|
|
}
|
|
}
|