2021-02-24 21:54:15 +01:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2020-08-30 22:43:16 +02:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2021-02-24 21:54:15 +01:00
|
|
|
import 'package:json_annotation/json_annotation.dart';
|
2020-08-30 22:43:16 +02:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
|
2021-02-27 17:02:55 +01:00
|
|
|
import '../l10n/l10n.dart';
|
|
|
|
|
2021-02-24 21:54:15 +01:00
|
|
|
part 'config_store.g.dart';
|
2020-10-26 00:49:24 +01:00
|
|
|
|
2020-10-26 00:39:57 +01:00
|
|
|
/// Store managing user-level configuration such as theme or language
|
2021-02-24 21:54:15 +01:00
|
|
|
@JsonSerializable()
|
2020-10-25 22:33:44 +01:00
|
|
|
class ConfigStore extends ChangeNotifier {
|
2021-02-24 21:54:15 +01:00
|
|
|
static const prefsKey = 'v1:ConfigStore';
|
|
|
|
static final _prefs = SharedPreferences.getInstance();
|
|
|
|
|
2021-04-09 00:11:44 +02:00
|
|
|
late ThemeMode _theme;
|
2021-02-24 21:54:15 +01:00
|
|
|
@JsonKey(defaultValue: ThemeMode.system)
|
2020-10-25 22:33:44 +01:00
|
|
|
ThemeMode get theme => _theme;
|
|
|
|
set theme(ThemeMode theme) {
|
|
|
|
_theme = theme;
|
|
|
|
notifyListeners();
|
|
|
|
save();
|
2020-08-30 22:43:16 +02:00
|
|
|
}
|
|
|
|
|
2021-04-09 00:11:44 +02:00
|
|
|
late bool _amoledDarkMode;
|
2021-02-24 21:54:15 +01:00
|
|
|
@JsonKey(defaultValue: false)
|
2020-10-25 22:33:44 +01:00
|
|
|
bool get amoledDarkMode => _amoledDarkMode;
|
|
|
|
set amoledDarkMode(bool amoledDarkMode) {
|
|
|
|
_amoledDarkMode = amoledDarkMode;
|
|
|
|
notifyListeners();
|
|
|
|
save();
|
2020-08-30 22:43:16 +02:00
|
|
|
}
|
|
|
|
|
2021-04-11 00:55:04 +02:00
|
|
|
late Locale _locale;
|
|
|
|
// default value is set in the `LocaleSerde.fromJson` method because json_serializable does
|
|
|
|
// not accept non-literals as defaultValue
|
2021-02-27 17:02:55 +01:00
|
|
|
@JsonKey(fromJson: LocaleSerde.fromJson, toJson: LocaleSerde.toJson)
|
2021-04-11 00:55:04 +02:00
|
|
|
Locale get locale => _locale;
|
2021-02-27 14:39:58 +01:00
|
|
|
set locale(Locale locale) {
|
|
|
|
_locale = locale;
|
|
|
|
notifyListeners();
|
|
|
|
save();
|
|
|
|
}
|
|
|
|
|
2021-02-24 21:54:15 +01:00
|
|
|
static Future<ConfigStore> load() async {
|
|
|
|
final prefs = await _prefs;
|
|
|
|
|
|
|
|
return _$ConfigStoreFromJson(
|
|
|
|
jsonDecode(prefs.getString(prefsKey) ?? '{}') as Map<String, dynamic>,
|
2021-04-09 00:11:44 +02:00
|
|
|
);
|
2020-08-30 22:43:16 +02:00
|
|
|
}
|
|
|
|
|
2020-10-25 22:33:44 +01:00
|
|
|
Future<void> save() async {
|
2021-02-24 21:54:15 +01:00
|
|
|
final prefs = await _prefs;
|
2020-08-30 22:43:16 +02:00
|
|
|
|
2021-02-24 21:54:15 +01:00
|
|
|
await prefs.setString(prefsKey, jsonEncode(_$ConfigStoreToJson(this)));
|
2020-08-30 22:43:16 +02:00
|
|
|
}
|
|
|
|
}
|