Files
sf-app-platform/apps/mobile_app/lib/providers/wallet_heartbeat_service.dart

50 lines
1.3 KiB
Dart

import 'dart:async';
import 'package:auth/auth.dart';
import 'package:flutter/foundation.dart';
import 'package:sf_shared/sf_shared.dart';
class WalletHeartbeatService {
WalletHeartbeatService({
required TreezorRepository repository,
required SessionLocalDatasource sessionLocal,
required void Function() onError,
}) : _repository = repository,
_sessionLocal = sessionLocal,
_onError = onError;
final TreezorRepository _repository;
final SessionLocalDatasource _sessionLocal;
final void Function() _onError;
Timer? _timer;
static const _interval = Duration(minutes: 3);
void start() {
if (_timer != null) return;
_beat();
_timer = Timer.periodic(_interval, (_) => _beat());
debugPrint('[WalletHeartbeat] started');
}
void stop() {
_timer?.cancel();
_timer = null;
debugPrint('[WalletHeartbeat] stopped');
}
Future<void> _beat() async {
final walletId = await _sessionLocal.getPaymentProfileId();
if (walletId == null || walletId.isEmpty) return;
try {
await _repository.getWallet(walletId: walletId);
debugPrint('[WalletHeartbeat] /wallets/$walletId => OK');
} catch (e) {
debugPrint('[WalletHeartbeat] error: $e');
stop();
_onError();
}
}
}