ente/mobile/lib/utils/fake_progress.dart

45 lines
924 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;
2023-09-01 09:45:56 +00:00
Timer? _timer;
2023-09-01 04:24:54 +00:00
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;
2023-09-01 09:45:56 +00:00
_timer?.cancel();
2023-09-01 04:24:54 +00:00
}
}
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);
}
}
}