Skip to content

Commit

Permalink
get music duration from presenter to represent in screen
Browse files Browse the repository at this point in the history
  • Loading branch information
MateusHBR committed Jul 3, 2021
1 parent 8539329 commit 30531e7
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class RxDartMusicPlayerPresenter implements MusicPlayerPresenter {
AudioPlayer? _audioPlayer;

@override
Stream<MusicPlayerState> get stateOfSongThatIsPlaying =>
Stream<MusicPlayerState> get stateOfSongThatIsPlayingStream =>
_audioPlayer!.playerStateStream
.map(
(audioPlayerEvent) =>
Expand All @@ -16,7 +16,24 @@ class RxDartMusicPlayerPresenter implements MusicPlayerPresenter {
.distinct();

@override
Stream<double> get volume => _audioPlayer!.volumeStream;
Stream<double> get volumeStream => _audioPlayer!.volumeStream;

@override
Stream<Duration> get musicDurationStream =>
_audioPlayer!.durationStream.map((newTimeEvent) {
if (newTimeEvent == null) {
return Duration(seconds: 0, milliseconds: 0);
}

return newTimeEvent;
});

double get musicDurationInSeconds =>
_audioPlayer!.duration?.inSeconds.toDouble() ?? 0.0;

@override
Stream<Duration> get currentPositionAtMusicStream =>
_audioPlayer!.positionStream;

@override
void initState(String musicPath) {
Expand Down
61 changes: 53 additions & 8 deletions lib/ui/pages/music_player/music_player_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,7 @@ class _MusicPlayerPageState extends State<MusicPlayerPage> {
const VerticalSpacing(27),
_musicNameAndBand(),
const VerticalSpacing(26),
Slider(
value: 0.5,
onChanged: (_) {},
inactiveColor: values.KColors.white.withOpacity(0.38),
activeColor: values.KColors.white,
),
_musicProgressBar(),
const VerticalSpacing(22),
_musicPayerActions(),
const VerticalSpacing(22),
Expand Down Expand Up @@ -153,6 +148,56 @@ class _MusicPlayerPageState extends State<MusicPlayerPage> {
);
}

Widget _musicProgressBar() {
return Column(
children: [
StreamBuilder<Duration>(
stream: presenter.currentPositionAtMusicStream,
initialData: Duration.zero,
builder: (context, snapshot) {
final currentPositionInSeconds =
snapshot.data!.inSeconds.toDouble();

return Slider(
value: currentPositionInSeconds,
max: presenter.musicDurationInSeconds.toDouble(),
onChanged: (_) {},
inactiveColor: values.KColors.white.withOpacity(0.38),
activeColor: values.KColors.white,
);
}),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
StreamBuilder<Duration>(
stream: presenter.currentPositionAtMusicStream,
initialData: Duration.zero,
builder: (context, snapshot) {
final currentPositionAtMusicTime = snapshot.data!;
return Text(
currentPositionAtMusicTime.inSeconds.toString(),
style: values.TextStyles.caption(),
);
},
),
StreamBuilder<Duration>(
stream: presenter.musicDurationStream,
initialData: Duration.zero,
builder: (context, snapshot) {
final musicDuration = snapshot.data!;

return Text(
musicDuration.inSeconds.toString(),
style: values.TextStyles.caption(),
);
},
),
],
),
],
);
}

Widget _musicPayerActions() {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
Expand All @@ -176,7 +221,7 @@ class _MusicPlayerPageState extends State<MusicPlayerPage> {

Widget _playButton() {
return StreamBuilder<MusicPlayerState>(
stream: presenter.stateOfSongThatIsPlaying,
stream: presenter.stateOfSongThatIsPlayingStream,
builder: (context, snapshot) {
final playerState = snapshot.data ?? MusicPlayerState.initial();

Expand Down Expand Up @@ -243,7 +288,7 @@ class _MusicPlayerPageState extends State<MusicPlayerPage> {

Widget _volumeSlider() {
return StreamBuilder<double>(
stream: presenter.volume,
stream: presenter.volumeStream,
initialData: 1.0,
builder: (context, snapshot) {
final volume = snapshot.data!;
Expand Down
7 changes: 5 additions & 2 deletions lib/ui/pages/music_player/music_player_presenter.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'package:music_player/ui/models/states/states.dart';

abstract class MusicPlayerPresenter {
Stream<MusicPlayerState> get stateOfSongThatIsPlaying;
Stream<double> get volume;
Stream<MusicPlayerState> get stateOfSongThatIsPlayingStream;
Stream<double> get volumeStream;
Stream<Duration> get musicDurationStream;
double get musicDurationInSeconds;
Stream<Duration> get currentPositionAtMusicStream;

void initState(String musicPath);
void dispose();
Expand Down

0 comments on commit 30531e7

Please sign in to comment.