3136e5bb7e
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.
144 lines
4.1 KiB
Dart
144 lines
4.1 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
|
|
import '../../core/api_gateway.dart';
|
|
|
|
/// One server-side ntfy notification choice, in the server's wire key.
|
|
class NtfySetting {
|
|
const NtfySetting(this.key, this.label, {this.subtitle});
|
|
|
|
final String key;
|
|
final String label;
|
|
|
|
/// Optional one-line explanation shown under [label].
|
|
final String? subtitle;
|
|
}
|
|
|
|
/// The server's global notification choices, in display order.
|
|
///
|
|
/// `run_finished` (server default off) makes every run end notify; without it
|
|
/// only runs started with "Notify me" announce their success or failure.
|
|
const ntfySettings = <NtfySetting>[
|
|
NtfySetting(
|
|
'run_finished',
|
|
'Notify when any run ends',
|
|
subtitle: 'Otherwise only runs started with Notify me',
|
|
),
|
|
NtfySetting('chat_needs_you', 'Chat needs your answer or approval'),
|
|
NtfySetting('chat_reply_finished', 'Chat reply finished'),
|
|
];
|
|
|
|
/// The server's switch for also publishing to ntfy. Optional: older servers
|
|
/// do not send it, and Settings hides the switch then.
|
|
const ntfyEnabledSetting = NtfySetting(
|
|
'ntfy_enabled',
|
|
'Also send to ntfy',
|
|
subtitle: 'For devices that still use the ntfy app',
|
|
);
|
|
|
|
/// Loads and changes the server's ntfy notification settings through the
|
|
/// shared gateway (`notification_settings` / `set_notification_settings`).
|
|
///
|
|
/// A toggle shows the new value at once and is reverted, with the server's
|
|
/// refusal as [error], when the write fails. Every reply carries all keys, so
|
|
/// the stored result always replaces the local copy.
|
|
class NtfySettingsController extends ChangeNotifier {
|
|
NtfySettingsController(this.gateway);
|
|
|
|
final ApiGateway gateway;
|
|
|
|
Map<String, bool>? _values;
|
|
bool _loading = false;
|
|
String? _saving;
|
|
String _error = '';
|
|
bool _loadFailed = false;
|
|
bool _disposed = false;
|
|
|
|
/// The stored values, or null before the first successful load.
|
|
Map<String, bool>? get values => _values;
|
|
bool get loading => _loading;
|
|
|
|
/// The key whose change is being written, if any.
|
|
String? get saving => _saving;
|
|
String get error => _error;
|
|
|
|
/// Whether the latest load failed; the stored values, if any, are kept.
|
|
bool get loadFailed => _loadFailed;
|
|
|
|
/// Whether the checkboxes accept a change now.
|
|
bool get enabled => _values != null && !_loading && _saving == null;
|
|
|
|
Future<void> load() async {
|
|
if (_loading) return;
|
|
_loading = true;
|
|
_error = '';
|
|
_loadFailed = false;
|
|
_notify();
|
|
try {
|
|
final reply = await gateway.call('notification_settings');
|
|
_values = _decode(reply);
|
|
} catch (error) {
|
|
_loadFailed = true;
|
|
_error = 'Could not load notification settings: $error';
|
|
} finally {
|
|
_loading = false;
|
|
_notify();
|
|
}
|
|
}
|
|
|
|
Future<void> toggle(String key, bool value) async {
|
|
final current = _values;
|
|
if (current == null || !enabled) return;
|
|
final previous = current[key];
|
|
_values = {...current, key: value};
|
|
_saving = key;
|
|
_error = '';
|
|
_notify();
|
|
try {
|
|
final reply = await gateway.call('set_notification_settings', {
|
|
'notification_settings': {key: value},
|
|
});
|
|
_values = _decode(reply);
|
|
} catch (error) {
|
|
final reverted = {...?_values};
|
|
if (previous == null) {
|
|
reverted.remove(key);
|
|
} else {
|
|
reverted[key] = previous;
|
|
}
|
|
_values = reverted;
|
|
_error = 'Could not save: $error';
|
|
} finally {
|
|
_saving = null;
|
|
_notify();
|
|
}
|
|
}
|
|
|
|
static Map<String, bool> _decode(JsonMap reply) {
|
|
final raw = reply['notification_settings'];
|
|
if (raw is! Map) {
|
|
throw const FormatException('the reply carried no notification settings');
|
|
}
|
|
final result = <String, bool>{};
|
|
for (final setting in ntfySettings) {
|
|
final value = raw[setting.key];
|
|
if (value is! bool) {
|
|
throw FormatException('the reply is missing ${setting.key}');
|
|
}
|
|
result[setting.key] = value;
|
|
}
|
|
final ntfy = raw[ntfyEnabledSetting.key];
|
|
if (ntfy is bool) result[ntfyEnabledSetting.key] = ntfy;
|
|
return result;
|
|
}
|
|
|
|
void _notify() {
|
|
if (!_disposed) notifyListeners();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_disposed = true;
|
|
super.dispose();
|
|
}
|
|
}
|