-
Notifications
You must be signed in to change notification settings - Fork 37
/
WPMediaCollectionDataSource.h
362 lines (302 loc) · 12.1 KB
/
WPMediaCollectionDataSource.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
@import AVFoundation;
typedef NS_OPTIONS(NSInteger, WPMediaType){
WPMediaTypeImage = 1,
WPMediaTypeVideo = 1 << 1,
WPMediaTypeAudio = 1 << 2,
WPMediaTypeOther = 1 << 3,
WPMediaTypeAll= 0XFF
};
static NSString * _Nonnull const WPMediaPickerErrorDomain = @"WPMediaPickerErrorDomain";
typedef NS_ENUM(NSInteger, WPMediaPickerErrorCode){
WPMediaPickerErrorCodePermissionDenied,
WPMediaPickerErrorCodeRestricted,
WPMediaPickerErrorCodeUnknown,
WPMediaPickerErrorCodeVideoURLNotAvailable
};
@protocol WPMediaMove <NSObject>
- (NSUInteger)from;
- (NSUInteger)to;
@end
typedef NS_ENUM(NSInteger, WPMediaLoadOptions){
WPMediaLoadOptionsGroups,
WPMediaLoadOptionsAssets,
WPMediaLoadOptionsGroupsAndAssets
};
@protocol WPMediaAsset;
typedef void (^WPMediaGroupChangesBlock)(void);
typedef void (^WPMediaChangesBlock)(BOOL incrementalChanges, NSIndexSet * _Nonnull removed, NSIndexSet * _Nonnull inserted, NSIndexSet * _Nonnull changed, NSArray<id<WPMediaMove>> * _Nonnull moves);
typedef void (^WPMediaSuccessBlock)(void);
typedef void (^WPMediaFailureBlock)(NSError * _Nullable error);
typedef void (^WPMediaAddedBlock)(_Nullable id<WPMediaAsset> media, NSError * _Nullable error);
typedef void (^WPMediaImageBlock)(UIImage * _Nullable result, NSError * _Nullable error);
typedef void (^WPMediaCountBlock)(NSInteger result, NSError * _Nullable error);
typedef void (^WPMediaAssetBlock)(AVAsset * _Nullable asset, NSError * _Nullable error);
typedef int32_t WPMediaRequestID;
/**
* The WPMediaGroup protocol is adopted by an object that mediates between a media collection and it's representation on
* an visualization like WPMediaGroupPickerViewController.
*/
@protocol WPMediaGroup <NSObject>
- (NSString *_Nonnull)name;
/**
* Asynchronously fetches an image that represents the group
*
* @param size the target size for the image, this may not be respected if the requested size is not available
*
* @param completionHandler a block that is invoked when the image is available or when an error occurs.
*
* @return an unique ID of the fetch operation
*/
- (WPMediaRequestID)imageWithSize:(CGSize)size completionHandler:(nonnull WPMediaImageBlock)completionHandler;
- (void)cancelImageRequest:(WPMediaRequestID)requestID;
/**
* The original object that represents a group on the underlying media implementation
*
* @return a object from the underlying media implementation
*/
- (id _Nonnull )baseGroup;
/**
* An unique identifer for the media group
*
* @return a string that uniquely identifies the group
*/
- (nonnull NSString *)identifier;
/**
The numbers of assets that exist in the group of a certain mediaType
@param mediaType the asset type to count
@param completionHandler a block that is executed when the real number of assets is know.
@return return an estimation of the current number of assets, if no estimate is known return NSNotFound
*/
- (NSInteger)numberOfAssetsOfType:(WPMediaType)mediaType completionHandler:(nullable WPMediaCountBlock)completionHandler;
@end
/**
* The WPMediaAsset protocol is adopted by an object that mediates between a concrete media asset and it's representation on
* a WPMediaCollectionViewCell.
*/
@protocol WPMediaAsset <NSObject>
/**
Asynchronously fetches an image that represents the asset with the requested size
@param size the target size for the image, this may not be respected if the requested size is not available. If the size request is zero the maximum available
@param completionHandler a block that is invoked when the image is available or when an error occurs.
@return an unique ID of the fetch operation that can be used to cancel it.
*/
- (WPMediaRequestID)imageWithSize:(CGSize)size completionHandler:(nonnull WPMediaImageBlock)completionHandler;
/**
* Cancels a previous ongoing request for an asset image
*
* @param requestID an identifier returned by the imageWithSize:completionHandler: method.
*/
- (void)cancelImageRequest:(WPMediaRequestID)requestID;
/**
Asynchronously fetches an AVAsset that represents the media object.
@param completionHandler a block that is invoked when the asset is available or when an error occurs.
@return an unique ID of the fetch operation that can be used to cancel it.
*/
- (WPMediaRequestID)videoAssetWithCompletionHandler:(nonnull WPMediaAssetBlock)completionHandler;
/**
* The media type of the asset. This could be an image, video, or another unknow type.
*
* @return a WPMEdiaType object.
*/
- (WPMediaType)assetType;
/**
* The duration of a video media asset. The is only available on video assets.
*
* @return The duration of a video asset. Always zero if the asset is not a video.
*/
- (NSTimeInterval)duration;
/**
* The original object that represents an asset on the underlying media implementation
*
* @return a object from the underlying media implementation
*/
- (nonnull id)baseAsset;
/**
* A unique identifier for the media asset
*
* @return a string that uniquely identifies the media asset
*/
- (nonnull NSString *)identifier;
/**
* The date when the asset was created.
*
* @return a NSDate object that represents the creation date of the asset.
*/
- (nonnull NSDate *)date;
/**
* The size, in pixels, of the asset’s image or video data.
*
* @return The size, in pixels, of the asset’s image or video data.
*/
- (CGSize)pixelSize;
@optional
/**
* @return The filename of this asset. Optional.
*/
- (nullable NSString *)filename;
/**
* @return The file extension of this asset (PDF, doc, etc). Optional.
*/
- (nullable NSString *)fileExtension;
/**
* @return The uniform type identifier for this asset. Optional.
*/
- (nullable NSString *)UTTypeIdentifier;
@end
/**
* The WPMediaCollectionDataSource protocol is adopted by an object that mediates between a media library implementation
* and a WPMediaPickerViewController / WPMediaCollectionViewController. The data source provides information about the media groups
* that exist and the media assets inside. It also provides methods to add new media assets to the library and observe changes that
* happen outside it's interface.
*/
@protocol WPMediaCollectionDataSource <NSObject>
/**
* Asks the data source for the number of groups existing on the media library.
*
* @return the number of groups existing on the media library.
*/
- (NSInteger)numberOfGroups;
/**
* Asks the data source for the group at a selected index.
*
* @param index index location the group requested.
*
* @return an object implementing WPMediaGroup protocol.
*/
- (nonnull id<WPMediaGroup>)groupAtIndex:(NSInteger)index;
/**
* Ask the data source for the current active group of the library
*
* @return an object implementing WPMediaGroup protocol.
*/
- (nullable id<WPMediaGroup>)selectedGroup;
/**
* Ask the data source to select a specific group and update it's assets for that group.
*
* @param group object implementing the WPMediaGroup protocol
*/
- (void)setSelectedGroup:(nonnull id<WPMediaGroup>)group;
/**
* Asks the data source for the number of assets existing on the currect selected group
*
* @return the number of assets existing on the current selected group.
*/
- (NSInteger)numberOfAssets;
/**
* Asks the data source for the asset at the selected index.
*
* @param index index location of the asset requested.
*
* @return an object implementing the WPMediaAsset protocol
*/
- (nonnull id<WPMediaAsset>)mediaAtIndex:(NSInteger)index;
/**
* Returns the object with the matching identifier if it exists on the datasource
*
* @param identifier a unique identifier for the media
*
* @return the media object if it exists or nil if it's not found.
*/
- (nullable id<WPMediaAsset>)mediaWithIdentifier:(nonnull NSString *)identifier;
/**
* Asks the data source to be notify about changes on the media library using the given callback block.
*
* @discussion the callback object is retained by the data source so it needs to
* be unregistered on the end to avoid leaks or retain cycles.
*
* @param callback a WPMediaChangesBlock that is invoked every time a change is detected.
*
* @return an opaque object that identifies the callback register. This should be used to later unregister the block
*/
- (nonnull id<NSObject>)registerChangeObserverBlock:(nonnull WPMediaChangesBlock)callback;
/**
* Asks the data source to be notify about changes on the media library group/albums using the given callback block.
*
* @discussion the callback object is retained by the data source so it needs to
* be unregistered on the end to avoid leaks or retain cycles.
*
* @param callback a WPMediaGroupChangessBlock that is invoked every time a change is detected.
*
* @return an opaque object that identifies the callback register. This should be used to later unregister the block
*/
- (nonnull id<NSObject>)registerGroupChangeObserverBlock:(nonnull WPMediaGroupChangesBlock)callback;
/**
* Asks the data source to unregister the block that is identified by the block key.
*
* @param blockKey the unique identifier of the block. This must have been obtained
* by a call to registerChangeObserverBlock
*/
- (void)unregisterChangeObserver:(nonnull id<NSObject>)blockKey;
/**
* Asks the data source to unregister the group observer block that is identified by the block key.
*
* @param blockKey the unique identifier of the block. This must have been obtained
* by a call to registerGroupChangesObserverBlock
*/
- (void)unregisterGroupChangeObserver:(nonnull id<NSObject>)blockKey;
/**
* Asks the data source to reload the data available of the media library. This should be invoked after changing the
* current active group or if a change is detected.
*
* @param options specifiy what type of data to load
* @param successBlock a block that is invoked when the data is loaded with success.
* @param failureBlock a block that is invoked when the are is any kind of error when loading the data.
*/
- (void)loadDataWithOptions:(WPMediaLoadOptions)options
success:(nullable WPMediaSuccessBlock)successBlock
failure:(nullable WPMediaFailureBlock)failureBlock;
/**
* Requests to the data source to add an image to the library.
*
* @param image an UIImage object with the asset to add
* @param metadata the metadata information of the image to add.
* @param completionBlock a block that is invoked when the image is added.
* On success the media parameter is returned with a new object implemeting the WPMedia protocol
* If an error occurs the media is nil and the error parameter contains a value
*/
- (void)addImage:(nonnull UIImage *)image metadata:(nullable NSDictionary *)metadata completionBlock:(nullable WPMediaAddedBlock)completionBlock;
/**
* Requests to the data source to add a video to the library.
*
* @param url an url pointing to a file that contains the video to be added to the library.
* @param completionBlock a block that is invoked when the image is added.
* On success the media parameter is returned with a new object implemeting the WPMedia protocol
* If an error occurs the media is nil and the error parameter contains a value
*/
- (void)addVideoFromURL:(nonnull NSURL *)url completionBlock:(nullable WPMediaAddedBlock)completionBlock;
/**
* Filter the assets acording to their media type.
*
* @param filter the WPMediaType to filter objects to. The default value is WPMediaTypeVideoOrImage
*/
- (void)setMediaTypeFilter:(WPMediaType)filter;
/**
*
*
* @return The media type filter that is being used.
*/
- (WPMediaType)mediaTypeFilter;
/**
* Sets the sorting order the assets are show based on creationDate
*
* @param ascending the order wich assets are retrieved, based on the creationDate. The default value is YES
*/
- (void)setAscendingOrdering:(BOOL)ascending;
/**
* The sorting order on wich the assets are returned
*
* @return if the assets are return in ascending order
*/
- (BOOL)ascendingOrdering;
@optional
/**
* Tells the Data Source that the search string has been changed
*
* @param searchText the new search text
*/
- (void)searchFor:(nullable NSString *)searchText;
/**
* Tells the Data Source that the search was cancelled by the user
*/
- (void)searchCancelled;
@end