Settings: remove local storage for user's info (displayName, avatar), use now mxSession.myUser

This commit is contained in:
giomfo
2014-12-11 14:52:04 +01:00
parent f54ad51f0e
commit cbc82594e3
4 changed files with 64 additions and 135 deletions
@@ -34,8 +34,6 @@ extern NSString *const kMatrixHandlerUnsupportedMessagePrefix;
@property (strong, nonatomic) NSArray *eventsFilterForMessages; @property (strong, nonatomic) NSArray *eventsFilterForMessages;
// Matrix user's settings // Matrix user's settings
@property (strong, nonatomic) NSString *userDisplayName;
@property (strong, nonatomic) NSString *userPictureURL;
@property (nonatomic) MXPresence userPresence; @property (nonatomic) MXPresence userPresence;
@property (nonatomic,readonly) BOOL isLogged; @property (nonatomic,readonly) BOOL isLogged;
@@ -44,7 +44,6 @@ static MatrixHandler *sharedHandler = nil;
@implementation MatrixHandler @implementation MatrixHandler
@synthesize homeServerURL, homeServer, userLogin, userId, accessToken; @synthesize homeServerURL, homeServer, userLogin, userId, accessToken;
@synthesize userDisplayName, userPictureURL;
+ (MatrixHandler *)sharedHandler { + (MatrixHandler *)sharedHandler {
@synchronized(self) { @synchronized(self) {
@@ -73,10 +72,6 @@ static MatrixHandler *sharedHandler = nil;
[self openSession]; [self openSession];
} }
} }
// The app will look for user's display name in incoming messages, it must not be nil.
if (self.userDisplayName == nil) {
self.userDisplayName = @"";
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
} }
@@ -90,23 +85,6 @@ static MatrixHandler *sharedHandler = nil;
self.mxRestClient = [[MXRestClient alloc] initWithCredentials:credentials]; self.mxRestClient = [[MXRestClient alloc] initWithCredentials:credentials];
if (self.mxRestClient) { if (self.mxRestClient) {
// Request user's display name
[self.mxRestClient displayNameForUser:self.userId success:^(NSString *displayname) {
self.userDisplayName = displayname;
} failure:^(NSError *error) {
NSLog(@"Get displayName failed: %@", error);
//Alert user
[[AppDelegate theDelegate] showErrorAsAlert:error];
}];
// Request user's avatar
[self.mxRestClient avatarUrlForUser:self.userId success:^(NSString *avatar_url) {
self.userPictureURL = avatar_url;
} failure:^(NSError *error) {
NSLog(@"Get picture url failed: %@", error);
//Alert user
[[AppDelegate theDelegate] showErrorAsAlert:error];
}];
// Use MXMemoryStore as MXStore to not loose message // Use MXMemoryStore as MXStore to not loose message
MXMemoryStore *store = [[MXMemoryStore alloc] init]; MXMemoryStore *store = [[MXMemoryStore alloc] init];
@@ -146,14 +124,6 @@ static MatrixHandler *sharedHandler = nil;
userUpdateListener = [self.mxSession.myUser listenToUserUpdate:^(MXEvent *event) { userUpdateListener = [self.mxSession.myUser listenToUserUpdate:^(MXEvent *event) {
// Consider only events related to user's presence // Consider only events related to user's presence
if (event.eventType == MXEventTypePresence) { if (event.eventType == MXEventTypePresence) {
// Update local storage
if (event.content[@"displayname"] && ![self.userDisplayName isEqualToString:event.content[@"displayname"]]) {
self.userDisplayName = event.content[@"displayname"];
}
if (event.content[@"avatar_url"] && ![self.userPictureURL isEqualToString:event.content[@"avatar_url"]]) {
self.userPictureURL = event.content[@"avatar_url"];
}
// Check presence
MXPresence presence = [MXTools presence:event.content[@"presence"]]; MXPresence presence = [MXTools presence:event.content[@"presence"]];
if (self.userPresence != presence) { if (self.userPresence != presence) {
// Handle user presence on multiple devices (keep the more pertinent) // Handle user presence on multiple devices (keep the more pertinent)
@@ -294,10 +264,6 @@ static MatrixHandler *sharedHandler = nil;
// Reset access token (mxSession is closed by setter) // Reset access token (mxSession is closed by setter)
self.accessToken = nil; self.accessToken = nil;
self.userId = nil; self.userId = nil;
// Reset local storage of user's settings
self.userDisplayName = @"";
self.userPictureURL = nil;
} }
- (void)forceInitialSync { - (void)forceInitialSync {
@@ -455,33 +421,6 @@ static MatrixHandler *sharedHandler = nil;
#pragma mark - Matrix user's settings #pragma mark - Matrix user's settings
- (NSString *)userDisplayName {
return [[NSUserDefaults standardUserDefaults] objectForKey:@"userdisplayname"];
}
- (void)setUserDisplayName:(NSString *)inUserDisplayName {
if ([inUserDisplayName isEqual:[NSNull null]] == NO && inUserDisplayName.length) {
[[NSUserDefaults standardUserDefaults] setObject:inUserDisplayName forKey:@"userdisplayname"];
} else {
// the app will look for this display name in incoming messages, it must not be nil.
[[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"userdisplayname"];
}
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (NSString *)userPictureURL {
return [[NSUserDefaults standardUserDefaults] objectForKey:@"userpictureurl"];
}
- (void)setUserPictureURL:(NSString *)inUserPictureURL {
if ([inUserPictureURL isEqual:[NSNull null]] == NO && inUserPictureURL.length) {
[[NSUserDefaults standardUserDefaults] setObject:inUserPictureURL forKey:@"userpictureurl"];
} else {
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"userpictureurl"];
}
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)setUserPresence:(MXPresence)userPresence andStatusMessage:(NSString *)statusMessage completion:(void (^)(void))completion { - (void)setUserPresence:(MXPresence)userPresence andStatusMessage:(NSString *)statusMessage completion:(void (^)(void))completion {
self.userPresence = userPresence; self.userPresence = userPresence;
// Update user presence on server side // Update user presence on server side
@@ -70,8 +70,8 @@ NSString *const kFailedEventId = @"failedEventId";
- (BOOL)containsBingWord { - (BOOL)containsBingWord {
MatrixHandler *mxHandler = [MatrixHandler sharedHandler]; MatrixHandler *mxHandler = [MatrixHandler sharedHandler];
NSString *pattern = nil; NSString *pattern = nil;
if (mxHandler.userDisplayName.length) { if (mxHandler.mxSession.myUser.displayname.length) {
pattern = [NSString stringWithFormat:@"\\b%@\\b", mxHandler.userDisplayName]; pattern = [NSString stringWithFormat:@"\\b%@\\b", mxHandler.mxSession.myUser.displayname];
} }
if (mxHandler.localPartFromUserId.length) { if (mxHandler.localPartFromUserId.length) {
if (pattern) { if (pattern) {
@@ -38,6 +38,9 @@ NSString* const kCommandsDescriptionText = @"The following commands are availabl
NSString *currentPictureURL; NSString *currentPictureURL;
NSString *uploadedPictureURL; NSString *uploadedPictureURL;
// Listen user's settings change
id userUpdateListener;
NSMutableArray *errorAlerts; NSMutableArray *errorAlerts;
UIButton *logoutBtn; UIButton *logoutBtn;
@@ -71,8 +74,7 @@ NSString* const kCommandsDescriptionText = @"The following commands are availabl
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:logoutBtn]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:logoutBtn];
errorAlerts = [NSMutableArray array]; errorAlerts = [NSMutableArray array];
[[MatrixHandler sharedHandler] addObserver:self forKeyPath:@"isInitialSyncDone" options:0 context:nil];
[self startViewConfiguration];
} }
- (void)didReceiveMemoryWarning { - (void)didReceiveMemoryWarning {
@@ -86,19 +88,8 @@ NSString* const kCommandsDescriptionText = @"The following commands are availabl
} }
- (void)dealloc { - (void)dealloc {
[[MatrixHandler sharedHandler] removeObserver:self forKeyPath:@"userDisplayName"]; [self reset];
[[MatrixHandler sharedHandler] removeObserver:self forKeyPath:@"userPictureURL"];
// Cancel picture loader (if any)
if (imageLoader) {
[MediaManager cancel:imageLoader];
imageLoader = nil;
}
// Cancel potential error alerts
for (CustomAlert *alert in errorAlerts){
[alert dismiss:NO];
}
errorAlerts = nil; errorAlerts = nil;
logoutBtn = nil; logoutBtn = nil;
@@ -106,16 +97,20 @@ NSString* const kCommandsDescriptionText = @"The following commands are availabl
allEventsSwitch = nil; allEventsSwitch = nil;
unsupportedMsgSwitch = nil; unsupportedMsgSwitch = nil;
sortMembersSwitch = nil; sortMembersSwitch = nil;
[[MatrixHandler sharedHandler] removeObserver:self forKeyPath:@"isInitialSyncDone"];
} }
- (void)viewWillAppear:(BOOL)animated { - (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated]; [super viewWillAppear:animated];
// Refresh displayed settings
[self.tableView reloadData]; // Refresh display
[self configureView];
[[MatrixHandler sharedHandler] addObserver:self forKeyPath:@"isResumeDone" options:0 context:nil];
} }
- (void)viewWillDisappear:(BOOL)animated { - (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated]; [super viewWillDisappear:animated];
[[MatrixHandler sharedHandler] removeObserver:self forKeyPath:@"isResumeDone"];
} }
#pragma mark - Internal methods #pragma mark - Internal methods
@@ -132,6 +127,12 @@ NSString* const kCommandsDescriptionText = @"The following commands are availabl
[alert dismiss:NO]; [alert dismiss:NO];
} }
// Remove listener
if (userUpdateListener) {
[[MatrixHandler sharedHandler].mxSession.myUser removeListener:userUpdateListener];
userUpdateListener = nil;
}
currentPictureURL = nil; currentPictureURL = nil;
uploadedPictureURL = nil; uploadedPictureURL = nil;
UIImage *image = [UIImage imageNamed:@"default-profile"]; UIImage *image = [UIImage imageNamed:@"default-profile"];
@@ -142,51 +143,42 @@ NSString* const kCommandsDescriptionText = @"The following commands are availabl
self.userDisplayName.text = nil; self.userDisplayName.text = nil;
} }
- (void)startViewConfiguration { - (void)configureView {
// Initialize
[self reset];
// Set current user's information and add observers
MatrixHandler *mxHandler = [MatrixHandler sharedHandler]; MatrixHandler *mxHandler = [MatrixHandler sharedHandler];
[_activityIndicator startAnimating]; [_activityIndicator startAnimating];
// Disable user's interactions // Disable user's interactions
_userPicture.enabled = NO; _userPicture.enabled = NO;
_userDisplayName.enabled = NO; _userDisplayName.enabled = NO;
// Set user's display name if ([mxHandler isInitialSyncDone]) {
currentDisplayName = mxHandler.userDisplayName; if (!userUpdateListener) {
self.userDisplayName.text = mxHandler.userDisplayName; // Set current user's information and add observers
[[MatrixHandler sharedHandler] addObserver:self forKeyPath:@"userDisplayName" options:0 context:nil]; [self updateUserPicture:mxHandler.mxSession.myUser.avatarUrl];
[mxHandler.mxRestClient displayNameForUser:mxHandler.userId success:^(NSString *displayname) { currentDisplayName = mxHandler.mxSession.myUser.displayname;
mxHandler.userDisplayName = displayname; self.userDisplayName.text = currentDisplayName;
// Set user's picture url
[self updateUserPicture:mxHandler.userPictureURL];
[[MatrixHandler sharedHandler] addObserver:self forKeyPath:@"userPictureURL" options:0 context:nil];
[mxHandler.mxRestClient avatarUrlForUser:mxHandler.userId success:^(NSString *avatar_url) {
mxHandler.userPictureURL = avatar_url;
[self endViewConfiguration];
} failure:^(NSError *error) { // Register listener to update user's information
NSLog(@"Get picture url failed: %@", error); userUpdateListener = [mxHandler.mxSession.myUser listenToUserUpdate:^(MXEvent *event) {
//Alert user // Update displayName
[[AppDelegate theDelegate] showErrorAsAlert:error]; if (![currentDisplayName isEqualToString:mxHandler.mxSession.myUser.displayname]) {
[self endViewConfiguration]; currentDisplayName = mxHandler.mxSession.myUser.displayname;
}]; self.userDisplayName.text = mxHandler.mxSession.myUser.displayname;
} failure:^(NSError *error) { }
NSLog(@"Get displayName failed: %@", error); // Update user's avatar
//Alert user [self updateUserPicture:mxHandler.mxSession.myUser.avatarUrl];
[[AppDelegate theDelegate] showErrorAsAlert:error]; // TODO display user's presence
[self endViewConfiguration]; }];
}]; }
} } else {
[self reset];
- (void)endViewConfiguration { }
[_activityIndicator stopAnimating];
_userPicture.enabled = YES;
_userDisplayName.enabled = YES;
if ([mxHandler isResumeDone]) {
[_activityIndicator stopAnimating];
_userPicture.enabled = YES;
_userDisplayName.enabled = YES;
}
[self.tableView reloadData]; [self.tableView reloadData];
} }
@@ -199,9 +191,8 @@ NSString* const kCommandsDescriptionText = @"The following commands are availabl
_userDisplayName.enabled = NO; _userDisplayName.enabled = NO;
MatrixHandler *mxHandler = [MatrixHandler sharedHandler]; MatrixHandler *mxHandler = [MatrixHandler sharedHandler];
[mxHandler.mxRestClient setDisplayName:displayname success:^{ [mxHandler.mxSession.myUser setDisplayName:displayname success:^{
currentDisplayName = displayname; currentDisplayName = displayname;
[_activityIndicator stopAnimating]; [_activityIndicator stopAnimating];
_userDisplayName.enabled = YES; _userDisplayName.enabled = YES;
} failure:^(NSError *error) { } failure:^(NSError *error) {
@@ -255,11 +246,10 @@ NSString* const kCommandsDescriptionText = @"The following commands are availabl
[self handleErrorDuringPictureSaving:error]; [self handleErrorDuringPictureSaving:error];
}]; }];
} else { } else {
[mxHandler.mxRestClient setAvatarUrl:uploadedPictureURL [mxHandler.mxSession.myUser setAvatarUrl:uploadedPictureURL
success:^{ success:^{
[MatrixHandler sharedHandler].userPictureURL = uploadedPictureURL; [self updateUserPicture:uploadedPictureURL];
uploadedPictureURL = nil; uploadedPictureURL = nil;
[_activityIndicator stopAnimating]; [_activityIndicator stopAnimating];
_userPicture.enabled = YES; _userPicture.enabled = YES;
} failure:^(NSError *error) { } failure:^(NSError *error) {
@@ -284,7 +274,7 @@ NSString* const kCommandsDescriptionText = @"The following commands are availabl
[errorAlerts removeObject:alert]; [errorAlerts removeObject:alert];
// Remove change // Remove change
uploadedPictureURL = nil; uploadedPictureURL = nil;
[self updateUserPicture:[MatrixHandler sharedHandler].userPictureURL]; [self updateUserPicture:[MatrixHandler sharedHandler].mxSession.myUser.avatarUrl];
}]; }];
[alert addActionWithTitle:@"Retry" style:CustomAlertActionStyleDefault handler:^(CustomAlert *alert) { [alert addActionWithTitle:@"Retry" style:CustomAlertActionStyleDefault handler:^(CustomAlert *alert) {
[errorAlerts removeObject:alert]; [errorAlerts removeObject:alert];
@@ -324,17 +314,20 @@ NSString* const kCommandsDescriptionText = @"The following commands are availabl
#pragma mark - KVO #pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([@"userDisplayName" isEqualToString:keyPath]) { if ([@"isInitialSyncDone" isEqualToString:keyPath]) {
// Refresh user's display name dispatch_async(dispatch_get_main_queue(), ^{
MatrixHandler *mxHandler = [MatrixHandler sharedHandler]; [self configureView];
if ([currentDisplayName isEqualToString:mxHandler.userDisplayName] == NO) { });
currentDisplayName = mxHandler.userDisplayName; } else if ([@"isResumeDone" isEqualToString:keyPath]) {
self.userDisplayName.text = mxHandler.userDisplayName; if ([[MatrixHandler sharedHandler] isResumeDone]) {
[_activityIndicator stopAnimating];
_userPicture.enabled = YES;
_userDisplayName.enabled = YES;
} else {
[_activityIndicator startAnimating];
_userPicture.enabled = NO;
_userDisplayName.enabled = NO;
} }
} else if ([@"userPictureURL" isEqualToString:keyPath]) {
// Refresh user's picture
MatrixHandler *mxHandler = [MatrixHandler sharedHandler];
[self updateUserPicture:mxHandler.userPictureURL];
} }
} }
@@ -351,7 +344,6 @@ NSString* const kCommandsDescriptionText = @"The following commands are availabl
mediaPicker.allowsEditing = NO; mediaPicker.allowsEditing = NO;
[[AppDelegate theDelegate].masterTabBarController presentMediaPicker:mediaPicker]; [[AppDelegate theDelegate].masterTabBarController presentMediaPicker:mediaPicker];
} else if (sender == logoutBtn) { } else if (sender == logoutBtn) {
[self reset];
[[AppDelegate theDelegate] logout]; [[AppDelegate theDelegate] logout];
} else if (sender == notificationsSwitch) { } else if (sender == notificationsSwitch) {
[AppSettings sharedSettings].enableNotifications = notificationsSwitch.on; [AppSettings sharedSettings].enableNotifications = notificationsSwitch.on;