-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.dart
309 lines (260 loc) · 10.1 KB
/
main.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
import 'package:flutter/material.dart';
import 'package:al_downloader/al_downloader.dart';
void main() {
runApp(const MyApp());
}
/* ----------------------------------------------UI---------------------------------------------- */
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
const title = 'al_downloader';
return MaterialApp(
title: title,
theme: ThemeData(primarySwatch: Colors.blue),
home: const MyHomePage(title: title),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
super.initState();
initialize();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: Stack(fit: StackFit.expand, children: [
Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const SizedBox(height: 10),
const Text('You are testing batch download',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
color: Colors.black)),
Expanded(child: theListview)
]),
Positioned(
left: 0,
right: 0,
bottom: MediaQuery.of(context).padding.bottom + 10,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
mainAxisSize: MainAxisSize.min,
children: theActionLists
.map((e) => Expanded(
child: Padding(
padding: const EdgeInsets.fromLTRB(3, 0, 3, 0),
child: MaterialButton(
padding: const EdgeInsets.all(0),
minWidth: 20,
height: 50,
color: Colors.blue,
textTheme: ButtonTextTheme.primary,
onPressed: e[1],
child: Text(
e[0],
style: const TextStyle(fontSize: 10),
)))))
.toList()))
]),
);
}
/// Core data in listView
get theListview => ListView.separated(
padding: EdgeInsets.only(
top: 20, bottom: MediaQuery.of(context).padding.bottom + 75),
shrinkWrap: true,
itemCount: models.length,
itemBuilder: (BuildContext context, int index) {
final model = models[index];
final order = index + 1;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Align(
alignment: Alignment.centerLeft,
child: Text(
'$order',
style: const TextStyle(
fontSize: 20,
color: Colors.black,
fontWeight: FontWeight.bold),
)),
Align(
alignment: Alignment.centerLeft,
child: Text(
model.url,
style: const TextStyle(fontSize: 11, color: Colors.black),
)),
SizedBox(
height: 30,
child: Stack(fit: StackFit.expand, children: [
LinearProgressIndicator(
value: model.progress,
backgroundColor: Colors.grey,
valueColor:
const AlwaysStoppedAnimation<Color>(Colors.blue),
),
Align(
alignment: Alignment.centerLeft,
child: Text(
'progress = ${model.progressForPercent}',
style: const TextStyle(
fontSize: 13, color: Colors.white),
),
),
Align(
alignment: Alignment.centerRight,
child: Text(
model.status.alDescription,
style: const TextStyle(
fontSize: 13, color: Colors.white),
))
]))
]);
},
separatorBuilder: (BuildContext context, int index) =>
const Divider(height: 10, color: Colors.transparent),
);
/// The action lists
late final theActionLists = <List>[
['download', _batchDownloadAction],
['pause', _pauseAllAction],
['cancel', _cancelAllAction],
['remove', _removeAllAction]
];
/* ----------------------------------------------Action---------------------------------------------- */
void _batchDownloadAction() {
batchDownload();
}
void _pauseAllAction() {
ALDownloader.pauseAll();
}
void _cancelAllAction() {
ALDownloader.cancelAll();
}
void _removeAllAction() {
ALDownloader.removeAll();
}
/* ----------------------------------------------ALDownloader---------------------------------------------- */
/// Initialize
void initialize() {
/// ALDownloader initilize
ALDownloader.initialize();
/// Configure print
ALDownloader.configurePrint(false, frequentEnabled: false);
// It is for download. It is a forever interface.
addForeverHandlerInterface();
// It is for batch download. It is an one-off interface.
addHandlerInterfaceForBatch();
}
/// Batch download
void batchDownload() {
final urls = models.map((e) => e.url).toList();
final id = ALDownloaderBatcher.download(urls,
handlerInterface:
ALDownloaderHandlerInterface(progressHandler: (progress) {
debugPrint('ALDownloader | batch | download progress = $progress\n');
}, succeededHandler: () {
debugPrint('ALDownloader | batch | download succeeded\n');
}, failedHandler: () {
debugPrint('ALDownloader | batch | download failed\n');
}, pausedHandler: () {
debugPrint('ALDownloader | batch | download paused\n');
}));
if (id != null) _handlerInterfaceIdsForBatch.add(id);
}
/// Add a forever handler interface
void addForeverHandlerInterface() {
for (final model in models) {
final url = model.url;
final id = ALDownloader.addForeverHandlerInterface(
ALDownloaderHandlerInterface(progressHandler: (progress) {
debugPrint(
'ALDownloader | download progress = $progress, url = $url\n');
model.status = ALDownloaderStatus.downloading;
model.progress = progress;
setState(() {});
}, succeededHandler: () {
debugPrint('ALDownloader | download succeeded, url = $url\n');
model.status = ALDownloaderStatus.succeeded;
setState(() {});
}, failedHandler: () async {
debugPrint('ALDownloader | download failed, url = $url\n');
final status = await ALDownloader.getStatusForUrl(url);
model.status = status;
setState(() {});
}, pausedHandler: () {
debugPrint('ALDownloader | download paused, url = $url\n');
model.status = ALDownloaderStatus.paused;
setState(() {});
}),
url);
_handlerInterfaceIds.add(id);
}
}
/// Add a handler interface for batch
void addHandlerInterfaceForBatch() {
final urls = models.map((e) => e.url).toList();
final id = ALDownloaderBatcher.addHandlerInterface(
ALDownloaderHandlerInterface(progressHandler: (progress) {
debugPrint('ALDownloader | batch | download progress = $progress\n');
}, succeededHandler: () {
debugPrint('ALDownloader | batch | download succeeded\n');
}, failedHandler: () {
debugPrint('ALDownloader | batch | download failed\n');
}, pausedHandler: () {
debugPrint('ALDownloader | batch | download paused\n');
}),
urls);
_handlerInterfaceIdsForBatch.add(id);
}
/// Remove handler interface for batch
void removeHandlerInterfaceForBatch() {
for (final element in _handlerInterfaceIdsForBatch) {
ALDownloaderBatcher.removeHandlerInterfaceForId(element);
}
_handlerInterfaceIdsForBatch.clear();
}
/// Manage [ALDownloaderHandlerInterface] by [ALDownloaderHandlerInterfaceId]
final _handlerInterfaceIds = <ALDownloaderHandlerInterfaceId>[];
/// Manage batch [ALDownloaderHandlerInterface] by [ALDownloaderHandlerInterfaceId]
final _handlerInterfaceIdsForBatch = <ALDownloaderHandlerInterfaceId>[];
}
/* ----------------------------------------------Model class---------------------------------------------- */
class DownloadModel {
final String url;
double progress = 0;
String get progressForPercent {
int aProgress = (progress * 100).toInt();
return '$aProgress%';
}
ALDownloaderStatus status = ALDownloaderStatus.unstarted;
DownloadModel(this.url);
}
extension _ALDownloaderStatusExtension on ALDownloaderStatus {
String get alDescription =>
['unstarted', 'downloading', 'paused', 'failed', 'succeeded'][index];
}
/* ----------------------------------------------Data---------------------------------------------- */
final models = kTestVideos.map((e) => DownloadModel(e)).toList();
final kTestVideos = [
'https://media.w3.org/2010/05/sintel/trailer.mp4',
'http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4',
'http://vjs.zencdn.net/v/oceans.mp4',
'http://mirror.aarnet.edu.au/pub/TED-talks/911Mothers_2010W-480p.mp4',
'http://downsc.chinaz.net/Files/DownLoad/sound1/201906/11582.mp3'
];