This repository has been archived by the owner on Jan 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
UpdateCheck.x
67 lines (52 loc) · 2.59 KB
/
UpdateCheck.x
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
@import Cocoa;
#import "HBTSPreferences.h"
#pragma mark - First run
static void showFirstRunAlert() {
NSAlert *alert = [[NSAlert alloc] init];
alert.messageText = @"Welcome to TypeStatus";
alert.informativeText = @"You’ll now see subtle notifications in your menu bar when someone is typing an iMessage to you or reads an iMessage you sent.\nIf you like TypeStatus, don’t forget to let your friends know about it!";
[alert runModal];
}
#pragma mark - Updates
static void checkUpdate() {
NSBundle *bundle = [NSBundle bundleWithIdentifier:@"ws.hbang.typestatus.mac"];
[HBTSPreferences sharedInstance].lastVersion = bundle.infoDictionary[@"CFBundleVersion"];
NSString *currentVersion = bundle.infoDictionary[@"CFBundleShortVersionString"];
NSString *messagesVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleVersion"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://cdn.hbang.ws/updates/typestatusmac.json?version=%@", currentVersion]] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30];
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (!data || !data.length) {
HBLogWarn(@"update check failed — no data received");
return;
}
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
if (!json) {
HBLogWarn(@"json deserialization failed");
return;
}
// if the version we got back is newer, and can be installed on this version of messages, show
// our update prompt
if ([json[@"version"] compare:currentVersion options:NSNumericSearch] == NSOrderedDescending
&& [json[@"minimumMessagesVersion"] compare:messagesVersion options:NSNumericSearch] != NSOrderedAscending) {
dispatch_async(dispatch_get_main_queue(), ^{
NSAlert *alert = [[NSAlert alloc] init];
alert.messageText = @"A TypeStatus update is available";
alert.informativeText = [NSString stringWithFormat:@"The new version is %@. You have version %@.", json[@"version"], currentVersion];
[alert addButtonWithTitle:@"Install"];
[alert addButtonWithTitle:@"No Thanks"];
// if the user selected the first button (install), open the url we got
if ([alert runModal] == NSAlertFirstButtonReturn) {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:json[@"url"]]];
}
});
}
}];
[task resume];
}
#pragma mark - Constructor
%ctor {
if (![HBTSPreferences sharedInstance].lastVersion) {
showFirstRunAlert();
}
checkUpdate();
}