Skip to content

Commit a640f33

Browse files
committed
chore: cleanup pref manager
1 parent 7f1a047 commit a640f33

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+170
-152
lines changed

lib/Adaptor/Episode/Widget/HandleProgress.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ Widget handleProgress({
1313
var sourceName =
1414
Provider.of<MediaServiceProvider>(context).currentService.getName;
1515
var currentProgress =
16-
PrefManager.getCustomVal<int>("$mediaId-$ep-$sourceName-current");
16+
loadCustomData<int>("$mediaId-$ep-$sourceName-current");
1717
var maxProgress =
18-
PrefManager.getCustomVal<int>("$mediaId-$ep-$sourceName-max");
18+
loadCustomData<int>("$mediaId-$ep-$sourceName-max");
1919
if (currentProgress == null || maxProgress == null || maxProgress == 0) {
2020
return const SizedBox.shrink();
2121
}

lib/Preferences/PrefManager.dart

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class PrefManager {
5151
static late Isar _irrelevantPreferences;
5252
static late Isar _protectedPreferences;
5353

54-
static final Map<Location, Map<String, dynamic>> _cache = {
54+
static final Map<Location, Map<String, dynamic>> cache = {
5555
Location.General: {},
5656
Location.Irrelevant: {},
5757
Location.Protected: {}, // add more and ios will crash
@@ -90,36 +90,36 @@ class PrefManager {
9090
final isar = _getPrefBox(location);
9191
final keyValues = await isar.keyValues.where().findAll();
9292
for (var item in keyValues) {
93-
_cache[location]?[item.key] = item.value;
93+
cache[location]?[item.key] = item.value;
9494
}
9595
final showResponse = await isar.showResponses.where().findAll();
9696
for (var item in showResponse) {
97-
_cache[location]?[item.key] = item;
97+
cache[location]?[item.key] = item;
9898
}
9999
final selected = await isar.selecteds.where().findAll();
100100
for (var item in selected) {
101-
_cache[location]?[item.key] = item;
101+
cache[location]?[item.key] = item;
102102
}
103103
final responseToken = await isar.responseTokens.where().findAll();
104104
for (var item in responseToken) {
105-
_cache[location]?[item.key] = item;
105+
cache[location]?[item.key] = item;
106106
}
107107
final playerSettings = await isar.playerSettings.where().findAll();
108108
for (var item in playerSettings) {
109-
_cache[location]?[item.key] = item;
109+
cache[location]?[item.key] = item;
110110
}
111111
}
112112
}
113113

114114
static void setVal<T>(Pref<T> pref, T value) {
115-
_cache[pref.location]?[pref.key] = value;
115+
cache[pref.location]?[pref.key] = value;
116116
final isar = _getPrefBox(pref.location);
117117
return _writeToIsar(isar, pref.key, value);
118118
}
119119

120120
static T getVal<T>(Pref<T> pref) {
121-
if (_cache[pref.location]?.containsKey(pref.key) == true) {
122-
return _cache[pref.location]![pref.key] as T;
121+
if (cache[pref.location]?.containsKey(pref.key) == true) {
122+
return cache[pref.location]![pref.key] as T;
123123
}
124124
return pref.defaultValue;
125125
}
@@ -130,16 +130,16 @@ class PrefManager {
130130
Location location = Location.Irrelevant,
131131
}) {
132132
final isar = _getPrefBox(location);
133-
_cache[location]?[key] = value;
133+
cache[location]?[key] = value;
134134
return _writeToIsar(isar, key, value);
135135
}
136136

137137
static T? getCustomVal<T>(
138138
String key, {
139139
Location location = Location.Irrelevant,
140140
}) {
141-
if (_cache[location]?.containsKey(key) == true) {
142-
return _cache[location]![key] as T;
141+
if (cache[location]?.containsKey(key) == true) {
142+
return cache[location]![key] as T;
143143
}
144144
return null;
145145
}
@@ -149,7 +149,7 @@ class PrefManager {
149149
T value, {
150150
Location location = Location.Irrelevant,
151151
}) async {
152-
_cache[location]?[key] = value;
152+
cache[location]?[key] = value;
153153
final isar = _getPrefBox(location);
154154
final keyValue = KeyValue()
155155
..key = key
@@ -167,7 +167,7 @@ class PrefManager {
167167
}
168168

169169
static void removeVal(Pref<dynamic> pref) async {
170-
_cache[pref.location]?.remove(pref.key);
170+
cache[pref.location]?.remove(pref.key);
171171
final isar = _getPrefBox(pref.location);
172172
return isar.writeTxn(() => isar.keyValues.deleteByKey(pref.key));
173173
}
@@ -176,10 +176,23 @@ class PrefManager {
176176
String key, {
177177
Location location = Location.Irrelevant,
178178
}) async {
179-
_cache[location]?.remove(key);
179+
cache[location]?.remove(key);
180180
final isar = _getPrefBox(location);
181181
return isar.writeTxn(() => isar.keyValues.deleteByKey(key));
182182
}
183+
static void removeEverything() async {
184+
185+
}
186+
static void addEverything(Map<Location, Map<String, dynamic>> cache) async {
187+
for (var location in Location.values) {
188+
final isar = _getPrefBox(location);
189+
for (var key in cache[location]!.keys) {
190+
final value = cache[location]![key];
191+
cache[location]?[key] = value;
192+
_writeToIsar(isar, key, value);
193+
}
194+
}
195+
}
183196

184197
static void _writeToIsar<T>(Isar? isar, String key, T value) {
185198
if (isar == null) return;

lib/Screens/Detail/Tabs/Watch/Anime/Widget/AnimeCompactSettings.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,15 +205,15 @@ class AnimeCompactSettings {
205205
Provider.of<MediaServiceProvider>(Get.context!, listen: false)
206206
.currentService
207207
.getName;
208-
PrefManager.setCustomVal("Selected-${media.id}-$sourceName", settings);
208+
saveCustomData("Selected-${media.id}-$sourceName", settings);
209209
}
210210

211211
Selected loadSelected() {
212212
var sourceName =
213213
Provider.of<MediaServiceProvider>(Get.context!, listen: false)
214214
.currentService
215215
.getName;
216-
return PrefManager.getCustomVal("Selected-${media.id}-$sourceName") ??
216+
return loadCustomData("Selected-${media.id}-$sourceName") ??
217217
Selected();
218218
}
219219
}

lib/Screens/Detail/Tabs/Watch/BaseParser.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ abstract class BaseParser extends GetxController {
5252
}
5353

5454
var lastUsedSource =
55-
PrefManager.getCustomVal<String>('${media.id}-lastUsedSource');
55+
loadCustomData<String>('${media.id}-lastUsedSource');
5656
if (lastUsedSource == null ||
5757
!sortedSources.any((e) => nameAndLang(e) == lastUsedSource)) {
5858
lastUsedSource = nameAndLang(sortedSources.first);
@@ -72,15 +72,15 @@ abstract class BaseParser extends GetxController {
7272
Provider.of<MediaServiceProvider>(Get.context!, listen: false)
7373
.currentService
7474
.getName;
75-
PrefManager.setCustomVal("Selected-$id-$sourceName", data);
75+
saveCustomData("Selected-$id-$sourceName", data);
7676
}
7777

7878
Selected loadSelected(Media mediaData) {
7979
var sourceName =
8080
Provider.of<MediaServiceProvider>(Get.context!, listen: false)
8181
.currentService
8282
.getName;
83-
return PrefManager.getCustomVal("Selected-${mediaData.id}-$sourceName") ??
83+
return loadCustomData("Selected-${mediaData.id}-$sourceName") ??
8484
Selected();
8585
}
8686

@@ -221,7 +221,7 @@ abstract class BaseParser extends GetxController {
221221
}
222222

223223
ShowResponse? _loadShowResponse(Source source, Media mediaData) {
224-
return PrefManager.getCustomVal<ShowResponse?>(
224+
return loadCustomData<ShowResponse?>(
225225
"${source.name}_${mediaData.id}_source");
226226
}
227227

@@ -233,7 +233,7 @@ abstract class BaseParser extends GetxController {
233233
name: response.name!,
234234
link: response.link!,
235235
coverUrl: response.imageUrl!);
236-
PrefManager.setCustomVal<ShowResponse>(
236+
saveCustomData<ShowResponse>(
237237
"${source.name}_${mediaData.id}_source", show);
238238
}
239239

lib/Screens/Detail/Tabs/Watch/BaseWatchScreen.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ abstract class BaseWatchScreen<T extends StatefulWidget> extends State<T> {
133133

134134
List<Widget> _buildYouTubeButton() {
135135
if (mediaData.anime?.youtube == null ||
136-
!PrefManager.getVal(PrefName.showYtButton)) {
136+
!loadData(PrefName.showYtButton)) {
137137
return [];
138138
}
139139

lib/Screens/Detail/Tabs/Watch/Manga/Widget/MangaCompactSettings.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,15 +240,15 @@ class MangaCompactSettings {
240240
Provider.of<MediaServiceProvider>(Get.context!, listen: false)
241241
.currentService
242242
.getName;
243-
PrefManager.setCustomVal("Selected-${media.id}-$sourceName", settings);
243+
saveCustomData("Selected-${media.id}-$sourceName", settings);
244244
}
245245

246246
Selected loadSelected() {
247247
var sourceName =
248248
Provider.of<MediaServiceProvider>(Get.context!, listen: false)
249249
.currentService
250250
.getName;
251-
return PrefManager.getCustomVal("Selected-${media.id}-$sourceName") ??
251+
return loadCustomData("Selected-${media.id}-$sourceName") ??
252252
Selected();
253253
}
254254
}

lib/Screens/Detail/Tabs/Watch/Widgets/SourceSelector.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class _SourceSelectorState extends ConsumerState<SourceSelector> {
5555
: source.name!;
5656
}
5757

58-
var lastUsedSource = PrefManager.getCustomVal<String>(
58+
var lastUsedSource = loadCustomData<String>(
5959
'${widget.mediaData.id}-lastUsedSource');
6060
if (lastUsedSource == null ||
6161
!sources.any((e) => nameAndLang(e) == lastUsedSource)) {
@@ -79,7 +79,7 @@ class _SourceSelectorState extends ConsumerState<SourceSelector> {
7979
borderColor: theme.primary,
8080
prefixIcon: Icons.source,
8181
onChanged: (name) async {
82-
PrefManager.setCustomVal(
82+
saveCustomData(
8383
'${widget.mediaData.id}-lastUsedSource', name);
8484
lastUsedSource = name;
8585
source = sources.firstWhereOrNull(

lib/Screens/Extensions/ExtensionList.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class _ExtensionScreenState extends ConsumerState<Extension> {
114114
widget.query.isEmpty ||
115115
element.name!.toLowerCase().contains(widget.query.toLowerCase()))
116116
.where((element) =>
117-
PrefManager.getVal(PrefName.NSFWExtensions) ||
117+
loadData(PrefName.NSFWExtensions) ||
118118
element.isNsfw == false)
119119
.toList();
120120
}

lib/Screens/Extensions/ExtensionScreen.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ Widget _extensionUpdateNumbers(BuildContext context, ItemType itemType,
254254
if (snapshot.hasData && snapshot.data!.isNotEmpty) {
255255
final entries = snapshot.data!
256256
.where(
257-
(element) => PrefManager.getVal(PrefName.NSFWExtensions)
257+
(element) => loadData(PrefName.NSFWExtensions)
258258
? true
259259
: element.isNsfw == false,
260260
)

lib/Screens/Player/Player.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class MediaPlayerState extends State<MediaPlayer>
105105
currentQuality = widget.videos[widget.index];
106106
videoPlayerController = WindowsPlayer(resizeMode, settings);
107107
var sourceName = context.currentService(listen: false).getName;
108-
var currentProgress = PrefManager.getCustomVal<int>(
108+
var currentProgress = loadCustomData<int>(
109109
"${widget.media.id}-${widget.currentEpisode.number}-$sourceName-current",
110110
);
111111
videoPlayerController.open(
@@ -114,7 +114,7 @@ class MediaPlayerState extends State<MediaPlayer>
114114
}
115115

116116
void _loadPlayerSettings() {
117-
settings = PrefManager.getVal(PrefName.playerSettings);
117+
settings = loadData(PrefName.playerSettings);
118118

119119
widget.media.anime?.playerSettings = settings;
120120

@@ -673,15 +673,15 @@ class MediaPlayerState extends State<MediaPlayer>
673673
Provider.of<MediaServiceProvider>(Get.context!, listen: false)
674674
.currentService
675675
.getName;
676-
PrefManager.setCustomVal("Selected-$id-$sourceName", data);
676+
saveCustomData("Selected-$id-$sourceName", data);
677677
}
678678

679679
Selected loadSelected(m.Media mediaData) {
680680
var sourceName =
681681
Provider.of<MediaServiceProvider>(Get.context!, listen: false)
682682
.currentService
683683
.getName;
684-
return PrefManager.getCustomVal("Selected-${mediaData.id}-$sourceName") ??
684+
return loadCustomData("Selected-${mediaData.id}-$sourceName") ??
685685
Selected();
686686
}
687687
}

0 commit comments

Comments
 (0)