-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathWeatherGroundManager.xm
executable file
·368 lines (300 loc) · 15.4 KB
/
WeatherGroundManager.xm
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#import "WeatherGroundManager.h"
#include <RemoteLog.h>
@implementation WeatherGroundManager
- (BOOL)boolForKey:(NSString *)key {
id object = [self.preferencesDictionary objectForKey:key];
return object ? [object boolValue] : NO;
}
- (int)intForKey:(NSString *)key {
id object = [self.preferencesDictionary objectForKey:key];
return object ? [object intValue] : 0;
}
+ (instancetype)sharedManager {
static dispatch_once_t onceToken = 0;
__strong static WeatherGroundManager *sharedManager = nil;
dispatch_once(&onceToken, ^{
sharedManager = [[self alloc] init];
});
return sharedManager;
}
- (instancetype)init {
self = [super init];
if (self) {
_preferencesDictionary = [NSDictionary dictionaryWithContentsOfFile:@"/var/mobile/Library/Preferences/com.tr1fecta.wgprefs.plist"];
// Convert to minutes from seconds
double interval = (double)[self intForKey:@"kAutoUpdateInterval"] * 60;
if (interval > 0) {
_autoUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(updateModel) userInfo:nil repeats:YES];
}
}
return self;
}
// In Apps
- (void)setStatusBarTextToWeatherInfo:(NSDictionary *)infoDict {
if (self.statusStringView != nil) {
NSMutableAttributedString *temperatureAttrString = [[self temperatureInfo:infoDict[@"unit"]] objectForKey:@"weatherString"];
self.statusStringView.attributedText = temperatureAttrString;
[self changeLabelTextWithAttributedString:temperatureAttrString];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.timeZone = [NSTimeZone localTimeZone];
formatter.dateFormat = @"HH:mm";
NSString *currentStatusTime = [formatter stringFromDate:[NSDate now]];
self.statusStringView.attributedText = nil;
[self changeLabelText:currentStatusTime];
});
}
}
- (void)changeLabelTextWithAttributedString:(NSMutableAttributedString *)text {
CATransition *animation = [CATransition animation];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.type = kCATransitionPush;
animation.subtype = kCATransitionFromTop;
animation.duration = 0.3;
[self.statusStringView.layer addAnimation:animation forKey:@"kCATransitionPush"];
self.statusStringView.attributedText = text;
}
- (void)changeLabelText:(NSString *)text {
CATransition *animation = [CATransition animation];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.type = kCATransitionPush;
animation.subtype = kCATransitionFromTop;
animation.duration = 0.3;
[self.statusStringView.layer addAnimation:animation forKey:@"kCATransitionPush"];
self.statusStringView.text = text;
}
- (void)setupDynamicWeatherBackgrounds {
SBWallpaperController *wallpaperController = [%c(SBWallpaperController) sharedInstance];
SBFWallpaperView *sharedWallpaperView;
SBFWallpaperView *lockscreenWallpaperView;
SBFWallpaperView *homescreenWallpaperView;
if (%c(SBWallpaperViewController)) {
SBWallpaperViewController *wallpaperViewController = [wallpaperController valueForKey:@"_wallpaperViewController"];
sharedWallpaperView = wallpaperViewController.sharedWallpaperView;
lockscreenWallpaperView = wallpaperViewController.lockscreenWallpaperView;
homescreenWallpaperView = wallpaperViewController.homescreenWallpaperView;
}
else {
sharedWallpaperView = wallpaperController.sharedWallpaperView;
lockscreenWallpaperView = wallpaperController.lockscreenWallpaperView;
homescreenWallpaperView = wallpaperController.homescreenWallpaperView;
}
// Always create this instance for the weather effects layer, but only add if enabled
self.sharedBgView = [[%c(WUIDynamicWeatherBackground) alloc] initWithFrame:UIScreen.mainScreen.bounds];
self.sharedBgView.city = [self myCity];
self.sharedBgView.condition.city = [self myCity];
if (sharedWallpaperView != nil && [self boolForKey:@"kUseEntireWeatherView"] && [self boolForKey:@"kUseWeatherEffectsOnly"] == NO) {
[sharedWallpaperView addSubview:self.sharedBgView];
[self setSharedImageWithView:self.sharedBgView];
}
// Check if the user is using 2 different wallpapers
if (lockscreenWallpaperView != nil && homescreenWallpaperView != nil && sharedWallpaperView == nil) {
if ([self boolForKey:@"kLockscreenEnabled"]) {
self.lockScreenBgView = [[%c(WUIDynamicWeatherBackground) alloc] initWithFrame:UIScreen.mainScreen.bounds];
self.lockScreenBgView.city = [self myCity];
self.lockScreenBgView.condition.city = [self myCity];
if ([self boolForKey:@"kUseEntireWeatherView"]) {
[lockscreenWallpaperView addSubview:self.lockScreenBgView];
[self setSharedImageWithView:self.lockScreenBgView];
}
}
if ([self boolForKey:@"kHomescreenEnabled"]) {
self.homeScreenBgView = [[%c(WUIDynamicWeatherBackground) alloc] initWithFrame:UIScreen.mainScreen.bounds];
self.homeScreenBgView.city = [self myCity];
self.homeScreenBgView.condition.city = [self myCity];
if ([self boolForKey:@"kUseEntireWeatherView"]) {
[homescreenWallpaperView addSubview:self.homeScreenBgView];
[self setSharedImageWithView:self.homeScreenBgView];
}
}
}
}
- (void)setupWeatherEffectLayers {
if ([self boolForKey:@"kUseWeatherEffectsOnly"] && [self boolForKey:@"kUseEntireWeatherView"] == NO) {
SBWallpaperController *wallpaperController = [%c(SBWallpaperController) sharedInstance];
SBFWallpaperView *sharedWallpaperView;
SBFWallpaperView *lockscreenWallpaperView;
SBFWallpaperView *homescreenWallpaperView;
if (%c(SBWallpaperViewController)) {
SBWallpaperViewController *wallpaperViewController = [wallpaperController valueForKey:@"_wallpaperViewController"];
sharedWallpaperView = wallpaperViewController.sharedWallpaperView;
lockscreenWallpaperView = wallpaperViewController.lockscreenWallpaperView;
homescreenWallpaperView = wallpaperViewController.homescreenWallpaperView;
}
else {
sharedWallpaperView = wallpaperController.sharedWallpaperView;
lockscreenWallpaperView = wallpaperController.lockscreenWallpaperView;
homescreenWallpaperView = wallpaperController.homescreenWallpaperView;
}
if (sharedWallpaperView != nil && self.sharedBgView != nil) {
CALayer *nLayer = [self weatherEffectsLayerForWeatherView:nil];
[sharedWallpaperView.layer addSublayer:nLayer];
}
else if (lockscreenWallpaperView != nil && homescreenWallpaperView != nil && self.lockScreenBgView != nil && self.homeScreenBgView != nil && sharedWallpaperView == nil) {
if ([self boolForKey:@"kLockscreenEnabled"]) {
CALayer *nLayer = [self weatherEffectsLayerForWeatherView:self.lockScreenBgView];
[lockscreenWallpaperView.layer addSublayer:nLayer];
}
if ([self boolForKey:@"kHomescreenEnabled"]) {
CALayer *nLayer = [self weatherEffectsLayerForWeatherView:self.homeScreenBgView];
[homescreenWallpaperView.layer addSublayer:nLayer];
}
}
}
}
- (CALayer *)weatherEffectsLayerForWeatherView:(WUIDynamicWeatherBackground *)weatherView {
CALayer *nLayer = weatherView != nil ? weatherView.condition.layer : self.sharedBgView.condition.layer;
nLayer.bounds = UIScreen.mainScreen.nativeBounds;
nLayer.allowsGroupOpacity = YES;
nLayer.position = CGPointMake(0, UIScreen.mainScreen.bounds.size.height);
nLayer.geometryFlipped = YES;
return nLayer;
}
- (void)setSharedImageWithView:(WUIDynamicWeatherBackground *)backgroundView {
if (backgroundView != nil) {
// Take a screenshot of the current view, to use on SBFWallpaperView's contentView's image, otherwise the background when pulling up on Notification Center and Lockscreen will be see through
UIGraphicsBeginImageContextWithOptions(backgroundView.bounds.size, NO, UIScreen.mainScreen.scale);
[backgroundView drawViewHierarchyInRect:backgroundView.bounds afterScreenUpdates:YES];
self.sharedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
- (void)updateModel {
if (!self.widgetVC) {
self.widgetVC = [[%c(WALockscreenWidgetViewController) alloc] init];
if ([self.widgetVC respondsToSelector:@selector(_setupWeatherModel)]) {
[self.widgetVC _setupWeatherModel];
}
}
if (self.widgetVC) {
if ([self.widgetVC.todayModel respondsToSelector:@selector(executeModelUpdateWithCompletion:)]) {
if ([self.widgetVC.todayModel isKindOfClass:[WATodayAutoupdatingLocationModel class]]) {
WATodayAutoupdatingLocationModel *autoUpdatingModel = (WATodayAutoupdatingLocationModel *)self.widgetVC.todayModel;
if ([autoUpdatingModel respondsToSelector:@selector(updateLocationTrackingStatus)]) {
[autoUpdatingModel updateLocationTrackingStatus];
}
}
[self.widgetVC.todayModel executeModelUpdateWithCompletion:nil];
}
if ([self.widgetVC respondsToSelector:@selector(todayModelWantsUpdate:)] && self.widgetVC.todayModel) {
[self.widgetVC todayModelWantsUpdate:self.widgetVC.todayModel];
}
if ([self.widgetVC respondsToSelector:@selector(updateWeather)]) {
[self.widgetVC updateWeather];
}
if ([self.widgetVC respondsToSelector:@selector(_updateTodayView)]) {
[self.widgetVC _updateTodayView];
}
if ([self.widgetVC respondsToSelector:@selector(_updateWithReason:)]) {
[self.widgetVC _updateWithReason:nil];
}
/*if ([self.widgetVC respondsToSelector:@selector(_temperature)]) {
self.currentTemperature = [self.widgetVC _temperature];
}
if ([self.widgetVC respondsToSelector:@selector(_locationName)]) {
self.myCity = [self.widgetVC _locationName];
}*/
}
if (self.widgetVC.todayModel.forecastModel.city) {
self.myCity = self.widgetVC.todayModel.forecastModel.city;
if (self.sharedBgView != nil) {
[self.sharedBgView setCity:[self myCity] animate:YES];
[self.sharedBgView.condition setCity:[self myCity] animationDuration:2];
/*if ([self boolForKey:@"kUseEntireWeatherView"]) {
[self setSharedImageWithView:self.sharedBgView];
}*/
if ([self boolForKey:@"kUseWeatherEffectsOnly"]) {
[self setupWeatherEffectLayers];
}
}
if (self.lockScreenBgView != nil) {
[self.lockScreenBgView setCity:[self myCity] animate:YES];
[self.lockScreenBgView.condition setCity:[self myCity] animationDuration:2];
/*if ([self boolForKey:@"kUseEntireWeatherView"] && [self boolForKey:@"kLockscreenEnabled"]) {
[self setSharedImageWithView: self.lockScreenBgView];
}*/
if ([self boolForKey: @ "kUseWeatherEffectsOnly"]) {
[self setupWeatherEffectLayers];
}
}
if (self.homeScreenBgView != nil) {
[self.homeScreenBgView setCity: [self myCity] animate: YES];
[self.homeScreenBgView.condition setCity: [self myCity] animationDuration: 2];
/*if ([self boolForKey:@"kUseEntireWeatherView"] && [self boolForKey:@"kHomescreenEnabled"]) {
[self setSharedImageWithView: self.homeScreenBgView];
}*/
if ([self boolForKey: @ "kUseWeatherEffectsOnly"]) {
[self setupWeatherEffectLayers];
}
}
}
}
- (void)pauseWG {
if (self.sharedBgView != nil) {
[self.sharedBgView.condition pause];
}
if (self.lockScreenBgView != nil) {
[self.lockScreenBgView.condition pause];
}
if (self.homeScreenBgView != nil) {
[self.homeScreenBgView.condition pause];
}
}
- (void)resumeWG {
if (self.sharedBgView != nil) {
[self.sharedBgView.condition resume];
}
if (self.lockScreenBgView != nil) {
[self.lockScreenBgView.condition resume];
}
if (self.homeScreenBgView != nil) {
[self.homeScreenBgView.condition resume];
}
}
- (void)updateCityForCity:(City *)city {
city = self.myCity;
}
- (NSDictionary *)temperatureInfo:(NSString *)unit {
[self updateModel];
int temperature = 0;
if (self.widgetVC != nil && self.widgetVC.todayModel.forecastModel.currentConditions != nil) {
if ([unit isEqualToString:@"celsius"]) {
temperature = (int)self.widgetVC.todayModel.forecastModel.currentConditions.temperature.celsius;
}
else if ([unit isEqualToString:@"fahrenheit"]) {
temperature = (int)ceil(self.widgetVC.todayModel.forecastModel.currentConditions.temperature.fahrenheit);
}
else if ([unit isEqualToString:@"kelvin"]) {
temperature = (int)ceil(self.widgetVC.todayModel.forecastModel.currentConditions.temperature.kelvin);
}
}
int conditionCode = [self currentConditionCode];
NSMutableAttributedString *weatherString = [self stringForWeatherImage:[self getImageForCondition:conditionCode style:1] withPrefix:[NSString stringWithFormat:@"%d°", temperature]];
NSDictionary *infoDict = @{@"weatherString": weatherString};
return infoDict;
}
- (int)currentConditionCode {
if (self.widgetVC != nil && self.widgetVC.todayModel.forecastModel.currentConditions != nil) {
int conditionCode = (int)self.widgetVC.todayModel.forecastModel.currentConditions.conditionCode;
return conditionCode;
}
return 0;
}
- (UIImage *)getImageForCondition:(NSInteger)conditionCode style:(int)style {
UIImage *image = [WeatherImageLoader conditionImageWithConditionIndex:conditionCode style:style];
return image;
}
- (NSMutableAttributedString *)stringForWeatherImage:(UIImage *)weatherImg withPrefix:(NSString *)prefixString{
// Make a new Mutable Attributed String
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", prefixString]];
// Make a new NSTextAttachment variable and set the image
NSTextAttachment *imgAttachment = [[NSTextAttachment alloc] init];
imgAttachment.bounds = CGRectMake(0,-12,35,35);
imgAttachment.image = weatherImg;
// Make a new attributed string with the NSTextAttachment
NSAttributedString *attrStringWithWeatherImage = [NSAttributedString attributedStringWithAttachment:imgAttachment];
// Insert the attributed string containing our NSTextAttachment at the start of the string - example: {weatherIcon} 14°
[attrString insertAttributedString:attrStringWithWeatherImage atIndex:0];
return attrString;
}
@end