Skip to content

Commit

Permalink
chore: move more constants (#1132)
Browse files Browse the repository at this point in the history
  • Loading branch information
Feichtmeier authored Jan 25, 2025
1 parent af765fa commit 88f16dd
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 110 deletions.
44 changes: 0 additions & 44 deletions lib/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,3 @@ const kSponsorLink = 'https://github.com/sponsors/Feichtmeier';
const kGitHubShortLink = 'ubuntu-flutter-community/musicpod';
const kFallbackThumbnailUrl =
'https://raw.githubusercontent.com/ubuntu-flutter-community/musicpod/main/snap/gui/musicpod.png';

// HTTP headers
const kMusicBrainzHeaders = {
'Accept': 'application/json',
'User-Agent': '$kAppTitle ($kRepoUrl)',
};

const kInternetArchiveHeaders = {
'User-Agent': '$kAppTitle ($kRepoUrl)',
};

const kDirectoryProperty = 'directory';
const kDownloadsCustomDir = 'downloadsCustomDir';
const kLocalAudioIndex = 'localAudioIndex';

const kNeverShowImportFails = 'neverShowImportFails';
const kEnableDiscordRPC = 'enableDiscordRPC';
const kEnableLastFmScrobbling = 'enableLastFmScrobbling';
const kLastFmApiKey = 'lastFmApiKey';
const klastFmSecret = 'lastFmSecret';
const kLastFmSessionKey = 'lastFmSessionKey';
const kLastFmUsername = 'lastFmUsername';
const kEnableListenBrainzScrobbling = 'enableListenBrainzScrobbling';
const kListenBrainzApiKey = 'listenBrainzApiKey';
const kLastCountryCode = 'lastCountryCode';
const kLastLanguageCode = 'lastLanguageCode';

const kUsePodcastIndex = 'usePodcastIndex';
const kThemeIndex = 'themeIndex';
const kPodcastIndexApiKey = 'podcastIndexApiKey';
const kPodcastIndexApiSecret = 'podcastIndexApiSecret';
const kUseArtistGridView = 'useArtistGridView';

const kFavRadioTags = 'favRadioTags';
const kFavCountryCodes = 'favCountryCodes';
const kFavLanguageCodes = 'favLanguageCodes';
const kAscendingFeeds = 'ascendingfeed:::';
const kPatchNotesDisposed = 'kPatchNotesDisposed';
const kCloseBtnAction = 'closeBtnAction';
const kUseMoreAnimations = 'useMoreAnimations';
const kShowPositionDuration = 'showPositionDuration';

const kPlaybackRate = 'playbackRate';
const kNotifyDataSafeMode = 'notifyDataSafeMode';
31 changes: 31 additions & 0 deletions lib/extensions/shared_preferences_x.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:shared_preferences/shared_preferences.dart';

extension SPKeys on SharedPreferences {
static const directory = 'directory';
static const downloads = 'downloadsCustomDir';
static const localAudioIndex = 'localAudioIndex';
static const neverShowImportFails = 'neverShowImportFails';
static const enableDiscord = 'enableDiscordRPC';
static const enableLastFm = 'enableLastFmScrobbling';
static const lastFmApiKey = 'lastFmApiKey';
static const lastFmSecret = 'lastFmSecret';
static const lastFmSessionKey = 'lastFmSessionKey';
static const lastFmUsername = 'lastFmUsername';
static const enableListenBrainz = 'enableListenBrainzScrobbling';
static const listenBrainzApiKey = 'listenBrainzApiKey';
static const lastCountryCode = 'lastCountryCode';
static const lastLanguageCode = 'lastLanguageCode';
static const usePodcastIndex = 'usePodcastIndex';
static const themeIndex = 'themeIndex';
static const podcastIndexApiKey = 'podcastIndexApiKey';
static const podcastIndexApiSecret = 'podcastIndexApiSecret';
static const favRadioTags = 'favRadioTags';
static const favCountryCodes = 'favCountryCodes';
static const favLanguageCodes = 'favLanguageCodes';
static const ascendingFeeds = 'ascendingfeed:::';
static const patchNotesDisposed = 'kPatchNotesDisposed';
static const closeBtnAction = 'closeBtnAction';
static const useMoreAnimations = 'useMoreAnimations';
static const showPositionDuration = 'showPositionDuration';
static const notifyDataSafeMode = 'notifyDataSafeMode';
}
41 changes: 24 additions & 17 deletions lib/library/library_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import '../common/data/audio.dart';
import '../common/file_names.dart';
import '../common/page_ids.dart';
import '../common/view/audio_filter.dart';
import '../constants.dart';
import '../extensions/shared_preferences_x.dart';
import '../persistence_utils.dart';

class LibraryService {
Expand Down Expand Up @@ -96,14 +96,14 @@ class LibraryService {
}

Set<String> get favRadioTags =>
_sharedPreferences.getStringList(kFavRadioTags)?.toSet() ?? {};
_sharedPreferences.getStringList(SPKeys.favRadioTags)?.toSet() ?? {};
bool isFavTag(String value) => favRadioTags.contains(value);

void addFavRadioTag(String name) {
if (favRadioTags.contains(name)) return;
final Set<String> tags = favRadioTags;
tags.add(name);
_sharedPreferences.setStringList(kFavRadioTags, tags.toList()).then(
_sharedPreferences.setStringList(SPKeys.favRadioTags, tags.toList()).then(
(saved) {
if (saved) _propertiesChangedController.add(true);
},
Expand All @@ -114,32 +114,33 @@ class LibraryService {
if (!favRadioTags.contains(name)) return;
final Set<String> tags = favRadioTags;
tags.remove(name);
_sharedPreferences.setStringList(kFavRadioTags, tags.toList()).then(
_sharedPreferences.setStringList(SPKeys.favRadioTags, tags.toList()).then(
(saved) {
if (saved) _propertiesChangedController.add(true);
},
);
}

String? get lastCountryCode => _sharedPreferences.getString(kLastCountryCode);
String? get lastCountryCode =>
_sharedPreferences.getString(SPKeys.lastCountryCode);
void setLastCountryCode(String value) {
_sharedPreferences.setString(kLastCountryCode, value).then(
_sharedPreferences.setString(SPKeys.lastCountryCode, value).then(
(saved) {
if (saved) _propertiesChangedController.add(true);
},
);
}

Set<String> get favCountryCodes =>
_sharedPreferences.getStringList(kFavCountryCodes)?.toSet() ?? {};
_sharedPreferences.getStringList(SPKeys.favCountryCodes)?.toSet() ?? {};
bool isFavCountry(String value) => favCountryCodes.contains(value);

void addFavCountryCode(String name) {
if (favCountryCodes.contains(name)) return;
final favCodes = favCountryCodes;
favCodes.add(name);
_sharedPreferences
.setStringList(kFavCountryCodes, favCodes.toList())
.setStringList(SPKeys.favCountryCodes, favCodes.toList())
.then((saved) {
if (saved) _propertiesChangedController.add(true);
});
Expand All @@ -150,29 +151,31 @@ class LibraryService {
final favCodes = favCountryCodes;
favCodes.remove(name);
_sharedPreferences
.setStringList(kFavCountryCodes, favCodes.toList())
.setStringList(SPKeys.favCountryCodes, favCodes.toList())
.then((saved) {
if (saved) _propertiesChangedController.add(true);
});
}

String? get lastLanguageCode =>
_sharedPreferences.getString(kLastLanguageCode);
_sharedPreferences.getString(SPKeys.lastLanguageCode);
void setLastLanguageCode(String value) {
_sharedPreferences.setString(kLastLanguageCode, value).then((saved) {
_sharedPreferences.setString(SPKeys.lastLanguageCode, value).then((saved) {
if (saved) _propertiesChangedController.add(true);
});
}

Set<String> get favLanguageCodes =>
_sharedPreferences.getStringList(kFavLanguageCodes)?.toSet() ?? {};
_sharedPreferences.getStringList(SPKeys.favLanguageCodes)?.toSet() ?? {};
bool isFavLanguage(String value) => favLanguageCodes.contains(value);

void addFavLanguageCode(String name) {
if (favLanguageCodes.contains(name)) return;
final favLangs = favLanguageCodes;
favLangs.add(name);
_sharedPreferences.setStringList(kFavLanguageCodes, favLangs.toList()).then(
_sharedPreferences
.setStringList(SPKeys.favLanguageCodes, favLangs.toList())
.then(
(saved) {
if (saved) _propertiesChangedController.add(true);
},
Expand All @@ -183,7 +186,9 @@ class LibraryService {
if (!favLanguageCodes.contains(name)) return;
final favLangs = favLanguageCodes;
favLangs.remove(name);
_sharedPreferences.setStringList(kFavLanguageCodes, favLangs.toList()).then(
_sharedPreferences
.setStringList(SPKeys.favLanguageCodes, favLangs.toList())
.then(
(saved) {
if (saved) _propertiesChangedController.add(true);
},
Expand Down Expand Up @@ -430,16 +435,18 @@ class LibraryService {
}

bool showPodcastAscending(String feedUrl) =>
_sharedPreferences.getBool(kAscendingFeeds + feedUrl) ?? false;
_sharedPreferences.getBool(SPKeys.ascendingFeeds + feedUrl) ?? false;

Future<void> _addAscendingPodcast(String feedUrl) async {
await _sharedPreferences.setBool(kAscendingFeeds + feedUrl, true).then(
await _sharedPreferences
.setBool(SPKeys.ascendingFeeds + feedUrl, true)
.then(
(_) => _propertiesChangedController.add(true),
);
}

Future<void> _removeAscendingPodcast(String feedUrl) async =>
_sharedPreferences.remove(kAscendingFeeds + feedUrl).then(
_sharedPreferences.remove(SPKeys.ascendingFeeds + feedUrl).then(
(_) => _propertiesChangedController.add(true),
);

Expand Down
12 changes: 10 additions & 2 deletions lib/radio/online_art_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ import '../extensions/string_x.dart';

const _kMusicBrainzAddress = 'https://musicbrainz.org/ws/2/recording/';
const _kCoverArtArchiveAddress = 'https://coverartarchive.org/release/';
const _kMusicBrainzHeaders = {
'Accept': 'application/json',
'User-Agent': '$kAppTitle ($kRepoUrl)',
};

const _kInternetArchiveHeaders = {
'User-Agent': '$kAppTitle ($kRepoUrl)',
};

class OnlineArtService {
OnlineArtService({required Dio dio}) : _dio = dio;
Expand Down Expand Up @@ -63,7 +71,7 @@ class _ComputeCapsule {

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

Expand Down Expand Up @@ -119,7 +127,7 @@ Future<String?> _fetchAlbumArtUrlFromReleaseId({
required Dio dio,
}) async {
try {
dio.options.headers = kInternetArchiveHeaders;
dio.options.headers = _kInternetArchiveHeaders;
dio.options.followRedirects = true;
dio.options.maxRedirects = 5;
dio.options.receiveTimeout = const Duration(seconds: 25);
Expand Down
Loading

0 comments on commit 88f16dd

Please sign in to comment.