Sketch out the profile view.

// FREEBIE
pull/1/head
Matthew Chen 8 years ago
parent 873f5208c4
commit 0bd23345a1

@ -167,7 +167,7 @@ NS_ASSUME_NONNULL_BEGIN
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
NSString *documentsPath = NSString *documentsPath =
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
attachmentsFolder = [documentsPath stringByAppendingFormat:@"/Attachments"]; attachmentsFolder = [documentsPath stringByAppendingPathComponent:@"Attachments"];
BOOL isDirectory; BOOL isDirectory;
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:attachmentsFolder isDirectory:&isDirectory]; BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:attachmentsFolder isDirectory:&isDirectory];

@ -4,6 +4,8 @@
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
extern NSString *const kNSNotificationName_LocalProfileDidChange;
// This class can be safely accessed and used from any thread. // This class can be safely accessed and used from any thread.
@interface OWSProfilesManager : NSObject @interface OWSProfilesManager : NSObject
@ -11,6 +13,10 @@ NS_ASSUME_NONNULL_BEGIN
+ (instancetype)sharedManager; + (instancetype)sharedManager;
- (nullable NSString *)localProfileName;
- (nullable UIImage *)localProfileAvatarImage;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

@ -10,9 +10,14 @@
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
NSString *const kNSNotificationName_LocalProfileDidChange = @"kNSNotificationName_LocalProfileDidChange";
NSString *const kOWSProfilesManager_Collection = @"kOWSProfilesManager_Collection"; NSString *const kOWSProfilesManager_Collection = @"kOWSProfilesManager_Collection";
// This key is used to persist the local user's profile key. // This key is used to persist the local user's profile key.
NSString *const kOWSProfilesManager_LocalProfileKey = @"kOWSProfilesManager_LocalProfileKey"; NSString *const kOWSProfilesManager_LocalProfileKey = @"kOWSProfilesManager_LocalProfileKey";
NSString *const kOWSProfilesManager_LocalProfileNameKey = @"kOWSProfilesManager_LocalProfileNameKey";
NSString *const kOWSProfilesManager_LocalProfileAvatarFilenameKey
= @"kOWSProfilesManager_LocalProfileAvatarFilenameKey";
// TODO: // TODO:
static const NSInteger kProfileKeyLength = 16; static const NSInteger kProfileKeyLength = 16;
@ -22,7 +27,11 @@ static const NSInteger kProfileKeyLength = 16;
@property (nonatomic, readonly) TSStorageManager *storageManager; @property (nonatomic, readonly) TSStorageManager *storageManager;
@property (nonatomic, readonly) OWSMessageSender *messageSender; @property (nonatomic, readonly) OWSMessageSender *messageSender;
@property (nonatomic, readonly, nullable) NSData *localProfileKey; @property (atomic, readonly, nullable) NSData *localProfileKey;
@property (atomic, nullable) NSString *localProfileName;
@property (atomic, nullable) UIImage *localProfileAvatarImage;
@property (atomic) BOOL hasLoadedLocalProfile;
@end @end
@ -84,6 +93,8 @@ static const NSInteger kProfileKeyLength = 16;
} }
OWSAssert(_localProfileKey.length == kProfileKeyLength); OWSAssert(_localProfileKey.length == kProfileKeyLength);
[self loadLocalProfileAsync];
return self; return self;
} }
@ -109,10 +120,71 @@ static const NSInteger kProfileKeyLength = 16;
return [SecurityUtils generateRandomBytes:kProfileKeyLength]; return [SecurityUtils generateRandomBytes:kProfileKeyLength];
} }
- (nullable NSData *)localProfileKey #pragma mark - Local Profile
- (void)loadLocalProfileAsync
{ {
OWSAssert(_localProfileKey.length == kProfileKeyLength); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
return _localProfileKey; NSString *_Nullable localProfileName = [self.storageManager objectForKey:kOWSProfilesManager_LocalProfileNameKey
inCollection:kOWSProfilesManager_Collection];
NSString *_Nullable localProfileAvatarFilename =
[self.storageManager objectForKey:kOWSProfilesManager_LocalProfileAvatarFilenameKey
inCollection:kOWSProfilesManager_Collection];
UIImage *_Nullable localProfileAvatar = nil;
if (localProfileAvatarFilename) {
localProfileAvatar = [self loadProfileAvatarsWithFilename:localProfileAvatarFilename];
}
dispatch_async(dispatch_get_main_queue(), ^{
self.localProfileName = localProfileName;
self.localProfileAvatarImage = localProfileAvatar;
self.hasLoadedLocalProfile = YES;
if (localProfileAvatar || localProfileName) {
[[NSNotificationCenter defaultCenter] postNotificationName:kNSNotificationName_LocalProfileDidChange
object:nil
userInfo:nil];
}
});
});
}
#pragma mark - Avatar Disk Cache
- (nullable UIImage *)loadProfileAvatarsWithFilename:(NSString *)filename
{
NSString *filePath = [self.profileAvatarsDirPath stringByAppendingPathComponent:filename];
UIImage *_Nullable image = [UIImage imageWithContentsOfFile:filePath];
return image;
}
- (NSString *)profileAvatarsDirPath
{
static NSString *profileAvatarsDirPath = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *documentsPath =
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
profileAvatarsDirPath = [documentsPath stringByAppendingPathComponent:@"ProfileAvatars"];
BOOL isDirectory;
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:profileAvatarsDirPath isDirectory:&isDirectory];
if (exists) {
OWSAssert(isDirectory);
DDLogInfo(@"Profile avatars directory already exists");
} else {
NSError *error = nil;
[[NSFileManager defaultManager] createDirectoryAtPath:profileAvatarsDirPath
withIntermediateDirectories:YES
attributes:nil
error:&error];
if (error) {
DDLogError(@"Failed to create profile avatars directory: %@", error);
}
}
});
return profileAvatarsDirPath;
} }
#pragma mark - Notifications #pragma mark - Notifications

