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
93 lines
2.7 KiB
Dart
93 lines
2.7 KiB
Dart
import 'package:cutdex_core/project.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import '../../workspace/external_media_import.dart';
|
|
import '../../workspace/media_import.dart';
|
|
import '../../workspace/media_import_feedback.dart';
|
|
import 'agent_media_attachments.dart';
|
|
|
|
/// Owns attachment operations for one conversation or draft, even while hidden.
|
|
class AgentComposerController extends ChangeNotifier {
|
|
AgentComposerController({
|
|
required this.canImport,
|
|
required this.pickFiles,
|
|
required this.importMedia,
|
|
List<AgentMediaAttachment>? attachments,
|
|
}) : attachments = attachments ?? [];
|
|
|
|
final bool Function() canImport;
|
|
final Future<List<ExternalMediaFile>> Function() pickFiles;
|
|
final Future<ExternalMediaSelection?> Function(List<ExternalMediaFile>)
|
|
importMedia;
|
|
final List<AgentMediaAttachment> attachments;
|
|
final feedback = MediaImportFeedbackController();
|
|
bool _busy = false;
|
|
bool _usingDraftModel = false;
|
|
bool get usingDraftModel => _usingDraftModel;
|
|
void useDraftModel() {
|
|
if (_disposed) return;
|
|
_usingDraftModel = true;
|
|
notifyListeners();
|
|
}
|
|
|
|
bool _disposed = false;
|
|
bool get busy => _busy;
|
|
|
|
void attach(AssetDocument asset) {
|
|
if (_disposed || attachments.any((item) => item.id == asset.id)) return;
|
|
attachments.add(AgentMediaAttachment.fromAsset(asset));
|
|
notifyListeners();
|
|
}
|
|
|
|
void remove(String id) {
|
|
attachments.removeWhere((item) => item.id == id);
|
|
notifyListeners();
|
|
}
|
|
|
|
void clear() {
|
|
attachments.clear();
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<ExternalMediaSelection?> pickAttachments() => _run(pickFiles);
|
|
|
|
Future<ExternalMediaSelection?> importAttachments(
|
|
List<ExternalMediaFile> files,
|
|
) => _run(() async => files);
|
|
|
|
Future<ExternalMediaSelection?> _run(
|
|
Future<List<ExternalMediaFile>> Function() select,
|
|
) async {
|
|
if (_disposed || _busy || !canImport()) return null;
|
|
_busy = true;
|
|
notifyListeners();
|
|
try {
|
|
final files = await select();
|
|
if (_disposed || files.isEmpty || !canImport()) return null;
|
|
final result = await importMedia(files);
|
|
// Deleting the conversation invalidates this destination. Hiding its
|
|
// presentation does not, so completed imports still populate the draft.
|
|
if (!_disposed) {
|
|
for (final asset in result?.selected ?? <AssetDocument>[]) {
|
|
attach(asset);
|
|
}
|
|
feedback.recordResult(result);
|
|
}
|
|
return result;
|
|
} catch (_) {
|
|
if (!_disposed) feedback.recordFailure();
|
|
rethrow;
|
|
} finally {
|
|
_busy = false;
|
|
if (!_disposed) notifyListeners();
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_disposed = true;
|
|
feedback.dispose();
|
|
super.dispose();
|
|
}
|
|
}
|