MotorSignal-App/lib/services/alerts/road_alert_providers.dart

186 lines
5.7 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'road_alert_models.dart';
import 'road_alert_provider_interface.dart';
import 'tomtom_road_alert_provider.dart';
import 'community_traffic_provider.dart';
import 'mock_road_alert_provider.dart';
final tomtomProviderInstance = Provider<TomTomRoadAlertProvider>((ref) {
return TomTomRoadAlertProvider();
});
final communityProviderInstance = Provider<CommunityTrafficRoadAlertProvider>((ref) {
return CommunityTrafficRoadAlertProvider();
});
final mockProviderInstance = Provider<MockRoadAlertProvider>((ref) {
return MockRoadAlertProvider();
});
// Master Enable
class RoadAlertsEnabledNotifier extends StateNotifier<bool> {
static const _key = 'road_alerts_enabled';
RoadAlertsEnabledNotifier() : super(true) {
_load();
}
Future<void> _load() async {
final prefs = await SharedPreferences.getInstance();
state = prefs.getBool(_key) ?? true;
}
Future<void> setEnabled(bool enabled) async {
state = enabled;
final prefs = await SharedPreferences.getInstance();
await prefs.setBool(_key, enabled);
}
}
final roadAlertsEnabledProvider =
StateNotifierProvider<RoadAlertsEnabledNotifier, bool>((ref) {
return RoadAlertsEnabledNotifier();
});
// Audio Alerts Toggle
class AudioAlertsEnabledNotifier extends StateNotifier<bool> {
static const _key = 'audio_alerts_enabled';
AudioAlertsEnabledNotifier() : super(true) {
_load();
}
Future<void> _load() async {
final prefs = await SharedPreferences.getInstance();
state = prefs.getBool(_key) ?? true;
}
Future<void> setEnabled(bool enabled) async {
state = enabled;
final prefs = await SharedPreferences.getInstance();
await prefs.setBool(_key, enabled);
}
}
final audioAlertsEnabledProvider =
StateNotifierProvider<AudioAlertsEnabledNotifier, bool>((ref) {
return AudioAlertsEnabledNotifier();
});
// Alert Radius in Meters
class AlertRadiusMetersNotifier extends StateNotifier<double> {
static const _key = 'alert_radius_meters';
AlertRadiusMetersNotifier() : super(804.7) {
_load();
}
Future<void> _load() async {
final prefs = await SharedPreferences.getInstance();
state = prefs.getDouble(_key) ?? 804.7;
}
Future<void> setRadius(double radiusMeters) async {
state = radiusMeters;
final prefs = await SharedPreferences.getInstance();
await prefs.setDouble(_key, radiusMeters);
}
}
final alertRadiusMetersProvider =
StateNotifierProvider<AlertRadiusMetersNotifier, double>((ref) {
return AlertRadiusMetersNotifier();
});
// Category Filtering Notifier
class EnabledCategoriesNotifier extends StateNotifier<Set<RoadAlertCategory>> {
EnabledCategoriesNotifier()
: super({
RoadAlertCategory.police,
RoadAlertCategory.speedCamera,
RoadAlertCategory.accident,
RoadAlertCategory.hazard,
RoadAlertCategory.roadClosure,
RoadAlertCategory.construction,
RoadAlertCategory.traffic,
RoadAlertCategory.other,
});
void toggleCategory(RoadAlertCategory category, bool enabled) {
final current = Set<RoadAlertCategory>.from(state);
if (enabled) {
current.add(category);
} else {
current.remove(category);
}
state = current;
}
}
final enabledCategoriesProvider =
StateNotifierProvider<EnabledCategoriesNotifier, Set<RoadAlertCategory>>((ref) {
return EnabledCategoriesNotifier();
});
// TomTom API Key
class TomTomApiKeyNotifier extends StateNotifier<String> {
static const _key = 'tomtom_api_key';
TomTomApiKeyNotifier() : super('') {
_load();
}
Future<void> _load() async {
final prefs = await SharedPreferences.getInstance();
state = prefs.getString(_key) ?? '';
}
Future<void> setApiKey(String apiKey) async {
state = apiKey.trim();
final prefs = await SharedPreferences.getInstance();
await prefs.setString(_key, state);
}
}
final tomtomApiKeyProvider =
StateNotifierProvider<TomTomApiKeyNotifier, String>((ref) {
return TomTomApiKeyNotifier();
});
// Community Traffic API Key
class CommunityApiKeyNotifier extends StateNotifier<String> {
static const _key = 'community_api_key';
CommunityApiKeyNotifier() : super('') {
_load();
}
Future<void> _load() async {
final prefs = await SharedPreferences.getInstance();
state = prefs.getString(_key) ?? '';
}
Future<void> setApiKey(String apiKey) async {
state = apiKey.trim();
final prefs = await SharedPreferences.getInstance();
await prefs.setString(_key, state);
}
}
final communityApiKeyProvider =
StateNotifierProvider<CommunityApiKeyNotifier, String>((ref) {
return CommunityApiKeyNotifier();
});
final tomtomStatusProvider = StateProvider<RoadAlertProviderStatus>((ref) {
final key = ref.watch(tomtomApiKeyProvider);
return key.isEmpty ? RoadAlertProviderStatus.notConfigured : RoadAlertProviderStatus.configured;
});
final communityStatusProvider = StateProvider<RoadAlertProviderStatus>((ref) {
final key = ref.watch(communityApiKeyProvider);
return key.isEmpty ? RoadAlertProviderStatus.notConfigured : RoadAlertProviderStatus.configured;
});
// Active Providers List
final activeRoadAlertProviders = Provider<List<RoadAlertProvider>>((ref) {
final tomtomKey = ref.watch(tomtomApiKeyProvider);
final communityKey = ref.watch(communityApiKeyProvider);
final list = <RoadAlertProvider>[];
if (tomtomKey.isNotEmpty) {
list.add(ref.watch(tomtomProviderInstance));
}
if (communityKey.isNotEmpty) {
list.add(ref.watch(communityProviderInstance));
}
// Include Debug Mock Provider only in debug mode
if (kDebugMode) {
list.add(ref.watch(mockProviderInstance));
}
return list;
});