forked from karelia/iMedia
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathIMBApplePhotosParserConfiguration.m
314 lines (270 loc) · 13.7 KB
/
IMBApplePhotosParserConfiguration.m
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
//
// IMBApplePhotosParserConfiguration.m
// iMedia
//
// Created by Jörg Jacobsen on 10.03.15.
//
//
#import "IMBApplePhotosParserConfiguration.h"
#import "IMBNodeObject.h"
#import "IMBImageProcessor.h"
#import "NSImage+iMedia.h"
#import "MLMediaGroup+iMedia.h"
#import "IMBAppleMediaLibraryPropertySynchronizer.h"
/**
Reverse-engineered keys of the Photos app media source's attributes.
Apple doesn't seem to yet publicly define these constants anywhere.
*/
/* Top Level Groups*/
NSString *kIMBPhotosMediaGroupTypeIdentifierRoot = @"com.apple.Photos.RootGroup";
NSString *kIMBPhotosMediaGroupTypeIdentifierFolder = @"com.apple.Photos.Folder";
NSString *kIMBPhotosMediaGroupTypeIdentifierAlbum = @"com.apple.Photos.Album";
NSString *kIMBPhotosMediaGroupTypeIdentifierFaces = @"com.apple.Photos.FacesAlbum"; // Used for Faces and single face
NSString *kIMBPhotosMediaGroupIdentifierMoments = @"AllMomentsGroup";
NSString *kIMBPhotosMediaGroupIdentifierCollections = @"AllCollectionsGroup";
NSString *kIMBPhotosMediaGroupIdentifierYears = @"AllYearsGroup";
NSString *kIMBPhotosMediaGroupIdentifierPlaces = @"allPlacedPhotosAlbum";
NSString *kIMBPhotosMediaGroupIdentifierShared = @"com.apple.Photos.SharedGroup";
NSString *kIMBPhotosMediaGroupIdentifierAlbums = @"TopLevelAlbums";
/* Albums */
NSString *kIMBPhotosMediaGroupIdentifierAllPhotos = @"allPhotosAlbum";
NSString *kIMBPhotosMediaGroupIdentifierPeople= @"peopleAlbum";
NSString *kIMBPhotosMediaGroupIdentifierLastImport = @"lastImportAlbum";
NSString *kIMBPhotosMediaGroupIdentifierFavorites = @"favoritesAlbum";
NSString *kIMBPhotosMediaGroupIdentifierPanoramas = @"panoramaAlbum";
NSString *kIMBPhotosMediaGroupIdentifierVideos = @"videoAlbum";
NSString *kIMBPhotosMediaGroupIdentifierSloMos = @"videoSloMoAlbum";
NSString *kIMBPhotosMediaGroupIdentifierTimelapse = @"videoTimelapseAlbum";
NSString *kIMBPhotosMediaGroupIdentifierBursts = @"burstAlbum";
/**
Parser configuration factory for Apple Photos app.
*/
IMBMLParserConfigurationFactory IMBMLPhotosParserConfigurationFactory =
^id<IMBAppleMediaLibraryParserDelegate>(MLMediaType mediaType)
{
NSSet *identifiersOfNonUserCreatedGroups = [NSSet setWithObjects:
kIMBPhotosMediaGroupIdentifierMoments,
kIMBPhotosMediaGroupIdentifierCollections,
kIMBPhotosMediaGroupIdentifierYears,
kIMBPhotosMediaGroupIdentifierPlaces,
kIMBPhotosMediaGroupIdentifierShared,
kIMBPhotosMediaGroupIdentifierAlbums,
kIMBPhotosMediaGroupIdentifierAllPhotos,
kIMBPhotosMediaGroupIdentifierPeople,
kIMBPhotosMediaGroupIdentifierLastImport,
kIMBPhotosMediaGroupIdentifierFavorites,
kIMBPhotosMediaGroupIdentifierPanoramas,
kIMBPhotosMediaGroupIdentifierVideos,
kIMBPhotosMediaGroupIdentifierSloMos,
kIMBPhotosMediaGroupIdentifierTimelapse,
kIMBPhotosMediaGroupIdentifierBursts,
nil];
return [[IMBApplePhotosParserConfiguration alloc] initWithMediaSourceIdentifier:MLMediaSourcePhotosIdentifier
AppleMediaLibraryMediaType:mediaType
identifiersOfNonUserCreatedGroups:identifiersOfNonUserCreatedGroups];
};
@implementation IMBApplePhotosParserConfiguration
- (NSURL *)sourceURLForMediaGroup:(MLMediaGroup *)mediaGroup
{
return self.mediaSource.attributes[@"libraryURL"];
}
/**
*/
- (BOOL)shouldUseMediaGroup:(MLMediaGroup *)mediaGroup
{
NSSet *unqualifiedGroupIdentifiers = [NSSet setWithObjects:
kIMBPhotosMediaGroupIdentifierMoments,
kIMBPhotosMediaGroupIdentifierCollections,
nil];
switch (self.mediaType) {
case MLMediaTypeImage:
unqualifiedGroupIdentifiers = [unqualifiedGroupIdentifiers
setByAddingObjectsFromSet:[NSSet setWithObjects:
kIMBPhotosMediaGroupIdentifierSloMos,
kIMBPhotosMediaGroupIdentifierTimelapse,
kIMBPhotosMediaGroupIdentifierVideos,
nil]];
break;
case MLMediaTypeMovie:
unqualifiedGroupIdentifiers = [unqualifiedGroupIdentifiers
setByAddingObjectsFromSet:[NSSet setWithObjects:
kIMBPhotosMediaGroupIdentifierPanoramas,
kIMBPhotosMediaGroupIdentifierBursts,
nil]];
break;
default:
break;
}
return (![unqualifiedGroupIdentifiers containsObject:mediaGroup.identifier]);
}
/**
*/
- (BOOL)shouldReuseMediaObjectsOfParentGroupForGroup:(MLMediaGroup *)mediaGroup
{
NSSet *qualifiedGroupIdentifiers = [NSSet setWithObjects:
kIMBPhotosMediaGroupIdentifierMoments,
kIMBPhotosMediaGroupIdentifierCollections,
kIMBPhotosMediaGroupIdentifierYears,
nil];
return [qualifiedGroupIdentifiers containsObject:mediaGroup.identifier];
}
/**
Returns whether group (aka node) is populated with child group objects rather than real media objects.
*/
- (BOOL)shouldUseChildGroupsAsMediaObjectsForMediaGroup:(MLMediaGroup *)mediaGroup
{
NSSet *qualifiedGroupIdentifiers = [NSSet setWithObjects:
kIMBPhotosMediaGroupIdentifierAlbums,
kIMBPhotosMediaGroupIdentifierPeople,
kIMBPhotosMediaGroupIdentifierYears,
nil];
BOOL whiteListed = [qualifiedGroupIdentifiers containsObject:mediaGroup.identifier];
NSSet *unqualifiedGroupIdentifiers = [NSSet setWithObjects:
kIMBPhotosMediaGroupTypeIdentifierRoot,
nil];
BOOL blackListed = [unqualifiedGroupIdentifiers containsObject:mediaGroup.typeIdentifier];
return (([[mediaGroup childGroups] count] > 0 && !blackListed) || whiteListed);
}
- (MLMediaObject *)keyMediaObjectForMediaGroup:(MLMediaGroup *)mediaGroup
{
NSString *keyPhotoKey = mediaGroup.attributes[@"KeyPhotoKey"];
// Gee, was hard to find out that this does the trick to enrich contents of attributes dictionary
if (!keyPhotoKey) {
[IMBAppleMediaLibraryPropertySynchronizer mediaObjectsForMediaGroup:mediaGroup];
if (mediaGroup.identifier) {
mediaGroup = [self.mediaSource mediaGroupForIdentifier:mediaGroup.identifier];
keyPhotoKey = mediaGroup.attributes[@"KeyPhotoKey"];
}
}
if (keyPhotoKey) {
return [self.mediaSource mediaObjectForIdentifier:keyPhotoKey];
} else {
// NSLog(@"No key photo in media group %@", mediaGroup.attributes);
return [super keyMediaObjectForMediaGroup:mediaGroup];
}
}
/**
*/
- (NSImage *)thumbnailForMediaGroup:(MLMediaGroup *)mediaGroup
{
return [self _thumbnailForMediaGroup:mediaGroup mosaic:YES];
}
/**
*/
- (NSImage *)_thumbnailForMediaGroup:(MLMediaGroup *)mediaGroup mosaic:(BOOL)mosaic
{
// Convenience
NSImage *(^thumbnailForName)(NSString *) = ^NSImage *(NSString *fileBaseName) {
return [NSImage imb_imageForResource:fileBaseName fromAppWithBundleIdentifier:@"com.apple.Photos" fallbackName:@"empty_album"];
};
if ([self isEmptyFolderMediaGroup:mediaGroup])
{
return thumbnailForName(@"empty_folder");
}
else if ([self isFolderMediaGroup:mediaGroup] || [self isFacesMediaGroup:mediaGroup])
{
NSArray *childGroups = [mediaGroup imb_childGroupsUptoMaxCount:9]; // Square of 3x3
if (mosaic) {
NSMutableArray *mosaicThumbnails = [NSMutableArray array];
for (MLMediaGroup *childGroup in childGroups) {
NSImage *thumbnail = [self _thumbnailForMediaGroup:childGroup mosaic:NO];
if (thumbnail) [mosaicThumbnails addObject:thumbnail];
}
NSImage *folderBackground = nil;
if (![self isFacesMediaGroup:mediaGroup]) {
folderBackground = thumbnailForName(@"folder_background");
}
NSImage *thumbnail = [[IMBImageProcessor sharedInstance] imageMosaicFromImages:mosaicThumbnails
withBackgroundImage:folderBackground
withCornerRadius:0.0];
if (!thumbnail && [self isFacesMediaGroup:mediaGroup]) {
thumbnail = thumbnailForName(@"FacesAlbum");
} else if (!thumbnail) {
thumbnail = thumbnailForName(@"empty_folder");
}
return thumbnail;
} else {
return [self _thumbnailForMediaGroup:(MLMediaGroup *)[childGroups firstObject] mosaic:mosaic];
}
} else if (mediaGroup != nil) {
// Media group is not a folder
MLMediaObject *keyMediaObject = [self keyMediaObjectForMediaGroup:mediaGroup];
if (keyMediaObject) {
NSImage *baseThumbnail = [self thumbnailForMediaObject:keyMediaObject];
return [self thumbnailForMediaGroup:mediaGroup baseThumbnail:baseThumbnail];
} else {
return thumbnailForName(@"empty_album");
}
}
return nil;
}
- (NSImage *)thumbnailForObject:(IMBObject *)object baseThumbnail:(NSImage *)thumbnail
{
if ([object isKindOfClass:[IMBNodeObject class]]) {
if (thumbnail == nil) {
NSDictionary *mediaGroupTypeToResourceName =
@{
@"com.apple.Photos.Folder" : @"empty_folder",
@"com.apple.Photos.Album" : @"empty_album",
};
NSString *resourceName = mediaGroupTypeToResourceName[object.preliminaryMetadata[@"typeIdentifier"]];
if (!resourceName) resourceName = @"empty_album";
thumbnail = [NSImage imb_imageForResource:resourceName fromAppWithBundleIdentifier:@"com.apple.Photos" fallbackName:nil];
}
NSString *groupTypeIdentifier = object.preliminaryMetadata[@"typeIdentifier"];
CGFloat cornerRadius = 0;
if ([groupTypeIdentifier isEqualToString:@"com.apple.Photos.FacesAlbum"]) {
cornerRadius = 255.0;
}
thumbnail = [[IMBImageProcessor sharedInstance] imageSquaredWithCornerRadius:cornerRadius fromImage:thumbnail];
}
return thumbnail;
}
- (NSImage *)thumbnailForMediaGroup:(MLMediaGroup *)mediaGroup baseThumbnail:(NSImage *)thumbnail
{
NSString *groupTypeIdentifier = mediaGroup.typeIdentifier;
CGFloat cornerRadius = 0;
if ([groupTypeIdentifier isEqualToString:@"com.apple.Photos.FacesAlbum"]) {
cornerRadius = 255.0;
}
thumbnail = [[IMBImageProcessor sharedInstance] imageSquaredWithCornerRadius:cornerRadius fromImage:thumbnail];
return thumbnail;
}
- (NSString *)countFormatForGroup: (MLMediaGroup *)mediaGroup plural:(BOOL)plural
{
NSDictionary *typeIdentifierToLocalizationKey =
@{
@"com.apple.Photos.FacesAlbum" : @"IMBFaceObjectViewController.countFormat"
};
NSString *localizationKey = typeIdentifierToLocalizationKey[mediaGroup.typeIdentifier];
if (localizationKey == nil && [self shouldUseChildGroupsAsMediaObjectsForMediaGroup:mediaGroup]) {
localizationKey = @"IMBSkimmableObjectViewController.countFormat";
}
NSString *localizationKeyPostfix = plural ? @"Plural" : @"Singular";
if (localizationKey) {
localizationKey = [localizationKey stringByAppendingString:localizationKeyPostfix];
return NSLocalizedStringWithDefaultValue(localizationKey,
nil, IMBBundle(), nil,
@"Format string for object count");
}
return [super countFormatForGroup:mediaGroup plural:plural];
}
#pragma mark - Utility
- (BOOL)isFolderMediaGroup:(MLMediaGroup *)mediaGroup
{
return ([mediaGroup.typeIdentifier isEqualToString:kIMBPhotosMediaGroupTypeIdentifierFolder]);
}
- (BOOL)isFacesMediaGroup:(MLMediaGroup *)mediaGroup
{
// Rule out all "face" media groups that have the same type as the "faces" media group
return ([mediaGroup.identifier isEqualToString:kIMBPhotosMediaGroupIdentifierPeople]);
}
- (BOOL)isEmptyFolderMediaGroup:(MLMediaGroup *)mediaGroup
{
return ([self isFolderMediaGroup:mediaGroup] && [[mediaGroup childGroups] count] == 0 );
}
- (BOOL)isEmptyButNotFolderMediaGroup:(MLMediaGroup *)mediaGroup
{
return (![self isFolderMediaGroup:mediaGroup] && [[mediaGroup childGroups] count] == 0 );
}
@end