display_controller.dart 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import 'package:flutter/foundation.dart';
  2. import '../data/repositories/settings_repository.dart';
  3. import '../data/services/native_bridge.dart';
  4. /// Applies the persisted "Prevent CarDash from sleeping" preference to the
  5. /// Android window. The flag only applies while this window is visible.
  6. class DisplayController extends ChangeNotifier {
  7. DisplayController(this._platform, this._settings)
  8. : _keepScreenOn = _settings.keepScreenOn;
  9. final LauncherPlatform _platform;
  10. final SettingsRepository _settings;
  11. bool _keepScreenOn;
  12. bool _disposed = false;
  13. bool get keepScreenOn => _keepScreenOn;
  14. Future<void> initialize() => _apply();
  15. Future<void> setKeepScreenOn(bool enabled) async {
  16. if (enabled == _keepScreenOn) return;
  17. _keepScreenOn = enabled;
  18. await _settings.setKeepScreenOn(enabled);
  19. await _apply();
  20. _notify();
  21. }
  22. /// Reapplies after resume, when Android may have recreated the window.
  23. Future<void> apply() => _apply();
  24. Future<void> _apply() async {
  25. try {
  26. await _platform.setKeepScreenOn(_keepScreenOn);
  27. } catch (_) {
  28. // Display flags are best-effort; settings remain persisted.
  29. }
  30. }
  31. void _notify() {
  32. if (!_disposed) notifyListeners();
  33. }
  34. @override
  35. void dispose() {
  36. _disposed = true;
  37. super.dispose();
  38. }
  39. }