Change polling interval to retrieve BusiLight status (#3)
Implement a more agressive polling interval to retrieve BusiLight status. From (in sec.): ``` - min: 0 - max: 60 - default: 5 - divisions: 12 ``` to (in sec.): ``` - min: 0 - max: 2 - default: 1 - divisions: 4 ``` Reviewed-on: #3
This commit was merged in pull request #3.
This commit is contained in:
@@ -10,7 +10,7 @@ import '../services/busylight_service.dart';
|
||||
const _kHostKey = 'busylight_host';
|
||||
const _kDefaultHost = 'http://igox-busylight.local';
|
||||
const _kPollIntervalKey = 'busylight_poll_interval';
|
||||
const _kDefaultPollInterval = 5; // seconds
|
||||
const _kDefaultPollInterval = 1.0; // seconds
|
||||
|
||||
final sharedPreferencesProvider = FutureProvider<SharedPreferences>(
|
||||
(_) => SharedPreferences.getInstance(),
|
||||
@@ -21,9 +21,9 @@ final deviceHostProvider = StateProvider<String>((ref) {
|
||||
return prefs?.getString(_kHostKey) ?? _kDefaultHost;
|
||||
});
|
||||
|
||||
final pollIntervalProvider = StateProvider<int>((ref) {
|
||||
final pollIntervalProvider = StateProvider<double>((ref) {
|
||||
final prefs = ref.watch(sharedPreferencesProvider).valueOrNull;
|
||||
return prefs?.getInt(_kPollIntervalKey) ?? _kDefaultPollInterval;
|
||||
return prefs?.getDouble(_kPollIntervalKey) ?? _kDefaultPollInterval.toDouble();
|
||||
});
|
||||
|
||||
// ── Service ──────────────────────────────────────────────────────────────────
|
||||
@@ -161,10 +161,11 @@ class PollingNotifier extends StateNotifier<void> {
|
||||
Timer? _timer;
|
||||
|
||||
void _start() {
|
||||
final interval = _ref.read(pollIntervalProvider);
|
||||
final intervalSeconds = _ref.read(pollIntervalProvider);
|
||||
final intervalMillis = (intervalSeconds * 1000).toInt();
|
||||
_timer?.cancel();
|
||||
if (interval <= 0) return;
|
||||
_timer = Timer.periodic(Duration(seconds: interval), (_) => _poll());
|
||||
if (intervalMillis <= 0) return;
|
||||
_timer = Timer.periodic(Duration(milliseconds: intervalMillis), (_) => _poll());
|
||||
}
|
||||
|
||||
void restart() => _start();
|
||||
|
||||
@@ -14,7 +14,7 @@ class SettingsScreen extends ConsumerStatefulWidget {
|
||||
|
||||
class _SettingsScreenState extends ConsumerState<SettingsScreen> {
|
||||
late TextEditingController _hostController;
|
||||
late int _pollInterval;
|
||||
late double _pollInterval;
|
||||
bool _startWithSession = false;
|
||||
|
||||
@override
|
||||
@@ -48,7 +48,7 @@ class _SettingsScreenState extends ConsumerState<SettingsScreen> {
|
||||
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString('busylight_host', host);
|
||||
await prefs.setInt('busylight_poll_interval', _pollInterval);
|
||||
await prefs.setDouble('busylight_poll_interval', _pollInterval);
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
@@ -58,10 +58,10 @@ class _SettingsScreenState extends ConsumerState<SettingsScreen> {
|
||||
}
|
||||
}
|
||||
|
||||
String _intervalLabel(int seconds) {
|
||||
if (seconds == 0) return 'Off';
|
||||
if (seconds < 60) return '${seconds}s';
|
||||
return '${seconds ~/ 60}m';
|
||||
String _intervalLabel(double seconds) {
|
||||
if (seconds == 0.0) return 'Off';
|
||||
if (seconds < 2.0) return '${seconds}s';
|
||||
return '${ (seconds)}s';
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -124,21 +124,21 @@ class _SettingsScreenState extends ConsumerState<SettingsScreen> {
|
||||
overlayColor: Colors.amber.withOpacity(0.2),
|
||||
),
|
||||
child: Slider(
|
||||
value: _pollInterval.toDouble(),
|
||||
min: 0,
|
||||
max: 60,
|
||||
divisions: 12,
|
||||
onChanged: (v) => setState(() => _pollInterval = v.round()),
|
||||
value: _pollInterval,
|
||||
min: 0.0,
|
||||
max: 2.0,
|
||||
divisions: 4,
|
||||
onChanged: (v) => setState(() => _pollInterval = v),
|
||||
),
|
||||
),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text('Off', style: TextStyle(color: Colors.grey.shade600, fontSize: 11)),
|
||||
Text('5s', style: TextStyle(color: Colors.grey.shade600, fontSize: 11)),
|
||||
Text('10s', style: TextStyle(color: Colors.grey.shade600, fontSize: 11)),
|
||||
Text('30s', style: TextStyle(color: Colors.grey.shade600, fontSize: 11)),
|
||||
Text('1m', style: TextStyle(color: Colors.grey.shade600, fontSize: 11)),
|
||||
Text('0.5s', style: TextStyle(color: Colors.grey.shade600, fontSize: 11)),
|
||||
Text('1s', style: TextStyle(color: Colors.grey.shade600, fontSize: 11)),
|
||||
Text('1.5s', style: TextStyle(color: Colors.grey.shade600, fontSize: 11)),
|
||||
Text('2s', style: TextStyle(color: Colors.grey.shade600, fontSize: 11)),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 6),
|
||||
|
||||
Reference in New Issue
Block a user