Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: migrate completely to dio, thanks @HrX03 #924

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/extensions/string_x.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
5 changes: 1 addition & 4 deletions lib/library/library_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -293,7 +292,6 @@ class LibraryService {
///
/// Podcasts
///
final dio = Dio();
Map<String, String> _downloads = {};
Map<String, String> get downloads => _downloads;
String? getDownload(String? url) => downloads[url];
Expand Down Expand Up @@ -496,7 +494,6 @@ class LibraryService {
}

Future<void> dispose() async {
dio.close();
await _propertiesChangedController.close();
}
}
24 changes: 21 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -72,9 +74,19 @@ void registerServicesAndViewModels({
required String version,
}) {
di
..registerLazySingleton(() => sharedPreferences)
..registerLazySingleton<SharedPreferences>(() => sharedPreferences)
..registerLazySingleton<Dio>(
() => Dio(),
dispose: (s) => s.close(),
)
..registerLazySingleton<OnlineArtService>(
() => OnlineArtService(
dio: di<Dio>(),
),
)
..registerLazySingleton<PlayerService>(
() => PlayerService(
onlineArtService: di<OnlineArtService>(),
controller: VideoController(
Player(
configuration: const PlayerConfiguration(title: kAppTitle),
Expand Down Expand Up @@ -160,11 +172,17 @@ void registerServicesAndViewModels({
dispose: (s) => s.dispose(),
)
..registerLazySingleton<RadioModel>(
() => RadioModel(radioService: di<RadioService>()),
() => RadioModel(
radioService: di<RadioService>(),
onlineArtService: di<OnlineArtService>(),
),
dispose: (s) => s.dispose(),
)
..registerLazySingleton<DownloadModel>(
() => DownloadModel(di<LibraryService>()),
() => DownloadModel(
libraryService: di<LibraryService>(),
dio: di<Dio>(),
),
)
..registerLazySingleton<SearchModel>(
() => SearchModel(
Expand Down
92 changes: 0 additions & 92 deletions lib/online_album_art_utils.dart

This file was deleted.

10 changes: 7 additions & 3 deletions lib/player/player_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Audio> audios});

class PlayerService {
PlayerService({
required VideoController controller,
}) : _controller = controller;
required OnlineArtService onlineArtService,
}) : _controller = controller,
_onlineArtService = onlineArtService;

final OnlineArtService _onlineArtService;

final VideoController _controller;
VideoController get controller => _controller;
Expand Down Expand Up @@ -284,7 +288,7 @@ class PlayerService {
);

final songInfo = mpvMetaData.icyTitle.splitByDash;
fetchAlbumArt(mpvMetaData.icyTitle).then(
_onlineArtService.fetchAlbumArt(mpvMetaData.icyTitle).then(
(albumArt) async {
await _setMediaControlsMetaData(
audio: (_audio ?? const Audio()).copyWith(
Expand Down
13 changes: 9 additions & 4 deletions lib/podcasts/download_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ import '../l10n/l10n.dart';
import '../library/library_service.dart';

class DownloadModel extends SafeChangeNotifier {
DownloadModel(this._service);
DownloadModel({
required LibraryService libraryService,
required Dio dio,
}) : _service = libraryService,
_dio = dio;

final LibraryService _service;
final Dio _dio;

final _values = <String, double?>{};
final _cancelTokens = <String, CancelToken?>{};
Expand Down Expand Up @@ -66,8 +71,8 @@ class DownloadModel extends SafeChangeNotifier {
return;
}

_service.dio.interceptors.add(LogInterceptor());
_service.dio.options.headers = {HttpHeaders.acceptEncodingHeader: '*'};
_dio.interceptors.add(LogInterceptor());
_dio.options.headers = {HttpHeaders.acceptEncodingHeader: '*'};

if (!Directory(downloadsDir).existsSync()) {
Directory(downloadsDir).createSync();
Expand Down Expand Up @@ -112,7 +117,7 @@ class DownloadModel extends SafeChangeNotifier {
? _cancelTokens.update(url, (value) => CancelToken())
: _cancelTokens.putIfAbsent(url, () => CancelToken());
try {
return await _service.dio.download(
return await _dio.download(
url,
path,
onReceiveProgress: (count, total) =>
Expand Down
97 changes: 97 additions & 0 deletions lib/radio/online_art_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';

import '../constants.dart';
import '../extensions/string_x.dart';

const _kMusicBrainzAddress = 'https://musicbrainz.org/ws/2/recording/';
const _kCoverArtArchiveAddress = 'https://coverartarchive.org/release/';

class OnlineArtService {
OnlineArtService({required Dio dio}) : _dio = dio;
final Dio _dio;

Future<String?> fetchAlbumArt(String icyTitle) async =>
get(icyTitle) ??
put(
key: icyTitle,
url: await compute(
_fetchAlbumArt,
_ComputeCapsule(icyTitle: icyTitle, dio: _dio),
),
);

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

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];
}

class _ComputeCapsule {
final String icyTitle;
final Dio dio;

_ComputeCapsule({required this.icyTitle, required this.dio});
}

Future<String?> _fetchAlbumArt(_ComputeCapsule capsule) async {
final dio = capsule.dio;
dio.options.headers = kAlbumArtHeaders;
final songInfo = capsule.icyTitle.splitByDash;
if (songInfo.songName == null || songInfo.artist == null) return null;

try {
final searchResponse = await dio.get(
_kMusicBrainzAddress,
queryParameters: {
'query':
'recording:"${songInfo.songName}"%20AND%20artist:"${songInfo.artist}"',
},
);

final recordings = searchResponse.data['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: releaseId,
dio: dio,
);

return albumArtUrl;
} on Exception catch (_) {
return null;
}
}

Future<String?> _fetchAlbumArtUrlFromReleaseId({
required String releaseId,
required Dio dio,
}) async {
try {
dio.options.headers = kAlbumArtHeaders;
final response = await dio.get('$_kCoverArtArchiveAddress$releaseId');

final images = response.data['images'] as List;

if (images.isNotEmpty) {
final artwork = images[0];

return (artwork['image']) as String?;
}
} on Exception catch (_) {
return null;
}

return null;
}
8 changes: 7 additions & 1 deletion lib/radio/radio_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ 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,
}) : _radioService = radioService;
required OnlineArtService onlineArtService,
}) : _radioService = radioService,
_onlineArtService = onlineArtService;

String? getCover(String icyTitle) => _onlineArtService.get(icyTitle);

Future<void> clickStation(Audio? station) async {
if (station?.description != null) {
Expand Down
Loading