import 'package:flutter/material.dart'; import 'package:flutter/foundation.dart'; import 'package:shared_preferences/shared_preferences.dart'; class SettingsProvider with ChangeNotifier { // Use 10.0.2.2 for Android emulator to access host's localhost static String get _defaultUrl => (defaultTargetPlatform == TargetPlatform.android && !kIsWeb) ? "http://10.0.2.2:8080" : "http://localhost:8080"; String _baseUrl = _defaultUrl; Color _themeColor = Colors.blue; ThemeMode _themeMode = ThemeMode.system; String get baseUrl => _baseUrl; Color get themeColor => _themeColor; ThemeMode get themeMode => _themeMode; SettingsProvider() { _loadSettings(); } void _loadSettings() async { final prefs = await SharedPreferences.getInstance(); _baseUrl = prefs.getString('baseUrl') ?? _baseUrl; final colorValue = prefs.getInt('themeColor'); if (colorValue != null) { _themeColor = Color(colorValue); } final savedThemeMode = prefs.getString('themeMode'); if (savedThemeMode != null) { _themeMode = _themeModeFromString(savedThemeMode); } notifyListeners(); } void setBaseUrl(String url) async { _baseUrl = url; final prefs = await SharedPreferences.getInstance(); await prefs.setString('baseUrl', url); notifyListeners(); } void setThemeColor(Color color) async { _themeColor = color; final prefs = await SharedPreferences.getInstance(); await prefs.setInt('themeColor', color.toARGB32()); notifyListeners(); } void setThemeMode(ThemeMode mode) async { _themeMode = mode; final prefs = await SharedPreferences.getInstance(); await prefs.setString('themeMode', mode.name); notifyListeners(); } // Also provide the RTMP URL based on the same hostname String get rtmpUrl { final uri = Uri.parse(_baseUrl); return "rtmp://${uri.host}:1935/live"; } String playbackUrl(String roomId) { final uri = Uri.parse(_baseUrl); if (kIsWeb) { return uri.replace(path: '/live/$roomId').toString(); } return "$rtmpUrl/$roomId"; } ThemeMode _themeModeFromString(String value) { switch (value) { case 'light': return ThemeMode.light; case 'dark': return ThemeMode.dark; default: return ThemeMode.system; } } }