37 lines
1.4 KiB
Dart
37 lines
1.4 KiB
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
final calibrationServiceProvider = Provider<CalibrationService>((ref) {
|
|
return CalibrationService();
|
|
});
|
|
|
|
class CalibrationService {
|
|
static const _pitchKey = 'calibration_pitch';
|
|
static const _rollKey = 'calibration_roll';
|
|
|
|
Future<void> saveCalibrationOffsets(double pitch, double roll, {int? motorcycleId}) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
if (motorcycleId != null) {
|
|
await prefs.setDouble('calibration_pitch_$motorcycleId', pitch);
|
|
await prefs.setDouble('calibration_roll_$motorcycleId', roll);
|
|
}
|
|
// Also save global fallback
|
|
await prefs.setDouble(_pitchKey, pitch);
|
|
await prefs.setDouble(_rollKey, roll);
|
|
}
|
|
|
|
Future<Map<String, double>> loadCalibrationOffsets({int? motorcycleId}) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
if (motorcycleId != null) {
|
|
final bikePitch = prefs.getDouble('calibration_pitch_$motorcycleId');
|
|
final bikeRoll = prefs.getDouble('calibration_roll_$motorcycleId');
|
|
if (bikePitch != null && bikeRoll != null) {
|
|
return {'pitch': bikePitch, 'roll': bikeRoll};
|
|
}
|
|
}
|
|
final pitch = prefs.getDouble(_pitchKey) ?? 0.0;
|
|
final roll = prefs.getDouble(_rollKey) ?? 0.0;
|
|
return {'pitch': pitch, 'roll': roll};
|
|
}
|
|
}
|