diff --git a/lib/extensions/string_x.dart b/lib/extensions/string_x.dart index e57c5fc44..1a77ac75c 100644 --- a/lib/extensions/string_x.dart +++ b/lib/extensions/string_x.dart @@ -19,13 +19,22 @@ extension StringExtension on String { ({String? songName, String? artist}) get splitByDash { String? songName; String? artist; - final split = this.split(' - '); + var split = this.split(' - '); if (split.isNotEmpty) { artist = split.elementAtOrNull(0); songName = split.elementAtOrNull(1); if (split.length == 3 && songName != null) { songName = songName + (split.elementAtOrNull(2) ?? ''); } + } else { + split = this.split('-'); + if (split.isNotEmpty) { + artist = split.elementAtOrNull(0); + songName = split.elementAtOrNull(1); + if (split.length == 3 && songName != null) { + songName = songName + (split.elementAtOrNull(2) ?? ''); + } + } } return (songName: songName, artist: artist); } diff --git a/lib/library/library_service.dart b/lib/library/library_service.dart index e97795bf2..eec09d4e4 100644 --- a/lib/library/library_service.dart +++ b/lib/library/library_service.dart @@ -2,12 +2,11 @@ import 'dart:async'; import 'dart:io'; import 'package:collection/collection.dart'; -import 'package:dio/dio.dart'; import 'package:shared_preferences/shared_preferences.dart'; +import '../common/data/audio.dart'; import '../common/view/audio_filter.dart'; import '../constants.dart'; -import '../common/data/audio.dart'; import '../persistence_utils.dart'; class LibraryService { @@ -293,7 +292,6 @@ class LibraryService { /// /// Podcasts /// - final dio = Dio(); Map _downloads = {}; Map get downloads => _downloads; String? getDownload(String? url) => downloads[url]; @@ -496,7 +494,6 @@ class LibraryService { } Future dispose() async { - dio.close(); await _propertiesChangedController.close(); } } diff --git a/lib/main.dart b/lib/main.dart index 8980f2545..9e1c1a73d 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -2,6 +2,7 @@ import 'dart:io'; import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:desktop_notifications/desktop_notifications.dart'; +import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:github/github.dart'; import 'package:gtk/gtk.dart'; @@ -29,6 +30,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_service.dart'; import 'radio/radio_model.dart'; import 'radio/radio_service.dart'; import 'search/search_model.dart'; @@ -72,9 +74,19 @@ void registerServicesAndViewModels({ required String version, }) { di - ..registerLazySingleton(() => sharedPreferences) + ..registerLazySingleton(() => sharedPreferences) + ..registerLazySingleton( + () => Dio(), + dispose: (s) => s.close(), + ) + ..registerLazySingleton( + () => OnlineArtService( + dio: di(), + ), + ) ..registerLazySingleton( () => PlayerService( + onlineArtService: di(), controller: VideoController( Player( configuration: const PlayerConfiguration(title: kAppTitle), @@ -160,11 +172,17 @@ void registerServicesAndViewModels({ dispose: (s) => s.dispose(), ) ..registerLazySingleton( - () => RadioModel(radioService: di()), + () => RadioModel( + radioService: di(), + onlineArtService: di(), + ), dispose: (s) => s.dispose(), ) ..registerLazySingleton( - () => DownloadModel(di()), + () => DownloadModel( + libraryService: di(), + dio: di(), + ), ) ..registerLazySingleton( () => SearchModel( diff --git a/lib/online_album_art_utils.dart b/lib/online_album_art_utils.dart deleted file mode 100644 index 023591101..000000000 --- a/lib/online_album_art_utils.dart +++ /dev/null @@ -1,92 +0,0 @@ -import 'dart:convert'; - -import 'package:flutter/foundation.dart'; -import 'package:http/http.dart' as http; - -import 'constants.dart'; -import 'extensions/string_x.dart'; - -Future fetchAlbumArt(String icyTitle) async { - return UrlStore().get(icyTitle) ?? - UrlStore() - .put(key: icyTitle, url: await compute(_fetchAlbumArt, icyTitle)); -} - -Future _fetchAlbumArt(String icyTitle) async { - final songInfo = icyTitle.splitByDash; - if (songInfo.songName == null || songInfo.artist == null) return null; - - final searchUrl = getAlbumArtServiceUri(songInfo); - if (searchUrl == null) return null; - - try { - final searchResponse = await http.get(searchUrl, headers: kAlbumArtHeaders); - - if (searchResponse.statusCode == 200) { - final searchData = jsonDecode(searchResponse.body); - final recordings = searchData['recordings'] as List; - - final firstRecording = recordings.firstOrNull; - - final releaseId = firstRecording == null - ? null - : firstRecording?['releases']?[0]?['id']; - - if (releaseId == null) return null; - - final albumArtUrl = await _fetchAlbumArtUrlFromReleaseId(releaseId); - - return albumArtUrl; - } - } on Exception catch (_) { - return null; - } - - return null; -} - -Future _fetchAlbumArtUrlFromReleaseId(String releaseId) async { - final url = Uri.parse( - 'https://coverartarchive.org/release/$releaseId', - ); - try { - final response = await http.get(url, headers: kAlbumArtHeaders); - - if (response.statusCode == 200) { - final data = jsonDecode(response.body); - final images = data['images'] as List; - - if (images.isNotEmpty) { - final artwork = images[0]; - - return (artwork['image']) as String?; - } - } - } on Exception catch (_) { - return null; - } - - return null; -} - -Uri? getAlbumArtServiceUri(({String? artist, String? songName}) artInfo) { - final address = - 'https://musicbrainz.org/ws/2/recording/?query=recording:"${artInfo.songName}"%20AND%20artist:"${artInfo.artist}"'; - return Uri.tryParse(address); -} - -class UrlStore { - static final UrlStore _instance = UrlStore._internal(); - factory UrlStore() => _instance; - UrlStore._internal(); - - final _value = {}; - - String? put({required String key, String? url}) { - return _value.containsKey(key) - ? _value.update(key, (value) => url) - : _value.putIfAbsent(key, () => url); - } - - String? get(String? icyTitle) => icyTitle == null ? null : _value[icyTitle]; -} diff --git a/lib/player/player_service.dart b/lib/player/player_service.dart index 3deedaef3..a55931466 100644 --- a/lib/player/player_service.dart +++ b/lib/player/player_service.dart @@ -18,15 +18,19 @@ import '../common/data/player_state.dart'; import '../constants.dart'; import '../extensions/string_x.dart'; import '../local_audio/cover_store.dart'; -import '../online_album_art_utils.dart'; import '../persistence_utils.dart'; +import '../radio/online_art_service.dart'; typedef Queue = ({String name, List