Dashboard - Selectable main sensor (gauge) + sub sensor, 10 selectable values (landscape 5 left + 5 right, portrait 2x5 on one screen) and 4 selectable flags; long-press to change; persisted. - Flags: the requirement examples (MIL, FAN Out, VTEC Out, Knock, WBO / IAT / CLT Error - errors derived from the stored DTC bits) plus the other S300 status bits (cuts, launch, closed loop, VTP, AC, brake). - Record button moved to the app bar; debug screen moved to Settings. Sensor list: 1 column on phone portrait, 2 on tablet/landscape; name, unit, min/max, segmented bar and value. Graph: auto-scaled Y, window max/min, current value, seconds axis and a touch cursor; RPM/MAP/TPS/ECT by default. Units: km/h|mph, degC|degF, kPa|bar|psi (display only), persisted. Light/dark: palette is a ThemeExtension used by every screen; theme persisted. SafeArea keeps content off the side navigation bar. Datalog playback now drives the dashboard, list, graph and DTC screen, with the playback bar on the main screen and seek updating the displays. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
124 lines
3.4 KiB
Dart
124 lines
3.4 KiB
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||
import 'package:shared_preferences/shared_preferences.dart';
|
||
|
||
import '../../../core/models/sensor_defs.dart';
|
||
|
||
/// What the dashboard shows, per the requirement document: a selectable
|
||
/// main sensor (gauge) + sub sensor, 10 selectable values (landscape: 5 left
|
||
/// + 5 right; portrait: 2×5) and 4 selectable flags. Persisted.
|
||
class DashboardConfig {
|
||
static const valueCount = 10;
|
||
static const flagCount = 4;
|
||
|
||
final String main;
|
||
final String sub;
|
||
final List<String> values;
|
||
final List<String> flags;
|
||
|
||
const DashboardConfig({
|
||
required this.main,
|
||
required this.sub,
|
||
required this.values,
|
||
required this.flags,
|
||
});
|
||
|
||
static const defaults = DashboardConfig(
|
||
main: 'rpm',
|
||
sub: 'vss',
|
||
values: [
|
||
'vss', 'ect', 'iat', 'map', 'o2', // left
|
||
'tps', 'bat', 'ign', 'inj', 'eth', // right
|
||
],
|
||
flags: ['mil', 'fanout', 'vtec', 'knock'],
|
||
);
|
||
|
||
DashboardConfig copyWith({
|
||
String? main,
|
||
String? sub,
|
||
List<String>? values,
|
||
List<String>? flags,
|
||
}) =>
|
||
DashboardConfig(
|
||
main: main ?? this.main,
|
||
sub: sub ?? this.sub,
|
||
values: values ?? this.values,
|
||
flags: flags ?? this.flags,
|
||
);
|
||
}
|
||
|
||
const _kMain = 'dash.main';
|
||
const _kSub = 'dash.sub';
|
||
const _kValues = 'dash.values';
|
||
const _kFlags = 'dash.flags';
|
||
|
||
class DashboardConfigNotifier extends StateNotifier<DashboardConfig> {
|
||
DashboardConfigNotifier() : super(DashboardConfig.defaults) {
|
||
_load();
|
||
}
|
||
|
||
static bool _isSensor(String id) => sensorDefs.any((d) => d.id == id);
|
||
static bool _isFlag(String id) => flagDefs.any((f) => f.id == id);
|
||
|
||
Future<void> _load() async {
|
||
try {
|
||
final p = await SharedPreferences.getInstance();
|
||
const d = DashboardConfig.defaults;
|
||
List<String> list(String key, int n, bool Function(String) valid,
|
||
List<String> fallback) {
|
||
final saved = p.getStringList(key);
|
||
if (saved == null || saved.length != n || !saved.every(valid)) {
|
||
return fallback;
|
||
}
|
||
return saved;
|
||
}
|
||
|
||
final main = p.getString(_kMain);
|
||
final sub = p.getString(_kSub);
|
||
final next = DashboardConfig(
|
||
main: main != null && _isSensor(main) ? main : d.main,
|
||
sub: sub != null && _isSensor(sub) ? sub : d.sub,
|
||
values: list(_kValues, DashboardConfig.valueCount, _isSensor, d.values),
|
||
flags: list(_kFlags, DashboardConfig.flagCount, _isFlag, d.flags),
|
||
);
|
||
if (mounted) state = next;
|
||
} catch (_) {
|
||
// Keep defaults if preferences can't be read.
|
||
}
|
||
}
|
||
|
||
void _persist() {
|
||
final s = state;
|
||
SharedPreferences.getInstance().then((p) {
|
||
p.setString(_kMain, s.main);
|
||
p.setString(_kSub, s.sub);
|
||
p.setStringList(_kValues, s.values);
|
||
p.setStringList(_kFlags, s.flags);
|
||
}).catchError((Object _) {});
|
||
}
|
||
|
||
void setMain(String id) {
|
||
state = state.copyWith(main: id);
|
||
_persist();
|
||
}
|
||
|
||
void setSub(String id) {
|
||
state = state.copyWith(sub: id);
|
||
_persist();
|
||
}
|
||
|
||
void setValue(int index, String id) {
|
||
state = state.copyWith(values: [...state.values]..[index] = id);
|
||
_persist();
|
||
}
|
||
|
||
void setFlag(int index, String id) {
|
||
state = state.copyWith(flags: [...state.flags]..[index] = id);
|
||
_persist();
|
||
}
|
||
}
|
||
|
||
final dashboardConfigProvider =
|
||
StateNotifierProvider<DashboardConfigNotifier, DashboardConfig>(
|
||
(ref) => DashboardConfigNotifier(),
|
||
);
|