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
/
Copy pathPFSimpleLiteColorCell.mm
executable file
·116 lines (88 loc) · 3.98 KB
/
PFSimpleLiteColorCell.mm
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
#import <UIKit/UIKit.h>
#import <CoreGraphics/CoreGraphics.h>
#import "libcolorpicker.h"
#import "PSSpecifier.h"
@interface PFSimpleLiteColorCell()
- (void)openColorAlert;
@property (nonatomic, retain) NSMutableDictionary *options;
@property (nonatomic, retain) PFColorAlert *alert;
@end
#define kPostNotification @"PostNotification"
#define kKey @"key"
#define kDefaults @"defaults"
#define kAlpha @"alpha"
#define kFallback @"fallback"
@implementation PFSimpleLiteColorCell
- (id)initWithStyle:(long long)style reuseIdentifier:(id)identifier specifier:(PSSpecifier *)specifier {
self = [super initWithStyle:style reuseIdentifier:identifier specifier:specifier];
[self setLCPOptions];
return self;
}
- (void)setLCPOptions {
self.options = [[self.specifier properties][@"libcolorpicker"] mutableCopy];
if (!self.options)
self.options = [NSMutableDictionary dictionary];
if (!self.options[kPostNotification]) {
NSString *option = [NSString stringWithFormat:@"%@_%@_libcolorpicker_refreshn",
self.options[kDefaults], self.options[kKey]];
[self.options setObject:option forKey:kPostNotification];
}
[(PSSpecifier *)self.specifier setProperty:self.options[kPostNotification]
forKey:@"NotificationListener"];
}
- (UIColor *)previewColor {
NSString *plistPath = [NSString stringWithFormat:@"/var/mobile/Library/Preferences/%@.plist", self.options[kDefaults]];
NSMutableDictionary *prefsDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
if (!prefsDict)
prefsDict = [NSMutableDictionary dictionary];
return LCPParseColorString([prefsDict objectForKey:self.options[kKey]], self.options[kFallback]); // this color will be used at startup
}
- (void)didMoveToSuperview {
[self setLCPOptions];
[super didMoveToSuperview];
[self.specifier setTarget:self];
[self.specifier setButtonAction:@selector(openColorAlert)];
}
- (void)openColorAlert {
if (!self.options[kDefaults] || !self.options[kKey] || !self.options[kFallback])
return;
// HBLogDebug(@"Loading with options %@", self.options);
NSString *plistPath = [NSString stringWithFormat:@"/var/mobile/Library/Preferences/%@.plist", self.options[kDefaults]];
NSMutableDictionary *prefsDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
if (!prefsDict)
prefsDict = [NSMutableDictionary dictionary];
UIColor *startColor = LCPParseColorString([prefsDict objectForKey:self.options[kKey]], self.options[kFallback]); // this color will be used at startup
BOOL showAlpha = self.options[kAlpha] ? [self.options[kAlpha] boolValue] : NO;
self.alert = [PFColorAlert colorAlertWithStartColor:startColor
showAlpha:showAlpha];
// show alert // Show alpha slider? // Code to run after close
[self.alert displayWithCompletion:^void (UIColor *pickedColor) {
// save pickedColor or do something with it
NSString *hexString = [UIColor hexFromColor:pickedColor];
hexString = [hexString stringByAppendingFormat:@":%f", pickedColor.alpha]; //if you want to use alpha
// ^^ parse fallback to ^red
// save hexString to your plist if desired
[prefsDict setObject:hexString forKey:self.options[kKey]];
[prefsDict writeToFile:plistPath atomically:YES];
NSString *notification = self.options[kPostNotification];
if (notification)
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(),
(CFStringRef)notification,
(CFStringRef)notification,
NULL,
YES);
}];
}
- (SEL)action {
return @selector(openColorAlert);
}
- (id)target {
return self;
}
- (SEL)cellAction {
return @selector(openColorAlert);
}
- (id)cellTarget {
return self;
}
@end