-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathstop_watch_timer.dart
429 lines (371 loc) Β· 11.8 KB
/
stop_watch_timer.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import 'dart:async';
import 'dart:math';
import 'package:rxdart/rxdart.dart';
/// StopWatchRecord
class StopWatchRecord {
StopWatchRecord({
this.rawValue,
this.hours,
this.minute,
this.second,
this.displayTime,
});
StopWatchRecord.fromRawValue(this.rawValue, {bool isLapHours = true})
: hours =
(rawValue != null) ? StopWatchTimer.getRawHours(rawValue) : null,
minute =
(rawValue != null) ? StopWatchTimer.getRawMinute(rawValue) : null,
second =
(rawValue != null) ? StopWatchTimer.getRawSecond(rawValue) : null,
displayTime = (rawValue != null)
? StopWatchTimer.getDisplayTime(
rawValue,
hours: isLapHours,
)
: null;
int? rawValue;
int? hours;
int? minute;
int? second;
String? displayTime;
@override
String toString() {
return 'h: $hours, m: $minute, s: $second, ms: $rawValue ($displayTime)';
}
}
/// StopWatch ExecuteType
enum StopWatchExecute { start, stop, reset, lap }
/// StopWatchMode
enum StopWatchMode { countUp, countDown }
/// StopWatchTimer
class StopWatchTimer {
StopWatchTimer({
this.isLapHours = true,
this.mode = StopWatchMode.countUp,
int presetMillisecond = 0,
this.refreshTime = 1,
this.onChange,
this.onChangeRawSecond,
this.onChangeRawMinute,
this.onStopped,
this.onEnded,
}) {
/// Set presetTime
_presetTime = presetMillisecond;
_initialPresetTime = presetMillisecond;
if (mode == StopWatchMode.countDown) {
_second = getRawSecond(presetMillisecond);
_minute = getRawMinute(presetMillisecond);
}
_rawTimeController = BehaviorSubject<int>.seeded(presetMillisecond);
_secondTimeController =
BehaviorSubject<int>.seeded(getRawSecond(presetMillisecond));
_minuteTimeController =
BehaviorSubject<int>.seeded(getRawMinute(presetMillisecond));
_elapsedTime.listen((value) {
_rawTimeController.add(value);
onChange?.call(value);
final latestSecond = getRawSecond(value);
if (_second != latestSecond) {
_secondTimeController.add(latestSecond);
_second = latestSecond;
onChangeRawSecond?.call(latestSecond);
}
final latestMinute = getRawMinute(value);
if (_minute != latestMinute) {
_minuteTimeController.add(latestMinute);
_minute = latestMinute;
onChangeRawMinute?.call(latestMinute);
}
});
}
final bool isLapHours;
final StopWatchMode mode;
final int refreshTime;
final void Function(int)? onChange;
final void Function(int)? onChangeRawSecond;
final void Function(int)? onChangeRawMinute;
final void Function()? onStopped;
final void Function()? onEnded;
final PublishSubject<int> _elapsedTime = PublishSubject<int>();
late BehaviorSubject<int> _rawTimeController;
ValueStream<int> get rawTime => _rawTimeController;
late BehaviorSubject<int> _secondTimeController;
ValueStream<int> get secondTime => _secondTimeController;
late BehaviorSubject<int> _minuteTimeController;
ValueStream<int> get minuteTime => _minuteTimeController;
final BehaviorSubject<List<StopWatchRecord>> _recordsController =
BehaviorSubject<List<StopWatchRecord>>.seeded([]);
ValueStream<List<StopWatchRecord>> get records => _recordsController;
final PublishSubject<bool> _onStoppedController = PublishSubject<bool>();
Stream<bool> get fetchStopped => _onStoppedController;
final PublishSubject<bool> _onEndedController = PublishSubject<bool>();
Stream<bool> get fetchEnded => _onEndedController;
bool get isRunning => _timer != null && _timer!.isActive;
int get initialPresetTime => _initialPresetTime;
/// Private
Timer? _timer;
/// Stores the [DateTime] moment in which the current count session
/// started.
int _currentSessionStartTime = 0;
/// Stores the sum of all previous count sessions.
/// ## Caveats
/// - If the counter is stopped, there is no current session in progress.
int _previousTotalSessionTime = 0;
late int _presetTime;
int _second = 0;
int _minute = 0;
List<StopWatchRecord> _records = [];
late int _initialPresetTime;
/// Get display time.
static String getDisplayTime(
int value, {
bool hours = true,
bool minute = true,
bool second = true,
bool milliSecond = true,
String hoursRightBreak = ':',
String minuteRightBreak = ':',
String secondRightBreak = '.',
}) {
final hoursStr = getDisplayTimeHours(value);
final mStr = getDisplayTimeMinute(value, hours: hours);
final sStr = getDisplayTimeSecond(value);
final msStr = getDisplayTimeMillisecond(value);
final result = <String>[];
if (hours) {
result.add(hoursStr);
}
if (minute) {
if (hours) {
result.add(hoursRightBreak);
}
result.add(mStr);
}
if (second) {
if (minute) {
result.add(minuteRightBreak);
}
result.add(sStr);
}
if (milliSecond) {
if (second) {
result.add(secondRightBreak);
}
result.add(msStr);
}
return result.join();
}
/// Get display hours time.
static String getDisplayTimeHours(int mSec) {
return getRawHours(mSec).toString().padLeft(2, '0');
}
/// Get display minute time.
static String getDisplayTimeMinute(int mSec, {bool hours = false}) {
if (hours) {
return getMinute(mSec).toString().padLeft(2, '0');
} else {
return getRawMinute(mSec).toString().padLeft(2, '0');
}
}
/// Get display second time.
static String getDisplayTimeSecond(int mSec) {
final s = (mSec % 60000 / 1000).floor();
return s.toString().padLeft(2, '0');
}
/// Get display millisecond time.
static String getDisplayTimeMillisecond(int mSec) {
final ms = (mSec % 1000 / 10).floor();
return ms.toString().padLeft(2, '0');
}
/// Get Raw Hours.
static int getRawHours(int milliSecond) =>
(milliSecond / (3600 * 1000)).floor();
/// Get Raw Minute. 0 ~ 59. 1 hours = 0.
static int getMinute(int milliSecond) =>
(milliSecond / (60 * 1000) % 60).floor();
/// Get Raw Minute
static int getRawMinute(int milliSecond) => (milliSecond / 60000).floor();
/// Get Raw Second
static int getRawSecond(int milliSecond) => (milliSecond / 1000).floor();
/// Get milli second from hour
static int getMilliSecFromHour(int hour) => hour * (3600 * 1000);
/// Get milli second from minute
static int getMilliSecFromMinute(int minute) => minute * 60000;
/// Get milli second from second
static int getMilliSecFromSecond(int second) => second * 1000;
/// When finish running timer, it need to dispose.
Future<void> dispose() async {
if (_elapsedTime.isClosed) {
throw Exception(
'This instance is already disposed. Please create timer object.',
);
}
final timer = _timer;
if (timer != null && timer.isActive) {
timer.cancel();
}
// Make sure elapsed time is closed before rawTimeController. This avoids
// failure when command `flutter test -x slow` is used to execute the tests.
// Otherwise, the subscriptions must be canceled before disposing the
// watch.
await _elapsedTime.close();
await Future.wait<void>([
_rawTimeController.close(),
_secondTimeController.close(),
_minuteTimeController.close(),
_recordsController.close(),
_onStoppedController.close(),
_onEndedController.close(),
]);
}
/// Start timer.
void onStartTimer() => _start();
/// Stop timer.
void onStopTimer() => _stop();
/// Reset timer.
void onResetTimer() => _reset();
/// Add Lap.
void onAddLap() => _lap();
/// Get display millisecond time.
void setPresetHoursTime(int value, {bool add = true}) =>
setPresetTime(mSec: value * 3600 * 1000, add: add);
void setPresetMinuteTime(int value, {bool add = true}) =>
setPresetTime(mSec: value * 60 * 1000, add: add);
void setPresetSecondTime(int value, {bool add = true}) =>
setPresetTime(mSec: value * 1000, add: add);
/// Set preset time. 1000 mSec => 1 sec
void setPresetTime({required int mSec, bool add = true}) {
switch (mode) {
case StopWatchMode.countUp:
final currentTotalTime = _getCountUpTime();
final currentTimeWithoutPreset = currentTotalTime - _presetTime;
if (add) {
if (mSec < 0 && currentTotalTime + mSec <= 0) {
// total time will be 0
_presetTime = -currentTimeWithoutPreset;
} else {
_presetTime += mSec;
}
} else {
if (mSec < 0 && currentTimeWithoutPreset + mSec < 0) {
_presetTime = -currentTimeWithoutPreset;
} else {
_presetTime = mSec;
}
}
break;
case StopWatchMode.countDown:
final currentRemainingTime = _getCountDownTime();
final currentElapsedTime = _presetTime - currentRemainingTime;
if (add) {
if (mSec < 0 && currentRemainingTime + mSec <= 0) {
// total time will be 0
_presetTime = currentElapsedTime;
} else {
_presetTime += mSec;
}
} else {
if (mSec < 0 && currentElapsedTime + mSec < 0) {
_presetTime = currentElapsedTime;
} else {
_presetTime = mSec;
}
}
break;
}
_elapsedTime.add(
mode == StopWatchMode.countUp ? _getCountUpTime() : _getCountDownTime(),
);
}
void clearPresetTime() {
_presetTime = _initialPresetTime;
switch (mode) {
case StopWatchMode.countUp:
_elapsedTime.add(_getCountUpTime());
break;
case StopWatchMode.countDown:
_elapsedTime.add(_getCountDownTime());
break;
}
}
void _handle(Timer timer) {
switch (mode) {
case StopWatchMode.countUp:
_elapsedTime.add(_getCountUpTime());
break;
case StopWatchMode.countDown:
final time = _getCountDownTime();
_elapsedTime.add(time);
if (time == 0) {
_stop();
_onEndedController.add(true);
onEnded?.call();
}
break;
}
}
int _getCountUpTime() =>
_getCurrentSessionTime() + _previousTotalSessionTime + _presetTime;
int _getCountDownTime() => max(
_presetTime - (_getCurrentSessionTime() + _previousTotalSessionTime),
0,
);
int _getCurrentSessionTime() => isRunning
? DateTime.now().millisecondsSinceEpoch - _currentSessionStartTime
: 0;
void _start() {
if (!isRunning) {
_currentSessionStartTime = DateTime.now().millisecondsSinceEpoch;
_timer = Timer.periodic(Duration(milliseconds: refreshTime), _handle);
}
}
bool _stop() {
if (!isRunning) {
return false;
}
_timer?.cancel();
_timer = null;
_previousTotalSessionTime +=
DateTime.now().millisecondsSinceEpoch - _currentSessionStartTime;
_onStoppedController.add(true);
onStopped?.call();
return true;
}
void _reset() {
if (isRunning) {
_timer?.cancel();
_timer = null;
}
if (isRunning && _currentSessionStartTime > 0) {
_onStoppedController.add(true);
onStopped?.call();
_onEndedController.add(true);
onEnded?.call();
}
_currentSessionStartTime = 0;
_previousTotalSessionTime = 0;
_records = [];
if (!_recordsController.isClosed) {
_recordsController.add(_records);
}
if (!_elapsedTime.isClosed) {
_elapsedTime.add(_presetTime);
}
}
void _lap() {
if (isRunning) {
final rawValue = _rawTimeController.value;
_records.add(
StopWatchRecord(
rawValue: rawValue,
hours: getRawHours(rawValue),
minute: getRawMinute(rawValue),
second: getRawSecond(rawValue),
displayTime: getDisplayTime(rawValue, hours: isLapHours),
),
);
_recordsController.add(_records);
}
}
}