Skip to content

Commit 88f16dd

Browse files
authored
chore: move more constants (#1132)
1 parent af765fa commit 88f16dd

File tree

6 files changed

+120
-110
lines changed

6 files changed

+120
-110
lines changed

lib/constants.dart

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,47 +11,3 @@ const kSponsorLink = 'https://github.com/sponsors/Feichtmeier';
1111
const kGitHubShortLink = 'ubuntu-flutter-community/musicpod';
1212
const kFallbackThumbnailUrl =
1313
'https://raw.githubusercontent.com/ubuntu-flutter-community/musicpod/main/snap/gui/musicpod.png';
14-
15-
// HTTP headers
16-
const kMusicBrainzHeaders = {
17-
'Accept': 'application/json',
18-
'User-Agent': '$kAppTitle ($kRepoUrl)',
19-
};
20-
21-
const kInternetArchiveHeaders = {
22-
'User-Agent': '$kAppTitle ($kRepoUrl)',
23-
};
24-
25-
const kDirectoryProperty = 'directory';
26-
const kDownloadsCustomDir = 'downloadsCustomDir';
27-
const kLocalAudioIndex = 'localAudioIndex';
28-
29-
const kNeverShowImportFails = 'neverShowImportFails';
30-
const kEnableDiscordRPC = 'enableDiscordRPC';
31-
const kEnableLastFmScrobbling = 'enableLastFmScrobbling';
32-
const kLastFmApiKey = 'lastFmApiKey';
33-
const klastFmSecret = 'lastFmSecret';
34-
const kLastFmSessionKey = 'lastFmSessionKey';
35-
const kLastFmUsername = 'lastFmUsername';
36-
const kEnableListenBrainzScrobbling = 'enableListenBrainzScrobbling';
37-
const kListenBrainzApiKey = 'listenBrainzApiKey';
38-
const kLastCountryCode = 'lastCountryCode';
39-
const kLastLanguageCode = 'lastLanguageCode';
40-
41-
const kUsePodcastIndex = 'usePodcastIndex';
42-
const kThemeIndex = 'themeIndex';
43-
const kPodcastIndexApiKey = 'podcastIndexApiKey';
44-
const kPodcastIndexApiSecret = 'podcastIndexApiSecret';
45-
const kUseArtistGridView = 'useArtistGridView';
46-
47-
const kFavRadioTags = 'favRadioTags';
48-
const kFavCountryCodes = 'favCountryCodes';
49-
const kFavLanguageCodes = 'favLanguageCodes';
50-
const kAscendingFeeds = 'ascendingfeed:::';
51-
const kPatchNotesDisposed = 'kPatchNotesDisposed';
52-
const kCloseBtnAction = 'closeBtnAction';
53-
const kUseMoreAnimations = 'useMoreAnimations';
54-
const kShowPositionDuration = 'showPositionDuration';
55-
56-
const kPlaybackRate = 'playbackRate';
57-
const kNotifyDataSafeMode = 'notifyDataSafeMode';
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import 'package:shared_preferences/shared_preferences.dart';
2+
3+
extension SPKeys on SharedPreferences {
4+
static const directory = 'directory';
5+
static const downloads = 'downloadsCustomDir';
6+
static const localAudioIndex = 'localAudioIndex';
7+
static const neverShowImportFails = 'neverShowImportFails';
8+
static const enableDiscord = 'enableDiscordRPC';
9+
static const enableLastFm = 'enableLastFmScrobbling';
10+
static const lastFmApiKey = 'lastFmApiKey';
11+
static const lastFmSecret = 'lastFmSecret';
12+
static const lastFmSessionKey = 'lastFmSessionKey';
13+
static const lastFmUsername = 'lastFmUsername';
14+
static const enableListenBrainz = 'enableListenBrainzScrobbling';
15+
static const listenBrainzApiKey = 'listenBrainzApiKey';
16+
static const lastCountryCode = 'lastCountryCode';
17+
static const lastLanguageCode = 'lastLanguageCode';
18+
static const usePodcastIndex = 'usePodcastIndex';
19+
static const themeIndex = 'themeIndex';
20+
static const podcastIndexApiKey = 'podcastIndexApiKey';
21+
static const podcastIndexApiSecret = 'podcastIndexApiSecret';
22+
static const favRadioTags = 'favRadioTags';
23+
static const favCountryCodes = 'favCountryCodes';
24+
static const favLanguageCodes = 'favLanguageCodes';
25+
static const ascendingFeeds = 'ascendingfeed:::';
26+
static const patchNotesDisposed = 'kPatchNotesDisposed';
27+
static const closeBtnAction = 'closeBtnAction';
28+
static const useMoreAnimations = 'useMoreAnimations';
29+
static const showPositionDuration = 'showPositionDuration';
30+
static const notifyDataSafeMode = 'notifyDataSafeMode';
31+
}

lib/library/library_service.dart

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import '../common/data/audio.dart';
88
import '../common/file_names.dart';
99
import '../common/page_ids.dart';
1010
import '../common/view/audio_filter.dart';
11-
import '../constants.dart';
11+
import '../extensions/shared_preferences_x.dart';
1212
import '../persistence_utils.dart';
1313

1414
class LibraryService {
@@ -96,14 +96,14 @@ class LibraryService {
9696
}
9797

9898
Set<String> get favRadioTags =>
99-
_sharedPreferences.getStringList(kFavRadioTags)?.toSet() ?? {};
99+
_sharedPreferences.getStringList(SPKeys.favRadioTags)?.toSet() ?? {};
100100
bool isFavTag(String value) => favRadioTags.contains(value);
101101

102102
void addFavRadioTag(String name) {
103103
if (favRadioTags.contains(name)) return;
104104
final Set<String> tags = favRadioTags;
105105
tags.add(name);
106-
_sharedPreferences.setStringList(kFavRadioTags, tags.toList()).then(
106+
_sharedPreferences.setStringList(SPKeys.favRadioTags, tags.toList()).then(
107107
(saved) {
108108
if (saved) _propertiesChangedController.add(true);
109109
},
@@ -114,32 +114,33 @@ class LibraryService {
114114
if (!favRadioTags.contains(name)) return;
115115
final Set<String> tags = favRadioTags;
116116
tags.remove(name);
117-
_sharedPreferences.setStringList(kFavRadioTags, tags.toList()).then(
117+
_sharedPreferences.setStringList(SPKeys.favRadioTags, tags.toList()).then(
118118
(saved) {
119119
if (saved) _propertiesChangedController.add(true);
120120
},
121121
);
122122
}
123123

124-
String? get lastCountryCode => _sharedPreferences.getString(kLastCountryCode);
124+
String? get lastCountryCode =>
125+
_sharedPreferences.getString(SPKeys.lastCountryCode);
125126
void setLastCountryCode(String value) {
126-
_sharedPreferences.setString(kLastCountryCode, value).then(
127+
_sharedPreferences.setString(SPKeys.lastCountryCode, value).then(
127128
(saved) {
128129
if (saved) _propertiesChangedController.add(true);
129130
},
130131
);
131132
}
132133

133134
Set<String> get favCountryCodes =>
134-
_sharedPreferences.getStringList(kFavCountryCodes)?.toSet() ?? {};
135+
_sharedPreferences.getStringList(SPKeys.favCountryCodes)?.toSet() ?? {};
135136
bool isFavCountry(String value) => favCountryCodes.contains(value);
136137

137138
void addFavCountryCode(String name) {
138139
if (favCountryCodes.contains(name)) return;
139140
final favCodes = favCountryCodes;
140141
favCodes.add(name);
141142
_sharedPreferences
142-
.setStringList(kFavCountryCodes, favCodes.toList())
143+
.setStringList(SPKeys.favCountryCodes, favCodes.toList())
143144
.then((saved) {
144145
if (saved) _propertiesChangedController.add(true);
145146
});
@@ -150,29 +151,31 @@ class LibraryService {
150151
final favCodes = favCountryCodes;
151152
favCodes.remove(name);
152153
_sharedPreferences
153-
.setStringList(kFavCountryCodes, favCodes.toList())
154+
.setStringList(SPKeys.favCountryCodes, favCodes.toList())
154155
.then((saved) {
155156
if (saved) _propertiesChangedController.add(true);
156157
});
157158
}
158159

159160
String? get lastLanguageCode =>
160-
_sharedPreferences.getString(kLastLanguageCode);
161+
_sharedPreferences.getString(SPKeys.lastLanguageCode);
161162
void setLastLanguageCode(String value) {
162-
_sharedPreferences.setString(kLastLanguageCode, value).then((saved) {
163+
_sharedPreferences.setString(SPKeys.lastLanguageCode, value).then((saved) {
163164
if (saved) _propertiesChangedController.add(true);
164165
});
165166
}
166167

167168
Set<String> get favLanguageCodes =>
168-
_sharedPreferences.getStringList(kFavLanguageCodes)?.toSet() ?? {};
169+
_sharedPreferences.getStringList(SPKeys.favLanguageCodes)?.toSet() ?? {};
169170
bool isFavLanguage(String value) => favLanguageCodes.contains(value);
170171

171172
void addFavLanguageCode(String name) {
172173
if (favLanguageCodes.contains(name)) return;
173174
final favLangs = favLanguageCodes;
174175
favLangs.add(name);
175-
_sharedPreferences.setStringList(kFavLanguageCodes, favLangs.toList()).then(
176+
_sharedPreferences
177+
.setStringList(SPKeys.favLanguageCodes, favLangs.toList())
178+
.then(
176179
(saved) {
177180
if (saved) _propertiesChangedController.add(true);
178181
},
@@ -183,7 +186,9 @@ class LibraryService {
183186
if (!favLanguageCodes.contains(name)) return;
184187
final favLangs = favLanguageCodes;
185188
favLangs.remove(name);
186-
_sharedPreferences.setStringList(kFavLanguageCodes, favLangs.toList()).then(
189+
_sharedPreferences
190+
.setStringList(SPKeys.favLanguageCodes, favLangs.toList())
191+
.then(
187192
(saved) {
188193
if (saved) _propertiesChangedController.add(true);
189194
},
@@ -430,16 +435,18 @@ class LibraryService {
430435
}
431436

432437
bool showPodcastAscending(String feedUrl) =>
433-
_sharedPreferences.getBool(kAscendingFeeds + feedUrl) ?? false;
438+
_sharedPreferences.getBool(SPKeys.ascendingFeeds + feedUrl) ?? false;
434439

435440
Future<void> _addAscendingPodcast(String feedUrl) async {
436-
await _sharedPreferences.setBool(kAscendingFeeds + feedUrl, true).then(
441+
await _sharedPreferences
442+
.setBool(SPKeys.ascendingFeeds + feedUrl, true)
443+
.then(
437444
(_) => _propertiesChangedController.add(true),
438445
);
439446
}
440447

441448
Future<void> _removeAscendingPodcast(String feedUrl) async =>
442-
_sharedPreferences.remove(kAscendingFeeds + feedUrl).then(
449+
_sharedPreferences.remove(SPKeys.ascendingFeeds + feedUrl).then(
443450
(_) => _propertiesChangedController.add(true),
444451
);
445452

lib/radio/online_art_service.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ import '../extensions/string_x.dart';
1010

1111
const _kMusicBrainzAddress = 'https://musicbrainz.org/ws/2/recording/';
1212
const _kCoverArtArchiveAddress = 'https://coverartarchive.org/release/';
13+
const _kMusicBrainzHeaders = {
14+
'Accept': 'application/json',
15+
'User-Agent': '$kAppTitle ($kRepoUrl)',
16+
};
17+
18+
const _kInternetArchiveHeaders = {
19+
'User-Agent': '$kAppTitle ($kRepoUrl)',
20+
};
1321

1422
class OnlineArtService {
1523
OnlineArtService({required Dio dio}) : _dio = dio;
@@ -63,7 +71,7 @@ class _ComputeCapsule {
6371

6472
Future<String?> _fetchAlbumArt(_ComputeCapsule capsule) async {
6573
final dio = capsule.dio;
66-
dio.options.headers = kMusicBrainzHeaders;
74+
dio.options.headers = _kMusicBrainzHeaders;
6775
final songInfo = capsule.icyTitle.splitByDash;
6876
if (songInfo.songName == null || songInfo.artist == null) return null;
6977

@@ -119,7 +127,7 @@ Future<String?> _fetchAlbumArtUrlFromReleaseId({
119127
required Dio dio,
120128
}) async {
121129
try {
122-
dio.options.headers = kInternetArchiveHeaders;
130+
dio.options.headers = _kInternetArchiveHeaders;
123131
dio.options.followRedirects = true;
124132
dio.options.maxRedirects = 5;
125133
dio.options.receiveTimeout = const Duration(seconds: 25);

0 commit comments

Comments
 (0)