From 32e4eb2a42476b7496b50e04c424a93a5552533f Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 16 Jun 2017 10:52:25 -0400 Subject: [PATCH 1/3] =?UTF-8?q?Add=20a=20=E2=80=9Clast=20app=20completed?= =?UTF-8?q?=20launch=E2=80=9D=20version.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit // FREEBIE --- src/Util/AppVersion.h | 8 ++++++-- src/Util/AppVersion.m | 30 +++++++++++++++++++++--------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/Util/AppVersion.h b/src/Util/AppVersion.h index 96d23c286..48c7dea18 100755 --- a/src/Util/AppVersion.h +++ b/src/Util/AppVersion.h @@ -1,6 +1,4 @@ // -// AppVersion.h -// // Copyright (c) 2017 Open Whisper Systems. All rights reserved. // @@ -10,6 +8,12 @@ @property (nonatomic, readonly) NSString *lastAppVersion; @property (nonatomic, readonly) NSString *currentAppVersion; +// Unlike lastAppVersion, this property isn't updated until +// appLaunchDidComplete is called. +@property (nonatomic, readonly) NSString *lastCompletedLaunchAppVersion; + + (instancetype)instance; +- (void)appLaunchDidComplete; + @end diff --git a/src/Util/AppVersion.m b/src/Util/AppVersion.m index 267c1c334..fd9ac4cb6 100755 --- a/src/Util/AppVersion.m +++ b/src/Util/AppVersion.m @@ -1,16 +1,19 @@ // -// AppVersion.m -// // Copyright (c) 2017 Open Whisper Systems. All rights reserved. // #import "AppVersion.h" +NSString *const kNSUserDefaults_FirstAppVersion = @"kNSUserDefaults_FirstAppVersion"; +NSString *const kNSUserDefaults_LastAppVersion = @"kNSUserDefaults_LastVersion"; +NSString *const kNSUserDefaults_LastCompletedLaunchAppVersion = @"kNSUserDefaults_LastCompletedLaunchAppVersion"; + @interface AppVersion () @property (nonatomic) NSString *firstAppVersion; @property (nonatomic) NSString *lastAppVersion; @property (nonatomic) NSString *currentAppVersion; +@property (nonatomic) NSString *lastCompletedLaunchAppVersion; @end @@ -31,19 +34,15 @@ - (void)configure { self.currentAppVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]; - NSString *kNSUserDefaults_FirstAppVersion = @"kNSUserDefaults_FirstAppVersion"; - NSString *kNSUserDefaults_LastAppVersion = @"kNSUserDefaults_LastVersion"; - // The version of the app when it was first launched. // nil if the app has never been launched before. self.firstAppVersion = [[NSUserDefaults standardUserDefaults] objectForKey:kNSUserDefaults_FirstAppVersion]; // The version of the app the last time it was launched. // nil if the app has never been launched before. -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wunused-variable" self.lastAppVersion = [[NSUserDefaults standardUserDefaults] objectForKey:kNSUserDefaults_LastAppVersion]; -#pragma clang diagnostic pop - + self.lastCompletedLaunchAppVersion = + [[NSUserDefaults standardUserDefaults] objectForKey:kNSUserDefaults_LastCompletedLaunchAppVersion]; + // Ensure the value for the "first launched version". if (!self.firstAppVersion) { self.firstAppVersion = self.currentAppVersion; @@ -59,6 +58,19 @@ DDLogInfo(@"firstAppVersion: %@", self.firstAppVersion); DDLogInfo(@"lastAppVersion: %@", self.lastAppVersion); DDLogInfo(@"currentAppVersion: %@", self.currentAppVersion); + DDLogInfo(@"lastCompletedLaunchAppVersion: %@", self.lastCompletedLaunchAppVersion); +} + +- (void)appLaunchDidComplete +{ + DDLogInfo(@"appLaunchDidComplete"); + + self.lastCompletedLaunchAppVersion = self.currentAppVersion; + + // Update the value for the "most recently launch-completed version". + [[NSUserDefaults standardUserDefaults] setObject:self.currentAppVersion + forKey:kNSUserDefaults_LastCompletedLaunchAppVersion]; + [[NSUserDefaults standardUserDefaults] synchronize]; } @end From b9f9b6a0c3583565dc0b292a8350fd57870cc550 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 16 Jun 2017 10:59:48 -0400 Subject: [PATCH 2/3] Add isFirstLaunch method to AppVersion. // FREEBIE --- src/Util/AppVersion.h | 2 ++ src/Util/AppVersion.m | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/src/Util/AppVersion.h b/src/Util/AppVersion.h index 48c7dea18..d5455f41a 100755 --- a/src/Util/AppVersion.h +++ b/src/Util/AppVersion.h @@ -16,4 +16,6 @@ - (void)appLaunchDidComplete; +- (BOOL)isFirstLaunch; + @end diff --git a/src/Util/AppVersion.m b/src/Util/AppVersion.m index fd9ac4cb6..de7f6c6fb 100755 --- a/src/Util/AppVersion.m +++ b/src/Util/AppVersion.m @@ -73,4 +73,9 @@ NSString *const kNSUserDefaults_LastCompletedLaunchAppVersion = @"kNSUserDefault [[NSUserDefaults standardUserDefaults] synchronize]; } +- (BOOL)isFirstLaunch +{ + return self.firstAppVersion != nil; +} + @end From 8ef118f5daa20edbc0cf5fb3423a7a81de744fa2 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 16 Jun 2017 11:17:12 -0400 Subject: [PATCH 3/3] Cache the attachments folder in TSAttachmentStream. // FREEBIE --- src/Messages/Attachments/TSAttachmentStream.m | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/Messages/Attachments/TSAttachmentStream.m b/src/Messages/Attachments/TSAttachmentStream.m index 38670b800..86d7ba36a 100644 --- a/src/Messages/Attachments/TSAttachmentStream.m +++ b/src/Messages/Attachments/TSAttachmentStream.m @@ -183,20 +183,31 @@ NS_ASSUME_NONNULL_BEGIN + (NSString *)attachmentsFolder { - NSString *documentsPath = - [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; - NSString *attachmentFolder = [documentsPath stringByAppendingFormat:@"/Attachments"]; - - NSError *error = nil; - [[NSFileManager defaultManager] createDirectoryAtPath:attachmentFolder - withIntermediateDirectories:YES - attributes:nil - error:&error]; - if (error) { - DDLogError(@"Failed to create attachments directory: %@", error); - } + static NSString *attachmentsFolder = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + NSString *documentsPath = + [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; + attachmentsFolder = [documentsPath stringByAppendingFormat:@"/Attachments"]; + + BOOL isDirectory; + BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:attachmentsFolder isDirectory:&isDirectory]; + if (exists) { + OWSAssert(isDirectory); - return attachmentFolder; + DDLogInfo(@"Attachments directory already exists"); + } else { + NSError *error = nil; + [[NSFileManager defaultManager] createDirectoryAtPath:attachmentsFolder + withIntermediateDirectories:YES + attributes:nil + error:&error]; + if (error) { + DDLogError(@"Failed to create attachments directory: %@", error); + } + } + }); + return attachmentsFolder; } + (NSUInteger)numberOfItemsInAttachmentsFolder