Files
cutdex/lib/features/settings/location_settings.dart
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

140 lines
4.4 KiB
Dart

import 'dart:async';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import '../../integrations/runtime/processing_resource_repository.dart';
import '../../persistence/settings/location_settings_repositories.dart';
import '../../settings/settings_write_queue.dart';
import '../../workspace/persistence/project_location_directory.dart';
part 'location_settings.g.dart';
part 'location_settings.freezed.dart';
enum LocationSettingsError { load, save }
@freezed
abstract class LocationSettingsState with _$LocationSettingsState {
const factory LocationSettingsState({
String? directory,
ProjectLocationStatus? status,
@Default(true) bool loading,
@Default(false) bool saving,
LocationSettingsError? error,
}) = _LocationSettingsState;
}
@riverpod
ProjectLocationDirectory projectLocationDirectory(Ref ref) =>
const ProjectLocationDirectory();
@riverpod
class ProjectLocationSettings extends _$ProjectLocationSettings {
final _writes = SettingsWriteQueue();
@override
LocationSettingsState build() {
final repository = ref.watch(projectLocationSettingsRepositoryProvider);
final directory = ref.watch(projectLocationDirectoryProvider);
unawaited(_writes.run(() => _load(repository, directory)));
return const LocationSettingsState();
}
Future<void> reload() => _writes.run(() async {
if (!ref.mounted) return;
await _load(
ref.read(projectLocationSettingsRepositoryProvider),
ref.read(projectLocationDirectoryProvider),
);
});
Future<void> _load(
ProjectLocationSettingsRepository repository,
ProjectLocationDirectory inspector,
) async {
if (!ref.mounted) return;
state = state.copyWith(loading: true, error: null, status: null);
try {
final directory = await repository.load();
if (!ref.mounted) return;
state = state.copyWith(directory: directory);
final status = directory == null
? null
: await inspector.inspect(directory);
if (ref.mounted) state = state.copyWith(status: status);
} catch (_) {
if (ref.mounted) {
state = state.copyWith(error: LocationSettingsError.load);
}
} finally {
if (ref.mounted) state = state.copyWith(loading: false);
}
}
Future<void> save(String directory) => _writes.run(() async {
if (!ref.mounted) return;
final keepAlive = ref.keepAlive();
state = state.copyWith(saving: true, error: null);
final repository = ref.read(projectLocationSettingsRepositoryProvider);
final inspector = ref.read(projectLocationDirectoryProvider);
try {
await repository.save(directory);
if (ref.mounted) await _load(repository, inspector);
} catch (_) {
if (ref.mounted) {
state = state.copyWith(error: LocationSettingsError.save);
}
} finally {
if (ref.mounted) state = state.copyWith(saving: false);
keepAlive.close();
}
});
}
@riverpod
class ResourceLocationSettings extends _$ResourceLocationSettings {
final _writes = SettingsWriteQueue();
@override
LocationSettingsState build() {
final repository = ref.watch(resourceLocationSettingsRepositoryProvider);
unawaited(_writes.run(() => _load(repository)));
return const LocationSettingsState();
}
Future<void> _load(ResourceLocationSettingsRepository repository) async {
if (!ref.mounted) return;
try {
final directory = await repository.load();
if (ref.mounted) state = state.copyWith(directory: directory);
} catch (_) {
if (ref.mounted) {
state = state.copyWith(error: LocationSettingsError.load);
}
} finally {
if (ref.mounted) state = state.copyWith(loading: false);
}
}
Future<void> save(String directory) => _writes.run(() async {
if (!ref.mounted) return;
final keepAlive = ref.keepAlive();
state = state.copyWith(saving: true, error: null);
try {
await ref
.read(resourceLocationSettingsRepositoryProvider)
.save(directory);
if (!ref.mounted) return;
ref.invalidate(processingResourceRepositoryProvider);
state = state.copyWith(directory: directory);
} catch (_) {
if (ref.mounted) {
state = state.copyWith(error: LocationSettingsError.save);
}
} finally {
if (ref.mounted) state = state.copyWith(saving: false);
keepAlive.close();
}
});
}