-
Notifications
You must be signed in to change notification settings - Fork 3
/
AnimeDetailPage.dart
310 lines (290 loc) · 9.68 KB
/
AnimeDetailPage.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import 'dart:math';
import 'package:animego/core/Firebase.dart';
import 'package:animego/core/Global.dart';
import 'package:animego/core/model/AnimeDetailedInfo.dart';
import 'package:animego/core/model/BasicAnime.dart';
import 'package:animego/core/model/EpisodeSection.dart';
import 'package:animego/core/model/EpisodelInfo.dart';
import 'package:animego/core/parser/DetailedInfoParser.dart';
import 'package:animego/core/parser/EpisodeListParser.dart';
import 'package:animego/ui/page/CategoryPage.dart';
import 'package:animego/ui/page/EpisodePage.dart';
import 'package:animego/ui/page/GenrePage.dart';
import 'package:animego/ui/widget/AnimeFlatButton.dart';
import 'package:animego/ui/widget/LoadingSwitcher.dart';
import 'package:animego/ui/widget/SearchAnimeButton.dart';
import 'package:flutter/material.dart';
/// AnimeDetailPage class
class AnimeDetailPage extends StatefulWidget {
const AnimeDetailPage({
Key? key,
required this.info,
}) : super(key: key);
final BasicAnime? info;
@override
_AnimeDetailPageState createState() => _AnimeDetailPageState();
}
class _AnimeDetailPageState extends State<AnimeDetailPage> {
bool loading = true;
bool loadingEpisode = false;
String? currEpisode;
AnimeDetailedInfo? info;
List<EpisodeInfo> episodes = [];
final global = Global();
bool isFavourite = false;
@override
void initState() {
super.initState();
FirebaseEventService().logUseAnimeInfo();
// Load data here
final parser =
DetailedInfoParser(global.getDomain() + (widget.info?.link ?? ''));
parser.downloadHTML().then((body) {
setState(() {
this.loading = false;
this.info = parser.parseHTML(body);
this.isFavourite = global.isFavourite(widget.info);
// Auto load if there is only one section
if (info?.episodes.length == 1) {
this.loadEpisode(info?.episodes.first);
}
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(loading ? 'Loading...' : info?.status ?? 'Error'),
actions: <Widget>[
if (!loading && info != null)
IconButton(
icon: Icon(
isFavourite ? Icons.favorite : Icons.favorite_border,
),
onPressed: () {
FirebaseEventService().logUseFavourite();
if (isFavourite)
global.removeFromFavourite(widget.info);
else
global.addToFavourite(widget.info);
setState(() {
isFavourite = !isFavourite;
});
},
),
],
),
body: SafeArea(
child: LoadingSwitcher(
child: this.renderBody(),
loading: this.loading,
)),
);
}
Widget renderBody() {
if (loading) {
return Center(
child: CircularProgressIndicator(),
);
} else {
return ListView(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(16.0),
child: Text(
info?.name ?? '',
textAlign: TextAlign.center,
style: TextStyle(fontWeight: FontWeight.w500, fontSize: 24),
),
),
Padding(
padding: const EdgeInsets.only(left: 16, right: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Flexible(
flex: 1,
child: info?.image != null
? Image.network(info!.image!)
: Container(),
),
Flexible(
flex: 1,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
centeredListTile('Released', info?.released ?? 'Unkown'),
centeredListTile(
'Episode(s)', info?.lastEpisode ?? 'Unkown'),
ListTile(
title: Text('Category', textAlign: TextAlign.center),
// TODO: This button is very hidden so the user may not know about this
subtitle: AnimeFlatButton(
onPressed: () {
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) {
return CategoryPage(
// The domain will be added later so DON'T ADD IT HERE
url: info?.categoryLink,
title: info?.category,
);
}),
);
},
child: Text(
info?.category ?? 'Unknown',
textAlign: TextAlign.center,
),
),
),
],
),
),
Flexible(
flex: 1,
child: Transform(
alignment: Alignment.center,
transform: Matrix4.rotationY(pi),
child: info?.image != null
? Image.network(info!.image!)
: Container(),
),
),
],
),
),
SearchAnimeButton(name: widget.info?.name),
Padding(
padding: const EdgeInsets.only(top: 8),
child: ListTile(
title: Text('Genre', textAlign: TextAlign.center),
subtitle: Wrap(
alignment: WrapAlignment.center,
spacing: 4,
children: info?.genre.map((e) {
return ActionChip(
label: Text(e.getAnimeGenreName()),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) {
return GenrePage(genre: e);
}),
);
},
);
}).toList(growable: false) ??
[],
),
),
),
centeredListTile('Summary', info?.summary ?? 'No summary'),
Padding(
padding: const EdgeInsets.only(top: 8),
child: Text(
'Episode List',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 16),
),
),
Wrap(
alignment: WrapAlignment.center,
children: info?.episodes.map((e) {
return Padding(
padding: const EdgeInsets.all(2),
child: InkWell(
onTap: this.loadingEpisode
? null
: () {
loadEpisode(e);
},
child: Padding(
padding: const EdgeInsets.all(4),
child: Text(
'${e.episodeStart} - ${e.episodeEnd}',
style: TextStyle(
decoration: currEpisode == e.episodeStart
? TextDecoration.underline
: TextDecoration.none,
),
),
),
),
);
}).toList(growable: false) ??
[],
),
renderEpisodeList(context),
],
);
}
}
void loadEpisode(EpisodeSection? e) {
// Don't update if the selection is the same
if (e?.episodeStart == this.currEpisode) return;
setState(() {
this.currEpisode = e?.episodeStart;
this.loadingEpisode = true;
});
FirebaseEventService().logUseEpisodeList();
final parser = EpisodeListParser(
global.getDomain() + '/load-list-episode',
e,
);
parser.downloadHTML().then((body) {
setState(() {
this.episodes = parser.parseHTML(body);
this.loadingEpisode = false;
});
});
}
Widget renderEpisodeList(BuildContext context) {
if (currEpisode == null) {
return Container();
} else if (loadingEpisode) {
return Center(
child: CircularProgressIndicator(),
);
} else {
return Padding(
padding: const EdgeInsets.only(bottom: 16),
child: Wrap(
alignment: WrapAlignment.center,
spacing: 8,
children: this.episodes.map((e) {
return ElevatedButton(
style: ButtonStyle(
textStyle: MaterialStateProperty.all(
TextStyle(color: Colors.white),
),
),
onPressed: () {
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) {
return EpisodePage(info: e);
}),
);
},
child: Text(e.name ?? '??'),
);
}).toList(growable: false),
),
);
}
}
Widget centeredListTile(String title, String subtitle) {
return ListTile(
title: Text(
title,
textAlign: TextAlign.center,
),
subtitle: Text(
subtitle,
textAlign: TextAlign.center,
),
);
}
}