This repository has been archived by the owner on Apr 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
REDownloadTasksQueue.h
404 lines (308 loc) · 12.9 KB
/
REDownloadTasksQueue.h
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
* Copyright (c) 2014 - 2017 Kulykov Oleh <info@resident.name>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#import <Foundation/Foundation.h>
#ifndef RE_EXTERN
#if defined(UIKIT_EXTERN)
# define RE_EXTERN UIKIT_EXTERN
#elif defined(FOUNDATION_EXTERN)
# define RE_EXTERN FOUNDATION_EXTERN
#else
# ifdef __cplusplus
# define RE_EXTERN extern "C"
# else
# define RE_EXTERN extern
# endif
#endif
#if defined(NS_INLINE)
# define RE_INLINE NS_INLINE
#elif defined(CG_INLINE)
# define RE_INLINE CG_INLINE
#else
# define RE_INLINE
#endif
#endif
#if !defined(_DEBUG_LOG) && !defined(_DEBUG_LOGA)
# if defined(DEBUG) || defined(_DEBUG)
# define _DEBUG_LOG(s) NSLog(s);
# define _DEBUG_LOGA(s, ...) NSLog(s, ##__VA_ARGS__);
# else
# define _DEBUG_LOG(s)
# define _DEBUG_LOGA(s, ...)
# endif
#endif
/**
@brief Reporting type of the queue.
*/
typedef NS_ENUM(NSUInteger, REDownloadTasksQueueReportType) {
/**
@brief Silent mode. In this case notifications and callback will be ignored.
*/
REDownloadTasksQueueReportNone = 0,
/**
@brief Reporting using 'NSNotificationCenter defaultCenter'.
@detailed In this case use notifications for listening queue.
*/
REDownloadTasksQueueReportViaNotifications = 1 << 0,
/**
@brief Reporting using blocks, of cource if blocks provided.
*/
REDownloadTasksQueueReportViaBlocks = 1 << 1
};
/**
@brief Default timeout of the download requests.
@detailed Value is 40 seconds.
*/
RE_EXTERN const NSTimeInterval kREDownloadTasksQueueDefaultRequestTimeout;
/**
@brief Default cache policy used for the download requests.
@detailed Value is NSURLRequestReloadIgnoringCacheData.
*/
RE_EXTERN const NSURLRequestCachePolicy kREDownloadTasksQueueDefaultRequestCachePolicy;
/**
@brief Arrived when download progress changed.
User info dictionary is: @{@"queue" : REDownloadTasksQueue, @"userObject" : UserObject, @"progress" : NSNumberValue}.
@warning Key = kREDownloadTasksQueueQueueKey; Value = REDownloadTasksQueue object.
@warning Key = kREDownloadTasksQueueUserObjectKey; Value = user object or [NSNull null]
@warning Key = kREDownloadTasksQueueProgressKey; Value = 'NSNumber' object.
*/
RE_EXTERN NSString * const _Nonnull kREDownloadTasksQueueProgressChangedNotification;
/**
@brief Arrived when download progress changed.
User info dictionary is: @{@"queue" : REDownloadTasksQueue, @"userObject" : UserObject, @"progress" : NSNumberValue}.
@warning Key = kREDownloadTasksQueueQueueKey; Value = REDownloadTasksQueue object.
@warning Key = kREDownloadTasksQueueUserObjectKey; Value = user object or [NSNull null]
@warning Key = kREDownloadTasksQueueDownloadURLKey; Value = 'NSURL' object.
@warning Key = kREDownloadTasksQueueStoreURLKey; Value = 'NSURL' object.
@warning Key = kREDownloadTasksQueueProgressKey; Value = 'NSNumber' object.
*/
RE_EXTERN NSString * const _Nonnull kREDownloadTasksQueueDidDownloadURLProgressChangedNotification;
/**
@brief Arrived on error.
User info dictionary is: @{@"queue" : REDownloadTasksQueue, @"userObject" : UserObject, @"error" : NSError object, @"downloadURL" : NSURL object, @"storeURL" : NSURL object}.
@warning Key = kREDownloadTasksQueueQueueKey; Value = DownloadTasksQueue object.
@warning Key = kREDownloadTasksQueueUserObjectKey; Value = user object or [NSNull null]
@warning Key = kREDownloadTasksQueueErrorKey; Value = 'NSError' object.
@warning Key = kREDownloadTasksQueueDownloadURLKey; Value = 'NSURL' object.
@warning Key = kREDownloadTasksQueueStoreURLKey; Value = 'NSURL' object.
*/
RE_EXTERN NSString * const _Nonnull kREDownloadTasksQueueErrorNotification;
/**
@brief Arrived on all tasks done.
User info dictionary is: @{@"queue" : REDownloadTasksQueue, @"userObject" : UserObject }.
@warning Key = kREDownloadTasksQueueQueueKey; Value = DownloadTasksQueue object.
@warning Key = kREDownloadTasksQueueUserObjectKey; Value = user object or [NSNull null]
*/
RE_EXTERN NSString * const _Nonnull kREDownloadTasksQueueDidFinishedNotification;
/**
@brief Key used for download queue.
@detailed Value is @"queue".
*/
RE_EXTERN NSString * const _Nonnull kREDownloadTasksQueueQueueKey;
/**
@brief Key used for download progress.
@detailed Value is @"progress".
*/
RE_EXTERN NSString * const _Nonnull kREDownloadTasksQueueProgressKey;
/**
@brief Key used for queue user object.
@detailed Value is @"userObject".
*/
RE_EXTERN NSString * const _Nonnull kREDownloadTasksQueueUserObjectKey;
/**
@brief Key used for queue error.
@detailed Value is @"error".
*/
RE_EXTERN NSString * const _Nonnull kREDownloadTasksQueueErrorKey;
/**
@brief Key used for store file URL object. To URL.
@detailed Value is @"storeURL".
*/
RE_EXTERN NSString * const _Nonnull kREDownloadTasksQueueStoreURLKey;
/**
@brief Key used for download URL object. From URL.
@detailed Value is @"downloadURL".
*/
RE_EXTERN NSString * const _Nonnull kREDownloadTasksQueueDownloadURLKey;
@class REDownloadTasksQueue;
/**
@brief Protocol of the download rasks queue delegate. All methods are optinal and called from 'main queue'.
@detailed Delegate will called only if available.
*/
@protocol REDownloadTasksQueueDelegate <NSObject>
@optional
/**
@brief Called when all queue tasks finished.
@param queue The download queue object.
*/
- (void) onREDownloadTasksQueueFinished:(nonnull REDownloadTasksQueue *) queue;
/**
@brief Called when downloading progress changed.
@param queue The download queue object.
@param progress Downloading progress in range from 0 to 1, [0, 1].
*/
- (void) onREDownloadTasksQueue:(nonnull REDownloadTasksQueue *) queue
progress:(float) progress;
/**
@brief Called when downloaded single url and stored to destination path
@param queue The download queue object.
@param downloadURL Downloaded URL.
@param storeURL URL for storing downloaded data.
@param progress Downloading progress in range from 0 to 1, [0, 1].
*/
- (void) onREDownloadTasksQueue:(nonnull REDownloadTasksQueue *) queue
didDownloadURL:(nullable NSURL *) downloadURL
andStoredToURL:(nullable NSURL *) storeURL
withProgress:(float) progress;
/**
@brief Called when error oqupaed.
@param queue The download queue object.
@param error The error object.
@param downloadURL Downloaded URL.
@param storeURL URL for storing downloaded data.
*/
- (void) onREDownloadTasksQueue:(nonnull REDownloadTasksQueue *) queue
error:(nullable NSError *) error
downloadURL:(nullable NSURL *) downloadURL
storeURL:(nullable NSURL *) storeURL;
@end
/**
@brief Class of the queue based on NSURLSessionDownloadTask tasks.
*/
@interface REDownloadTasksQueue : NSObject
/**
@brief Weak queue delegate.
*/
@property (nonatomic, weak, nullable) id<REDownloadTasksQueueDelegate> delegate;
/**
@brief Type of reporting using binary OR flags. Use REDownloadTasksQueueReportType values.
@detailed Default is both methods for reporting and will be used if available.
*/
@property (nonatomic, assign, readwrite) NSUInteger reportType;
/**
@brief Cache policy of the requests. Used when some url is adding to queue.
@detailed Default value is kREDownloadTasksQueueDefaultRequestCachePolicy.
*/
@property (nonatomic, assign, readwrite) NSURLRequestCachePolicy cachePolicy;
/**
@brief Timeout interval of the requests. Used when some url is adding to queue.
@detailed Default value is 'kREDownloadTasksQueueDefaultRequestTimeout'.
*/
@property (nonatomic, assign, readwrite) NSTimeInterval timeoutInterval;
/**
@brief Downloading progress of the queue. Calculates on downloaded data size.
@detailed Default is 0. Value between 0 and 1, [0, 1].
*/
@property (nonatomic, assign, readonly) float downloadProgress;
/**
@brief User defined object for identifing queue. Posted with notifications.
@detailed Default is '[NSNull null]'.
*/
@property (nonatomic, strong, nullable) id userObject;
/**
@brief Number of tasks in the queue.
@detailed During downloading this value is decrementing, when some task successfully finished.
*/
@property (nonatomic, assign, readonly) NSUInteger tasksCount;
/**
@brief Should queue continue when task error occupied.
@detained Default is NO.
*/
@property (nonatomic, assign, readwrite) BOOL continueOnTaskError;
/**
@brief Block handler for reporting queue download progress. Can be NULL.
@detailed Arrived on 'main queue'.
@warning Used ONLY if 'REDownloadTasksQueueReportViaBlocks' type present in 'reportType' property.
*/
@property (nonatomic, copy, nullable) void(^onProgressHandler)(REDownloadTasksQueue * _Nonnull queue, float progress);
/**
@brief Block handler for reporting queue download progress. Can be NULL.
@detailed Arrived on 'main queue'.
@param downloadURL Downloaded URL.
@param storeURL URL for storing downloaded data.
@warning Used ONLY if 'REDownloadTasksQueueReportViaBlocks' type present in 'reportType' property.
*/
@property (nonatomic, copy, nullable) void(^onDidDownloadURLProgressHandler)(REDownloadTasksQueue * _Nonnull queue, NSURL * _Nullable downloadURL, NSURL * _Nullable storeURL, float progress);
/**
@brief Block handler for reporting queue finished work(all tasks successfully finished).
@detailed Arrived on 'main queue'.
@warning Used ONLY if 'REDownloadTasksQueueReportViaBlocks' type present in 'reportType' property.
*/
@property (nonatomic, copy, nullable) void(^onFinishedHandler)(REDownloadTasksQueue * _Nonnull queue);
/**
@brief Block handler for reporting queue error. Before this, queue is cancelled.
@detailed Arrived on 'main queue'.
@warning Used ONLY if 'REDownloadTasksQueueReportViaBlocks' type present in 'reportType' property.
*/
@property (nonatomic, copy, nullable) void(^onErrorOccurredHandler)(REDownloadTasksQueue * _Nonnull queue, NSError * _Nullable error, NSURL * _Nullable downloadURL, NSURL * _Nullable storeFilePathURL);
/**
@brief Number of the tasks which is resumed.
@detailed Default value is 4. Values range is [1, 64].
*/
@property (nonatomic, assign, readwrite) NSUInteger numberOfResumedTasks;
/**
@brief Number of concurrent tasks.
@detailed Default value is 2. Values range is [1, 32].
*/
@property (nonatomic, assign, readwrite) NSUInteger numberOfMaximumConcurrentTasks;
/**
@brief Checks is queue is cancelled.
@return YES - if all tasks cancelled, othervice NO.
*/
- (BOOL) isCanceled;
/**
@brief Add url for downloading.
@param urlString The URL string for download. Can be nil.
@param storePath Store path for downloaded data. Can be nil.
@return YES - if successfully added, othervice NO.
*/
- (BOOL) addURLString:(nullable NSString *) urlString withStorePath:(nullable NSString *) storePath;
/**
@brief Add url for downloading.
@param url The URL for download. Can be nil. Also checked url is not file or file reference.
@param storePath Store path for downloaded data. Can be nil.
@return YES - if successfully added, othervice NO.
*/
- (BOOL) addURL:(nullable NSURL *) url withStorePath:(nullable NSString *) storePath;
/**
@brief Add url request for downloading.
@param urlRequest Manually setuped URL request. Can be nil. Also checked request url is not file or file reference.
@param storePath Store path for downloaded data. Can be nil.
@return YES - if successfully added, othervice NO.
*/
- (BOOL) addURLRequest:(nullable NSURLRequest *) urlRequest withStorePath:(nullable NSString *) storePath;
/**
@brief Starts queue.
@warning Don't forget call cancelWithCompletionHandler: method if queue not correctly finished.
*/
- (void) start;
/**
@brief Cancel all tasks and waits untill all tasks is canceled before triger handler.
@warning You shoul always call this method if you want to release queue, cause internal session object
holds strongly this queue.
@param handler Handler triger on all tasks cancelled on 'main queue'. Can be NULL.
*/
- (void) cancelWithCompletionHandler:(nullable void(^)(void)) handler;
@end
/**
@brief Include queue serialization functionality
*/
#import "REDownloadTasksQueue+Serialization.h"