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.
144 lines
4.4 KiB
Dart
144 lines
4.4 KiB
Dart
import 'dart:async';
|
|
|
|
import '../diagnostics/app_diagnostics.dart';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
|
|
import '../features/projects/conversation_sidebar.dart';
|
|
import 'project_state.dart';
|
|
import 'projects.dart';
|
|
import 'view_settings.dart';
|
|
|
|
part 'workspace_navigation.g.dart';
|
|
part 'workspace_navigation.freezed.dart';
|
|
|
|
@freezed
|
|
abstract class WorkspaceNavigationState with _$WorkspaceNavigationState {
|
|
const factory WorkspaceNavigationState({
|
|
@Default(false) bool ready,
|
|
@Default(false) bool switchingProject,
|
|
String? selectedProjectId,
|
|
}) = _WorkspaceNavigationState;
|
|
}
|
|
|
|
@Riverpod(keepAlive: true)
|
|
class WorkspaceNavigation extends _$WorkspaceNavigation {
|
|
int _revision = 0;
|
|
Completer<void> _changed = Completer<void>();
|
|
Completer<void> _quiet = Completer<void>();
|
|
Timer? _debounce;
|
|
RecentProject? _queued;
|
|
|
|
@override
|
|
WorkspaceNavigationState build() {
|
|
ref.onDispose(() {
|
|
_debounce?.cancel();
|
|
if (!_changed.isCompleted) _changed.complete();
|
|
});
|
|
ref.listen(
|
|
projectsProvider.select((value) => value.value?.activeProject?.id),
|
|
(before, after) {
|
|
if (before != after && state.ready) {
|
|
unawaited(
|
|
ref
|
|
.read(workspaceViewSettingsProvider.notifier)
|
|
.save(activeProjectId: after)
|
|
.catchError((Object error, StackTrace stack) {
|
|
AppDiagnostics.failure(
|
|
'settings',
|
|
'saveWorkspaceSelection',
|
|
error,
|
|
stack,
|
|
);
|
|
}),
|
|
);
|
|
}
|
|
},
|
|
);
|
|
unawaited(Future<void>.microtask(_restore));
|
|
return const WorkspaceNavigationState();
|
|
}
|
|
|
|
Future<void> _restore() async {
|
|
try {
|
|
final settings = await ref.read(workspaceViewSettingsProvider.future);
|
|
if (!ref.mounted) return;
|
|
final workspace = await ref.read(projectsProvider.future);
|
|
if (!ref.mounted) return;
|
|
final project = workspace.recent
|
|
.where(
|
|
(item) =>
|
|
item.id == settings.activeProjectId &&
|
|
item.availability.canOpen,
|
|
)
|
|
.firstOrNull;
|
|
if (workspace.activeProject == null && project != null) {
|
|
await openProject(project);
|
|
}
|
|
if (!ref.mounted) return;
|
|
await ref.read(conversationSidebarProvider.future);
|
|
} catch (error, stack) {
|
|
AppDiagnostics.failure('project', 'restoreWorkspace', error, stack);
|
|
} finally {
|
|
if (ref.mounted) state = state.copyWith(ready: true);
|
|
}
|
|
}
|
|
|
|
Future<void> openProject(RecentProject project) async {
|
|
final workspace = ref.read(projectsProvider).value;
|
|
if (state.selectedProjectId == project.id ||
|
|
(!state.switchingProject &&
|
|
(workspace?.projectBusy == true ||
|
|
workspace?.activeProject?.id == project.id))) {
|
|
return;
|
|
}
|
|
_revision++;
|
|
_changed.complete();
|
|
_changed = Completer<void>();
|
|
_debounce?.cancel();
|
|
_quiet = Completer<void>();
|
|
_debounce = Timer(const Duration(milliseconds: 100), _quiet.complete);
|
|
_queued = project;
|
|
state = state.copyWith(selectedProjectId: project.id);
|
|
if (state.switchingProject) return;
|
|
state = state.copyWith(switchingProject: true);
|
|
try {
|
|
while (ref.mounted && _queued != null) {
|
|
final revision = _revision;
|
|
await Future.any([_quiet.future, _changed.future]);
|
|
if (!ref.mounted) return;
|
|
if (revision != _revision) continue;
|
|
final target = _queued!;
|
|
_queued = null;
|
|
if (ref.read(projectsProvider).value?.activeProject?.id == target.id) {
|
|
continue;
|
|
}
|
|
final opened = await ref
|
|
.read(projectsProvider.notifier)
|
|
.open(
|
|
target.path,
|
|
expectedId: target.id,
|
|
isCurrent: () => ref.mounted && revision == _revision,
|
|
);
|
|
if (!ref.mounted) return;
|
|
if (opened && _queued == null) {
|
|
await ref
|
|
.read(workspaceViewSettingsProvider.notifier)
|
|
.save(activeProjectId: target.id);
|
|
}
|
|
}
|
|
} finally {
|
|
if (ref.mounted) {
|
|
state = state.copyWith(
|
|
switchingProject: false,
|
|
selectedProjectId: null,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|