Initial version
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
/// Display units. `auto` follows the scooter's own imperial bit.
|
||||
///
|
||||
/// ASSUMPTION (INFERRED, ride verification pending): native protocol speed and
|
||||
/// distance values are kilometres. Imperial display converts from km.
|
||||
enum UnitPreference { auto, metric, imperial }
|
||||
|
||||
/// Dashboard layouts the rider can pick from.
|
||||
enum ClusterLayout {
|
||||
arc('Arc', 'Speed inside a round gauge'),
|
||||
digital('Digital', 'Big number with a speed bar'),
|
||||
tiles('Tiles', 'Everything as cards');
|
||||
|
||||
const ClusterLayout(this.label, this.description);
|
||||
final String label;
|
||||
final String description;
|
||||
}
|
||||
|
||||
/// Accent colour presets. Blue is the OpenScooter default.
|
||||
enum AccentColor {
|
||||
blue('Blue', Color(0xFF3D8BFF)),
|
||||
cyan('Cyan', Color(0xFF22C1D8)),
|
||||
green('Green', Color(0xFF34C77B)),
|
||||
lime('Lime', Color(0xFFB4E33D)),
|
||||
amber('Amber', Color(0xFFFFB020)),
|
||||
orange('Orange', Color(0xFFFF6B2C)),
|
||||
red('Red', Color(0xFFFF4D5E)),
|
||||
pink('Pink', Color(0xFFFF5CA8)),
|
||||
purple('Purple', Color(0xFF9B6CFF)),
|
||||
white('White', Color(0xFFF2F4F8));
|
||||
|
||||
const AccentColor(this.label, this.color);
|
||||
final String label;
|
||||
final Color color;
|
||||
}
|
||||
|
||||
/// App-wide user preferences, persisted with shared_preferences.
|
||||
class AppSettings extends ChangeNotifier {
|
||||
AppSettings._();
|
||||
static final AppSettings instance = AppSettings._();
|
||||
|
||||
static const _kAccent = 'accent';
|
||||
static const _kLayout = 'layout';
|
||||
static const _kUnits = 'units';
|
||||
|
||||
SharedPreferences? _prefs;
|
||||
|
||||
AccentColor _accent = AccentColor.blue;
|
||||
ClusterLayout _layout = ClusterLayout.arc;
|
||||
UnitPreference _units = UnitPreference.auto;
|
||||
|
||||
AccentColor get accent => _accent;
|
||||
ClusterLayout get layout => _layout;
|
||||
UnitPreference get units => _units;
|
||||
|
||||
Future<void> load() async {
|
||||
try {
|
||||
_prefs = await SharedPreferences.getInstance();
|
||||
_accent = _byName(AccentColor.values, _prefs!.getString(_kAccent)) ?? _accent;
|
||||
_layout = _byName(ClusterLayout.values, _prefs!.getString(_kLayout)) ?? _layout;
|
||||
_units = _byName(UnitPreference.values, _prefs!.getString(_kUnits)) ?? _units;
|
||||
} catch (_) {
|
||||
// Defaults are fine if preferences are unavailable.
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
set accent(AccentColor v) {
|
||||
_accent = v;
|
||||
_prefs?.setString(_kAccent, v.name);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
set layout(ClusterLayout v) {
|
||||
_layout = v;
|
||||
_prefs?.setString(_kLayout, v.name);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
set units(UnitPreference v) {
|
||||
_units = v;
|
||||
_prefs?.setString(_kUnits, v.name);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
static T? _byName<T extends Enum>(List<T> values, String? name) {
|
||||
if (name == null) return null;
|
||||
for (final v in values) {
|
||||
if (v.name == name) return v;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user