Last active
May 16, 2023 16:32
-
-
Save folaoluwafemi/fa15c109708c8f5ef110ebee5fb790e8 to your computer and use it in GitHub Desktop.
A mixin for adding timer to state or screens e.g when there's a reset password otp timer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
/// A mixin for adding timer to state or screens | |
///e.g when there's a reset password otp timer | |
/// | |
/// Add it to your immutable state class like so: | |
/// class PasswordState extends AuthState with TimerMixin { | |
/// ... | |
/// | |
/// @override | |
/// timerDuration => 30; //in seconds | |
/// } | |
/// | |
mixin TimerMixin { | |
late final StreamController<Duration> _timerSub = | |
StreamController<Duration>(); | |
///in seconds | |
int get timerDuration; | |
StreamController<Duration> get timerStreamSub { | |
timer; | |
return _timerSub; | |
} | |
bool _timerCompleted = false; | |
bool get timerCompleted => _timerCompleted; | |
set timerCompleted(bool timerCompleted) { | |
if (_timerCompleted ^ timerCompleted) { | |
_timerCompleted = timerCompleted; | |
} | |
} | |
Timer get _newTimer => Timer.periodic( | |
const Duration(seconds: 1), | |
(timer) { | |
timerCompleted = false; | |
_timerSub.add(Duration(seconds: timerDuration - timer.tick)); | |
if (timer.tick == timerDuration) { | |
timerCompleted = true; | |
timer.cancel(); | |
} | |
}, | |
); | |
late Timer timer = _newTimer; | |
void resetTimerIfInactive() { | |
if (!timer.isActive && timer.tick >= timerDuration) { | |
timer = _newTimer; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment