Skip to content

Commit

Permalink
fix: cache status not updated
Browse files Browse the repository at this point in the history
  • Loading branch information
phanan committed Aug 8, 2021
1 parent d56dd91 commit d1db389
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
4 changes: 2 additions & 2 deletions lib/ui/screens/info_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ class AlbumInfoPane extends StatelessWidget {
}
}

showInfoSheet(BuildContext context, {required Song song}) {
showModalBottomSheet(
Future<void> showInfoSheet(BuildContext context, {required Song song}) {
return showModalBottomSheet<void>(
context: context,
isScrollControlled: true,
backgroundColor: Colors.transparent,
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/screens/now_playing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class NowPlayingScreen extends StatelessWidget {
],
),
const SizedBox(height: 8),
const ProgressBar(),
ProgressBar(song: song),
],
);

Expand Down
27 changes: 16 additions & 11 deletions lib/ui/widgets/now_playing/progress_bar.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import 'package:app/extensions/assets_audio_player.dart';
import 'package:app/extensions/duration.dart';
import 'package:app/mixins/stream_subscriber.dart';
import 'package:app/models/song.dart';
import 'package:app/providers/audio_provider.dart';
import 'package:app/providers/song_provider.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class ProgressBar extends StatefulWidget {
const ProgressBar({Key? key}) : super(key: key);
final Song song;

const ProgressBar({Key? key, required this.song}) : super(key: key);

_ProgressBarState createState() => _ProgressBarState();
}

class _ProgressBarState extends State<ProgressBar> with StreamSubscriber {
late final AudioProvider audio;
late final SongProvider songProvider;

late Duration _duration, _position;

TextStyle timeStampStyle = const TextStyle(
Expand All @@ -27,19 +26,25 @@ class _ProgressBarState extends State<ProgressBar> with StreamSubscriber {
void initState() {
super.initState();
audio = context.read();
songProvider = context.read();

setState(() {
_duration = Duration(
seconds: songProvider.byId(audio.player.songId!).length.toInt(),
);
});
setState(() => _duration = Duration(seconds: widget.song.length.toInt()));

subscribe(audio.player.currentPosition.listen((position) {
setState(() => _position = position);
}));
}

/// Since this widget is rendered inside NowPlayingScreen, change to current
/// song in the parent will not trigger initState() and as a result not
/// refresh the progress bar's values.
/// For that, we hook into didUpdateWidget().
/// See https://stackoverflow.com/questions/54759920/flutter-why-is-child-widgets-initstate-is-not-called-on-every-rebuild-of-pa.
@override
void didUpdateWidget(covariant ProgressBar oldWidget) {
super.didUpdateWidget(oldWidget);
setState(() => _duration = Duration(seconds: widget.song.length.toInt()));
}

@override
void dispose() {
unsubscribeAll();
Expand Down
16 changes: 16 additions & 0 deletions lib/ui/widgets/song_cache_icon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ class _SongCacheIconState extends State<SongCacheIcon> with StreamSubscriber {
});
}

/// Since this widget is rendered inside NowPlayingScreen, change to current
/// song in the parent will not trigger initState() and as a result not
/// refresh the song's cache status.
/// For that, we hook into didUpdateWidget().
/// See https://stackoverflow.com/questions/54759920/flutter-why-is-child-widgets-initstate-is-not-called-on-every-rebuild-of-pa.
@override
void didUpdateWidget(covariant SongCacheIcon oldWidget) {
super.didUpdateWidget(oldWidget);
_resolveCacheStatus();
}

Future<void> _resolveCacheStatus() async {
bool hasState = await cache.hasCache(song: widget.song);
setState(() => _hasCache = hasState);
}

@override
void dispose() {
unsubscribeAll();
Expand Down

0 comments on commit d1db389

Please sign in to comment.