Skip to content

Commit

Permalink
fix: radio history tile images after new station uuid mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Feichtmeier committed Nov 5, 2024
1 parent ce9cf1b commit a1be4b3
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 99 deletions.
8 changes: 7 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import 'player/player_service.dart';
import 'podcasts/download_model.dart';
import 'podcasts/podcast_model.dart';
import 'podcasts/podcast_service.dart';
import 'radio/online_art_model.dart';
import 'radio/online_art_service.dart';
import 'radio/radio_model.dart';
import 'radio/radio_service.dart';
Expand Down Expand Up @@ -182,6 +183,12 @@ void registerServicesAndViewModels({
)..init(),
dispose: (s) => s.dispose(),
)
..registerLazySingleton<OnlineArtModel>(
() => OnlineArtModel(
onlineArtService: di<OnlineArtService>(),
)..init(),
dispose: (s) => s.dispose(),
)
..registerLazySingleton<AppModel>(
() => AppModel(
appVersion: version,
Expand All @@ -207,7 +214,6 @@ void registerServicesAndViewModels({
..registerLazySingleton<RadioModel>(
() => RadioModel(
radioService: di<RadioService>(),
onlineArtService: di<OnlineArtService>(),
),
dispose: (s) => s.dispose(),
)
Expand Down
3 changes: 3 additions & 0 deletions lib/player/player_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ class PlayerModel extends SafeChangeNotifier {

int getRadioHistoryLength({String? filter}) =>
filteredRadioHistory(filter: filter).length;
MpvMetaData? getMetadata(String? icyTitle) =>
icyTitle == null ? null : _playerService.radioHistory[icyTitle];

Iterable<MapEntry<String, MpvMetaData>> filteredRadioHistory({
required String? filter,
}) {
Expand Down
4 changes: 2 additions & 2 deletions lib/player/player_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ class PlayerService {
}
if (validHistoryElement) {
_addRadioHistoryElement(
icyTitle: mpvMetaData!.icyTitle.everyWordCapitalized,
icyTitle: mpvMetaData!.icyTitle,
mpvMetaData: mpvMetaData!.copyWith(
icyName: audio?.title?.trim() ?? _mpvMetaData?.icyName ?? '',
),
);

await _processParsedIcyTitle(mpvMetaData!.icyTitle.everyWordCapitalized);
await _processParsedIcyTitle(mpvMetaData!.icyTitle);
}
_propertiesChangedController.add(true);
}
Expand Down
26 changes: 26 additions & 0 deletions lib/radio/online_art_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'dart:async';

import 'package:safe_change_notifier/safe_change_notifier.dart';

import 'online_art_service.dart';

class OnlineArtModel extends SafeChangeNotifier {
OnlineArtModel({
required OnlineArtService onlineArtService,
}) : _onlineArtService = onlineArtService;

final OnlineArtService _onlineArtService;
StreamSubscription<bool>? _propertiesChangedSub;
String? getCover(String icyTitle) => _onlineArtService.get(icyTitle);

void init() {
_propertiesChangedSub ??=
_onlineArtService.propertiesChanged.listen((_) => notifyListeners());
}

@override
Future<void> dispose() async {
await _propertiesChangedSub?.cancel();
super.dispose();
}
}
23 changes: 14 additions & 9 deletions lib/radio/online_art_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ const _kCoverArtArchiveAddress = 'https://coverartarchive.org/release/';
class OnlineArtService {
OnlineArtService({required Dio dio}) : _dio = dio;
final Dio _dio;
final _propertiesChangedController = StreamController<bool>.broadcast();
Stream<bool> get propertiesChanged => _propertiesChangedController.stream;
final _errorController = StreamController<String?>.broadcast();
Stream<String?> get error => _errorController.stream;

Future<String?> fetchAlbumArt(String icyTitle) async {
_errorController.add(null);
return get(icyTitle) ??
final albumArtUrl = get(icyTitle) ??
put(
key: icyTitle,
url: await compute(
Expand All @@ -29,23 +33,24 @@ class OnlineArtService {
return null;
}),
);
_propertiesChangedController.add(true);

return albumArtUrl;
}

final _value = <String, String?>{};
final _store = <String, String?>{};

String? put({required String key, String? url}) {
return _value.containsKey(key)
? _value.update(key, (value) => url)
: _value.putIfAbsent(key, () => url);
return _store.containsKey(key)
? _store.update(key, (value) => url)
: _store.putIfAbsent(key, () => url);
}

String? get(String? icyTitle) => icyTitle == null ? null : _value[icyTitle];

final _errorController = StreamController<String?>.broadcast();
Stream<String?> get error => _errorController.stream;
String? get(String? icyTitle) => icyTitle == null ? null : _store[icyTitle];

Future<void> dispose() async {
await _errorController.close();
await _propertiesChangedController.close();
}
}

Expand Down
11 changes: 2 additions & 9 deletions lib/radio/radio_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,13 @@ import 'package:safe_change_notifier/safe_change_notifier.dart';

import '../common/data/audio.dart';
import '../l10n/l10n.dart';
import 'online_art_service.dart';
import 'radio_service.dart';

class RadioModel extends SafeChangeNotifier {
final RadioService _radioService;
final OnlineArtService _onlineArtService;

RadioModel({
required RadioService radioService,
required OnlineArtService onlineArtService,
}) : _radioService = radioService,
_onlineArtService = onlineArtService;

String? getCover(String icyTitle) => _onlineArtService.get(icyTitle);
RadioModel({required RadioService radioService})
: _radioService = radioService;

Future<void> clickStation(Audio? station) async {
if (station?.description != null) {
Expand Down
36 changes: 21 additions & 15 deletions lib/radio/view/icy_image.dart
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import 'package:flutter/material.dart';
import 'package:watch_it/watch_it.dart';

import '../../common/data/mpv_meta_data.dart';
import '../../common/view/icons.dart';
import '../../common/view/mpv_metadata_dialog.dart';
import '../../common/view/safe_network_image.dart';
import '../../constants.dart';
import '../../l10n/l10n.dart';
import '../../player/player_model.dart';
import '../radio_model.dart';
import '../online_art_model.dart';

class IcyImage extends StatelessWidget with WatchItMixin {
const IcyImage({
super.key,
required this.mpvMetaData,
required this.icyTitle,
this.height = kAudioTrackWidth,
this.width = kAudioTrackWidth,
this.borderRadius,
this.fallBackWidget,
this.fit,
});

final MpvMetaData mpvMetaData;
final String? icyTitle;

final double height, width;
final BorderRadius? borderRadius;
Expand All @@ -31,28 +30,35 @@ class IcyImage extends StatelessWidget with WatchItMixin {
@override
Widget build(BuildContext context) {
final bR = borderRadius ?? BorderRadius.circular(4);
watchPropertyValue(
(PlayerModel m) => m.remoteImageUrl,
);
final imageUrl = icyTitle == null
? null
: watchPropertyValue(
(OnlineArtModel m) => m.getCover(icyTitle!),
);
// watchPropertyValue((PlayerModel m) => m.remoteImageUrl);

return Tooltip(
message: context.l10n.metadata,
child: ClipRRect(
borderRadius: bR,
child: InkWell(
borderRadius: bR,
onTap: () => showDialog(
context: context,
builder: (context) => MpvMetadataDialog(
image: di<RadioModel>().getCover(mpvMetaData.icyTitle),
mpvMetaData: mpvMetaData,
),
),
onTap: () {
final metadata = di<PlayerModel>().getMetadata(icyTitle);
if (metadata == null) return;
showDialog(
context: context,
builder: (context) => MpvMetadataDialog(
image: imageUrl,
mpvMetaData: metadata,
),
);
},
child: SizedBox(
height: height,
width: width,
child: SafeNetworkImage(
url: di<RadioModel>().getCover(mpvMetaData.icyTitle),
url: imageUrl,
fallBackIcon: fallBackWidget ?? Icon(Iconz.radio),
filterQuality: FilterQuality.medium,
fit: fit ?? BoxFit.fitHeight,
Expand Down
9 changes: 6 additions & 3 deletions lib/radio/view/radio_history_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ class RadioHistoryList extends StatelessWidget with WatchItMixin {
.elementAt(reversedIndex);
return simpleList
? RadioHistoryTile.simple(
entry: e,
icyTitle: e.key,
selected: current?.icyTitle != null &&
current?.icyTitle == e.value.icyTitle,
)
: RadioHistoryTile(
entry: e,
icyTitle: e.key,
selected: current?.icyTitle != null &&
current?.icyTitle == e.value.icyTitle,
);
Expand All @@ -77,12 +77,14 @@ class SliverRadioHistoryList extends StatelessWidget with WatchItMixin {
this.emptyMessage,
this.padding,
this.emptyIcon,
required this.allowNavigation,
});

final String? filter;
final Widget? emptyMessage;
final Widget? emptyIcon;
final EdgeInsetsGeometry? padding;
final bool allowNavigation;

@override
Widget build(BuildContext context) {
Expand All @@ -109,9 +111,10 @@ class SliverRadioHistoryList extends StatelessWidget with WatchItMixin {
.filteredRadioHistory(filter: filter)
.elementAt(reversedIndex);
return RadioHistoryTile(
entry: e,
icyTitle: e.key,
selected: current?.icyTitle != null &&
current?.icyTitle == e.value.icyTitle,
allowNavigation: allowNavigation,
);
},
childCount: length,
Expand Down
Loading

0 comments on commit a1be4b3

Please sign in to comment.