2026-03-21 01:34:22 +01:00
|
|
|
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>(
|
|
|
|
|
(_) => SharedPreferences.getInstance(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
final deviceHostProvider = StateProvider<String>((ref) {
|
|
|
|
|
final prefs = ref.watch(sharedPreferencesProvider).valueOrNull;
|
|
|
|
|
return prefs?.getString(_kHostKey) ?? _kDefaultHost;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// ── Service ──────────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
final busylightServiceProvider = Provider<BusylightService>((ref) {
|
|
|
|
|
final host = ref.watch(deviceHostProvider);
|
|
|
|
|
return BusylightService(baseUrl: host);
|
|
|
|
|
});
|
|
|
|
|
|
2026-10-16 19:12:16 +02:00
|
|
|
final busylightSnapshotProvider =
|
|
|
|
|
StateNotifierProvider<LiveStatusNotifier, AsyncValue<BusylightUpdate>>(
|
|
|
|
|
(ref) => LiveStatusNotifier(ref.watch(busylightServiceProvider)),
|
|
|
|
|
);
|
2026-03-21 01:34:22 +01:00
|
|
|
|
|
|
|
|
// ── State notifiers ──────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
class BusylightStateNotifier extends StateNotifier<AsyncValue<BusylightStatus>> {
|
|
|
|
|
BusylightStateNotifier(this._service, BusylightStatus? initial)
|
|
|
|
|
: super(initial != null
|
|
|
|
|
? AsyncValue.data(initial)
|
2026-10-16 19:12:16 +02:00
|
|
|
: const AsyncValue.loading());
|
2026-03-21 01:34:22 +01:00
|
|
|
|
|
|
|
|
final BusylightService _service;
|
2026-10-16 19:12:16 +02:00
|
|
|
int _revision = 0;
|
2026-03-21 01:34:22 +01:00
|
|
|
|
|
|
|
|
Future<void> setStatus(BusylightStatus status) async {
|
|
|
|
|
// Keep current value visible during the API call — no loading state
|
2026-10-16 19:12:16 +02:00
|
|
|
final revision = ++_revision;
|
2026-03-21 01:34:22 +01:00
|
|
|
final result = await AsyncValue.guard(() => _service.setStatus(status));
|
2026-10-16 19:12:16 +02:00
|
|
|
if (mounted && revision == _revision) state = result;
|
2026-03-21 01:34:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Update state locally without making an API call (e.g. for `colored`)
|
|
|
|
|
void setLocalStatus(BusylightStatus status) {
|
2026-10-16 19:12:16 +02:00
|
|
|
_revision++;
|
2026-03-21 01:34:22 +01:00
|
|
|
state = AsyncValue.data(status);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final busylightStatusProvider =
|
|
|
|
|
StateNotifierProvider<BusylightStateNotifier, AsyncValue<BusylightStatus>>(
|
|
|
|
|
(ref) {
|
|
|
|
|
final snapshot = ref.watch(busylightSnapshotProvider).valueOrNull;
|
|
|
|
|
return BusylightStateNotifier(
|
|
|
|
|
ref.watch(busylightServiceProvider),
|
|
|
|
|
snapshot?.status,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// ── Brightness ───────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
class BrightnessNotifier extends StateNotifier<double> {
|
|
|
|
|
BrightnessNotifier(this._service, double initial) : super(initial);
|
|
|
|
|
final BusylightService _service;
|
|
|
|
|
|
|
|
|
|
Future<void> set(double value) async {
|
|
|
|
|
state = value;
|
|
|
|
|
await _service.setBrightness(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void silentSet(double value) => state = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final brightnessProvider = StateNotifierProvider<BrightnessNotifier, double>(
|
|
|
|
|
(ref) {
|
|
|
|
|
final snapshot = ref.watch(busylightSnapshotProvider).valueOrNull;
|
|
|
|
|
return BrightnessNotifier(
|
|
|
|
|
ref.watch(busylightServiceProvider),
|
2026-10-16 19:12:16 +02:00
|
|
|
snapshot?.color.brightness ?? 0.3,
|
2026-03-21 01:34:22 +01:00
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// ── Color ─────────────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
class ColorNotifier extends StateNotifier<BusylightColor> {
|
|
|
|
|
ColorNotifier(this._service, BusylightColor initial) : super(initial);
|
|
|
|
|
final BusylightService _service;
|
|
|
|
|
|
|
|
|
|
Future<void> set(BusylightColor color) async {
|
|
|
|
|
state = color;
|
|
|
|
|
await _service.setColor(color);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void silentSet(BusylightColor color) => state = color;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final colorProvider = StateNotifierProvider<ColorNotifier, BusylightColor>(
|
|
|
|
|
(ref) {
|
|
|
|
|
final snapshot = ref.watch(busylightSnapshotProvider).valueOrNull;
|
|
|
|
|
return ColorNotifier(
|
|
|
|
|
ref.watch(busylightServiceProvider),
|
|
|
|
|
snapshot?.color ?? BusylightColor.white,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-10-16 19:12:16 +02:00
|
|
|
class LiveStatusNotifier extends StateNotifier<AsyncValue<BusylightUpdate>> {
|
|
|
|
|
LiveStatusNotifier(
|
|
|
|
|
this._service, {
|
|
|
|
|
this.reconnectDelay = const Duration(seconds: 2),
|
|
|
|
|
}) : super(const AsyncValue.loading()) {
|
|
|
|
|
_connect();
|
2026-03-21 01:34:22 +01:00
|
|
|
}
|
|
|
|
|
|
2026-10-16 19:12:16 +02:00
|
|
|
final BusylightService _service;
|
|
|
|
|
final Duration reconnectDelay;
|
|
|
|
|
Timer? _reconnectTimer;
|
|
|
|
|
StreamSubscription<BusylightUpdate>? _subscription;
|
2026-03-21 01:34:22 +01:00
|
|
|
|
2026-10-16 19:12:16 +02:00
|
|
|
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,
|
|
|
|
|
);
|
2026-03-21 01:34:22 +01:00
|
|
|
}
|
|
|
|
|
|
2026-10-16 19:12:16 +02:00
|
|
|
void _disconnected(Object error, StackTrace stackTrace) {
|
|
|
|
|
if (!mounted) return;
|
|
|
|
|
state = AsyncValue<BusylightUpdate>.error(error, stackTrace)
|
|
|
|
|
.copyWithPrevious(state);
|
|
|
|
|
_reconnectTimer?.cancel();
|
|
|
|
|
_reconnectTimer = Timer(reconnectDelay, _connect);
|
2026-03-21 01:34:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
2026-10-16 19:12:16 +02:00
|
|
|
_reconnectTimer?.cancel();
|
|
|
|
|
_subscription?.cancel();
|
2026-03-21 01:34:22 +01:00
|
|
|
super.dispose();
|
|
|
|
|
}
|
2026-10-16 19:12:16 +02:00
|
|
|
}
|