Skip to content

Commit

Permalink
Improved code
Browse files Browse the repository at this point in the history
  • Loading branch information
gokadzev committed Dec 28, 2023
1 parent f958a37 commit e727b5d
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 75 deletions.
36 changes: 13 additions & 23 deletions lib/API/musify.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -438,10 +437,8 @@ const Duration _cacheDuration = Duration(hours: 12);
Future<String> 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',
Expand All @@ -454,7 +451,7 @@ Future<String> 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);
Expand All @@ -470,35 +467,28 @@ Future<String> getLiveStreamUrl(String songId) async {

Future<String> 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));
addOrUpdateData('cache', cacheKey, audioUrl);
return audioUrl;
}

AudioStreamInfo selectAudioQuality(
List<AudioStreamInfo> availableSources,
bool isQualityChanged,
) {
if (!isQualityChanged) {
return availableSources.withHighestBitrate();
}
AudioStreamInfo selectAudioQuality(List<AudioStreamInfo> 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();
}
}

Expand Down
1 change: 0 additions & 1 deletion lib/enums/quality_enum.dart

This file was deleted.

18 changes: 0 additions & 18 deletions lib/extensions/audio_quality.dart

This file was deleted.

2 changes: 0 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -285,7 +284,6 @@ void main() async {
Future<void> initialisation() async {
try {
await Hive.initFlutter();
Hive.registerAdapter(AudioQualityAdapter());
await Hive.openBox('settings');
await Hive.openBox('user');
await Hive.openBox('userNoBackup');
Expand Down
38 changes: 10 additions & 28 deletions lib/screens/more_page.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions lib/services/settings_manager.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:musify/enums/quality_enum.dart';

// Preferences

Expand All @@ -16,8 +15,8 @@ final sponsorBlockSupport = ValueNotifier<bool>(
Hive.box('settings').get('sponsorBlockSupport', defaultValue: false),
);

final audioQualitySetting = ValueNotifier<AudioQuality?>(
Hive.box('settings').get('audioQuality', defaultValue: null),
final audioQualitySetting = ValueNotifier<String>(
Hive.box('settings').get('audioQuality', defaultValue: 'high'),
);

// Non-Storage Notifiers
Expand Down

0 comments on commit e727b5d

Please sign in to comment.