Skip to content

Commit c2d768c

Browse files
authored
chore: unobserve property on dispose in radioservice, watch podcast updates in podcast page (#1367)
1 parent fbd6a9e commit c2d768c

11 files changed

+79
-42
lines changed

lib/player/observe_property.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export 'observe_property_stub.dart'
2+
if (dart.library.html) 'observe_property_web.dart'
3+
if (dart.library.io) 'observe_property_io.dart';
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import 'package:media_kit/media_kit.dart';
2+
3+
import '../common/logging.dart';
4+
5+
Future<void> observeProperty({
6+
required String property,
7+
required Player player,
8+
Future<void> Function(String)? listener,
9+
}) async {
10+
NativePlayer? nativePlayer;
11+
try {
12+
nativePlayer = player.platform as NativePlayer;
13+
} on Exception catch (e) {
14+
printMessageInDebugMode(e);
15+
}
16+
17+
if (nativePlayer == null) {
18+
return Future.value();
19+
}
20+
21+
if (listener == null) {
22+
if (nativePlayer.observed.containsKey(property)) {
23+
return nativePlayer.unobserveProperty(property);
24+
}
25+
} else if (!nativePlayer.observed.containsKey(property)) {
26+
return nativePlayer.observeProperty(property, listener);
27+
}
28+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import 'package:media_kit/media_kit.dart';
2+
3+
Future<void> observeProperty({
4+
required String property,
5+
required Player player,
6+
Future<void> Function(String)? listener,
7+
}) async => throw UnimplementedError();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import 'package:media_kit/media_kit.dart';
2+
3+
Future<void> observeProperty({
4+
required String property,
5+
required Player player,
6+
Future<void> Function(String)? listener,
7+
}) async {}

lib/player/player_service.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ class PlayerService {
425425

426426
String? _remoteImageUrl;
427427
String? get remoteImageUrl => _remoteImageUrl;
428-
Future<void> setRemoteImageUrl(String? url) async {
428+
void setRemoteImageUrl(String? url) {
429429
if (_color != null) {
430430
_setColor(null);
431431
}

lib/player/view/audio_visualizer.dart

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,15 @@ class _AudioVisualizerState extends State<AudioVisualizer> {
6363

6464
@override
6565
Widget build(BuildContext context) {
66-
final color = watchPropertyValue(
67-
(PlayerModel m) => m.color ?? context.colorScheme.primary,
68-
);
69-
return Center(
70-
child: AnimatedOpacity(
71-
opacity: color != context.colorScheme.primary ? 0.9 : 1,
72-
duration: const Duration(milliseconds: 300),
73-
child: WaveformWidget(
74-
controller: _controller,
75-
height: widget.height,
76-
style: WaveformStyle(
77-
waveColor: color,
78-
backgroundColor: Colors.transparent,
79-
waveformStyle: WaveformDrawStyle.bars,
80-
),
81-
),
66+
final color = watchPropertyValue((PlayerModel m) => m.color);
67+
return WaveformWidget(
68+
controller: _controller,
69+
height: widget.height,
70+
style: WaveformStyle(
71+
waveColor: color ?? context.colorScheme.primary,
72+
barCount: 35,
73+
backgroundColor: Colors.transparent,
74+
waveformStyle: WaveformDrawStyle.bars,
8275
),
8376
);
8477
}

lib/player/view/full_height_player_image.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,9 @@ class FullHeightPlayerImage extends StatelessWidget with WatchItMixin {
3030
@override
3131
Widget build(BuildContext context) {
3232
final audio = watchPropertyValue((PlayerModel m) => m.audio);
33-
final showAudioVisualizer =
34-
this.showAudioVisualizer &&
35-
watchPropertyValue((PlayerModel m) => m.showAudioVisualizer);
36-
37-
if (showAudioVisualizer) {
38-
return AudioVisualizer(height: height ?? 400);
39-
}
33+
final showAudioVisualizer = watchPropertyValue(
34+
(PlayerModel m) => m.showAudioVisualizer && this.showAudioVisualizer,
35+
);
4036

4137
final size = context.isPortrait
4238
? fullHeightPlayerImageSize
@@ -74,6 +70,10 @@ class FullHeightPlayerImage extends StatelessWidget with WatchItMixin {
7470
);
7571
}
7672

73+
if (showAudioVisualizer) {
74+
return AudioVisualizer(height: height ?? 200);
75+
}
76+
7777
return SizedBox(
7878
height: theHeight,
7979
width: theWidth,

lib/player/view/player_color.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ class PlayerColor extends StatelessWidget with WatchItMixin {
2929
final blurredPlayerBackground = watchPropertyValue(
3030
(SettingsModel m) => m.blurredPlayerBackground,
3131
);
32+
final showAudioVisualizer = watchPropertyValue(
33+
(PlayerModel m) => m.showAudioVisualizer,
34+
);
3235

33-
if (blurredPlayerBackground) {
36+
if (!showAudioVisualizer && blurredPlayerBackground) {
3437
return _BlurredPlayerColor(size: size);
3538
}
3639

@@ -80,7 +83,6 @@ class _BlurredPlayerColor extends StatelessWidget {
8083
? Colors.white
8184
: theme.scaffoldBackgroundColor,
8285
child: FullHeightPlayerImage(
83-
showAudioVisualizer: false,
8486
emptyFallBack: true,
8587
borderRadius: BorderRadius.zero,
8688
fit: BoxFit.cover,

lib/podcasts/view/podcast_page.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class PodcastPage extends StatelessWidget with WatchItMixin {
4343
final showPodcastsAscending = watchPropertyValue(
4444
(LibraryModel m) => m.showPodcastAscending(feedUrl),
4545
);
46+
watchPropertyValue((LibraryModel m) => m.podcastUpdatesLength);
4647

4748
watchPropertyValue(
4849
(PodcastModel m) => m.getPodcastEpisodesFromCache(feedUrl)?.length,

lib/radio/radio_service.dart

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'dart:async';
22

33
import 'package:basic_utils/basic_utils.dart';
4-
import 'package:media_kit/media_kit.dart';
54
import 'package:radio_browser_api/radio_browser_api.dart';
65

76
import '../common/data/audio.dart';
@@ -10,6 +9,7 @@ import '../common/data/mpv_meta_data.dart';
109
import '../common/logging.dart';
1110
import '../expose/expose_service.dart';
1211
import '../extensions/string_x.dart';
12+
import '../player/observe_property_io.dart';
1313
import '../player/player_service.dart';
1414
import 'online_art_service.dart';
1515

@@ -35,9 +35,10 @@ class RadioService {
3535

3636
Future<void> init({bool observePlayer = true}) async {
3737
if (observePlayer) {
38-
await (_playerService.player.platform as NativePlayer).observeProperty(
39-
'metadata',
40-
_onMpvMetadata,
38+
await observeProperty(
39+
property: 'metadata',
40+
player: _playerService.player,
41+
listener: _onMpvMetadata,
4142
);
4243
}
4344

@@ -262,9 +263,8 @@ class RadioService {
262263

263264
Future<void> dispose() async {
264265
await _propertiesChangedController.close();
265-
await (_playerService.player.platform as NativePlayer).unobserveProperty(
266-
'metadata',
267-
);
266+
267+
await observeProperty(property: 'metadata', player: _playerService.player);
268268
}
269269

270270
//
@@ -279,8 +279,7 @@ class RadioService {
279279
}
280280

281281
Future<void> _onMpvMetadata(data) async {
282-
if (_playerService.audio?.audioType != AudioType.radio ||
283-
!data.contains('icy-title')) {
282+
if (!data.contains('icy-title')) {
284283
return;
285284
}
286285
final newData = MpvMetaData.fromJson(data);

0 commit comments

Comments
 (0)