This repository was archived by the owner on Dec 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathPFColorAlert.m
More file actions
executable file
·197 lines (158 loc) · 7.99 KB
/
PFColorAlert.m
File metadata and controls
executable file
·197 lines (158 loc) · 7.99 KB
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
#import "PFColorAlert.h"
#import "PFColorAlertViewController.h"
#import "UIColor+PFColor.h"
#import "UIKitAdditions.h"
#import <objc/runtime.h>
extern void LCPShowTwitterFollowAlert(UIViewController *viewController,
NSString *title,
NSString *welcomeMessage,
NSString *twitterUsername);
#define degreesToRadians(x) ((x) * M_PI / 180.0)
@interface PFColorAlert ()
@property (nonatomic, retain) UIWindow *darkeningWindow;
@property (nonatomic, retain) UIWindow *previousKeyWindow;
@property (nonatomic, retain) PFColorAlertViewController *mainViewController;
@property (nonatomic, assign) BOOL isOpen;
@property (nonatomic, copy) void (^completionBlock)(UIColor *pickedColor);
@end
@implementation PFColorAlert
+ (PFColorAlert *)colorAlertWithStartColor:(UIColor *)startColor showAlpha:(BOOL)showAlpha {
return [[[PFColorAlert alloc] initWithStartColor:startColor showAlpha:showAlpha] autorelease];
}
- (PFColorAlert *)initWithStartColor:(UIColor *)startColor showAlpha:(BOOL)showAlpha {
self = [super init];
self.isOpen = NO;
CGRect winFrame = [UIScreen mainScreen].bounds;
CGFloat deviceHeight = winFrame.size.height;
/* `statusBarOrientation` is deprecated as of iOS 13. However, it
seems to be the only reliable way to get the device rotation.
All other ways fail in one way or another. */
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsLandscape(orientation)) {
deviceHeight = winFrame.size.width;
winFrame.size.width = winFrame.size.height;
winFrame.size.height = deviceHeight;
}
if (@available(iOS 13, *)) {
NSSet<UIScene *> *connectedScenes = [[UIApplication sharedApplication] connectedScenes];
UIScene *firstScene = [[connectedScenes allObjects] objectAtIndex:0];
self.darkeningWindow = [[UIWindow alloc] initWithWindowScene:firstScene];
self.darkeningWindow.frame = winFrame;
} else {
self.darkeningWindow = [[UIWindow alloc] initWithFrame:winFrame];
}
self.darkeningWindow.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4f];
float winWidthCalc = winFrame.size.width * 0.09f;
float winHeightCalc = winFrame.size.height * 0.09f;
winFrame.origin.x = winWidthCalc / 2;
winFrame.size.width -= winWidthCalc;
winFrame.size.height -= winHeightCalc;
if (@available(iOS 13, *)) {
NSSet<UIScene *> *connectedScenes = [[UIApplication sharedApplication] connectedScenes];
UIScene *firstScene = [[connectedScenes allObjects] objectAtIndex:0];
self.popWindow = [[UIWindow alloc] initWithWindowScene:firstScene];
self.popWindow.frame = winFrame;
} else {
self.popWindow = [[UIWindow alloc] initWithFrame:winFrame];
}
self.popWindow.layer.masksToBounds = true;
self.popWindow.layer.cornerRadius = 15;
CGRect mainFrame = CGRectMake(0, 0, winFrame.size.width, winFrame.size.height);
self.mainViewController = [[PFColorAlertViewController alloc] initWithViewFrame:mainFrame
startColor:startColor
showAlpha:showAlpha];
self.darkeningWindow.hidden = NO;
self.darkeningWindow.alpha = 0.0f;
self.previousKeyWindow = [UIApplication sharedApplication].keyWindow;
[self.darkeningWindow makeKeyAndVisible];
self.popWindow.rootViewController = self.mainViewController;
#ifndef DEBUG
self.darkeningWindow.windowLevel = UIWindowLevelAlert - 2;
self.popWindow.windowLevel = UIWindowLevelAlert - 1;
#endif
self.popWindow.backgroundColor = UIColor.clearColor;
self.popWindow.hidden = NO;
self.popWindow.alpha = 0.0f;
[self makeViewDynamic:self.popWindow deviceHeight:deviceHeight];
return self;
}
- (void)makeViewDynamic:(UIView *)view deviceHeight:(CGFloat)deviceHeight {
CGRect dynamicFrame = view.frame;
dynamicFrame.size.height = [self.mainViewController topMostSliderLastYCoordinate] +
self.mainViewController.view.frame.size.width / 6;
dynamicFrame.origin.y = deviceHeight / 2 - dynamicFrame.size.height / 2;
// Rotate 180 deg on iPad if using it upside down
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad &&
[UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown)
view.transform = CGAffineTransformMakeRotation(degreesToRadians(180));
view.frame = dynamicFrame;
}
- (void)displayWithCompletion:(void (^)(UIColor *pickedColor))completionBlock {
if (self.isOpen)
return;
self.completionBlock = completionBlock;
[self retain];
[self.popWindow makeKeyAndVisible];
[UIView animateWithDuration:0.3f animations:^{
self.darkeningWindow.alpha = 1.0f;
self.popWindow.alpha = 1.0f;
} completion:^(BOOL finished) {
self.isOpen = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(close)];
self.darkeningWindow.userInteractionEnabled = YES;
[self.darkeningWindow addGestureRecognizer:tapGesture];
[tapGesture release];
NSString *prefPath = @"/var/mobile/Library/Preferences/com.pixelfiredev.libcolorpicker.plist";
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile:prefPath];
if (!dict)
dict = [[NSMutableDictionary new] autorelease];
NSString *kDidShow = @"didShowWelcomeScreen";
if (!dict[kDidShow]) {
LCPShowTwitterFollowAlert(self.mainViewController,
@"Welcome to libcolorpicker!",
@"Hey there! Thanks for installing libcolorpicker (the color picker library for devs)! If you'd like to follow our team on Twitter for more updates, tweak giveaways and other cool stuff, hit the button below!",
@"PixelFireDev");
[dict setObject:@YES forKey:kDidShow];
[dict writeToFile:prefPath atomically:YES];
}
if (objc_getClass("UIAlertController")) {
NSString *pasteboard = [UIPasteboard generalPasteboard].string;
if (!pasteboard || [pasteboard isEqualToString:[[self.mainViewController getColor] hexFromColor]])
return;
NSRange range = [pasteboard rangeOfString:@"^#(?:[0-9a-fA-F]{3}){1,2}$" options:NSRegularExpressionSearch];
if (range.location != NSNotFound)
[self.mainViewController presentPasteHexStringQuestion:pasteboard];
}
}];
}
- (void)showWithStartColor:(UIColor *)startColor
showAlpha:(BOOL)showAlpha
completion:(void (^)(UIColor *pickedColor))completionBlock {
UIAlertView *deprecated = [[UIAlertView alloc] initWithTitle:@"libcolorpicker" message:@"Hey! It appears like this preference bundle is trying to use deprecated methods to invoke the color picker and requires an update. Please inform the dev of this tweak about it."
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
[deprecated show];
}
- (void)close {
if (!self.isOpen)
return;
[UIView animateWithDuration:0.3f animations:^{
self.darkeningWindow.alpha = 0.0f;
self.popWindow.alpha = 0.0f;
} completion:^(BOOL finished) {
if (self.completionBlock)
self.completionBlock([self.mainViewController getColor]);
self.popWindow.hidden = YES;
self.isOpen = NO;
[self.previousKeyWindow makeKeyAndVisible];
}];
}
- (void)dealloc {
[self.mainViewController release];
[self.popWindow release];
[self.darkeningWindow release];
self.completionBlock = nil;
[super dealloc];
}
@end