Skip to content

Commit

Permalink
Can be set preset time in running timer. Added clearPresetTime and on…
Browse files Browse the repository at this point in the history
…Ended. (#14)
  • Loading branch information
hukusuke1007 authored Jun 15, 2021
1 parent 8d1780c commit ec09ab0
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 68 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# 1.2.0+1
# 1.3.0
Can be set preset time in running timer. Added clearPresetTime and onEnded.

## 1.2.0+1
Modified README.

## 1.2.0
Expand Down
104 changes: 70 additions & 34 deletions example/lib/count_down_timer_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ class _State extends State<CountDownTimerPage> {

final StopWatchTimer _stopWatchTimer = StopWatchTimer(
mode: StopWatchMode.countDown,
presetMillisecond: StopWatchTimer.getMilliSecFromMinute(1),
presetMillisecond: StopWatchTimer.getMilliSecFromSecond(3),
onChange: (value) => print('onChange $value'),
onChangeRawSecond: (value) => print('onChangeRawSecond $value'),
onChangeRawMinute: (value) => print('onChangeRawMinute $value'),
onEnded: () {
print('onEnded');
},
);

final _scrollController = ScrollController();
Expand Down Expand Up @@ -248,10 +251,12 @@ class _State extends State<CountDownTimerPage> {
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(horizontal: 4),
child: RaisedButton(
padding: const EdgeInsets.all(4),
color: Colors.lightBlue,
shape: const StadiumBorder(),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.lightBlue,
onPrimary: Colors.white,
shape: const StadiumBorder(),
),
onPressed: () async {
_stopWatchTimer.onExecute
.add(StopWatchExecute.start);
Expand All @@ -264,10 +269,12 @@ class _State extends State<CountDownTimerPage> {
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 4),
child: RaisedButton(
padding: const EdgeInsets.all(4),
color: Colors.green,
shape: const StadiumBorder(),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.green,
onPrimary: Colors.white,
shape: const StadiumBorder(),
),
onPressed: () async {
_stopWatchTimer.onExecute
.add(StopWatchExecute.stop);
Expand All @@ -280,10 +287,12 @@ class _State extends State<CountDownTimerPage> {
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 4),
child: RaisedButton(
padding: const EdgeInsets.all(4),
color: Colors.red,
shape: const StadiumBorder(),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.red,
onPrimary: Colors.white,
shape: const StadiumBorder(),
),
onPressed: () async {
_stopWatchTimer.onExecute
.add(StopWatchExecute.reset);
Expand All @@ -304,10 +313,12 @@ class _State extends State<CountDownTimerPage> {
children: <Widget>[
Padding(
padding: const EdgeInsets.all(0).copyWith(right: 8),
child: RaisedButton(
padding: const EdgeInsets.all(4),
color: Colors.deepPurpleAccent,
shape: const StadiumBorder(),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.deepPurpleAccent,
onPrimary: Colors.white,
shape: const StadiumBorder(),
),
onPressed: () async {
_stopWatchTimer.onExecute
.add(StopWatchExecute.lap);
Expand All @@ -328,10 +339,12 @@ class _State extends State<CountDownTimerPage> {
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(horizontal: 4),
child: RaisedButton(
padding: const EdgeInsets.all(4),
color: Colors.pinkAccent,
shape: const StadiumBorder(),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.pinkAccent,
onPrimary: Colors.white,
shape: const StadiumBorder(),
),
onPressed: () async {
_stopWatchTimer.setPresetHoursTime(1);
},
Expand All @@ -343,10 +356,12 @@ class _State extends State<CountDownTimerPage> {
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 4),
child: RaisedButton(
padding: const EdgeInsets.all(4),
color: Colors.pinkAccent,
shape: const StadiumBorder(),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.pinkAccent,
onPrimary: Colors.white,
shape: const StadiumBorder(),
),
onPressed: () async {
_stopWatchTimer.setPresetMinuteTime(59);
},
Expand All @@ -358,12 +373,14 @@ class _State extends State<CountDownTimerPage> {
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 4),
child: RaisedButton(
padding: const EdgeInsets.all(4),
color: Colors.pinkAccent,
shape: const StadiumBorder(),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.pinkAccent,
onPrimary: Colors.white,
shape: const StadiumBorder(),
),
onPressed: () async {
_stopWatchTimer.setPresetSecondTime(59);
_stopWatchTimer.setPresetSecondTime(10);
},
child: const Text(
'Set Second',
Expand All @@ -376,10 +393,12 @@ class _State extends State<CountDownTimerPage> {
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 4),
child: RaisedButton(
padding: const EdgeInsets.all(4),
color: Colors.pinkAccent,
shape: const StadiumBorder(),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.pinkAccent,
onPrimary: Colors.white,
shape: const StadiumBorder(),
),
onPressed: () async {
_stopWatchTimer.setPresetTime(mSec: 3599 * 1000);
},
Expand All @@ -389,6 +408,23 @@ class _State extends State<CountDownTimerPage> {
),
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 4),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.pinkAccent,
onPrimary: Colors.white,
shape: const StadiumBorder(),
),
onPressed: () async {
_stopWatchTimer.clearPresetTime();
},
child: const Text(
'Clear PresetTime',
style: TextStyle(color: Colors.white),
),
),
),
],
),
)
Expand Down
19 changes: 18 additions & 1 deletion example/lib/count_up_timer_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ class _State extends State<CountUpTimerPage> {
color: Colors.pinkAccent,
shape: const StadiumBorder(),
onPressed: () async {
_stopWatchTimer.setPresetSecondTime(59);
_stopWatchTimer.setPresetSecondTime(10);
},
child: const Text(
'Set Second',
Expand All @@ -389,6 +389,23 @@ class _State extends State<CountUpTimerPage> {
),
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 4),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.pinkAccent,
onPrimary: Colors.white,
shape: const StadiumBorder(),
),
onPressed: () async {
_stopWatchTimer.clearPresetTime();
},
child: const Text(
'Clear PresetTime',
style: TextStyle(color: Colors.white),
),
),
),
],
),
)
Expand Down
79 changes: 49 additions & 30 deletions lib/stop_watch_timer.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';
import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:rxdart/rxdart.dart';

