debouncedSave method
Implementation
Future<void> debouncedSave(StepData data) async {
final now = TZDateTime.now(_timezone);
final nextMidnight = now.add(const Duration(days: 1)).subtract(Duration(
hours: now.hour,
minutes: now.minute,
seconds: now.second,
milliseconds: now.millisecond,
microseconds: now.microsecond));
// 마지막 저장이 오래되었거나,
// 날짜가 바뀌기 직전이거나,
// 데이터의 날짜가 달라졌다면, 타이머 캔슬하고 바로 저장한다.
if (_lastSaveTime == null ||
_lastSaveTime!.isBefore(now.subtract(_maxDeboundDuration)) ||
(nextMidnight.difference(now).inMilliseconds < 1000 &&
now.difference(_lastSaveTime!).inMilliseconds > 1000) ||
data.todayDate != formatDate(_lastSaveTime!)) {
await save(data);
_debounceTimer?.cancel();
_debounceTimer = null;
return;
}
if (_debounceTimer?.isActive ?? false) {
_debounceTimer!.cancel();
_debounceTimer = null;
}
_debounceTimer = Timer(_debounceDuration, () async {
await save(data);
});
}