weather_controller.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import 'dart:async';
  2. import 'package:flutter/foundation.dart';
  3. import '../../data/models/location_reading.dart';
  4. import '../../data/services/telemetry_bridge.dart';
  5. import '../../data/services/weather_service.dart';
  6. /// Location-based temperature fallback used when OBD-II is not the source.
  7. ///
  8. /// Persistence and the Settings switch live in [TelemetrySettingsController];
  9. /// this controller only owns permission, fetching and freshness.
  10. class WeatherController extends ChangeNotifier {
  11. WeatherController(this._platform, {WeatherService? weatherService})
  12. : _weatherService = weatherService ?? WeatherService();
  13. final TelemetryPlatform _platform;
  14. final WeatherService _weatherService;
  15. bool _enabled = false;
  16. bool _loading = false;
  17. bool _active = false;
  18. bool _disposed = false;
  19. double? _temperatureCelsius;
  20. String? _locationName;
  21. String? _error;
  22. Timer? _refreshTimer;
  23. bool get enabled => _enabled;
  24. bool get loading => _loading;
  25. double? get temperatureCelsius => _temperatureCelsius;
  26. String? get locationName => _locationName;
  27. String? get error => _error;
  28. bool get hasTemperature =>
  29. _enabled && _temperatureCelsius != null && _locationName != null;
  30. /// Restores the persisted weather preference at cold start without prompting
  31. /// for permission. [start] fetches only when location is already granted.
  32. void restoreEnabled(bool enabled) {
  33. _enabled = enabled;
  34. }
  35. Future<void> start() async {
  36. if (!_enabled || _active) return;
  37. _active = true;
  38. await refresh();
  39. _refreshTimer?.cancel();
  40. _refreshTimer = Timer.periodic(
  41. const Duration(minutes: 15),
  42. (_) => unawaited(refresh()),
  43. );
  44. }
  45. void stop() {
  46. _active = false;
  47. _refreshTimer?.cancel();
  48. _refreshTimer = null;
  49. }
  50. /// Enables the fallback and asks for location access at that moment.
  51. Future<bool> setEnabled(bool value) async {
  52. if (value == _enabled) return true;
  53. if (value) {
  54. final granted = await _platform.ensureLocationPermission();
  55. if (!granted) {
  56. _error = 'Location permission is required for weather.';
  57. _notify();
  58. return false;
  59. }
  60. }
  61. _enabled = value;
  62. if (value) {
  63. _active = true;
  64. await refresh();
  65. _refreshTimer?.cancel();
  66. _refreshTimer = Timer.periodic(
  67. const Duration(minutes: 15),
  68. (_) => unawaited(refresh()),
  69. );
  70. } else {
  71. stop();
  72. _temperatureCelsius = null;
  73. _locationName = null;
  74. _error = null;
  75. }
  76. _notify();
  77. return true;
  78. }
  79. Future<void> refresh() async {
  80. if (!_enabled || _loading) return;
  81. _loading = true;
  82. _notify();
  83. try {
  84. final LocationReading location;
  85. try {
  86. location = await _platform.getLocation();
  87. } on TelemetryException catch (problem) {
  88. _error = problem.message;
  89. _temperatureCelsius = null;
  90. _locationName = null;
  91. return;
  92. }
  93. final temperature = await _weatherService.temperatureCelsius(
  94. latitude: location.latitude,
  95. longitude: location.longitude,
  96. );
  97. if (temperature == null) {
  98. _error = 'Weather is unavailable.';
  99. _temperatureCelsius = null;
  100. _locationName = null;
  101. return;
  102. }
  103. _temperatureCelsius = temperature;
  104. _locationName = location.displayPlace;
  105. _error = null;
  106. } finally {
  107. _loading = false;
  108. _notify();
  109. }
  110. }
  111. void _notify() {
  112. if (!_disposed) notifyListeners();
  113. }
  114. @override
  115. void dispose() {
  116. _disposed = true;
  117. _refreshTimer?.cancel();
  118. super.dispose();
  119. }
  120. }