47 lines
1.1 KiB
Dart
47 lines
1.1 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:dio/dio.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart' show WidgetRef;
|
|
import 'package:sf_shared/sf_shared.dart';
|
|
|
|
class LegacyHeartbeatService {
|
|
LegacyHeartbeatService({
|
|
required WidgetRef ref,
|
|
required void Function() onUnauthorized,
|
|
}) : _ref = ref,
|
|
_onUnauthorized = onUnauthorized;
|
|
|
|
final WidgetRef _ref;
|
|
final void Function() _onUnauthorized;
|
|
Timer? _timer;
|
|
|
|
static const _interval = Duration(minutes: 2);
|
|
|
|
void start() {
|
|
if (_timer != null) return;
|
|
_beat();
|
|
_timer = Timer.periodic(_interval, (_) => _beat());
|
|
debugPrint('[LegacyHeartbeat] started');
|
|
}
|
|
|
|
void stop() {
|
|
_timer?.cancel();
|
|
_timer = null;
|
|
debugPrint('[LegacyHeartbeat] stopped');
|
|
}
|
|
|
|
Future<void> _beat() async {
|
|
try {
|
|
await _ref.read(legacyDevicesProvider.notifier).refresh();
|
|
debugPrint('[LegacyHeartbeat] devices refreshed');
|
|
} catch (e) {
|
|
debugPrint('[LegacyHeartbeat] error: $e');
|
|
if (e is DioException && e.response?.statusCode == 401) {
|
|
stop();
|
|
_onUnauthorized();
|
|
}
|
|
}
|
|
}
|
|
}
|