Skip to content

Commit 24d11d6

Browse files
committed
feat: cache episodeList
1 parent 5e78eda commit 24d11d6

File tree

7 files changed

+117
-127
lines changed

7 files changed

+117
-127
lines changed

lib/Screens/Info/MediaScreen.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class MediaInfoPageState extends State<MediaInfoPage> {
3333
@override
3434
void initState() {
3535
super.initState();
36-
_viewModel = Get.put(MediaPageViewModel(), tag: widget.mediaData.id.toString());
36+
_viewModel =
37+
Get.put(MediaPageViewModel(), tag: widget.mediaData.id.toString());
3738
load();
3839
}
3940

@@ -106,7 +107,8 @@ class MediaInfoPageState extends State<MediaInfoPage> {
106107
),
107108
BottomNavigationBarItem(
108109
icon: Icon(
109-
isAnime ? Icons.movie_filter_rounded : Icons.import_contacts),
110+
isAnime ? Icons.movie_filter_rounded : Icons.import_contacts,
111+
),
110112
label: isAnime ? 'WATCH' : 'READ',
111113
),
112114
const BottomNavigationBarItem(

lib/Screens/Info/Tabs/Watch/Anime/AnimeParser.dart

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,11 @@ class AnimeParser extends BaseParser {
2020
var kitsuEpisodeList = Rxn<Map<String, Episode>>(null);
2121
var fillerEpisodesList = Rxn<Map<String, Episode>>(null);
2222
var viewType = 0.obs;
23+
var dataLoaded = false.obs;
2324

24-
@override
25-
void reset() {
26-
super.reset();
27-
episodeList.value = null;
28-
anifyEpisodeList.value = null;
29-
kitsuEpisodeList.value = null;
30-
fillerEpisodesList.value = null;
31-
}
3225

3326
void init(media mediaData) async {
27+
if (dataLoaded.value) return;
3428
viewType.value = mediaData.selected?.recyclerStyle ??
3529
PrefManager.getVal(PrefName.AnimeDefaultView);
3630
await Future.wait([
@@ -40,10 +34,10 @@ class AnimeParser extends BaseParser {
4034
}
4135

4236
@override
43-
Future<void> wrongTitle(context, source, mediaData, onChange) async {
44-
super.wrongTitle(context, source, mediaData, (m) {
37+
Future<void> wrongTitle(context, mediaData, onChange) async {
38+
super.wrongTitle(context, mediaData, (m) {
4539
episodeList.value = null;
46-
getEpisode(m, source);
40+
getEpisode(m, source.value!);
4741
});
4842
}
4943

@@ -56,7 +50,7 @@ class AnimeParser extends BaseParser {
5650
void getEpisode(MManga media, Source source) async {
5751
if (media.link == null) return;
5852
var m = await getDetail(url: media.link!, source: source);
59-
53+
dataLoaded.value = true;
6054
var chapters = m.chapters;
6155
episodeList.value = Map.fromEntries(
6256
chapters?.reversed.map((e) {

lib/Screens/Info/Tabs/Watch/Anime/AnimeWatchScreen.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ class AnimeWatchScreenState extends BaseWatchScreen<AnimeWatchScreen> {
3131
void initState() {
3232
super.initState();
3333
_viewModel = Get.put(AnimeParser(), tag: widget.mediaData.id.toString());
34+
3435
widget.mediaData.selected = _viewModel.loadSelected(widget.mediaData);
36+
3537
WidgetsBinding.instance.addPostFrameCallback((_) {
36-
_viewModel.reset();
3738
_viewModel.init(widget.mediaData);
3839
});
3940
}

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@ import 'Widgets/WrongTitle.dart';
1515
abstract class BaseParser extends GetxController {
1616
var selectedMedia = Rxn<MManga?>(null);
1717
var status = Rxn<String>(null);
18+
var source = Rxn<Source>(null);
1819

19-
@mustCallSuper
20-
void reset() {
21-
selectedMedia.value = null;
22-
status.value = null;
23-
}
2420

2521
void saveSelected(int id , Selected data) {
2622
PrefManager.setCustomVal("Selected-$id", data);
@@ -165,16 +161,15 @@ abstract class BaseParser extends GetxController {
165161

166162
Future<void> wrongTitle(
167163
BuildContext context,
168-
Source source,
169164
media mediaData,
170165
Function(MManga)? onChange) async {
171166
var dialog = WrongTitleDialog(
172-
source: source,
167+
source: source.value!,
173168
mediaData: mediaData,
174169
selectedMedia: selectedMedia,
175170
onChanged: (m) {
176171
selectedMedia.value = m;
177-
_saveShowResponse(mediaData, m, source, selected: true);
172+
_saveShowResponse(mediaData, m, source.value!, selected: true);
178173
onChange?.call(m);
179174
});
180175
showCustomBottomDialog(context, dialog);

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import 'BaseParser.dart';
1111
import 'Widgets/SourceSelector.dart';
1212

1313
abstract class BaseWatchScreen<T extends StatefulWidget> extends State<T> {
14-
Source? source;
15-
1614
BaseParser get viewModel;
1715

1816
media get mediaData;
@@ -22,7 +20,7 @@ abstract class BaseWatchScreen<T extends StatefulWidget> extends State<T> {
2220
void onSourceChange(Source source) {
2321
WidgetsBinding.instance.addPostFrameCallback((_) {
2422
setState(() {
25-
this.source = source;
23+
viewModel.source.value = source;
2624
viewModel.searchMedia(source, mediaData);
2725
});
2826
});
@@ -35,7 +33,7 @@ abstract class BaseWatchScreen<T extends StatefulWidget> extends State<T> {
3533
children: [
3634
...releasingIn(mediaData, context),
3735
_buildContent(),
38-
if (source != null)
36+
if (viewModel.source.value != null)
3937
...widgetList
4038
else
4139
Center(
@@ -70,12 +68,12 @@ abstract class BaseWatchScreen<T extends StatefulWidget> extends State<T> {
7068
)),
7169
const SizedBox(height: 12),
7270
SourceSelector(
73-
currentSource: source,
71+
currentSource: viewModel.source.value,
7472
onSourceChange: onSourceChange,
7573
mediaData: mediaData,
7674
),
7775
const SizedBox(height: 16),
78-
if (source != null) _buildWrongTitle(),
76+
if (viewModel.source.value != null) _buildWrongTitle(),
7977
],
8078
),
8179
);
@@ -87,7 +85,7 @@ abstract class BaseWatchScreen<T extends StatefulWidget> extends State<T> {
8785
mainAxisAlignment: MainAxisAlignment.end,
8886
children: [
8987
GestureDetector(
90-
onTap: () => viewModel.wrongTitle(context, source!, mediaData, null),
88+
onTap: () => viewModel.wrongTitle(context, mediaData, null),
9189
child: Text(
9290
'Wrong Title?',
9391
style: TextStyle(

lib/Screens/Info/Tabs/Watch/Manga/MangaWatchScreen.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ class MangaWatchScreenState extends BaseWatchScreen<MangaWatchScreen> {
2929
super.initState();
3030
_viewModel = Get.put(MangaParser(), tag: widget.mediaData.id.toString());
3131
widget.mediaData.selected = _viewModel.loadSelected(widget.mediaData);
32-
WidgetsBinding.instance.addPostFrameCallback((_) {
33-
_viewModel.reset();
34-
});
32+
3533
}
3634

3735
@override

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

Lines changed: 96 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ class SourceSelector extends ConsumerStatefulWidget {
1818
final Function(Source source) onSourceChange;
1919
final media mediaData;
2020

21-
const SourceSelector(
22-
{super.key,
23-
this.currentSource,
24-
required this.onSourceChange,
25-
required this.mediaData,});
21+
const SourceSelector({
22+
super.key,
23+
this.currentSource,
24+
required this.onSourceChange,
25+
required this.mediaData,
26+
});
2627

2728
@override
2829
ConsumerState<ConsumerStatefulWidget> createState() => _SourceSelectorState();
@@ -37,103 +38,104 @@ class _SourceSelectorState extends ConsumerState<SourceSelector> {
3738

3839
return Material(
3940
color: Colors.transparent,
40-
child: sources.when(
41-
data: (List<Source> sources) {
42-
List installedSources = sources
43-
.where((source) => source.isAdded!)
44-
.toList()
45-
.reversed
46-
.toList();
41+
child: sources.when(
42+
data: (List<Source> sources) {
43+
List installedSources = sources
44+
.where((source) => source.isAdded!)
45+
.toList()
46+
.reversed
47+
.toList();
4748

48-
if (installedSources.isEmpty) {
49+
String nameAndLang(Source source) {
50+
bool isDuplicateName =
51+
installedSources.where((s) => s.name == source.name).length > 1;
4952

50-
return const Column(
51-
children: [
52-
buildDropdownMenu(
53-
padding: EdgeInsets.all(0),
54-
currentValue: 'No sources installed',
55-
options: ['No sources installed'],
56-
prefixIcon: Icons.source,
57-
),
58-
],
59-
);
60-
}
53+
return isDuplicateName
54+
? '${source.name!} - ${completeLanguageName(source.lang!.toLowerCase())}'
55+
: source.name!;
56+
}
6157

62-
String nameAndLang(Source source) {
63-
bool isDuplicateName =
64-
installedSources.where((s) => s.name == source.name).length > 1;
58+
final sourceName =
59+
installedSources.map((e) => nameAndLang(e)).toList();
60+
var lastUsedSource = PrefManager.getCustomVal<String>(
61+
'${widget.mediaData.id}-lastUsedSource');
62+
if (lastUsedSource == null ||
63+
!installedSources.any((e) => nameAndLang(e) == lastUsedSource)) {
64+
lastUsedSource = nameAndLang(installedSources.first);
65+
}
6566

66-
return isDuplicateName
67-
? '${source.name!} - ${completeLanguageName(source.lang!.toLowerCase())}'
68-
: source.name!;
69-
}
67+
Source source = installedSources
68+
.firstWhereOrNull((e) => nameAndLang(e) == lastUsedSource!);
7069

71-
final sourceName = installedSources.map((e) => nameAndLang(e)).toList();
72-
var lastUsedSource = PrefManager.getCustomVal<String>(
73-
'${widget.mediaData.id}-lastUsedSource');
74-
if (lastUsedSource == null ||
75-
!installedSources.any((e) => nameAndLang(e) == lastUsedSource)) {
76-
lastUsedSource = nameAndLang(installedSources.first);
77-
}
70+
if (widget.currentSource?.id != source.id) {
71+
widget.onSourceChange(source);
72+
}
73+
var theme = Theme.of(context).colorScheme;
7874

79-
var source = installedSources
80-
.firstWhereOrNull((e) => nameAndLang(e) == lastUsedSource!);
81-
82-
if (widget.currentSource != source) {
83-
widget.onSourceChange(source!);
84-
}
85-
var theme = Theme.of(context).colorScheme;
86-
return Column(
87-
children: [
88-
Row(
75+
if (installedSources.isEmpty) {
76+
return const Column(
8977
children: [
90-
Expanded(
91-
child: buildDropdownMenu(
92-
padding: const EdgeInsets.all(0),
93-
currentValue: lastUsedSource,
94-
options: sourceName,
95-
borderColor: theme.primary,
96-
prefixIcon: Icons.source,
97-
onChanged: (name) async {
98-
PrefManager.setCustomVal(
99-
'${widget.mediaData.id}-lastUsedSource', name);
100-
lastUsedSource = name;
101-
source = installedSources.firstWhereOrNull(
102-
(e) => nameAndLang(e) == lastUsedSource!);
103-
if (widget.currentSource != source) {
104-
widget.onSourceChange(source!);
105-
}
106-
},
107-
),
108-
),
109-
const SizedBox(width: 8),
110-
GestureDetector(
111-
onTap: () {
112-
var sourcePreference = getSourcePreference(source: source)
113-
.map(
114-
(e) => getSourcePreferenceEntry(e.key!, source.id!))
115-
.toList();
116-
navigateToPage(
117-
context,
118-
SourcePreferenceWidget(
119-
source: source,
120-
sourcePreference: sourcePreference,
121-
),
122-
);
123-
},
124-
child: Icon(
125-
Icons.settings,
126-
size: 32,
127-
color: theme.onSurface,
128-
),
78+
buildDropdownMenu(
79+
padding: EdgeInsets.all(0),
80+
currentValue: 'No sources installed',
81+
options: ['No sources installed'],
82+
prefixIcon: Icons.source,
12983
),
13084
],
131-
),
132-
],
133-
);
134-
},
135-
error: (error, stackTrace) => Text('Error: $error'),
136-
loading: () => const Center(child: CircularProgressIndicator()),
137-
));
85+
);
86+
}
87+
return Column(
88+
children: [
89+
Row(
90+
children: [
91+
Expanded(
92+
child: buildDropdownMenu(
93+
padding: const EdgeInsets.all(0),
94+
currentValue: lastUsedSource,
95+
options: sourceName,
96+
borderColor: theme.primary,
97+
prefixIcon: Icons.source,
98+
onChanged: (name) async {
99+
PrefManager.setCustomVal(
100+
'${widget.mediaData.id}-lastUsedSource', name);
101+
lastUsedSource = name;
102+
source = installedSources.firstWhereOrNull(
103+
(e) => nameAndLang(e) == lastUsedSource!);
104+
if (widget.currentSource?.id != source.id) {
105+
widget.onSourceChange(source);
106+
}
107+
},
108+
),
109+
),
110+
const SizedBox(width: 8),
111+
GestureDetector(
112+
onTap: () {
113+
var sourcePreference = getSourcePreference(source: source)
114+
.map((e) =>
115+
getSourcePreferenceEntry(e.key!, source.id!))
116+
.toList();
117+
navigateToPage(
118+
context,
119+
SourcePreferenceWidget(
120+
source: source,
121+
sourcePreference: sourcePreference,
122+
),
123+
);
124+
},
125+
child: Icon(
126+
Icons.settings,
127+
size: 32,
128+
color: theme.onSurface,
129+
),
130+
),
131+
],
132+
),
133+
],
134+
);
135+
},
136+
error: (error, stackTrace) => Text('Error: $error'),
137+
loading: () => const Center(child: CircularProgressIndicator()),
138+
),
139+
);
138140
}
139141
}

0 commit comments

Comments
 (0)