relatica/lib/services/setting_service.dart

45 wiersze
1.1 KiB
Dart
Czysty Zwykły widok Historia

2023-01-20 01:37:35 +00:00
import 'package:flutter/material.dart';
2023-01-16 16:06:11 +00:00
import 'package:shared_preferences/shared_preferences.dart';
2023-01-20 01:37:35 +00:00
import '../utils/theme_mode_extensions.dart';
class SettingsService extends ChangeNotifier {
2023-01-16 16:06:11 +00:00
late final SharedPreferences _prefs;
var _initialized = false;
SettingsService();
var _lowBandwidthMode = false;
bool get lowBandwidthMode => _lowBandwidthMode;
set lowBandwidthMode(bool value) {
_lowBandwidthMode = value;
2023-01-16 16:06:11 +00:00
_prefs.setBool(_lowBandwidthModeKey, _lowBandwidthMode);
notifyListeners();
}
2023-01-16 16:06:11 +00:00
2023-01-20 01:37:35 +00:00
var _themeMode = ThemeMode.system;
ThemeMode get themeMode => _themeMode;
set themeMode(ThemeMode mode) {
_themeMode = mode;
_prefs.setString(_themeModeKey, _themeMode.name);
notifyListeners();
}
2023-01-16 16:06:11 +00:00
Future<void> initialize() async {
if (_initialized) {
return;
}
_prefs = await SharedPreferences.getInstance();
_lowBandwidthMode = _prefs.getBool(_lowBandwidthModeKey) ?? false;
2023-01-20 01:37:35 +00:00
_themeMode = ThemeModeExtensions.parse(_prefs.getString(_themeModeKey));
2023-01-16 16:06:11 +00:00
_initialized = true;
}
}
2023-01-16 16:06:11 +00:00
const _lowBandwidthModeKey = 'LowBandwidthMode';
2023-01-20 01:37:35 +00:00
const _themeModeKey = 'ThemeMode';