This commit is contained in:
2026-07-27 21:32:03 +02:00
parent 160b4e4cda
commit 77c1747b40
23 changed files with 1392 additions and 87 deletions
+66
View File
@@ -0,0 +1,66 @@
import 'package:flutter/foundation.dart';
import '../models/settings.dart';
import '../services/settings_service.dart';
class SettingsViewModel extends ChangeNotifier {
final SettingsService settingsService;
SettingsViewModel({required this.settingsService});
AppSettings _settings = AppSettings.defaults;
bool _isLoading = false;
bool _hasLoaded = false;
String? _errorMessage;
AppSettings get settings => _settings;
bool get isLoading => _isLoading;
bool get hasLoaded => _hasLoaded;
String? get errorMessage => _errorMessage;
Future<void> ensureLoaded() async {
if (_hasLoaded || _isLoading) return;
await load();
}
Future<void> load() async {
if (_isLoading) return;
_isLoading = true;
_errorMessage = null;
notifyListeners();
try {
_settings = await settingsService.getSettings();
} catch (_) {
_errorMessage = 'Could not load settings.';
} finally {
_hasLoaded = true;
_isLoading = false;
notifyListeners();
}
}
/// Just flips the preference flag. Caller is responsible for having
/// already set/verified the actual PIN via PinLockViewModel first.
Future<void> updatePinRequired(bool value) =>
_save(_settings.copyWith(pinRequired: value));
Future<void> updateThemeMode(AppThemeMode mode) =>
_save(_settings.copyWith(themeMode: mode));
Future<void> _save(AppSettings updated) async {
final previous = _settings;
_settings = updated;
_errorMessage = null;
notifyListeners();
try {
await settingsService.saveSettings(updated);
} catch (_) {
_settings = previous;
_errorMessage = 'Could not save settings.';
notifyListeners();
}
}
}