-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathATLApplicationSection.m
183 lines (155 loc) · 4.55 KB
/
ATLApplicationSection.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
#import <Foundation/Foundation.h>
#import "ATLApplicationSection.h"
@implementation ATLApplicationSection
+ (ApplicationSectionType)sectionTypeFromString:(NSString*)typeString
{
if([typeString isEqualToString:kApplicationSectionTypeAll])
{
return SECTION_TYPE_ALL;
}
else if([typeString isEqualToString:kApplicationSectionTypeSystem])
{
return SECTION_TYPE_SYSTEM;
}
else if([typeString isEqualToString:kApplicationSectionTypeUser])
{
return SECTION_TYPE_USER;
}
else if([typeString isEqualToString:kApplicationSectionTypeHidden])
{
return SECTION_TYPE_HIDDEN;
}
else if([typeString isEqualToString:kApplicationSectionTypeVisible])
{
return SECTION_TYPE_VISIBLE;
}
return SECTION_TYPE_CUSTOM;
}
+ (NSString*)stringFromSectionType:(ApplicationSectionType)sectionType
{
switch(sectionType)
{
case SECTION_TYPE_ALL:
return kApplicationSectionTypeAll;
case SECTION_TYPE_SYSTEM:
return kApplicationSectionTypeSystem;
case SECTION_TYPE_USER:
return kApplicationSectionTypeUser;
case SECTION_TYPE_HIDDEN:
return kApplicationSectionTypeHidden;
case SECTION_TYPE_VISIBLE:
return kApplicationSectionTypeVisible;
default:
return kApplicationSectionTypeCustom;
}
}
+ (NSString*)sectionTitleForNonCustomSectionType:(ApplicationSectionType)sectionType
{
switch(sectionType)
{
case SECTION_TYPE_ALL:
case SECTION_TYPE_VISIBLE:
return @"Applications";
case SECTION_TYPE_SYSTEM:
return @"System Applications";
case SECTION_TYPE_USER:
return @"User Applications";
case SECTION_TYPE_HIDDEN:
return @"Hidden Applications";
default:
return nil;
}
}
+ (__kindof ATLApplicationSection*)applicationSectionWithDictionary:(NSDictionary*)sectionDictionary
{
NSString* customClassString = sectionDictionary[@"customClass"];
if(customClassString)
{
Class customClass = NSClassFromString(customClassString);
return [[customClass alloc] _initWithDictionary:sectionDictionary];
}
else
{
return [[ATLApplicationSection alloc] _initWithDictionary:sectionDictionary];
}
}
- (instancetype)_initWithDictionary:(NSDictionary*)sectionDictionary
{
NSString* sectionTypeString = sectionDictionary[@"sectionType"];
if(!sectionTypeString) return nil;
ApplicationSectionType sectionType = [[self class] sectionTypeFromString:sectionTypeString];
if(sectionType == SECTION_TYPE_CUSTOM)
{
NSString* predicateString = sectionDictionary[@"sectionPredicate"];
NSPredicate* predicate = [NSPredicate predicateWithFormat:predicateString];
NSString* sectionName = sectionDictionary[@"sectionName"];
self = [self initCustomSectionWithPredicate:predicate sectionName:sectionName];
}
else
{
self = [self initNonCustomSectionWithType:sectionType];
}
return self;
}
- (instancetype)initNonCustomSectionWithType:(ApplicationSectionType)sectionType
{
self = [super init];
self.sectionType = sectionType;
return self;
}
- (instancetype)initCustomSectionWithPredicate:(NSPredicate*)predicate sectionName:(NSString*)sectionName
{
self = [super init];
self.sectionType = SECTION_TYPE_CUSTOM;
_customPredicate = predicate;
_sectionName = sectionName;
return self;
}
- (void)setSectionType:(ApplicationSectionType)sectionType
{
_sectionType = sectionType;
if(_sectionType != SECTION_TYPE_CUSTOM)
{
_sectionName = [[self class] sectionTitleForNonCustomSectionType:sectionType];
}
}
- (NSArray<NSSortDescriptor*>*)sortDescriptorsForApplications
{
return @[[NSSortDescriptor sortDescriptorWithKey:@"atl_fastDisplayName" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]];
}
- (void)populateFromAllApplications:(NSArray*)allApplications
{
NSPredicate* predicateToUse;
switch(_sectionType)
{
case SECTION_TYPE_ALL:
break;
case SECTION_TYPE_SYSTEM:
predicateToUse = [NSPredicate predicateWithFormat:@"atl_isSystemApplication == YES"];
break;
case SECTION_TYPE_USER:
predicateToUse = [NSPredicate predicateWithFormat:@"atl_isUserApplication == YES"];
break;
case SECTION_TYPE_HIDDEN:
predicateToUse = [NSPredicate predicateWithFormat:@"atl_isHidden == YES"];
break;
case SECTION_TYPE_VISIBLE:
predicateToUse = [NSPredicate predicateWithFormat:@"atl_isHidden == NO"];
break;
default:
predicateToUse = _customPredicate;
break;
}
NSArray* filteredApplications;
if(predicateToUse)
{
filteredApplications = [allApplications filteredArrayUsingPredicate:predicateToUse];
}
else
{
filteredApplications = allApplications;
}
NSArray* filteredAndSortedApplications = [filteredApplications sortedArrayUsingDescriptors:[self sortDescriptorsForApplications]];
_applicationsInSection = filteredAndSortedApplications;
}
@end