Skip to content

Commit 3db1acd

Browse files
committed
chore: random stuff
1 parent c76fb10 commit 3db1acd

33 files changed

+748
-538
lines changed

lib/Api/Anilist/AnilistMutations.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ import 'package:dartotsu/Functions/Function.dart';
33
import 'package:dartotsu/Functions/string_extensions.dart';
44
import 'package:dartotsu/Services/Api/Mutations.dart';
55
import 'package:flutter/cupertino.dart';
6-
import 'package:get/get.dart';
7-
import 'package:get/get_core/src/get_main.dart';
86

9-
import '../../Screens/Detail/Tabs/Watch/Anime/AnimeParser.dart';
107
import 'Anilist.dart';
118
import 'Data/data.dart';
129
import 'Data/fuzzyData.dart';

lib/Api/Anilist/AnilistQueries/GetBannerImages.dart

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@ extension on AnilistQueries {
2222
if (url == null || url.isEmpty || checkTime()) {
2323
final response =
2424
await executeQuery<MediaListCollectionResponse>(_queryBanner(type));
25-
final bannerImages = response?.data?.mediaListCollection?.lists
26-
?.expand((list) => list.entries ?? [] as List<api.MediaList>)
27-
.where((e) => !(e.media?.isAdult == true))
28-
.map((entry) => entry.media?.bannerImage)
29-
.where((imageUrl) => imageUrl != null && imageUrl != 'null')
30-
.toList() ??
31-
[];
32-
bannerImages.shuffle(Random());
33-
var random = bannerImages.isNotEmpty ? bannerImages.first : null;
25+
final entries = response?.data?.mediaListCollection?.lists
26+
?.expand((list) => list.entries ?? [] as List<api.MediaList>)
27+
.where((e) => !(e.media?.isAdult ?? false))
28+
.map((e) => e.media?.bannerImage)
29+
.where((url) => url != null && url != 'null')
30+
.cast<String>()
31+
.toList() ?? [];
32+
33+
entries.shuffle(Random());
34+
var random = entries.isNotEmpty ? entries.first : null;
3435

3536
saveCustomData("banner_${type}_url", random);
3637
saveCustomData(

lib/Api/Anilist/AnilistQueries/GetHomePageData.dart

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,23 @@ extension on AnilistQueries {
1313

1414
Future<void> processMedia(String type, List<api.MediaList>? currentMedia,
1515
List<api.MediaList>? repeatingMedia) async {
16-
(List<Media>, List<Media>) process(Map<String, dynamic> params) {
16+
(List<Media>, List<Media>) process(
17+
List<api.MediaList> l, List<int> c, List<int> r, bool h) {
1718
Map<int, Media> subMap = {};
1819
List<Media> returnArray = [];
1920
List<Media> removedMedia = [];
20-
var removeList = params["removeList"] as List<int>;
21-
var hidePrivate = params["hidePrivate"] as bool;
22-
for (var entry in (params["list"] ?? []) as List<api.MediaList>) {
21+
for (var entry in l) {
2322
var media = Media.mediaListData(entry);
24-
if (!removeList.contains(media.id) &&
25-
(!hidePrivate || !media.isListPrivate)) {
23+
if (!r.contains(media.id) && (!h || !media.isListPrivate)) {
2624
media.cameFromContinue = true;
2725
subMap[media.id] = media;
2826
} else {
2927
removedMedia.add(media);
3028
}
3129
}
32-
var list = params["continueList"] as List<int>;
33-
if (list.isNotEmpty) {
34-
returnArray.addAll(list.reversed
30+
31+
if (c.isNotEmpty) {
32+
returnArray.addAll(c.reversed
3533
.where((id) => subMap.containsKey(id))
3634
.map((id) => subMap[id]!));
3735
returnArray
@@ -45,28 +43,23 @@ extension on AnilistQueries {
4543

4644
List<int> list = loadCustomData<List<int>>("continue${type}List") ?? [];
4745
var mediaList = (currentMedia ?? []) + (repeatingMedia ?? []);
48-
var returnArray = await compute(process, {
49-
"list": mediaList,
50-
"removeList": removeList,
51-
"hidePrivate": hidePrivate,
52-
"continueList": list
53-
});
46+
var returnArray = await isolate(
47+
() => process(mediaList, list, removeList, hidePrivate),
48+
);
5449

5550
removedMedia.addAll(returnArray.$2);
5651
returnMap["current$type"] = returnArray.$1;
5752
}
5853

5954
Future<void> processFavorites(
6055
String type, List<api.MediaEdge>? favorites) async {
61-
(List<Media>, List<Media>) process(Map<String, dynamic> params) {
56+
(List<Media>, List<Media>) process(
57+
List<api.MediaEdge> l, List<int> r, bool h) {
6258
List<Media> returnArray = [];
6359
List<Media> removedMedia = [];
64-
var removeList = params["removeList"] as List<int>;
65-
var hidePrivate = params["hidePrivate"] as bool;
66-
for (var entry in (params["list"] ?? []) as List<api.MediaEdge>) {
60+
for (var entry in l) {
6761
var media = Media.mediaEdgeData(entry);
68-
if (!removeList.contains(media.id) &&
69-
(!hidePrivate || !media.isListPrivate)) {
62+
if (!r.contains(media.id) && (!h || !media.isListPrivate)) {
7063
returnArray.add(media);
7164
} else {
7265
removedMedia.add(media);
@@ -75,11 +68,10 @@ extension on AnilistQueries {
7568
return (returnArray, removedMedia);
7669
}
7770

78-
var returnArray = await compute(process, {
79-
"list": favorites,
80-
"removeList": removeList,
81-
"hidePrivate": hidePrivate,
82-
});
71+
var returnArray = await isolate(
72+
() => process(favorites ?? [], removeList, hidePrivate),
73+
);
74+
8375
removedMedia.addAll(returnArray.$2);
8476
returnMap["favorite$type"] = returnArray.$1;
8577
}
@@ -98,10 +90,8 @@ extension on AnilistQueries {
9890
List<api.MediaList>? b,
9991
) async {
10092
Map<int, Media> subMap = {};
101-
List<Media> process(Map<String, dynamic> params) {
102-
var recommendations =
103-
(params["recommended"] ?? []) as List<Recommendation>;
104-
for (var entry in recommendations) {
93+
List<Media> process(List<Recommendation> r, List<api.MediaList> l) {
94+
for (var entry in r) {
10595
var mediaRecommendation = entry.mediaRecommendation;
10696
if (mediaRecommendation != null) {
10797
var media = Media.mediaData(mediaRecommendation);
@@ -110,8 +100,7 @@ extension on AnilistQueries {
110100
}
111101
}
112102

113-
var mediaList = (params["list"] ?? []) as List<api.MediaList>;
114-
for (var entry in mediaList) {
103+
for (var entry in l) {
115104
var media = Media.mediaListData(entry);
116105
if (['RELEASING', 'FINISHED'].contains(media.status)) {
117106
media.relation = entry.media?.type?.name ?? "";
@@ -124,8 +113,12 @@ extension on AnilistQueries {
124113
return list;
125114
}
126115

127-
var list = await compute(
128-
process, {"list": (a ?? []) + (b ?? []), "recommended": r});
116+
var list = await isolate(
117+
() => process(
118+
r ?? [],
119+
(a ?? []) + (b ?? []),
120+
),
121+
);
129122

130123
returnMap["recommendations"] = list;
131124
}

lib/Api/EpisodeDetails/GetMediaIDs/GetMediaIDs.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,9 @@ class GetMediaIDs {
9393
}
9494

9595
if (data != null && !checkTime()) {
96-
var jsonData = jsonDecode(data);
96+
var jsonData = jsonDecode(data) as List<dynamic>;
9797
_animeListFuture = jsonData
9898
.map((e) => AnimeID.fromJson(e))
99-
.whereType<AnimeID>()
10099
.toList();
101100
loading.value = false;
102101
return _animeListFuture;

lib/Api/Simkl/Data/Generated/Media.g.dart

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Api/Simkl/Data/Media.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,10 @@ enum Status {
529529
@JsonValue("soon")
530530
SOON,
531531
@JsonValue("premiere")
532-
PREMIERE
532+
PREMIERE,
533+
534+
@JsonValue("production")
535+
PRODUCTION
533536
}
534537

535538
final statusValues = EnumValues({
@@ -541,7 +544,8 @@ final statusValues = EnumValues({
541544
"ongoing": Status.ONGOING,
542545
"ended": Status.ENDED,
543546
'soon': Status.SOON,
544-
'premiere': Status.PREMIERE
547+
'premiere': Status.PREMIERE,
548+
'production': Status.PRODUCTION
545549
});
546550

547551
class EnumValues<T> {

lib/Functions/Function.dart

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:async';
2+
import 'dart:isolate';
23

34
import 'package:dartotsu/logger.dart';
45
import 'package:flutter/material.dart';
@@ -182,3 +183,56 @@ List<T> mergeMapValues<T>(Map<String, List<T>> dataMap) {
182183

183184
return uniqueItems.toList();
184185
}
186+
187+
Future<T> isolate<T>(T Function() fn) async {
188+
final receivePort = ReceivePort();
189+
final errorPort = ReceivePort();
190+
191+
await Isolate.spawn<_IsolatePayload<T>>(
192+
_isolateEntry,
193+
_IsolatePayload<T>(
194+
sendPort: receivePort.sendPort,
195+
fn: fn,
196+
),
197+
onError: errorPort.sendPort,
198+
);
199+
200+
final completer = Completer<T>();
201+
202+
receivePort.listen((data) {
203+
if (!completer.isCompleted) {
204+
completer.complete(data as T);
205+
}
206+
receivePort.close();
207+
errorPort.close();
208+
});
209+
210+
errorPort.listen((e) {
211+
if (!completer.isCompleted) {
212+
completer.completeError(e);
213+
}
214+
receivePort.close();
215+
errorPort.close();
216+
});
217+
218+
return completer.future;
219+
}
220+
221+
class _IsolatePayload<T> {
222+
final SendPort sendPort;
223+
final T Function() fn;
224+
225+
_IsolatePayload({
226+
required this.sendPort,
227+
required this.fn,
228+
});
229+
}
230+
231+
void _isolateEntry<T>(_IsolatePayload<T> payload) {
232+
try {
233+
final result = payload.fn();
234+
payload.sendPort.send(result);
235+
} catch (e) {
236+
Isolate.exit(payload.sendPort, e);
237+
}
238+
}

lib/Screens/Anime/AnimeScreen.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import '../../Services/Screens/BaseAnimeScreen.dart';
1212
import '../../Services/ServiceSwitcher.dart';
1313
import '../../Theme/Colors.dart';
1414
import '../../Theme/ThemeProvider.dart';
15-
import '../../Theme/LanguageSwitcher.dart';
1615
import '../../Widgets/ScrollConfig.dart';
1716
import '../Home/Widgets/LoadingWidget.dart';
1817
import '../Home/Widgets/ServiceSwitcherBar.dart';

0 commit comments

Comments
 (0)