/// StopWatchRecord
Expand Down Expand Up @@ -34,8 +35,11 @@ class StopWatchTimer {
this.onChange,
this.onChangeRawSecond,
this.onChangeRawMinute,
this.onEnded,
}) {
/// Set presetTime
_presetTime = presetMillisecond;
initialPresetTime = presetMillisecond;

_elapsedTime.listen((value) {
_rawTimeController.add(value);
Expand Down Expand Up @@ -86,6 +90,7 @@ class StopWatchTimer {
final Function(int)? onChange;
final Function(int)? onChangeRawSecond;
final Function(int)? onChangeRawMinute;
final VoidCallback? onEnded;

final PublishSubject<int> _elapsedTime = PublishSubject<int>();

Expand Down Expand Up @@ -118,6 +123,9 @@ class StopWatchTimer {
int? _minute;
List<StopWatchRecord> _records = [];

bool get isRunning => _timer != null && _timer!.isActive;
late int initialPresetTime;

/// Get display time.
static String getDisplayTime(
int value, {
Expand Down Expand Up @@ -221,9 +229,6 @@ class StopWatchTimer {
}

/// Get display millisecond time.
// ignore: avoid_bool_literals_in_conditional_expressions
bool get isRunning => _timer != null ? _timer!.isActive : false;

void setPresetHoursTime(int value) =>
setPresetTime(mSec: value * 3600 * 1000);

Expand All @@ -233,55 +238,69 @@ class StopWatchTimer {

/// Set preset time. 1000 mSec => 1 sec
void setPresetTime({required int mSec}) {
if (_timer == null && _stopTime == 0) {
_presetTime = mSec;
_elapsedTime.add(_presetTime);
_presetTime += mSec;
_elapsedTime.add(_presetTime);
}

void clearPresetTime() {
if (mode == StopWatchMode.countUp) {
_presetTime = initialPresetTime;
_elapsedTime.add(isRunning ? _getCountUpTime(_presetTime) : _presetTime);
} else if (mode == StopWatchMode.countDown) {
_presetTime = initialPresetTime;
_elapsedTime
.add(isRunning ? _getCountDownTime(_presetTime) : _presetTime);
} else {
print(
'Can be not set preset time because of timer is not reset. please reset timer.');
throw Exception('No support mode');
}
}

void _handle(Timer timer) {
switch (mode) {
case StopWatchMode.countUp:
final time = DateTime.now().millisecondsSinceEpoch -
_startTime +
_stopTime +
_presetTime;
_elapsedTime.add(time);
break;
case StopWatchMode.countDown:
final time = max(
_presetTime -
(DateTime.now().millisecondsSinceEpoch - _startTime + _stopTime),
0,
);
_elapsedTime.add(time);
if (time == 0) {
_stop();
if (mode == StopWatchMode.countUp) {
_elapsedTime.add(_getCountUpTime(_presetTime));
} else if (mode == StopWatchMode.countDown) {
final time = _getCountDownTime(_presetTime);
_elapsedTime.add(time);
if (time == 0) {
_stop();
if (onEnded != null) {
onEnded!();
}
break;
}
} else {
throw Exception('No support mode');
}
}

int _getCountUpTime(int presetTime) =>
DateTime.now().millisecondsSinceEpoch -
_startTime +
_stopTime +
presetTime;

int _getCountDownTime(int presetTime) => max(
presetTime -
(DateTime.now().millisecondsSinceEpoch - _startTime + _stopTime),
0,
);

void _start() {
if (_timer == null || !_timer!.isActive) {
if (!isRunning) {
_startTime = DateTime.now().millisecondsSinceEpoch;
_timer = Timer.periodic(const Duration(milliseconds: 1), _handle);
}
}

void _stop() {
if (_timer != null && _timer!.isActive) {
if (isRunning) {
_timer!.cancel();
_timer = null;
_stopTime += DateTime.now().millisecondsSinceEpoch - _startTime;
}
}

void _reset() {
if (_timer != null && _timer!.isActive) {
if (isRunning) {
_timer!.cancel();
_timer = null;
}
Expand All @@ -295,7 +314,7 @@ class StopWatchTimer {
}

void _lap() {
if (_timer != null && _timer!.isActive) {
if (isRunning) {
final rawValue = _rawTimeController.value;
_records.add(StopWatchRecord(
rawValue: rawValue,
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: stop_watch_timer
description: This is Stop Watch Timer for flutter plugin. It easily create application of stop watch for sports if use this plugin.
version: 1.2.0+1
version: 1.3.0
repository: https://github.com/hukusuke1007/stop_watch_timer
homepage: https://github.com/hukusuke1007/stop_watch_timer

Expand All @@ -10,7 +10,7 @@ environment:
dependencies:
flutter:
sdk: flutter
rxdart: ^0.27.0
rxdart: ^0.27.1

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit ec09ab0

Please sign in to comment.