Files
pipelines/app/lib/features/settings/storage/storage_controller.dart
T

126 lines
3.4 KiB
Dart

import 'package:flutter/foundation.dart';
import '../../../core/api_gateway.dart';
import 'storage_models.dart';
class StorageController extends ChangeNotifier {
StorageController(this.gateway);
final ApiGateway gateway;
JsonMap? overview;
JsonMap? preview;
JsonMap? result;
String? error;
String? notice;
bool busy = false;
bool _disposed = false;
int _revision = 0;
void invalidatePreview() {
_revision++;
preview = null;
result = null;
notice = null;
_notify();
}
Future<JsonMap> _call(String type, [JsonMap fields = const {}]) async {
final reply = await gateway.call(type, fields);
if (reply['error'] case final String message when message.isNotEmpty) {
throw StateError(message);
}
return reply;
}
JsonMap _overview(Object? value) {
final data = storageMap(value);
if (data['total_bytes'] is! num ||
(data['total_bytes'] as num) < 0 ||
data['categories'] is! List ||
data['policy'] is! Map) {
throw StateError(
'The server returned incomplete storage information. Refresh or update the server.',
);
}
return data;
}
Future<void> load() => _operation(() async {
final reply = await _call('get_storage');
if (reply['storage'] is! Map) {
throw StateError('Storage information is unavailable on this server.');
}
overview = _overview(reply['storage']);
});
Future<void> save(StoragePolicy policy) => _operation(() async {
final reply = await _call('set_storage_policy', {
'storage_policy': policy.toJson(),
});
if (reply['storage'] is! Map) {
throw StateError('The server did not confirm the saved policy.');
}
overview = _overview(reply['storage']);
preview = null;
result = null;
notice = 'Retention policy saved on this server.';
});
Future<void> plan(StoragePolicy policy) => _operation(() async {
final revision = _revision;
final reply = await _call('preview_storage_cleanup', {
'storage_policy': policy.toJson(),
});
if (revision != _revision) return;
if (reply['storage_cleanup'] is! Map) {
throw StateError('The server did not return a cleanup preview.');
}
preview = storageMap(reply['storage_cleanup']);
result = null;
});
Future<void> apply(StoragePolicy policy) => _operation(() async {
final token = preview?['preview_token'];
if (token is! String || token.isEmpty) {
throw StateError('Preview cleanup again before removing data.');
}
final reply = await _call('apply_storage_cleanup', {
'storage_policy': policy.toJson(),
'storage_cleanup_token': token,
});
if (reply['storage_cleanup'] is! Map) {
throw StateError('The server did not return a cleanup result.');
}
result = storageMap(reply['storage_cleanup']);
preview = null;
final current = await _call('get_storage');
overview = _overview(current['storage']);
});
Future<void> _operation(Future<void> Function() action) async {
if (busy || _disposed) return;
busy = true;
error = null;
notice = null;
_notify();
try {
await action();
} catch (exception) {
error = exception is StateError
? exception.message.toString()
: '$exception';
} finally {
busy = false;
_notify();
}
}
void _notify() {
if (!_disposed) notifyListeners();
}
@override
void dispose() {
_disposed = true;
super.dispose();
}
}