49 lines
1.4 KiB
Dart
49 lines
1.4 KiB
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import '../bluetooth/bt_poller.dart';
|
|
|
|
class AppSettings {
|
|
final EcuType ecuType;
|
|
final Duration pollingInterval;
|
|
|
|
const AppSettings({
|
|
this.ecuType = EcuType.s300,
|
|
this.pollingInterval = const Duration(milliseconds: 50),
|
|
});
|
|
|
|
AppSettings copyWith({EcuType? ecuType, Duration? pollingInterval}) =>
|
|
AppSettings(
|
|
ecuType: ecuType ?? this.ecuType,
|
|
pollingInterval: pollingInterval ?? this.pollingInterval,
|
|
);
|
|
}
|
|
|
|
const _ecuTypeKey = 'ecuType';
|
|
|
|
class SettingsNotifier extends StateNotifier<AppSettings> {
|
|
SettingsNotifier() : super(const AppSettings()) {
|
|
_load();
|
|
}
|
|
|
|
Future<void> _load() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final saved = prefs.getString(_ecuTypeKey);
|
|
if (saved == EcuType.kpro.name) {
|
|
state = state.copyWith(ecuType: EcuType.kpro);
|
|
}
|
|
}
|
|
|
|
void setEcuType(EcuType type) {
|
|
state = state.copyWith(ecuType: type);
|
|
SharedPreferences.getInstance()
|
|
.then((prefs) => prefs.setString(_ecuTypeKey, type.name));
|
|
}
|
|
|
|
void setPollingInterval(Duration d) =>
|
|
state = state.copyWith(pollingInterval: d);
|
|
}
|
|
|
|
final settingsProvider = StateNotifierProvider<SettingsNotifier, AppSettings>(
|
|
(ref) => SettingsNotifier(),
|
|
);
|