@ -277,7 +277,7 @@ static NSString *keychainDBPassAccount = @"TSDatabasePass";
#if TARGET_OS_IPHONE #if TARGET_OS_IPHONE
NSURL *fileURL = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; NSURL *fileURL = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSString *path = [fileURL path]; NSString *path = [fileURL path];
databasePath = [path stringByAppendingFormat:@"/%@", databaseName]; databasePath = [path stringByAppendingPathComponent:databaseName];
#elif TARGET_OS_MAC #elif TARGET_OS_MAC
NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier]; NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
@ -289,7 +289,7 @@ static NSString *keychainDBPassAccount = @"TSDatabasePass";
[fileManager createDirectoryAtURL:appDirectory withIntermediateDirectories:NO attributes:nil error:nil]; [fileManager createDirectoryAtURL:appDirectory withIntermediateDirectories:NO attributes:nil error:nil];
} }
databasePath = [appDirectory.filePathURL.absoluteString stringByAppendingFormat:@"/%@", databaseName]; databasePath = [appDirectory.filePathURL.absoluteString stringByAppendingPathComponent:databaseName];
#endif #endif
return databasePath; return databasePath;

@ -364,36 +364,41 @@ NSString *const OWSMimeTypeUnknownForTests = @"unknown/mimetype";
} }
+ (NSString *)filePathForImage:(NSString *)uniqueId ofMIMEType:(NSString *)contentType inFolder:(NSString *)folder { + (NSString *)filePathForImage:(NSString *)uniqueId ofMIMEType:(NSString *)contentType inFolder:(NSString *)folder {
return [[folder stringByAppendingFormat:@"/%@", uniqueId] return [self filePathForData:uniqueId
stringByAppendingPathExtension:[self getSupportedExtensionFromImageMIMEType:contentType]]; withFileExtension:[self getSupportedExtensionFromImageMIMEType:contentType]
inFolder:folder];
} }
+ (NSString *)filePathForVideo:(NSString *)uniqueId ofMIMEType:(NSString *)contentType inFolder:(NSString *)folder { + (NSString *)filePathForVideo:(NSString *)uniqueId ofMIMEType:(NSString *)contentType inFolder:(NSString *)folder {
return [[folder stringByAppendingFormat:@"/%@", uniqueId] return [self filePathForData:uniqueId
stringByAppendingPathExtension:[self getSupportedExtensionFromVideoMIMEType:contentType]]; withFileExtension:[self getSupportedExtensionFromVideoMIMEType:contentType]
inFolder:folder];
} }
+ (NSString *)filePathForAudio:(NSString *)uniqueId ofMIMEType:(NSString *)contentType inFolder:(NSString *)folder { + (NSString *)filePathForAudio:(NSString *)uniqueId ofMIMEType:(NSString *)contentType inFolder:(NSString *)folder {
return [[folder stringByAppendingFormat:@"/%@", uniqueId] return [self filePathForData:uniqueId
stringByAppendingPathExtension:[self getSupportedExtensionFromAudioMIMEType:contentType]]; withFileExtension:[self getSupportedExtensionFromAudioMIMEType:contentType]
inFolder:folder];
} }
+ (NSString *)filePathForAnimated:(NSString *)uniqueId ofMIMEType:(NSString *)contentType inFolder:(NSString *)folder { + (NSString *)filePathForAnimated:(NSString *)uniqueId ofMIMEType:(NSString *)contentType inFolder:(NSString *)folder {
return [[folder stringByAppendingFormat:@"/%@", uniqueId] return [self filePathForData:uniqueId
stringByAppendingPathExtension:[self getSupportedExtensionFromAnimatedMIMEType:contentType]]; withFileExtension:[self getSupportedExtensionFromAnimatedMIMEType:contentType]
inFolder:folder];
} }
+ (NSString *)filePathForBinaryData:(NSString *)uniqueId ofMIMEType:(NSString *)contentType inFolder:(NSString *)folder + (NSString *)filePathForBinaryData:(NSString *)uniqueId ofMIMEType:(NSString *)contentType inFolder:(NSString *)folder
{ {
return [[folder stringByAppendingFormat:@"/%@", uniqueId] return [self filePathForData:uniqueId
stringByAppendingPathExtension:[self getSupportedExtensionFromBinaryDataMIMEType:contentType]]; withFileExtension:[self getSupportedExtensionFromBinaryDataMIMEType:contentType]
inFolder:folder];
} }
+ (NSString *)filePathForData:(NSString *)uniqueId + (NSString *)filePathForData:(NSString *)uniqueId
withFileExtension:(NSString *)fileExtension withFileExtension:(NSString *)fileExtension
inFolder:(NSString *)folder inFolder:(NSString *)folder
{ {
return [[folder stringByAppendingFormat:@"/%@", uniqueId] stringByAppendingPathExtension:fileExtension]; return [folder stringByAppendingPathComponent:[uniqueId stringByAppendingPathExtension:fileExtension]];
} }
+ (nullable NSString *)utiTypeForMIMEType:(NSString *)mimeType + (nullable NSString *)utiTypeForMIMEType:(NSString *)mimeType

Loading…
Cancel
Save