ente/lib/utils/fake_progress.dart
2023-09-01 15:15:56 +05:30

45 lines
924 B
Dart

import 'dart:async';
import "package:flutter/foundation.dart";
typedef FakeProgressCallback = void Function(int count);
class FakePeriodicProgress {
final FakeProgressCallback? callback;
final Duration duration;
Timer? _timer;
bool _shouldRun = true;
int runCount = 0;
FakePeriodicProgress({
required this.callback,
required this.duration,
});
void start() {
assert(_shouldRun, "Cannot start a stopped FakePeriodicProgress");
Future.delayed(duration, _invokePeriodically);
}
void stop() {
if (_shouldRun) {
_shouldRun = false;
_timer?.cancel();
}
}
void _invokePeriodically() {
if (_shouldRun) {
try {
runCount++;
callback?.call(runCount);
} catch (e) {
debugPrint("Error in FakePeriodicProgress callback: $e");
stop();
return;
}
_timer = Timer(duration, _invokePeriodically);
}
}
}