From 3e4b9f0e3835e7f676a215b4b3aa0203b31399e8 Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Mon, 26 Oct 2020 10:17:46 +0100 Subject: [PATCH 1/2] Add example implementing a pausable countdown --- CHANGELOG.md | 4 +++ README.md | 66 +++++++++++++++++++++++++++++++++++++++++- example/countdown.dart | 59 +++++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 example/countdown.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d3ee17..3ed14bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## dev + +- Add example implementing a pausable countdown. + ## 0.1.0+1 - Add build status and sponsorship badges. diff --git a/README.md b/README.md index 264ab1f..cb34534 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ A [Dart](https://dart.dev/) [timer](https://api.dart.dev/stable/dart-async/Timer/Timer.html) that can be paused, resumed and reset. -## Example +## Example using `start()`, `pause()` and `reset()` ```dart import 'package:pausable_timer/pausable_timer.dart'; @@ -70,3 +70,67 @@ void main() async { print('isCancelled: ${timer.isCancelled}'); } ``` + +## Example pausable countdown implementation + +```dart +import 'package:pausable_timer/pausable_timer.dart'; + +/// Example on how to implement countdown making a PausableTimer periodic. +void main() async { + PausableTimer timer; + var countDown = 5; + + print('Create a periodic timer that fires every 1 second and starts it'); + timer = PausableTimer( + Duration(seconds: 1), + () { + countDown--; + // If we reached 0, we don't reset and restart the time, so it won't fire + // again, but it can be reused afterwards if needed. If we cancel the + // timer, then it can be reused after the countdown is over. + if (countDown > 0) { + timer + ..reset() + ..start(); + } + // This is really what your callback do. + print('\t$countDown'); + }, + )..start(); + + print('And wait 2.1 seconds...'); + print('(0.1 extra to make sure there is no race between the timer and the ' + 'waiting here)'); + await Future.delayed(timer.duration * 2.1); + print('By now 2 events should have fired: 4, 3\n'); + + print('We can pause it now'); + timer.pause(); + + print('And we wait for 2 more seconds...'); + await Future.delayed(timer.duration * 2); + print("But our timer doesn't care while it's paused\n"); + + print('So we start it again'); + timer.start(); + print('And wait for 3.1 seconds more...'); + await Future.delayed(timer.duration * 3.1); + print('And we are done: 2, 1 and 0 should have been printed'); + + print('The timer should be unpaused, inactive, expired and not cancelled'); + print('isPaused: ${timer.isPaused}'); + print('isActive: ${timer.isActive}'); + print('isExpired: ${timer.isExpired}'); + print('isCancelled: ${timer.isCancelled}'); + + print('We can now reset it and start it again, now for 3 seconds'); + countDown = 3; + timer + ..reset() + ..start(); + print('And wait for 3.1 seconds...'); + await Future.delayed(timer.duration * 3.1); + print('And it should be done printing: 2, 1 and 0'); +} +``` diff --git a/example/countdown.dart b/example/countdown.dart new file mode 100644 index 0000000..0512116 --- /dev/null +++ b/example/countdown.dart @@ -0,0 +1,59 @@ +import 'package:pausable_timer/pausable_timer.dart'; + +/// Example on how to implement countdown making a PausableTimer periodic. +void main() async { + PausableTimer timer; + var countDown = 5; + + print('Create a periodic timer that fires every 1 second and starts it'); + timer = PausableTimer( + Duration(seconds: 1), + () { + countDown--; + // If we reached 0, we don't reset and restart the time, so it won't fire + // again, but it can be reused afterwards if needed. If we cancel the + // timer, then it can be reused after the countdown is over. + if (countDown > 0) { + timer + ..reset() + ..start(); + } + // This is really what your callback do. + print('\t$countDown'); + }, + )..start(); + + print('And wait 2.1 seconds...'); + print('(0.1 extra to make sure there is no race between the timer and the ' + 'waiting here)'); + await Future.delayed(timer.duration * 2.1); + print('By now 2 events should have fired: 4, 3\n'); + + print('We can pause it now'); + timer.pause(); + + print('And we wait for 2 more seconds...'); + await Future.delayed(timer.duration * 2); + print("But our timer doesn't care while it's paused\n"); + + print('So we start it again'); + timer.start(); + print('And wait for 3.1 seconds more...'); + await Future.delayed(timer.duration * 3.1); + print('And we are done: 2, 1 and 0 should have been printed'); + + print('The timer should be unpaused, inactive, expired and not cancelled'); + print('isPaused: ${timer.isPaused}'); + print('isActive: ${timer.isActive}'); + print('isExpired: ${timer.isExpired}'); + print('isCancelled: ${timer.isCancelled}'); + + print('We can now reset it and start it again, now for 3 seconds'); + countDown = 3; + timer + ..reset() + ..start(); + print('And wait for 3.1 seconds...'); + await Future.delayed(timer.duration * 3.1); + print('And it should be done printing: 2, 1 and 0'); +} From 5610e8faca1af98448d535e15effc06961b318d0 Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Mon, 26 Oct 2020 10:18:30 +0100 Subject: [PATCH 2/2] Bump version to 0.1.0+2 --- CHANGELOG.md | 2 +- pubspec.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ed14bf..f2dad85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -## dev +## 0.1.0+2 - Add example implementing a pausable countdown. diff --git a/pubspec.yaml b/pubspec.yaml index 1dea1ee..38dc78b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -3,7 +3,7 @@ description: A timer implementation that can be paused, resumed and reset. homepage: https://github.com/llucax/pausable_timer repository: https://github.com/llucax/pausable_timer -version: 0.1.0+1 +version: 0.1.0+2 environment: sdk: ">=2.7.0 <3.0.0"