-
Notifications
You must be signed in to change notification settings - Fork 5
/
FAFolderCell.m
277 lines (216 loc) · 11.2 KB
/
FAFolderCell.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
/**
FAFolderCell.m
FoldMusic
version 1.2.0, July 15th, 2012
Copyright (C) 2012 theiostream
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
theiostream
**/
// UI by Maximus. (@_mxms). He's also an awesome developer.
#import "FAFolderCell.h"
#import "FACalloutView.h"
#import <AppSupport/CPDistributedMessagingCenter.h>
@interface UIDevice (blah_fuck)
- (BOOL)isWildcat;
@end
#define isiPad() ([UIDevice instancesRespondToSelector:@selector(isWildcat)] && [[UIDevice currentDevice] isWildcat])
// FIXME: I repeat this function declaration here and at FAMediaPickerController.
static UIImage *UIImageResize(UIImage *image, CGSize newSize) {
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
void FADrawLineAtPath(UIView *view, CGPathRef path) {
CAShapeLayer *shape = [CAShapeLayer layer];
[shape setLineWidth:1.f];
[shape setLineCap:kCALineCapRound];
[shape setStrokeColor:[UIColorFromHexWithAlpha(0xFFFFFF, 0.37) CGColor]];
[shape setPath:path];
[[view layer] addSublayer:shape];
[(FAFolderCell *)view setShapeLayer:shape];
}
@implementation FAFolderCell
+ (UIFont *)labelFont {
if (kCFCoreFoundationVersionNumber >= 800) return [UIFont fontWithName:@"HelveticaNeue-Light" size:18];
return [UIFont fontWithName:@"Helvetica" size:18];
}
+ (UIFont *)smallLabelFont {
if (kCFCoreFoundationVersionNumber >= 800) return [UIFont fontWithName:@"HelveticaNeue-Light" size:14];
return [UIFont fontWithName:@"Helvetica" size:14];
}
+ (UILabel *)_makeLabelWithRect:(CGRect)rect {
UILabel *lbl = [[[UILabel alloc] initWithFrame:rect] autorelease];
[lbl setFont:[FAFolderCell labelFont]];
[lbl setBackgroundColor:[UIColor clearColor]];
[lbl setTextColor:[UIColor whiteColor]];
// This made it lag so much.
// Really, it did.
/*CALayer *layer = [lbl layer];
[layer setShadowColor:[[UIColor blackColor] CGColor]];
[layer setShadowOpacity:.5f];
[layer setShadowOffset:CGSizeMake(0, 1)];
[layer setShouldRasterize:YES];*/
return lbl;
}
+ (CGFloat)_widthForProperty:(NSString *)property {
if ([property isEqualToString:MPMediaItemPropertyPlaybackDuration] || [property isEqualToString:MPMediaItemPropertyPlayCount])
// FIXME: Why the fuck?
return isiPad() ? 105.f : 65.f;
else
return isiPad() ? 240.f : 120.f;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
if (kCFCoreFoundationVersionNumber >= 800)
[self setBackgroundColor:[UIColor clearColor]];
idx = 0;
_property = [MPMediaItemPropertyPlaybackDuration retain];
_placeholder = [UIImageResize([UIImage imageWithContentsOfFile:@"/System/Library/PrivateFrameworks/iPodUI.framework/CoverFlowPlaceHolder44.png"], CGSizeMake(32, 32)) retain];
//_state = MPMusicPlaybackStateStopped;
/*UILongPressGestureRecognizer *pressGesture = [[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(showProgressCallout)] autorelease];
[self addGestureRecognizer:pressGesture];*/
_label = [FAFolderCell _makeLabelWithRect:CGRectMake(47, 8, 0, 20)];
[[self contentView] addSubview:_label];
CGFloat wd = [FAFolderCell _widthForProperty:_property];
_infoLabel = [FAFolderCell _makeLabelWithRect:CGRectMake((isiPad() ? (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) ? 768 : 1024) : 320)-wd+10, 8, 0, 20)];
[[self contentView] addSubview:_infoLabel];
__imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(10, 2.5, 32, 32)] autorelease];
[[__imageView layer] setBorderWidth:.5f];
[[__imageView layer] setBorderColor:[[UIColor whiteColor] CGColor]];
[[self contentView] addSubview:__imageView];
/*UIImage *ind_ = [UIImage imageWithContentsOfFile:@"/System/Library/PrivateFrameworks/iPodUI.framework/NowPlayingListItemIcon.png"];
_speaker = [[[UIImageView alloc] initWithFrame:CGRectMake((_label.frame.size.width-50)+1, _label.frame.origin.y, 28, 24)] autorelease];
[_speaker setImage:ind_];
[_speaker setHidden:YES];
[self addSubview:_speaker];*/
//CGFloat wid = (isiPad() ? (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) ? 768 : 1024) : 320)-wd;
CGFloat wid = [self bounds].size.width - wd;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, wid, [self bounds].origin.y);
CGPathAddLineToPoint(path, NULL, wid, [self bounds].origin.y+38.f);
FADrawLineAtPath(self, path);
_path = CGPathCreateCopy(path);
CGPathRelease(path);
}
return self;
}
- (UIImage *)placeholderImage {
MPMediaItemArtwork *artwork = [_item valueForProperty:MPMediaItemPropertyArtwork];
UIImage *artworkImage = [artwork imageWithSize:CGSizeMake(32, 32)];
if (artworkImage)
return artworkImage;
return _placeholder;
}
- (void)setShapeLayer:(CAShapeLayer *)shape {
_shape = shape;
}
- (void)layoutSubviews {
[super layoutSubviews];
UIImage *artwork = [self placeholderImage];
[__imageView setImage:artwork];
[_label setText:[_item valueForProperty:MPMediaItemPropertyTitle]];
[_infoLabel setText:[self valueForProperty]];
CGFloat wd = [FAFolderCell _widthForProperty:_property];
CGMutablePathRef path = CGPathCreateMutable();
/*CGPathMoveToPoint(path, NULL, (isiPad() ? (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) ? 768 : 1024) : 320)-wd, [self bounds].origin.y);
CGPathAddLineToPoint(path, NULL, (isiPad() ? (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) ? 768 : 1024) : 320)-wd, [self bounds].origin.y+38.f);*/
CGPathMoveToPoint(path, NULL, [self bounds].size.width-wd, [self bounds].origin.y);
CGPathAddLineToPoint(path, NULL, [self bounds].size.width-wd, [self bounds].origin.y+38.f);
[_shape setPath:path];
[_label setFont:(wd==65.f||isiPad() ? [FAFolderCell labelFont] : [FAFolderCell smallLabelFont])];
/*[_label setFrame:(CGRect){_label.frame.origin, {(isiPad() ? (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) ? 768 : 1024) : 320)-(wd+50), _label.frame.size.height}}];
[_infoLabel setFrame:CGRectMake((isiPad() ? (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) ? 768 : 1024) : 320)-wd+10, 8, wd-15, 20)];*/
[_label setFrame:(CGRect){_label.frame.origin, {[self bounds].size.width-(wd+50), _label.frame.size.height}}];
[_infoLabel setFrame:CGRectMake([self bounds].size.width-wd+10, 8, wd-15, 20)];
}
- (void)_changeDetailAnimated {
CGFloat wd = [FAFolderCell _widthForProperty:_property];
CGMutablePathRef path = CGPathCreateMutable();
/*CGPathMoveToPoint(path, NULL, (isiPad() ? (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) ? 768 : 1024) : 320)-wd, [self bounds].origin.y);
CGPathAddLineToPoint(path, NULL, (isiPad() ? (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) ? 768 : 1024) : 320)-wd, [self bounds].origin.y+38.f);*/
CGPathMoveToPoint(path, NULL, [self bounds].size.width-wd, [self bounds].origin.y);
CGPathAddLineToPoint(path, NULL, [self bounds].size.width-wd, [self bounds].origin.y+38.f);
[_shape setPath:path];
_path = CGPathCreateCopy(path);
CGPathRelease(path);
[UIView animateWithDuration:.1f animations:^{
[_label setFont:(wd==65.f||isiPad() ? [FAFolderCell labelFont] : [FAFolderCell smallLabelFont])];
//[_label setFrame:(CGRect){_label.frame.origin, {(isiPad() ? (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) ? 768 : 1024) : 320)-(wd+50), _label.frame.size.height}}];
[_label setFrame:(CGRect){_label.frame.origin, {[self bounds].size.width-(wd+50), _label.frame.size.height}}];
[_infoLabel setAlpha:0.f];
//[_infoLabel setFrame:CGRectMake((isiPad() ? (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) ? 768 : 1024) : 320)-wd+10, 8, wd-15, 20)];
[_infoLabel setFrame:CGRectMake([self bounds].size.width-wd+10, 8, wd-15, 20)];
[_infoLabel setText:[self valueForProperty]];
[_infoLabel setAlpha:1.f];
}];
}
- (NSString *)valueForProperty {
id value = [_item valueForProperty:_property];
// Handle the duration special case
if ([_property isEqualToString:MPMediaItemPropertyPlaybackDuration]) {
int tsec = (int)round([value doubleValue]);
int min = tsec/60;
int sec = tsec-min*60;
char *zero = sec<10 ? "0" : ""; // FIXME: This seems wrong.
return [NSString stringWithFormat:@"%i:%s%i", min, zero, sec];
}
if ([value isKindOfClass:[NSNumber class]])
return [value stringValue];
else if ([value isKindOfClass:[NSString class]]) {
if (value)
return value;
}
return @"N/A";
}
- (void)setDetailProperty:(NSString *)property change:(BOOL)change {
[_property release];
_property = [property retain];
if (change) [self _changeDetailAnimated];
else [self setNeedsDisplay];
}
- (void)setMediaItem:(MPMediaItem *)item {
_item = [item retain];
[self setNeedsDisplay];
}
/*// The first part of this is basically a copy of what we do inside Tweak.xm
// This tweak really needs less code duplicates.
- (void)showProgressCallout {
CPDistributedMessagingCenter *center = [CPDistributedMessagingCenter centerNamed:@"am.theiostre.foldalbum.player"];
NSData *nowPlayingData = [[center sendMessageAndReceiveReplyName:@"NowPlayingItem" userInfo:nil] objectForKey:@"Item"];
if (nowPlayingData) {
MPMediaItem *nowPlayingItem = [NSKeyedUnarchiver unarchiveObjectWithData:nowPlayingData];
NSNumber *nowPlayingPersistent = [nowPlayingItem valueForProperty:MPMediaItemPropertyPersistentID];
NSNumber *cellItemPersistent = [_item valueForProperty:MPMediaItemPropertyPersistentID];
if ([nowPlayingPersistent compare:cellItemPersistent] == NSOrderedSame) {
UISlider *slider = [[[UISlider alloc] initWithFrame:CGRectMake(0, 0, 200, 20)] autorelease];
FACalloutView *alert = [[[FACalloutView alloc] init] autorelease];
[alert placeQuitButtonInView:[self superview]];
[alert setCenteredView:slider animated:YES];
[alert setAnchorPoint:CGPointMake(30, 30) boundaryRect:[[UIScreen mainScreen] applicationFrame] animate:YES];
[[self superview] addSubview:alert];
}
}
}*/
- (void)dealloc {
CGPathRelease(_path);
[_item release];
[_property release];
[_placeholder release];
[super dealloc];
}
@end