Files
pipelines/app/lib/features/settings/settings_server_load.dart
T
alan 3136e5bb7e Redesign Settings as grouped searchable page
Replace the tabbed Settings flow with a single grouped page that searches and lazily loads server-backed sections. Preserve existing settings behavior while adding inline loading and retry states and updating coverage for the new navigation model.
2026-11-12 02:54:07 +00:00

67 lines
1.6 KiB
Dart

import 'package:flutter/foundation.dart';
/// One server-backed Settings value.
///
/// It loads once when its section mounts, never starts a second load while one
/// is in flight, and keeps the last good value when a refresh fails, so a
/// flaky server shows stale data with a Retry rather than an empty section.
class ServerLoad<T> extends ChangeNotifier {
ServerLoad(this._fetch);
final Future<T> Function() _fetch;
T? _value;
bool _hasValue = false;
Object? _error;
bool _loading = false;
Future<void>? _inFlight;
bool _disposed = false;
/// The last value the server returned, or null before the first success.
T? get value => _value;
bool get hasValue => _hasValue;
/// The error from the latest attempt, cleared when a new attempt starts.
Object? get error => _error;
bool get loading => _loading;
/// Starts a load unless one is already running; returns that load's future.
Future<void> load() {
final running = _inFlight;
if (running != null) return running;
_loading = true;
_error = null;
_notify();
final future = _run();
_inFlight = future;
return future;
}
/// Retries after a failure or refreshes a cached value.
Future<void> refresh() => load();
Future<void> _run() async {
try {
final next = await _fetch();
if (_disposed) return;
_value = next;
_hasValue = true;
} catch (error) {
if (!_disposed) _error = error;
} finally {
_loading = false;
_inFlight = null;
_notify();
}
}
void _notify() {
if (!_disposed) notifyListeners();
}
@override
void dispose() {
_disposed = true;
super.dispose();
}
}