@@ -6,6 +6,7 @@ import 'package:tubesavely/extension/extension.dart';
6
6
import 'package:tubesavely/generated/l10n.dart' ;
7
7
import 'package:tubesavely/http/http_request.dart' ;
8
8
import 'package:tubesavely/model/emuns.dart' ;
9
+ import 'package:tubesavely/model/execute_model.dart' ;
9
10
import 'package:tubesavely/model/pair.dart' ;
10
11
import 'package:tubesavely/model/video_model.dart' ;
11
12
import 'package:tubesavely/storage/storage.dart' ;
@@ -25,9 +26,7 @@ class _DownloadPageState extends State<DownloadPage> with AutomaticKeepAliveClie
25
26
List <VideoModel > videoModelList = [];
26
27
List <String > taskList = [];
27
28
28
- Map <String , double > progressMap = {};
29
- Map <String , String > progressTextMap = {};
30
- Map <String , ExecuteStatus > statusMap = {};
29
+ Map <String , ExecuteModel > executeModelMap = {};
31
30
32
31
void _extractVideo (String url) async {
33
32
if (! url.isValidUrl ()) {
@@ -72,9 +71,8 @@ class _DownloadPageState extends State<DownloadPage> with AutomaticKeepAliveClie
72
71
73
72
void _download (VideoModel model) async {
74
73
setState (() {
75
- progressMap[model.original_url ?? '' ] = 0 ;
76
- statusMap[model.original_url ?? '' ] = ExecuteStatus .Executing ;
77
- progressTextMap[model.original_url ?? '' ] = S .current.statusDownloadProgress;
74
+ executeModelMap[model.original_url ?? '' ] = ExecuteModel (
75
+ key: model.original_url, progress: 0 , progressText: S .current.statusDownloadProgress, status: ExecuteStatus .Executing );
78
76
});
79
77
80
78
final quality = Storage ().getString (StorageKeys .DOWNLOAD_QUALITY_KEY );
@@ -83,28 +81,33 @@ class _DownloadPageState extends State<DownloadPage> with AutomaticKeepAliveClie
83
81
FormatModel ? target = videoList? .firstWhere ((item) => VideoResolutionUtil .format (item.resolution ?? '' ) == quality,
84
82
orElse: () => videoList.first);
85
83
84
+ ExecuteModel ? executeModel = executeModelMap[model.original_url ?? "" ];
86
85
Downloader .start (target? .url ?? '' , model.title ?? '' ,
87
86
audioUrl: audioList? .isEmpty == true ? null : audioList? [1 ].url,
88
87
resolution: VideoResolutionUtil .format (target? .resolution ?? '' ), onProgress: (type, value) {
89
88
setState (() {
90
- progressMap[model.original_url ?? '' ] = value;
89
+ executeModel ? .progress = value;
91
90
if (type == ProgressType .download) {
92
- progressTextMap[model.original_url ?? '' ] = S .current.statusDownloadProgress;
91
+ executeModel ? .progressText = S .current.statusDownloadProgress;
93
92
} else if (type == ProgressType .recode) {
94
- progressTextMap[model.original_url ?? '' ] = S .current.statusRecodeProgress;
93
+ executeModel ? .progressText = S .current.statusRecodeProgress;
95
94
} else if (type == ProgressType .merge) {
96
- progressTextMap[model.original_url ?? '' ] = S .current.statusMergeProgress;
95
+ executeModel ? .progressText = S .current.statusMergeProgress;
97
96
}
98
97
if (value >= 100 ) {
99
- statusMap[model.original_url ?? '' ] = ExecuteStatus .Success ;
100
- progressTextMap[model.original_url ?? '' ] = S .current.statusComplete;
98
+ executeModel ? .status = ExecuteStatus .Success ;
99
+ executeModel ? .progressText = S .current.statusComplete;
101
100
}
102
101
});
102
+ }, onSuccess: (path) {
103
+ executeModel? .path = path;
104
+ executeModel? .status = ExecuteStatus .Success ;
103
105
}, onFailure: (error) {
104
106
setState (() {
105
- statusMap[model.original_url ?? '' ] = ExecuteStatus .Idle ;
106
- progressMap[model.original_url ?? '' ] = 0 ;
107
- progressTextMap[model.original_url ?? '' ] = S .current.statusFailed;
107
+ executeModel? .status = ExecuteStatus .Idle ;
108
+ executeModel? .progress = 0 ;
109
+ executeModel? .progressText = S .current.statusFailed;
110
+ ToastUtil .error (S .current.statusFailed);
108
111
});
109
112
});
110
113
}
@@ -147,7 +150,7 @@ class _DownloadPageState extends State<DownloadPage> with AutomaticKeepAliveClie
147
150
shape: RoundedRectangleBorder (borderRadius: BorderRadius .circular (50 ))),
148
151
onPressed: () async {
149
152
videoModelList.forEach ((model) async {
150
- if (statusMap [model.original_url ?? '' ] == null ) {
153
+ if (executeModelMap [model.original_url] ? .status == null ) {
151
154
_download (model);
152
155
}
153
156
});
@@ -250,7 +253,7 @@ class _DownloadPageState extends State<DownloadPage> with AutomaticKeepAliveClie
250
253
children: [
251
254
Expanded (
252
255
child: LinearProgressIndicator (
253
- value: (progressMap [model.original_url] ?? 0 ) / 100 ,
256
+ value: (executeModelMap [model.original_url]? .progress ?? 0 ) / 100 ,
254
257
minHeight: 2 ,
255
258
color: Theme .of (context).primaryColor,
256
259
borderRadius: BorderRadius .circular (50 ),
@@ -260,15 +263,15 @@ class _DownloadPageState extends State<DownloadPage> with AutomaticKeepAliveClie
260
263
width: 10 ,
261
264
),
262
265
Text (
263
- '${progressTextMap [model .original_url ] ?? '' } ${(progressMap [model .original_url ]?.toStringAsFixed (2 ) ?? 0 )}%' ,
266
+ '${executeModelMap [model .original_url ]?. progressText ?? '' } ${(executeModelMap [model .original_url ]?. progress ?.toStringAsFixed (2 ) ?? 0 )}%' ,
264
267
style: TextStyle (fontSize: 12 , color: Theme .of (context).colorScheme.onSurface.withOpacity (0.6 )))
265
268
],
266
269
)
267
270
],
268
271
)),
269
272
Row (
270
273
children: [
271
- statusMap [model.original_url ?? '' ] == ExecuteStatus .Executing
274
+ executeModelMap [model.original_url] ? .status == ExecuteStatus .Executing
272
275
? Container (
273
276
width: 40 ,
274
277
height: 40 ,
@@ -290,7 +293,8 @@ class _DownloadPageState extends State<DownloadPage> with AutomaticKeepAliveClie
290
293
)),
291
294
IconButton (
292
295
onPressed: () async {
293
- CommonUtil .openDesktopDirectory (Storage ().getString (StorageKeys .CACHE_DIR_KEY ) ?? '' );
296
+ CommonUtil .openDesktopDirectory (
297
+ executeModelMap[model.original_url]? .path ?? Storage ().getString (StorageKeys .CACHE_DIR_KEY ) ?? '' );
294
298
},
295
299
icon: const Icon (
296
300
Icons .folder_open,
@@ -300,9 +304,7 @@ class _DownloadPageState extends State<DownloadPage> with AutomaticKeepAliveClie
300
304
onPressed: () {
301
305
setState (() {
302
306
videoModelList.remove (model);
303
- statusMap.remove (model.original_url ?? '' );
304
- progressMap.remove (model.original_url ?? '' );
305
- progressTextMap.remove (model.original_url ?? '' );
307
+ executeModelMap.remove (model.original_url);
306
308
taskList.remove (model.original_url);
307
309
});
308
310
},
0 commit comments