import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:shared_preferences/shared_preferences.dart'; final settingsProvider = AsyncNotifierProvider(SettingsNotifier.new); class AppSettings { final String mirrorHost; final int mirrorPort; final String workoutPlayerUrl; final String? plexHost; final int plexPort; final String? plexToken; final String? haBaseUrl; final String? haToken; final String? spotifyClientId; final bool spotifyConnected; const AppSettings({ this.mirrorHost = 'mirror.local', this.mirrorPort = 9090, this.workoutPlayerUrl = 'http://192.168.86.172:8091', this.plexHost, this.plexPort = 32400, this.plexToken, this.haBaseUrl, this.haToken, this.spotifyClientId, this.spotifyConnected = false, }); AppSettings copyWith({ String? mirrorHost, int? mirrorPort, String? workoutPlayerUrl, String? plexHost, int? plexPort, String? plexToken, String? haBaseUrl, String? haToken, String? spotifyClientId, bool? spotifyConnected, }) => AppSettings( mirrorHost: mirrorHost ?? this.mirrorHost, mirrorPort: mirrorPort ?? this.mirrorPort, workoutPlayerUrl: workoutPlayerUrl ?? this.workoutPlayerUrl, plexHost: plexHost ?? this.plexHost, plexPort: plexPort ?? this.plexPort, plexToken: plexToken ?? this.plexToken, haBaseUrl: haBaseUrl ?? this.haBaseUrl, haToken: haToken ?? this.haToken, spotifyClientId: spotifyClientId ?? this.spotifyClientId, spotifyConnected: spotifyConnected ?? this.spotifyConnected, ); } class SettingsNotifier extends AsyncNotifier { @override Future build() async { final prefs = await SharedPreferences.getInstance(); return AppSettings( mirrorHost: prefs.getString('mirror_host') ?? 'mirror.local', mirrorPort: prefs.getInt('mirror_port') ?? 9090, workoutPlayerUrl: prefs.getString('workout_player_url') ?? 'http://192.168.86.172:8091', plexHost: prefs.getString('plex_host'), plexPort: prefs.getInt('plex_port') ?? 32400, plexToken: prefs.getString('plex_token'), haBaseUrl: prefs.getString('ha_base_url'), haToken: prefs.getString('ha_token'), spotifyClientId: prefs.getString('spotify_client_id'), spotifyConnected: prefs.getBool('spotify_connected') ?? false, ); } Future save(AppSettings settings) async { final prefs = await SharedPreferences.getInstance(); await prefs.setString('mirror_host', settings.mirrorHost); await prefs.setInt('mirror_port', settings.mirrorPort); await prefs.setString('workout_player_url', settings.workoutPlayerUrl); if (settings.plexHost != null) { await prefs.setString('plex_host', settings.plexHost!); } else { await prefs.remove('plex_host'); } await prefs.setInt('plex_port', settings.plexPort); if (settings.plexToken != null) { await prefs.setString('plex_token', settings.plexToken!); } else { await prefs.remove('plex_token'); } if (settings.haBaseUrl != null) { await prefs.setString('ha_base_url', settings.haBaseUrl!); } else { await prefs.remove('ha_base_url'); } if (settings.haToken != null) { await prefs.setString('ha_token', settings.haToken!); } else { await prefs.remove('ha_token'); } if (settings.spotifyClientId != null) { await prefs.setString('spotify_client_id', settings.spotifyClientId!); } else { await prefs.remove('spotify_client_id'); } await prefs.setBool('spotify_connected', settings.spotifyConnected); state = AsyncValue.data(settings); } }