mirror of
https://github.com/DefinedNet/mobile_nebula.git
synced 2025-03-06 08:36:36 +00:00
* Enable `flutter_lints` linting * Fix unmarked deps, we aren't on web so we don't need a URL strategy * ` dart fix --apply --code=use_super_parameters` * `dart fix --apply --code=use_key_in_widget_constructors` * `dart fix --apply --code=use_function_type_syntax_for_parameters` * Ignore code-generated `lib/services/theme.dart` file * `dart fix --apply --code=unnecessary_this` * `dart fix --apply --code=unnecessary_null_in_if_null_operators` * `dart fix --apply --code=unnecessary_new` * `dart fix --apply --code=sort_child_properties_last` * `dart fix --apply --code=sized_box_for_whitespace` * `dart fix --apply --code=prefer_typing_uninitialized_variables` * `dart fix --apply --code=prefer_is_empty` * `dart fix --apply --code=prefer_interpolation_to_compose_strings` * `dart fix --apply --code=prefer_final_fields` * `dart fix --apply --code=prefer_const_constructors_in_immutables` * `dart fix --apply --code=prefer_collection_literals` * `dart fix --apply --code=no_leading_underscores_for_local_identifiers` * `dart fix --apply --code=curly_braces_in_flow_control_structures` * `dart fix --apply --code=avoid_function_literals_in_foreach_calls` * `dart fix --apply --code=annotate_overrides` * Add CI for dart linting * `dart format lib/` * Re-enable the `usePathUrlStrategy` call, with proper deps https://docs.flutter.dev/ui/navigation/url-strategies#configuring-the-url-strategy
109 lines
2.3 KiB
Dart
109 lines
2.3 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/scheduler.dart';
|
|
import 'package:mobile_nebula/services/storage.dart';
|
|
import 'package:sentry_flutter/sentry_flutter.dart';
|
|
|
|
bool DEFAULT_LOG_WRAP = false;
|
|
bool DEFAULT_TRACK_ERRORS = true;
|
|
|
|
class Settings {
|
|
final _storage = Storage();
|
|
final StreamController _change = StreamController.broadcast();
|
|
var _settings = <String, dynamic>{};
|
|
|
|
bool get useSystemColors {
|
|
return _getBool('systemDarkMode', true);
|
|
}
|
|
|
|
set useSystemColors(bool enabled) {
|
|
if (!enabled) {
|
|
// Clear the dark mode to let the default system config take over, user can override from there
|
|
_settings.remove('darkMode');
|
|
}
|
|
_set('systemDarkMode', enabled);
|
|
}
|
|
|
|
bool get darkMode {
|
|
return _getBool('darkMode', SchedulerBinding.instance.platformDispatcher.platformBrightness == Brightness.dark);
|
|
}
|
|
|
|
set darkMode(bool enabled) {
|
|
_set('darkMode', enabled);
|
|
}
|
|
|
|
bool get logWrap {
|
|
return _getBool('logWrap', DEFAULT_LOG_WRAP);
|
|
}
|
|
|
|
set logWrap(bool enabled) {
|
|
_set('logWrap', enabled);
|
|
}
|
|
|
|
bool get trackErrors {
|
|
return _getBool('trackErrors', DEFAULT_TRACK_ERRORS);
|
|
}
|
|
|
|
set trackErrors(bool enabled) {
|
|
_set('trackErrors', enabled);
|
|
|
|
// Side-effect: Disable Sentry immediately
|
|
if (!enabled) {
|
|
Sentry.close();
|
|
}
|
|
}
|
|
|
|
String _getString(String key, String defaultValue) {
|
|
final val = _settings[key];
|
|
if (val is String) {
|
|
return val;
|
|
}
|
|
return defaultValue;
|
|
}
|
|
|
|
bool _getBool(String key, bool defaultValue) {
|
|
final val = _settings[key];
|
|
if (val is bool) {
|
|
return val;
|
|
}
|
|
return defaultValue;
|
|
}
|
|
|
|
void _set(String key, dynamic value) {
|
|
_settings[key] = value;
|
|
_save();
|
|
}
|
|
|
|
Stream onChange() {
|
|
return _change.stream;
|
|
}
|
|
|
|
void _save() {
|
|
final content = jsonEncode(_settings);
|
|
//TODO: handle errors
|
|
_storage.writeFile("config.json", content).then((_) {
|
|
_change.add(null);
|
|
});
|
|
}
|
|
|
|
static final Settings _instance = Settings._internal();
|
|
|
|
factory Settings() {
|
|
return _instance;
|
|
}
|
|
|
|
Settings._internal() {
|
|
_storage.readFile("config.json").then((rawConfig) {
|
|
if (rawConfig != null) {
|
|
_settings = jsonDecode(rawConfig);
|
|
}
|
|
|
|
_change.add(null);
|
|
});
|
|
}
|
|
|
|
void dispose() {
|
|
_change.close();
|
|
}
|
|
}
|