weather_controller.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import 'dart:async';
  2. import 'package:flutter/foundation.dart';
  3. import '../../data/models/location_reading.dart';
  4. import '../../data/repositories/settings_repository.dart';
  5. import '../../data/services/telemetry_bridge.dart';
  6. import '../../data/services/weather_service.dart';
  7. /// Location-based temperature fallback used only when OBD has no ambient value.
  8. class WeatherController extends ChangeNotifier {
  9. WeatherController(
  10. this._platform,
  11. this._settings, {
  12. WeatherService? weatherService,
  13. }) : _weatherService = weatherService ?? WeatherService();
  14. final TelemetryPlatform _platform;
  15. final SettingsRepository _settings;
  16. final WeatherService _weatherService;
  17. bool _enabled = false;
  18. bool _loading = false;
  19. bool _active = false;
  20. bool _disposed = false;
  21. double? _temperatureCelsius;
  22. String? _locationName;
  23. String? _error;
  24. Timer? _refreshTimer;
  25. bool get enabled => _enabled;
  26. bool get loading => _loading;
  27. double? get temperatureCelsius => _temperatureCelsius;
  28. String? get locationName => _locationName;
  29. String? get error => _error;
  30. bool get hasTemperature =>
  31. _enabled && _temperatureCelsius != null && _locationName != null;
  32. void initialize() {
  33. _enabled = _settings.weatherLocationTemperature;
  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. await _settings.setWeatherLocationTemperature(value);
  63. if (value) {
  64. _active = true;
  65. await refresh();
  66. _refreshTimer?.cancel();
  67. _refreshTimer = Timer.periodic(
  68. const Duration(minutes: 15),
  69. (_) => unawaited(refresh()),
  70. );
  71. } else {
  72. stop();
  73. _temperatureCelsius = null;
  74. _locationName = null;
  75. _error = null;
  76. }
  77. _notify();
  78. return true;
  79. }
  80. Future<void> refresh() async {
  81. if (!_enabled || _loading) return;
  82. _loading = true;
  83. _notify();
  84. try {
  85. final LocationReading location;
  86. try {
  87. location = await _platform.getLocation();
  88. } on TelemetryException catch (problem) {
  89. _error = problem.message;
  90. _temperatureCelsius = null;
  91. _locationName = null;
  92. return;
  93. }
  94. final temperature = await _weatherService.temperatureCelsius(
  95. latitude: location.latitude,
  96. longitude: location.longitude,
  97. );
  98. if (temperature == null) {
  99. _error = 'Weather is unavailable.';
  100. _temperatureCelsius = null;
  101. _locationName = null;
  102. return;
  103. }
  104. _temperatureCelsius = temperature;
  105. _locationName = location.displayPlace;
  106. _error = null;
  107. } finally {
  108. _loading = false;
  109. _notify();
  110. }
  111. }
  112. void _notify() {
  113. if (!_disposed) notifyListeners();
  114. }
  115. @override
  116. void dispose() {
  117. _disposed = true;
  118. _refreshTimer?.cancel();
  119. super.dispose();
  120. }
  121. }