worldhopper/lib/providers/eink_mode_provider.dart
Felipe M. 0b57896793 feat: eink-mode (#1)
Co-authored-by: Felipe M. <me@fmartingr.com>
Co-committed-by: Felipe M. <me@fmartingr.com>
2026-02-12 22:25:38 +01:00

44 lines
1.4 KiB
Dart

import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:worldhopper/helpers/eink_detection_helper.dart';
part 'eink_mode_provider.g.dart';
/// Key for storing e-ink mode preference in SharedPreferences
const String _einkModeKey = 'eink_mode';
/// Provider for e-ink mode state management.
///
/// On first launch (no stored preference), auto-detects whether the device is
/// a known e-ink device and persists the result. Subsequent launches use the
/// stored value so the user's explicit choice is always respected.
@riverpod
class EinkModeNotifier extends _$EinkModeNotifier {
@override
bool build() {
_loadEinkMode();
return false; // Default value until loaded
}
/// Load e-ink mode from SharedPreferences, auto-detecting on first launch.
Future<void> _loadEinkMode() async {
final prefs = await SharedPreferences.getInstance();
final stored = prefs.getBool(_einkModeKey);
if (stored != null) {
state = stored;
} else {
// First launch — auto-detect
final detected = await EinkDetectionHelper.isEinkDevice();
state = detected;
await prefs.setBool(_einkModeKey, detected);
}
}
/// Set e-ink mode and persist to SharedPreferences.
Future<void> setEinkMode(bool enabled) async {
state = enabled;
final prefs = await SharedPreferences.getInstance();
await prefs.setBool(_einkModeKey, enabled);
}
}