From f1712646638eea45d8b2ac36b44ca5eed73cfb9f Mon Sep 17 00:00:00 2001 From: Marcelo Amaro Date: Fri, 13 Oct 2023 11:23:01 -0300 Subject: [PATCH] feat: transform isInitialized into an observable in DSAudioPlayerController --- .../chat/ds_audio_player.controller.dart | 3 +- .../chat/audio/ds_audio_player.widget.dart | 55 +++++++++++-------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/lib/src/controllers/chat/ds_audio_player.controller.dart b/lib/src/controllers/chat/ds_audio_player.controller.dart index df88bad7..0f12ee37 100644 --- a/lib/src/controllers/chat/ds_audio_player.controller.dart +++ b/lib/src/controllers/chat/ds_audio_player.controller.dart @@ -5,8 +5,7 @@ import 'package:rxdart/rxdart.dart' as rx_dart; class DSAudioPlayerController extends GetxController { final audioSpeed = RxDouble(1.0); final player = AudioPlayer(); - - bool isInitialized = false; + final isInitialized = RxBool(false); /// Collects the data useful for displaying in a SeekBar widget. /// diff --git a/lib/src/widgets/chat/audio/ds_audio_player.widget.dart b/lib/src/widgets/chat/audio/ds_audio_player.widget.dart index 7ad7d55b..ef01775a 100644 --- a/lib/src/widgets/chat/audio/ds_audio_player.widget.dart +++ b/lib/src/widgets/chat/audio/ds_audio_player.widget.dart @@ -126,9 +126,9 @@ class _DSAudioPlayerState extends State ), ); - _controller.isInitialized = true; + _controller.isInitialized.value = true; } catch (_) { - _controller.isInitialized = false; + _controller.isInitialized.value = false; } } @@ -176,15 +176,17 @@ class _DSAudioPlayerState extends State final processingState = playerState?.processingState; final playing = playerState?.playing; if (playing != true) { - return DSPlayButton( - onPressed: _controller.isInitialized - ? _controller.player.play - : () => {}, - isLoading: [ProcessingState.loading, ProcessingState.buffering] - .contains(processingState), - color: _controller.isInitialized - ? widget.controlForegroundColor - : DSColors.contentDisable, + return Obx( + () => DSPlayButton( + onPressed: _controller.isInitialized.value + ? _controller.player.play + : () => {}, + isLoading: [ProcessingState.loading, ProcessingState.buffering] + .contains(processingState), + color: _controller.isInitialized.value + ? widget.controlForegroundColor + : DSColors.contentDisable, + ), ); } else if (processingState != ProcessingState.completed) { return DSPauseButton( @@ -202,19 +204,24 @@ class _DSAudioPlayerState extends State stream: _controller.positionDataStream, builder: (context, snapshot) { final positionData = snapshot.data; - return DSAudioSeekBar( - duration: positionData?.duration ?? Duration.zero, - position: positionData?.position ?? Duration.zero, - bufferedPosition: positionData?.bufferedPosition ?? Duration.zero, - onChangeEnd: - _controller.isInitialized ? _controller.player.play : null, - onChanged: _controller.isInitialized ? _controller.player.seek : null, - onChangeStart: _controller.player.pause, - labelColor: widget.labelColor, - bufferActiveTrackColor: widget.bufferActiveTrackColor, - bufferInactiveTrackColor: widget.bufferInactiveTrackColor, - sliderActiveTrackColor: widget.sliderActiveTrackColor, - sliderThumbColor: widget.sliderThumbColor, + return Obx( + () => DSAudioSeekBar( + duration: positionData?.duration ?? Duration.zero, + position: positionData?.position ?? Duration.zero, + bufferedPosition: positionData?.bufferedPosition ?? Duration.zero, + onChangeEnd: _controller.isInitialized.value + ? _controller.player.play + : null, + onChanged: _controller.isInitialized.value + ? _controller.player.seek + : null, + onChangeStart: _controller.player.pause, + labelColor: widget.labelColor, + bufferActiveTrackColor: widget.bufferActiveTrackColor, + bufferInactiveTrackColor: widget.bufferInactiveTrackColor, + sliderActiveTrackColor: widget.sliderActiveTrackColor, + sliderThumbColor: widget.sliderThumbColor, + ), ); }, );