Skip to content

Commit d1db389

Browse files
committed
fix: cache status not updated
1 parent d56dd91 commit d1db389

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

lib/ui/screens/info_sheet.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@ class AlbumInfoPane extends StatelessWidget {
251251
}
252252
}
253253

254-
showInfoSheet(BuildContext context, {required Song song}) {
255-
showModalBottomSheet(
254+
Future<void> showInfoSheet(BuildContext context, {required Song song}) {
255+
return showModalBottomSheet<void>(
256256
context: context,
257257
isScrollControlled: true,
258258
backgroundColor: Colors.transparent,

lib/ui/screens/now_playing.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class NowPlayingScreen extends StatelessWidget {
8484
],
8585
),
8686
const SizedBox(height: 8),
87-
const ProgressBar(),
87+
ProgressBar(song: song),
8888
],
8989
);
9090

lib/ui/widgets/now_playing/progress_bar.dart

+16-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
import 'package:app/extensions/assets_audio_player.dart';
21
import 'package:app/extensions/duration.dart';
32
import 'package:app/mixins/stream_subscriber.dart';
3+
import 'package:app/models/song.dart';
44
import 'package:app/providers/audio_provider.dart';
5-
import 'package:app/providers/song_provider.dart';
65
import 'package:flutter/material.dart';
76
import 'package:provider/provider.dart';
87

98
class ProgressBar extends StatefulWidget {
10-
const ProgressBar({Key? key}) : super(key: key);
9+
final Song song;
10+
11+
const ProgressBar({Key? key, required this.song}) : super(key: key);
1112

1213
_ProgressBarState createState() => _ProgressBarState();
1314
}
1415

1516
class _ProgressBarState extends State<ProgressBar> with StreamSubscriber {
1617
late final AudioProvider audio;
17-
late final SongProvider songProvider;
18-
1918
late Duration _duration, _position;
2019

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

32-
setState(() {
33-
_duration = Duration(
34-
seconds: songProvider.byId(audio.player.songId!).length.toInt(),
35-
);
36-
});
30+
setState(() => _duration = Duration(seconds: widget.song.length.toInt()));
3731

3832
subscribe(audio.player.currentPosition.listen((position) {
3933
setState(() => _position = position);
4034
}));
4135
}
4236

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

lib/ui/widgets/song_cache_icon.dart

+16
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ class _SongCacheIconState extends State<SongCacheIcon> with StreamSubscriber {
3939
});
4040
}
4141

42+
/// Since this widget is rendered inside NowPlayingScreen, change to current
43+
/// song in the parent will not trigger initState() and as a result not
44+
/// refresh the song's cache status.
45+
/// For that, we hook into didUpdateWidget().
46+
/// See https://stackoverflow.com/questions/54759920/flutter-why-is-child-widgets-initstate-is-not-called-on-every-rebuild-of-pa.
47+
@override
48+
void didUpdateWidget(covariant SongCacheIcon oldWidget) {
49+
super.didUpdateWidget(oldWidget);
50+
_resolveCacheStatus();
51+
}
52+
53+
Future<void> _resolveCacheStatus() async {
54+
bool hasState = await cache.hasCache(song: widget.song);
55+
setState(() => _hasCache = hasState);
56+
}
57+
4258
@override
4359
void dispose() {
4460
unsubscribeAll();

0 commit comments

Comments
 (0)