import 'dart:async'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../models/busylight_color.dart'; import '../models/busylight_status.dart'; import '../services/busylight_service.dart'; // ── Device config ──────────────────────────────────────────────────────────── const _kHostKey = 'busylight_host'; const _kDefaultHost = 'http://igox-busylight.local'; final sharedPreferencesProvider = FutureProvider( (_) => SharedPreferences.getInstance(), ); final deviceHostProvider = StateProvider((ref) { final prefs = ref.watch(sharedPreferencesProvider).valueOrNull; return prefs?.getString(_kHostKey) ?? _kDefaultHost; }); // ── Service ────────────────────────────────────────────────────────────────── final busylightServiceProvider = Provider((ref) { final host = ref.watch(deviceHostProvider); return BusylightService(baseUrl: host); }); final busylightSnapshotProvider = StateNotifierProvider>( (ref) => LiveStatusNotifier(ref.watch(busylightServiceProvider)), ); // ── State notifiers ────────────────────────────────────────────────────────── class BusylightStateNotifier extends StateNotifier> { BusylightStateNotifier(this._service, BusylightStatus? initial) : super(initial != null ? AsyncValue.data(initial) : const AsyncValue.loading()); final BusylightService _service; int _revision = 0; Future setStatus(BusylightStatus status) async { // Keep current value visible during the API call — no loading state final revision = ++_revision; final result = await AsyncValue.guard(() => _service.setStatus(status)); if (mounted && revision == _revision) state = result; } /// Update state locally without making an API call (e.g. for `colored`) void setLocalStatus(BusylightStatus status) { _revision++; state = AsyncValue.data(status); } } final busylightStatusProvider = StateNotifierProvider>( (ref) { final snapshot = ref.watch(busylightSnapshotProvider).valueOrNull; return BusylightStateNotifier( ref.watch(busylightServiceProvider), snapshot?.status, ); }, ); // ── Brightness ─────────────────────────────────────────────────────────────── class BrightnessNotifier extends StateNotifier { BrightnessNotifier(this._service, double initial) : super(initial); final BusylightService _service; Future set(double value) async { state = value; await _service.setBrightness(value); } void silentSet(double value) => state = value; } final brightnessProvider = StateNotifierProvider( (ref) { final snapshot = ref.watch(busylightSnapshotProvider).valueOrNull; return BrightnessNotifier( ref.watch(busylightServiceProvider), snapshot?.color.brightness ?? 0.3, ); }, ); // ── Color ───────────────────────────────────────────────────────────────────── class ColorNotifier extends StateNotifier { ColorNotifier(this._service, BusylightColor initial) : super(initial); final BusylightService _service; Future set(BusylightColor color) async { state = color; await _service.setColor(color); } void silentSet(BusylightColor color) => state = color; } final colorProvider = StateNotifierProvider( (ref) { final snapshot = ref.watch(busylightSnapshotProvider).valueOrNull; return ColorNotifier( ref.watch(busylightServiceProvider), snapshot?.color ?? BusylightColor.white, ); }, ); class LiveStatusNotifier extends StateNotifier> { LiveStatusNotifier( this._service, { this.reconnectDelay = const Duration(seconds: 2), }) : super(const AsyncValue.loading()) { _connect(); } final BusylightService _service; final Duration reconnectDelay; Timer? _reconnectTimer; StreamSubscription? _subscription; void _connect() { if (!mounted) return; _subscription = _service.watchStatus().listen( (update) { if (!mounted) return; state = AsyncValue.data(update); }, onError: _disconnected, onDone: () => _disconnected( const BusylightException('Live status connection closed'), StackTrace.current, ), cancelOnError: true, ); } void _disconnected(Object error, StackTrace stackTrace) { if (!mounted) return; state = AsyncValue.error(error, stackTrace) .copyWithPrevious(state); _reconnectTimer?.cancel(); _reconnectTimer = Timer(reconnectDelay, _connect); } @override void dispose() { _reconnectTimer?.cancel(); _subscription?.cancel(); super.dispose(); } }