Skip to content

Commit

Permalink
Improve format audtio duration. (MixinNetwork#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
YeungKC authored Nov 5, 2021
1 parent 316e596 commit 05976af
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
1 change: 1 addition & 0 deletions lib/utils/extension/extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@ part 'src/stream.dart';
part 'src/string.dart';
part 'src/ui.dart';
part 'src/info.dart';
part 'src/duration.dart';

void importExtension() {}
11 changes: 11 additions & 0 deletions lib/utils/extension/src/duration.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
part of '../extension.dart';

extension DurationToMinutesSecondsExtension on Duration {
String get asMinutesSeconds {
var duration = this;
if (inMilliseconds < 1000) {
duration = const Duration(milliseconds: 1000);
}
return '${duration.inMinutes.toString().padLeft(2, '0')}:${duration.inSeconds.remainder(60).toString().padLeft(2, '0')}';
}
}
2 changes: 1 addition & 1 deletion lib/widgets/message/item/audio_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class AudioMessage extends HookWidget {
),
const SizedBox(height: 8),
Text(
'${duration.inSeconds}‘',
duration.asMinutesSeconds,
style: TextStyle(
fontSize: MessageItemWidget.tertiaryFontSize,
color: context.theme.secondaryText,
Expand Down
11 changes: 5 additions & 6 deletions lib/widgets/message/item/video_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,11 @@ class VideoMessageWidget extends HookWidget {
),
HookBuilder(builder: (context) {
final durationText = useMessageConverter(
converter: (state) => formatVideoDuration(Duration(
milliseconds:
int.tryParse(state.mediaDuration ?? '') ?? 0)),
converter: (state) => Duration(
milliseconds:
int.tryParse(state.mediaDuration ?? '') ??
0)
.asMinutesSeconds,
);

return Positioned(
Expand Down Expand Up @@ -182,6 +184,3 @@ class VideoMessageWidget extends HookWidget {
);
}
}

String formatVideoDuration(Duration duration) =>
'${duration.inMinutes.toString().padLeft(2, '0')}:${duration.inSeconds.remainder(60).toString().padLeft(2, '0')}';
17 changes: 10 additions & 7 deletions test/widgets/message/video_message_test.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import 'package:flutter_app/widgets/message/item/video_message.dart';
import 'package:flutter_app/utils/extension/extension.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
test('test format video duration', () {
var duration = const Duration(minutes: 12, seconds: 20);
expect(formatVideoDuration(duration), '12:20');
expect(duration.asMinutesSeconds, '12:20');

duration = const Duration(seconds: 20);
expect(formatVideoDuration(duration), '00:20');
expect(duration.asMinutesSeconds, '00:20');

duration = const Duration(seconds: 120);
expect(formatVideoDuration(duration), '02:00');
expect(duration.asMinutesSeconds, '02:00');

duration = const Duration(minutes: 1200, seconds: 20);
expect(formatVideoDuration(duration), '1200:20');
expect(duration.asMinutesSeconds, '1200:20');

duration = const Duration(minutes: 120, seconds: 20);
expect(formatVideoDuration(duration), '120:20');
expect(duration.asMinutesSeconds, '120:20');

duration = const Duration(seconds: 121);
expect(formatVideoDuration(duration), '02:01');
expect(duration.asMinutesSeconds, '02:01');

duration = const Duration(milliseconds: 999);
expect(duration.asMinutesSeconds, '00:01');
});
}

0 comments on commit 05976af

Please sign in to comment.