AppFlowy/frontend/appflowy_flutter/lib/util/time.dart
Mohammad Zolfaghari aa621a8d84
feat: timer field (#5349)
* feat: wip timer field

* feat: timer field fixing errors

* feat: wip timer field frontend

* fix: parsing TimerCellDataPB

* feat: parse time string to minutes

* fix: don't allow none number input

* fix: timer filter

* style: cargo fmt

* fix: clippy errors

* refactor: rename field type timer to time

* refactor: missed some variable and files to rename

* style: cargo fmt fix

* feat: format time field type data in frontend

* style: fix cargo fmt

* fix: fixes after merge

---------

Co-authored-by: Mathias Mogensen <mathiasrieckm@gmail.com>
2024-06-13 08:52:13 +02:00

44 lines
1.0 KiB
Dart

final RegExp timerRegExp =
RegExp(r'(?:(?<hours>\d*)h)? ?(?:(?<minutes>\d*)m)?');
int? parseTime(String timerStr) {
int? res = int.tryParse(timerStr);
if (res != null) {
return res;
}
final matches = timerRegExp.firstMatch(timerStr);
if (matches == null) {
return null;
}
final hours = int.tryParse(matches.namedGroup('hours') ?? "");
final minutes = int.tryParse(matches.namedGroup('minutes') ?? "");
if (hours == null && minutes == null) {
return null;
}
final expected =
"${hours != null ? '${hours}h' : ''}${hours != null && minutes != null ? ' ' : ''}${minutes != null ? '${minutes}m' : ''}";
if (timerStr != expected) {
return null;
}
res = 0;
res += hours != null ? hours * 60 : res;
res += minutes ?? 0;
return res;
}
String formatTime(int minutes) {
if (minutes >= 60) {
if (minutes % 60 == 0) {
return "${minutes ~/ 60}h";
}
return "${minutes ~/ 60}h ${minutes % 60}m";
} else if (minutes >= 0) {
return "${minutes}m";
}
return "";
}