diff --git a/lib/API/musify.dart b/lib/API/musify.dart index 3d8770fc1..980cfd050 100644 --- a/lib/API/musify.dart +++ b/lib/API/musify.dart @@ -8,7 +8,6 @@ import 'package:hive/hive.dart'; import 'package:http/http.dart' as http; import 'package:musify/DB/albums.db.dart'; import 'package:musify/DB/playlists.db.dart'; -import 'package:musify/enums/quality_enum.dart'; import 'package:musify/extensions/l10n.dart'; import 'package:musify/main.dart'; import 'package:musify/services/data_manager.dart'; @@ -438,10 +437,8 @@ const Duration _cacheDuration = Duration(hours: 12); Future getSong(String songId, bool isLive) async { try { final qualitySetting = audioQualitySetting.value; - final isQualityChanged = qualitySetting != null; - final cacheKey = - 'song_$songId${isQualityChanged ? '_${qualitySetting.name}' : ''}_url'; + final cacheKey = 'song_${songId}_${qualitySetting}_url'; final cachedUrl = await getData( 'cache', @@ -454,7 +451,7 @@ Future getSong(String songId, bool isLive) async { } else if (isLive) { return await getLiveStreamUrl(songId); } else { - return await getAudioUrl(songId, isQualityChanged, cacheKey); + return await getAudioUrl(songId, cacheKey); } } catch (e, stackTrace) { logger.log('Error while getting song streaming URL', e, stackTrace); @@ -470,11 +467,10 @@ Future getLiveStreamUrl(String songId) async { Future getAudioUrl( String songId, - bool isQualityChanged, String cacheKey, ) async { final manifest = await yt.videos.streamsClient.getManifest(songId); - final audioQuality = selectAudioQuality(manifest.audioOnly, isQualityChanged); + final audioQuality = selectAudioQuality(manifest.audioOnly); final audioUrl = audioQuality.url.toString(); unawaited(updateRecentlyPlayed(songId)); @@ -482,23 +478,17 @@ Future getAudioUrl( return audioUrl; } -AudioStreamInfo selectAudioQuality( - List availableSources, - bool isQualityChanged, -) { - if (!isQualityChanged) { - return availableSources.withHighestBitrate(); - } +AudioStreamInfo selectAudioQuality(List availableSources) { + final qualitySetting = audioQualitySetting.value; - final selectedQuality = audioQualitySetting.value ?? AudioQuality.bestQuality; - switch (selectedQuality) { - case AudioQuality.lowQuality: - return availableSources.last; - case AudioQuality.mediumQuality: - return availableSources[availableSources.length ~/ 2]; - case AudioQuality.bestQuality: - default: - return availableSources.first; + if (qualitySetting == 'low') { + return availableSources.last; + } else if (qualitySetting == 'medium') { + return availableSources[availableSources.length ~/ 2]; + } else if (qualitySetting == 'high') { + return availableSources.first; + } else { + return availableSources.withHighestBitrate(); } } diff --git a/lib/enums/quality_enum.dart b/lib/enums/quality_enum.dart deleted file mode 100644 index aa660bfe5..000000000 --- a/lib/enums/quality_enum.dart +++ /dev/null @@ -1 +0,0 @@ -enum AudioQuality { lowQuality, mediumQuality, bestQuality } diff --git a/lib/extensions/audio_quality.dart b/lib/extensions/audio_quality.dart deleted file mode 100644 index fb0bf10b8..000000000 --- a/lib/extensions/audio_quality.dart +++ /dev/null @@ -1,18 +0,0 @@ -import 'package:hive_flutter/hive_flutter.dart'; -import 'package:musify/enums/quality_enum.dart'; - -class AudioQualityAdapter extends TypeAdapter { - @override - final int typeId = 1; - - @override - AudioQuality read(BinaryReader reader) { - final value = reader.readInt(); - return AudioQuality.values[value]; - } - - @override - void write(BinaryWriter writer, AudioQuality obj) { - writer.writeInt(obj.index); - } -} diff --git a/lib/main.dart b/lib/main.dart index a8941a0fd..1e272aaaa 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -11,7 +11,6 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:google_fonts/google_fonts.dart'; import 'package:hive_flutter/hive_flutter.dart'; -import 'package:musify/extensions/audio_quality.dart'; import 'package:musify/extensions/l10n.dart'; import 'package:musify/services/audio_service.dart'; import 'package:musify/services/data_manager.dart'; @@ -285,7 +284,6 @@ void main() async { Future initialisation() async { try { await Hive.initFlutter(); - Hive.registerAdapter(AudioQualityAdapter()); await Hive.openBox('settings'); await Hive.openBox('user'); await Hive.openBox('userNoBackup'); diff --git a/lib/screens/more_page.dart b/lib/screens/more_page.dart index 500b9d8a4..080b42ee3 100644 --- a/lib/screens/more_page.dart +++ b/lib/screens/more_page.dart @@ -1,7 +1,6 @@ import 'package:fluentui_system_icons/fluentui_system_icons.dart'; import 'package:flutter/material.dart'; import 'package:musify/API/version.dart'; -import 'package:musify/enums/quality_enum.dart'; import 'package:musify/extensions/l10n.dart'; import 'package:musify/main.dart'; import 'package:musify/screens/about_page.dart'; @@ -364,11 +363,7 @@ class MorePage extends StatelessWidget { backgroundColor: Colors.transparent, context: context, builder: (BuildContext context) { - final availableQualities = [ - AudioQuality.lowQuality, - AudioQuality.mediumQuality, - AudioQuality.bestQuality, - ]; + final availableQualities = ['low', 'medium', 'high']; return Center( child: Container( @@ -385,36 +380,23 @@ class MorePage extends StatelessWidget { itemCount: availableQualities.length, itemBuilder: (context, index) { final quality = availableQualities[index]; - final isDefault = - audioQualitySetting.value == null && - quality == AudioQuality.bestQuality; final isCurrentQuality = audioQualitySetting.value == quality; return Padding( padding: const EdgeInsets.all(10), child: Card( - elevation: - isCurrentQuality || isDefault ? 0 : 4, + elevation: isCurrentQuality ? 0 : 4, child: ListTile( - title: Text(quality.name), + title: Text(quality), onTap: () { - if (quality == AudioQuality.bestQuality) { - // Save null when "Best Quality" is selected - addOrUpdateData( - 'settings', - 'audioQuality', - null, - ); - audioQualitySetting.value = null; - } else { - addOrUpdateData( - 'settings', - 'audioQuality', - quality, - ); - audioQualitySetting.value = quality; - } + addOrUpdateData( + 'settings', + 'audioQuality', + quality, + ); + audioQualitySetting.value = quality; + showToast( context, context.l10n!.audioQualityMsg, diff --git a/lib/services/settings_manager.dart b/lib/services/settings_manager.dart index ad6f35e9a..72004c358 100644 --- a/lib/services/settings_manager.dart +++ b/lib/services/settings_manager.dart @@ -1,6 +1,5 @@ import 'package:flutter/material.dart'; import 'package:hive_flutter/hive_flutter.dart'; -import 'package:musify/enums/quality_enum.dart'; // Preferences @@ -16,8 +15,8 @@ final sponsorBlockSupport = ValueNotifier( Hive.box('settings').get('sponsorBlockSupport', defaultValue: false), ); -final audioQualitySetting = ValueNotifier( - Hive.box('settings').get('audioQuality', defaultValue: null), +final audioQualitySetting = ValueNotifier( + Hive.box('settings').get('audioQuality', defaultValue: 'high'), ); // Non-Storage Notifiers