ente/lib/utils/fake_progress.dart

45 lines
927 B
Dart
Raw Normal View History

2023-09-01 04:24:54 +00:00
import 'dart:async';
import "package:flutter/foundation.dart";
typedef FakeProgressCallback = void Function(int count);
class FakePeriodicProgress {
final FakeProgressCallback? callback;
final Duration duration;
late 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++;
2023-09-01 05:07:42 +00:00
callback?.call(runCount);
2023-09-01 04:24:54 +00:00
} catch (e) {
debugPrint("Error in FakePeriodicProgress callback: $e");
stop();
2023-09-01 05:07:42 +00:00
return;
2023-09-01 04:24:54 +00:00
}
_timer = Timer(duration, _invokePeriodically);
}
}
}