2014-11-24 09:38:23 +00:00
/ *
Copyright 2014 OpenMarket Ltd
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : // www . apache . org / licenses / LICENSE -2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
# import "SettingsViewController.h"
# import "AppDelegate.h"
# import "AppSettings.h"
2014-12-12 13:18:34 +00:00
# import "APNSHandler.h"
2014-11-24 09:38:23 +00:00
# import "MatrixHandler.h"
# import "MediaManager.h"
# import "SettingsTableViewCell.h"
# define SETTINGS_SECTION _NOTIFICATIONS _INDEX 0
# define SETTINGS_SECTION _ROOMS _INDEX 1
# define SETTINGS_SECTION _CONFIGURATION _INDEX 2
# define SETTINGS_SECTION _COMMANDS _INDEX 3
2014-12-19 09:02:09 +00:00
# define SETTINGS_SECTION _ROOMS _DISPLAY _ALL _EVENTS _INDEX 0
# define SETTINGS_SECTION _ROOMS _HIDE _UNSUPPORTED _MESSAGES _INDEX 1
# define SETTINGS_SECTION _ROOMS _SORT _MEMBERS _INDEX 2
# define SETTINGS_SECTION _ROOMS _DISPLAY _LEFT _MEMBERS _INDEX 3
# define SETTINGS_SECTION _ROOMS _CLEAR _CACHE _INDEX 4
# define SETTINGS_SECTION _ROOMS _INDEX _COUNT 5
2014-11-24 09:38:23 +00:00
NSString * const kConfigurationFormatText = @ "Home server: %@\r\nIdentity server: %@\r\nUser ID: %@\r\nAccess token: %@" ;
NSString * const kCommandsDescriptionText = @ "The following commands are available in the room chat:\r\n\r\n /nick <display_name>: change your display name\r\n /me <action>: send the action you are doing. /me will be replaced by your display name\r\n /join <room_alias>: join a room\r\n /kick <user_id> [<reason>]: kick the user\r\n /ban <user_id> [<reason>]: ban the user\r\n /unban <user_id>: unban the user\r\n /op <user_id> <power_level>: set user power level\r\n /deop <user_id>: reset user power level to the room default value" ;
@ interface SettingsViewController ( ) {
id imageLoader ;
NSString * currentDisplayName ;
NSString * currentPictureURL ;
NSString * uploadedPictureURL ;
2014-12-11 13:52:04 +00:00
// Listen user ' s settings change
id userUpdateListener ;
2014-11-24 09:38:23 +00:00
NSMutableArray * errorAlerts ;
UIButton * logoutBtn ;
2014-12-12 13:18:34 +00:00
UISwitch * apnsNotificationsSwitch ;
UISwitch * inAppNotificationsSwitch ;
2014-11-24 09:38:23 +00:00
UISwitch * allEventsSwitch ;
UISwitch * unsupportedMsgSwitch ;
UISwitch * sortMembersSwitch ;
2014-12-17 13:40:39 +00:00
UISwitch * displayLeftMembersSwitch ;
2015-01-05 13:25:11 +00:00
// user info update
BOOL isAvatarUpdated ;
BOOL isDisplayNameUpdated ;
2015-01-05 15:53:41 +00:00
// do not hide the spinner while switching between viewcontroller
BOOL isAvatarUploading ;
BOOL isDisplayNameUploading ;
2014-11-24 09:38:23 +00:00
}
@ property ( strong , nonatomic ) IBOutlet UITableView * tableView ;
@ property ( weak , nonatomic ) IBOutlet UIView * tableHeader ;
@ property ( weak , nonatomic ) IBOutlet UIButton * userPicture ;
@ property ( weak , nonatomic ) IBOutlet UITextField * userDisplayName ;
2015-01-05 13:25:11 +00:00
@ property ( weak , nonatomic ) IBOutlet UIButton * saveUserInfoButton ;
@ property ( strong , nonatomic ) IBOutlet UIView * activityIndicatorBackgroundView ;
2014-11-24 09:38:23 +00:00
@ property ( strong , nonatomic ) IBOutlet UIActivityIndicatorView * activityIndicator ;
2015-01-05 15:53:41 +00:00
@ property ( strong , nonatomic ) CustomAlert * customAlert ;
2014-11-24 09:38:23 +00:00
- ( IBAction ) onButtonPressed : ( id ) sender ;
@ end
@ implementation SettingsViewController
- ( void ) viewDidLoad {
[ super viewDidLoad ] ;
// Do any additional setup after loading the view , typically from a nib .
// Add logout button in nav bar
logoutBtn = [ UIButton buttonWithType : UIButtonTypeSystem ] ;
logoutBtn . frame = CGRectMake ( 0 , 0 , 60 , 44 ) ;
[ logoutBtn setTitle : @ "Logout" forState : UIControlStateNormal ] ;
[ logoutBtn setTitle : @ "Logout" forState : UIControlStateHighlighted ] ;
[ logoutBtn addTarget : self action : @ selector ( onButtonPressed : ) forControlEvents : UIControlEventTouchUpInside ] ;
self . navigationItem . rightBarButtonItem = [ [ UIBarButtonItem alloc ] initWithCustomView : logoutBtn ] ;
2014-12-16 07:47:48 +00:00
// keep the aspect ratio of the contact thumbnail
2014-12-16 07:49:19 +00:00
// scale it to fit the button frame
2014-12-16 07:47:48 +00:00
[ [ self . userPicture imageView ] setContentMode : UIViewContentModeScaleAspectFill ] ;
[ [ self . userPicture imageView ] setClipsToBounds : YES ] ;
2014-11-24 09:38:23 +00:00
errorAlerts = [ NSMutableArray array ] ;
2015-01-05 14:11:40 +00:00
[ [ MatrixHandler sharedHandler ] addObserver : self forKeyPath : @ "status" options : 0 context : nil ] ;
2015-01-05 13:25:11 +00:00
isAvatarUpdated = NO ;
isDisplayNameUpdated = NO ;
2015-01-05 15:53:41 +00:00
isAvatarUploading = NO ;
isDisplayNameUploading = NO ;
2015-01-05 13:25:11 +00:00
_saveUserInfoButton . enabled = NO ;
_activityIndicatorBackgroundView . hidden = YES ;
2014-11-24 09:38:23 +00:00
}
- ( void ) didReceiveMemoryWarning {
[ super didReceiveMemoryWarning ] ;
// Dispose of any resources that can be recreated .
if ( imageLoader ) {
[ MediaManager cancel : imageLoader ] ;
imageLoader = nil ;
}
}
- ( void ) dealloc {
2014-12-11 13:52:04 +00:00
[ self reset ] ;
2014-11-24 09:38:23 +00:00
errorAlerts = nil ;
logoutBtn = nil ;
2014-12-12 13:18:34 +00:00
apnsNotificationsSwitch = nil ;
inAppNotificationsSwitch = nil ;
2014-11-24 09:38:23 +00:00
allEventsSwitch = nil ;
unsupportedMsgSwitch = nil ;
sortMembersSwitch = nil ;
2014-12-17 13:40:39 +00:00
displayLeftMembersSwitch = nil ;
2015-01-05 14:11:40 +00:00
[ [ MatrixHandler sharedHandler ] removeObserver : self forKeyPath : @ "status" ] ;
2014-11-24 09:38:23 +00:00
}
- ( void ) viewWillAppear : ( BOOL ) animated {
[ super viewWillAppear : animated ] ;
2014-12-11 13:52:04 +00:00
// Refresh display
2015-01-05 13:25:11 +00:00
[ self startUserInfoUploadAnimation ] ;
2014-12-11 13:52:04 +00:00
[ self configureView ] ;
[ [ MatrixHandler sharedHandler ] addObserver : self forKeyPath : @ "isResumeDone" options : 0 context : nil ] ;
2014-12-12 13:18:34 +00:00
[ [ NSNotificationCenter defaultCenter ] addObserver : self selector : @ selector ( onAPNSHandlerHasBeenUpdated ) name : kAPNSHandlerHasBeenUpdated object : nil ] ;
2014-11-24 09:38:23 +00:00
}
- ( void ) viewWillDisappear : ( BOOL ) animated {
[ super viewWillDisappear : animated ] ;
2015-01-05 15:53:41 +00:00
2014-12-11 13:52:04 +00:00
[ [ MatrixHandler sharedHandler ] removeObserver : self forKeyPath : @ "isResumeDone" ] ;
2014-12-12 13:18:34 +00:00
[ [ NSNotificationCenter defaultCenter ] removeObserver : self name : kAPNSHandlerHasBeenUpdated object : nil ] ;
2014-11-24 09:38:23 +00:00
}
2015-01-05 15:53:41 +00:00
- ( BOOL ) checkPendingSave : ( blockSettings_onCheckSave ) handler {
// there is a profile update and there is no pending update
if ( ( isAvatarUpdated || isDisplayNameUpdated ) && ( ! isDisplayNameUploading ) && ( ! isAvatarUploading ) ) {
dispatch_async ( dispatch_get _main _queue ( ) , ^ {
__weak typeof ( self ) weakSelf = self ;
self . customAlert = [ [ CustomAlert alloc ] initWithTitle : nil message : @ "Save profile update" style : CustomAlertStyleAlert ] ;
self . customAlert . cancelButtonIndex = [ self . customAlert addActionWithTitle : @ "Cancel" style : CustomAlertActionStyleDefault handler : ^ ( CustomAlert * alert ) {
handler ( ) ;
weakSelf . customAlert = nil ;
} ] ;
[ self . customAlert addActionWithTitle : @ "OK" style : CustomAlertActionStyleDefault handler : ^ ( CustomAlert * alert ) {
[ weakSelf saveDisplayName ] ;
weakSelf . customAlert = nil ;
handler ( ) ;
} ] ;
[ self . customAlert showInViewController : self ] ;
} ) ;
return YES ;
}
return NO ;
}
2014-11-24 09:38:23 +00:00
# pragma mark - Internal methods
2014-12-12 13:18:34 +00:00
- ( void ) onAPNSHandlerHasBeenUpdated {
// Force table reload to update notifications section
apnsNotificationsSwitch = nil ;
[ self . tableView reloadData ] ;
}
2015-01-05 13:25:11 +00:00
- ( void ) updateAvatarImage : ( UIImage * ) image {
[ self . userPicture setImage : image forState : UIControlStateNormal ] ;
[ self . userPicture setImage : image forState : UIControlStateHighlighted ] ;
[ self . userPicture setImage : image forState : UIControlStateDisabled ] ;
}
2014-11-24 09:38:23 +00:00
- ( void ) reset {
// Cancel picture loader ( if any )
if ( imageLoader ) {
[ MediaManager cancel : imageLoader ] ;
imageLoader = nil ;
}
// Cancel potential error alerts
for ( CustomAlert * alert in errorAlerts ) {
[ alert dismiss : NO ] ;
}
2014-12-11 13:52:04 +00:00
// Remove listener
if ( userUpdateListener ) {
[ [ MatrixHandler sharedHandler ] . mxSession . myUser removeListener : userUpdateListener ] ;
userUpdateListener = nil ;
}
2014-11-24 09:38:23 +00:00
currentPictureURL = nil ;
uploadedPictureURL = nil ;
2015-01-05 13:25:11 +00:00
[ self updateAvatarImage : [ UIImage imageNamed : @ "default-profile" ] ] ;
2014-11-24 09:38:23 +00:00
currentDisplayName = nil ;
self . userDisplayName . text = nil ;
}
2015-01-05 13:25:11 +00:00
- ( void ) startUserInfoUploadAnimation {
if ( _activityIndicatorBackgroundView . hidden ) {
_activityIndicatorBackgroundView . hidden = NO ;
[ _activityIndicator startAnimating ] ;
}
_saveUserInfoButton . enabled = NO ;
}
- ( void ) stopUserInfoUploadAnimation {
if ( ! _activityIndicatorBackgroundView . hidden ) {
_activityIndicatorBackgroundView . hidden = YES ;
[ _activityIndicator stopAnimating ] ;
}
_saveUserInfoButton . enabled = isAvatarUpdated || isDisplayNameUpdated ;
}
2014-12-11 13:52:04 +00:00
- ( void ) configureView {
2015-01-05 15:53:41 +00:00
// ignore any refresh until there is a pending upload
if ( isDisplayNameUploading || isAvatarUploading ) {
return ;
}
2014-11-24 09:38:23 +00:00
MatrixHandler * mxHandler = [ MatrixHandler sharedHandler ] ;
2014-12-11 13:52:04 +00:00
2014-11-24 09:38:23 +00:00
// Disable user ' s interactions
_userPicture . enabled = NO ;
_userDisplayName . enabled = NO ;
2015-01-05 14:11:40 +00:00
if ( mxHandler . status = = MatrixHandlerStatusServerSyncDone ) {
2014-12-11 13:52:04 +00:00
if ( ! userUpdateListener ) {
// Set current user ' s information and add observers
[ self updateUserPicture : mxHandler . mxSession . myUser . avatarUrl ] ;
currentDisplayName = mxHandler . mxSession . myUser . displayname ;
self . userDisplayName . text = currentDisplayName ;
2014-11-24 09:38:23 +00:00
2015-01-05 13:25:11 +00:00
[ self stopUserInfoUploadAnimation ] ;
2014-12-11 13:52:04 +00:00
// Register listener to update user ' s information
userUpdateListener = [ mxHandler . mxSession . myUser listenToUserUpdate : ^ ( MXEvent * event ) {
// Update displayName
if ( ! [ currentDisplayName isEqualToString : mxHandler . mxSession . myUser . displayname ] ) {
currentDisplayName = mxHandler . mxSession . myUser . displayname ;
self . userDisplayName . text = mxHandler . mxSession . myUser . displayname ;
}
// Update user ' s avatar
[ self updateUserPicture : mxHandler . mxSession . myUser . avatarUrl ] ;
2015-01-05 13:25:11 +00:00
// update button management
isDisplayNameUpdated = isAvatarUpdated = NO ;
_saveUserInfoButton . enabled = NO ;
2014-12-11 13:52:04 +00:00
// TODO display user ' s presence
} ] ;
}
2015-01-05 14:11:40 +00:00
} else if ( mxHandler . status = = MatrixHandlerStatusStoreDataReady ) {
// Set local user ' s information ( the data may not be up - to - date )
[ self updateUserPicture : mxHandler . mxSession . myUser . avatarUrl ] ;
currentDisplayName = mxHandler . mxSession . myUser . displayname ;
self . userDisplayName . text = currentDisplayName ;
} else if ( mxHandler . status = = MatrixHandlerStatusLoggedOut ) {
2014-12-11 13:52:04 +00:00
[ self reset ] ;
}
2014-11-24 09:38:23 +00:00
2014-12-11 13:52:04 +00:00
if ( [ mxHandler isResumeDone ] ) {
2015-01-05 13:25:11 +00:00
[ self stopUserInfoUploadAnimation ] ;
2014-12-11 13:52:04 +00:00
_userPicture . enabled = YES ;
_userDisplayName . enabled = YES ;
}
2014-11-24 09:38:23 +00:00
[ self . tableView reloadData ] ;
}
- ( void ) saveDisplayName {
// Check whether the display name has been changed
NSString * displayname = self . userDisplayName . text ;
2014-12-16 23:59:57 +00:00
if ( ( displayname . length || currentDisplayName . length ) && [ displayname isEqualToString : currentDisplayName ] = = NO ) {
2014-11-24 09:38:23 +00:00
// Save display name
2015-01-05 13:25:11 +00:00
[ self startUserInfoUploadAnimation ] ;
2014-11-24 09:38:23 +00:00
_userDisplayName . enabled = NO ;
2015-01-05 15:53:41 +00:00
isDisplayNameUploading = YES ;
2014-11-24 09:38:23 +00:00
MatrixHandler * mxHandler = [ MatrixHandler sharedHandler ] ;
2014-12-11 13:52:04 +00:00
[ mxHandler . mxSession . myUser setDisplayName : displayname success : ^ {
2015-01-05 13:25:11 +00:00
// save the current displayname
2014-11-24 09:38:23 +00:00
currentDisplayName = displayname ;
2015-01-05 13:25:11 +00:00
// no more update in progress
isDisplayNameUpdated = NO ;
// need to uploaded the avatar
if ( isAvatarUpdated ) {
[ self savePicture ] ;
} else {
// the job is ended
[ self stopUserInfoUploadAnimation ] ;
}
2014-11-24 09:38:23 +00:00
_userDisplayName . enabled = YES ;
2015-01-05 15:53:41 +00:00
isDisplayNameUploading = NO ;
2014-11-24 09:38:23 +00:00
} failure : ^ ( NSError * error ) {
NSLog ( @ "Set displayName failed: %@" , error ) ;
2015-01-05 13:25:11 +00:00
[ self stopUserInfoUploadAnimation ] ;
2014-11-24 09:38:23 +00:00
_userDisplayName . enabled = YES ;
2015-01-05 15:53:41 +00:00
isDisplayNameUploading = NO ;
2014-11-24 09:38:23 +00:00
// Alert user
NSString * title = [ error . userInfo valueForKey : NSLocalizedFailureReasonErrorKey ] ;
if ( ! title ) {
title = @ "Display name change failed" ;
}
NSString * msg = [ error . userInfo valueForKey : NSLocalizedDescriptionKey ] ;
CustomAlert * alert = [ [ CustomAlert alloc ] initWithTitle : title message : msg style : CustomAlertStyleAlert ] ;
[ errorAlerts addObject : alert ] ;
alert . cancelButtonIndex = [ alert addActionWithTitle : @ "Cancel" style : CustomAlertActionStyleDefault handler : ^ ( CustomAlert * alert ) {
[ errorAlerts removeObject : alert ] ;
// Remove change
self . userDisplayName . text = currentDisplayName ;
} ] ;
[ alert addActionWithTitle : @ "Retry" style : CustomAlertActionStyleDefault handler : ^ ( CustomAlert * alert ) {
[ errorAlerts removeObject : alert ] ;
[ self saveDisplayName ] ;
} ] ;
[ alert showInViewController : self ] ;
} ] ;
}
}
- ( void ) savePicture {
MatrixHandler * mxHandler = [ MatrixHandler sharedHandler ] ;
// Save picture
2015-01-05 13:25:11 +00:00
[ self startUserInfoUploadAnimation ] ;
2014-11-24 09:38:23 +00:00
_userPicture . enabled = NO ;
2015-01-05 15:53:41 +00:00
isAvatarUploading = YES ;
2014-11-24 09:38:23 +00:00
if ( uploadedPictureURL = = nil ) {
// Upload picture
[ mxHandler . mxRestClient uploadContent : UIImageJPEGRepresentation ( [ self . userPicture imageForState : UIControlStateNormal ] , 0.5 )
mimeType : @ "image/jpeg"
timeout : 30
success : ^ ( NSString * url ) {
// Store uploaded picture url and trigger picture saving
uploadedPictureURL = url ;
[ self savePicture ] ;
} failure : ^ ( NSError * error ) {
NSLog ( @ "Upload image failed: %@" , error ) ;
2015-01-05 13:25:11 +00:00
[ self stopUserInfoUploadAnimation ] ;
2014-11-24 09:38:23 +00:00
_userPicture . enabled = YES ;
2015-01-05 15:53:41 +00:00
isAvatarUploading = NO ;
2014-11-24 09:38:23 +00:00
[ self handleErrorDuringPictureSaving : error ] ;
2015-01-07 08:11:21 +00:00
} uploadProgress : ^ ( NSUInteger bytesWritten , long long totalBytesWritten , long long totalBytesExpectedToWrite ) {
// need to display the progress ?
2014-11-24 09:38:23 +00:00
} ] ;
} else {
2014-12-11 13:52:04 +00:00
[ mxHandler . mxSession . myUser setAvatarUrl : uploadedPictureURL
2014-11-24 09:38:23 +00:00
success : ^ {
2015-01-05 09:15:26 +00:00
// uploadedPictureURL becomes the uploaded picture
currentPictureURL = uploadedPictureURL ;
// manage the nil case .
2014-12-11 13:52:04 +00:00
[ self updateUserPicture : uploadedPictureURL ] ;
2014-11-24 09:38:23 +00:00
uploadedPictureURL = nil ;
2015-01-05 13:25:11 +00:00
isAvatarUpdated = NO ;
if ( isDisplayNameUpdated ) {
[ self saveDisplayName ] ;
} else {
_saveUserInfoButton . enabled = NO ;
[ self stopUserInfoUploadAnimation ] ;
}
2015-01-05 15:53:41 +00:00
// update statuses
_userPicture . enabled = YES ;
isAvatarUploading = NO ;
2014-11-24 09:38:23 +00:00
} failure : ^ ( NSError * error ) {
NSLog ( @ "Set avatar url failed: %@" , error ) ;
2015-01-05 13:25:11 +00:00
[ self stopUserInfoUploadAnimation ] ;
2015-01-05 15:53:41 +00:00
2014-11-24 09:38:23 +00:00
_userPicture . enabled = YES ;
2015-01-05 15:53:41 +00:00
isAvatarUploading = NO ;
// update statuses
2014-11-24 09:38:23 +00:00
[ self handleErrorDuringPictureSaving : error ] ;
} ] ;
}
}
- ( void ) handleErrorDuringPictureSaving : ( NSError * ) error {
NSString * title = [ error . userInfo valueForKey : NSLocalizedFailureReasonErrorKey ] ;
if ( ! title ) {
title = @ "Picture change failed" ;
}
NSString * msg = [ error . userInfo valueForKey : NSLocalizedDescriptionKey ] ;
CustomAlert * alert = [ [ CustomAlert alloc ] initWithTitle : title message : msg style : CustomAlertStyleAlert ] ;
[ errorAlerts addObject : alert ] ;
alert . cancelButtonIndex = [ alert addActionWithTitle : @ "Cancel" style : CustomAlertActionStyleDefault handler : ^ ( CustomAlert * alert ) {
[ errorAlerts removeObject : alert ] ;
// Remove change
uploadedPictureURL = nil ;
2014-12-11 13:52:04 +00:00
[ self updateUserPicture : [ MatrixHandler sharedHandler ] . mxSession . myUser . avatarUrl ] ;
2014-11-24 09:38:23 +00:00
} ] ;
[ alert addActionWithTitle : @ "Retry" style : CustomAlertActionStyleDefault handler : ^ ( CustomAlert * alert ) {
[ errorAlerts removeObject : alert ] ;
[ self savePicture ] ;
} ] ;
[ alert showInViewController : self ] ;
}
- ( void ) updateUserPicture : ( NSString * ) avatar_url {
if ( currentPictureURL = = nil || [ currentPictureURL isEqualToString : avatar_url ] = = NO ) {
// Cancel previous loader ( if any )
if ( imageLoader ) {
[ MediaManager cancel : imageLoader ] ;
imageLoader = nil ;
}
currentPictureURL = [ avatar_url isEqual : [ NSNull null ] ] ? nil : avatar_url ;
if ( currentPictureURL ) {
// Load user ' s picture
imageLoader = [ MediaManager loadPicture : currentPictureURL success : ^ ( UIImage * image ) {
2015-01-05 13:25:11 +00:00
[ self updateAvatarImage : image ] ;
2014-11-24 09:38:23 +00:00
} failure : ^ ( NSError * error ) {
// Reset picture URL in order to try next time
currentPictureURL = nil ;
} ] ;
} else {
// Set placeholder
2015-01-05 13:25:11 +00:00
[ self updateAvatarImage : [ UIImage imageNamed : @ "default-profile" ] ] ;
2014-11-24 09:38:23 +00:00
}
}
}
# pragma mark - KVO
- ( void ) observeValueForKeyPath : ( NSString * ) keyPath ofObject : ( id ) object change : ( NSDictionary * ) change context : ( void * ) context {
2015-01-05 14:11:40 +00:00
if ( [ @ "status" isEqualToString : keyPath ] ) {
2014-12-11 13:52:04 +00:00
dispatch_async ( dispatch_get _main _queue ( ) , ^ {
[ self configureView ] ;
} ) ;
} else if ( [ @ "isResumeDone" isEqualToString : keyPath ] ) {
if ( [ [ MatrixHandler sharedHandler ] isResumeDone ] ) {
2015-01-05 13:25:11 +00:00
[ self stopUserInfoUploadAnimation ] ;
2014-12-11 13:52:04 +00:00
_userPicture . enabled = YES ;
_userDisplayName . enabled = YES ;
} else {
2015-01-05 13:25:11 +00:00
[ self startUserInfoUploadAnimation ] ;
2014-12-11 13:52:04 +00:00
_userPicture . enabled = NO ;
_userDisplayName . enabled = NO ;
2014-11-24 09:38:23 +00:00
}
}
}
# pragma mark - Actions
- ( IBAction ) onButtonPressed : ( id ) sender {
[ self dismissKeyboard ] ;
2015-01-05 13:25:11 +00:00
if ( sender = = _saveUserInfoButton ) {
if ( isDisplayNameUpdated ) {
_saveUserInfoButton . enabled = NO ;
[ self saveDisplayName ] ;
} else if ( isAvatarUpdated ) {
_saveUserInfoButton . enabled = NO ;
[ self savePicture ] ;
}
} else if ( sender = = _userPicture ) {
2014-11-24 09:38:23 +00:00
// Open picture gallery
UIImagePickerController * mediaPicker = [ [ UIImagePickerController alloc ] init ] ;
mediaPicker . delegate = self ;
mediaPicker . sourceType = UIImagePickerControllerSourceTypePhotoLibrary ;
mediaPicker . allowsEditing = NO ;
[ [ AppDelegate theDelegate ] . masterTabBarController presentMediaPicker : mediaPicker ] ;
} else if ( sender = = logoutBtn ) {
[ [ AppDelegate theDelegate ] logout ] ;
2014-12-12 13:18:34 +00:00
} else if ( sender = = apnsNotificationsSwitch ) {
[ APNSHandler sharedHandler ] . isActive = apnsNotificationsSwitch . on ;
} else if ( sender = = inAppNotificationsSwitch ) {
[ AppSettings sharedSettings ] . enableInAppNotifications = inAppNotificationsSwitch . on ;
2014-11-24 09:38:23 +00:00
} else if ( sender = = allEventsSwitch ) {
[ AppSettings sharedSettings ] . displayAllEvents = allEventsSwitch . on ;
} else if ( sender = = unsupportedMsgSwitch ) {
[ AppSettings sharedSettings ] . hideUnsupportedMessages = unsupportedMsgSwitch . on ;
} else if ( sender = = sortMembersSwitch ) {
[ AppSettings sharedSettings ] . sortMembersUsingLastSeenTime = sortMembersSwitch . on ;
2014-12-17 13:40:39 +00:00
} else if ( sender = = displayLeftMembersSwitch ) {
[ AppSettings sharedSettings ] . displayLeftUsers = displayLeftMembersSwitch . on ;
2014-11-24 09:38:23 +00:00
}
}
# pragma mark - keyboard
2015-01-05 13:25:11 +00:00
- ( void ) manageSaveChangeButton {
// check if there is a displayname update
NSString * displayname = self . userDisplayName . text ;
isDisplayNameUpdated = ( ( displayname . length || currentDisplayName . length ) && [ displayname isEqualToString : currentDisplayName ] = = NO ) ;
_saveUserInfoButton . enabled = isDisplayNameUpdated || isAvatarUpdated ;
}
2014-12-16 23:59:57 +00:00
- ( void ) dismissKeyboard {
2014-11-24 09:38:23 +00:00
// Hide the keyboard
[ _userDisplayName resignFirstResponder ] ;
2015-01-05 13:25:11 +00:00
[ self manageSaveChangeButton ] ;
2014-11-24 09:38:23 +00:00
}
# pragma mark - UITextField delegate
2015-01-05 13:25:11 +00:00
- ( BOOL ) textFieldShouldReturn : ( UITextField * ) textField {
if ( _userDisplayName = = textField ) {
// "Done" key has been pressed
[ self dismissKeyboard ] ;
}
2014-11-24 09:38:23 +00:00
return YES ;
}
2015-01-05 13:25:11 +00:00
- ( IBAction ) textFieldDidChange : ( id ) sender {
if ( sender = = _userDisplayName ) {
[ self manageSaveChangeButton ] ;
}
}
2014-12-19 09:02:09 +00:00
# pragma mark - Table view delegate
- ( void ) tableView : ( UITableView * ) aTableView didSelectRowAtIndexPath : ( NSIndexPath * ) indexPath {
if ( self . tableView = = aTableView ) {
// tap on clear application cache
if ( ( indexPath . section = = SETTINGS_SECTION _ROOMS _INDEX ) && ( indexPath . row = = SETTINGS_SECTION _ROOMS _CLEAR _CACHE _INDEX ) ) {
[ [ MatrixHandler sharedHandler ] forceInitialSync : YES ] ;
}
[ aTableView deselectRowAtIndexPath : indexPath animated : YES ] ;
}
}
2014-11-24 09:38:23 +00:00
# pragma mark - Table view data source
- ( NSInteger ) numberOfSectionsInTableView : ( UITableView * ) tableView {
return 4 ;
}
- ( NSInteger ) tableView : ( UITableView * ) tableView numberOfRowsInSection : ( NSInteger ) section {
if ( section = = SETTINGS_SECTION _NOTIFICATIONS _INDEX ) {
2014-12-12 13:18:34 +00:00
if ( [ APNSHandler sharedHandler ] . isAvailable ) {
return 2 ;
}
2014-11-24 09:38:23 +00:00
return 1 ;
} else if ( section = = SETTINGS_SECTION _ROOMS _INDEX ) {
2014-12-19 09:02:09 +00:00
return SETTINGS_SECTION _ROOMS _INDEX _COUNT ;
2014-11-24 09:38:23 +00:00
} else if ( section = = SETTINGS_SECTION _CONFIGURATION _INDEX ) {
return 1 ;
} else if ( section = = SETTINGS_SECTION _COMMANDS _INDEX ) {
return 1 ;
}
return 0 ;
}
- ( CGFloat ) tableView : ( UITableView * ) tableView heightForRowAtIndexPath : ( NSIndexPath * ) indexPath {
if ( indexPath . section = = SETTINGS_SECTION _NOTIFICATIONS _INDEX ) {
return 44 ;
} else if ( indexPath . section = = SETTINGS_SECTION _ROOMS _INDEX ) {
return 44 ;
} else if ( indexPath . section = = SETTINGS_SECTION _CONFIGURATION _INDEX ) {
UITextView * textView = [ [ UITextView alloc ] initWithFrame : CGRectMake ( 0 , 0 , tableView . frame . size . width , MAXFLOAT ) ] ;
textView . font = [ UIFont systemFontOfSize : 14 ] ;
MatrixHandler * mxHandler = [ MatrixHandler sharedHandler ] ;
textView . text = [ NSString stringWithFormat : kConfigurationFormatText , mxHandler . homeServerURL , nil , mxHandler . userId , mxHandler . accessToken ] ;
CGSize contentSize = [ textView sizeThatFits : textView . frame . size ] ;
return contentSize . height + 1 ;
} else if ( indexPath . section = = SETTINGS_SECTION _COMMANDS _INDEX ) {
UITextView * textView = [ [ UITextView alloc ] initWithFrame : CGRectMake ( 0 , 0 , tableView . frame . size . width , MAXFLOAT ) ] ;
textView . font = [ UIFont systemFontOfSize : 14 ] ;
textView . text = kCommandsDescriptionText ;
CGSize contentSize = [ textView sizeThatFits : textView . frame . size ] ;
return contentSize . height + 1 ;
}
return 44 ;
}
2014-12-12 13:18:34 +00:00
- ( CGFloat ) tableView : ( UITableView * ) tableView heightForHeaderInSection : ( NSInteger ) section {
2014-11-24 09:38:23 +00:00
return 30 ;
}
2014-12-12 13:18:34 +00:00
- ( CGFloat ) tableView : ( UITableView * ) tableView heightForFooterInSection : ( NSInteger ) section {
2014-11-24 09:38:23 +00:00
return 1 ;
}
2014-12-12 13:18:34 +00:00
- ( UIView * ) tableView : ( UITableView * ) tableView viewForHeaderInSection : ( NSInteger ) section {
2014-11-24 09:38:23 +00:00
UILabel * sectionHeader = [ [ UILabel alloc ] initWithFrame : [ tableView rectForHeaderInSection : section ] ] ;
sectionHeader . font = [ UIFont boldSystemFontOfSize : 16 ] ;
sectionHeader . backgroundColor = [ UIColor colorWithRed : 0.9 green : 0.9 blue : 0.9 alpha : 1.0 ] ;
if ( section = = SETTINGS_SECTION _NOTIFICATIONS _INDEX ) {
sectionHeader . text = @ " Notifications" ;
} else if ( section = = SETTINGS_SECTION _ROOMS _INDEX ) {
sectionHeader . text = @ " Rooms" ;
} else if ( section = = SETTINGS_SECTION _CONFIGURATION _INDEX ) {
sectionHeader . text = @ " Configuration" ;
} else if ( section = = SETTINGS_SECTION _COMMANDS _INDEX ) {
sectionHeader . text = @ " Commands" ;
} else {
sectionHeader = nil ;
}
return sectionHeader ;
}
- ( UITableViewCell * ) tableView : ( UITableView * ) tableView cellForRowAtIndexPath : ( NSIndexPath * ) indexPath {
2014-12-19 09:02:09 +00:00
UITableViewCell * cell = nil ;
2014-11-24 09:38:23 +00:00
if ( indexPath . section = = SETTINGS_SECTION _NOTIFICATIONS _INDEX ) {
SettingsTableCellWithSwitch * notificationsCell = [ tableView dequeueReusableCellWithIdentifier : @ "SettingsCellWithSwitch" forIndexPath : indexPath ] ;
2014-12-12 13:18:34 +00:00
if ( indexPath . row = = 0 ) {
notificationsCell . settingLabel . text = @ "Enable In-App notifications" ;
notificationsCell . settingSwitch . on = [ [ AppSettings sharedSettings ] enableInAppNotifications ] ;
inAppNotificationsSwitch = notificationsCell . settingSwitch ;
} else {
notificationsCell . settingLabel . text = @ "Enable push notifications" ;
notificationsCell . settingSwitch . on = [ [ APNSHandler sharedHandler ] isActive ] ;
apnsNotificationsSwitch = notificationsCell . settingSwitch ;
}
2014-11-24 09:38:23 +00:00
cell = notificationsCell ;
} else if ( indexPath . section = = SETTINGS_SECTION _ROOMS _INDEX ) {
2014-12-19 09:02:09 +00:00
if ( indexPath . row = = SETTINGS_SECTION _ROOMS _CLEAR _CACHE _INDEX ) {
cell = [ [ UITableViewCell alloc ] initWithStyle : UITableViewCellStyleDefault reuseIdentifier : @ "ClearCacheCell" ] ;
if ( ! cell ) {
cell = [ [ UITableViewCell alloc ] initWithStyle : UITableViewCellStyleDefault reuseIdentifier : @ "ClearCacheCell" ] ;
}
2014-12-19 10:03:09 +00:00
cell . textLabel . text = [ NSString stringWithFormat : @ "Clear cache (%@)" , [ NSByteCountFormatter stringFromByteCount : [ MatrixHandler sharedHandler ] . MXCacheSize countStyle : NSByteCountFormatterCountStyleFile ] ] ;
;
2014-12-19 09:02:09 +00:00
cell . textLabel . textAlignment = NSTextAlignmentCenter ;
cell . textLabel . textColor = [ AppDelegate theDelegate ] . masterTabBarController . tabBar . tintColor ;
2014-12-17 13:40:39 +00:00
} else {
2014-12-19 09:02:09 +00:00
SettingsTableCellWithSwitch * roomsSettingCell = [ tableView dequeueReusableCellWithIdentifier : @ "SettingsCellWithSwitch" forIndexPath : indexPath ] ;
if ( indexPath . row = = SETTINGS_SECTION _ROOMS _DISPLAY _ALL _EVENTS _INDEX ) {
roomsSettingCell . settingLabel . text = @ "Display all events" ;
roomsSettingCell . settingSwitch . on = [ [ AppSettings sharedSettings ] displayAllEvents ] ;
allEventsSwitch = roomsSettingCell . settingSwitch ;
} else if ( indexPath . row = = SETTINGS_SECTION _ROOMS _HIDE _UNSUPPORTED _MESSAGES _INDEX ) {
roomsSettingCell . settingLabel . text = @ "Hide unsupported messages" ;
roomsSettingCell . settingSwitch . on = [ [ AppSettings sharedSettings ] hideUnsupportedMessages ] ;
unsupportedMsgSwitch = roomsSettingCell . settingSwitch ;
} else if ( indexPath . row = = SETTINGS_SECTION _ROOMS _SORT _MEMBERS _INDEX ) {
roomsSettingCell . settingLabel . text = @ "Sort members by last seen time" ;
roomsSettingCell . settingSwitch . on = [ [ AppSettings sharedSettings ] sortMembersUsingLastSeenTime ] ;
sortMembersSwitch = roomsSettingCell . settingSwitch ;
} else if ( indexPath . row = = SETTINGS_SECTION _ROOMS _DISPLAY _LEFT _MEMBERS _INDEX ) {
roomsSettingCell . settingLabel . text = @ "Display left members" ;
roomsSettingCell . settingSwitch . on = [ [ AppSettings sharedSettings ] displayLeftUsers ] ;
displayLeftMembersSwitch = roomsSettingCell . settingSwitch ;
}
cell = roomsSettingCell ;
2014-11-24 09:38:23 +00:00
}
} else if ( indexPath . section = = SETTINGS_SECTION _CONFIGURATION _INDEX ) {
SettingsTableCellWithTextView * configCell = [ tableView dequeueReusableCellWithIdentifier : @ "SettingsCellWithTextView" forIndexPath : indexPath ] ;
MatrixHandler * mxHandler = [ MatrixHandler sharedHandler ] ;
configCell . settingTextView . text = [ NSString stringWithFormat : kConfigurationFormatText , mxHandler . homeServerURL , nil , mxHandler . userId , mxHandler . accessToken ] ;
cell = configCell ;
} else if ( indexPath . section = = SETTINGS_SECTION _COMMANDS _INDEX ) {
SettingsTableCellWithTextView * commandsCell = [ tableView dequeueReusableCellWithIdentifier : @ "SettingsCellWithTextView" forIndexPath : indexPath ] ;
commandsCell . settingTextView . text = kCommandsDescriptionText ;
cell = commandsCell ;
}
return cell ;
}
# pragma mark - UIImagePickerControllerDelegate
- ( void ) imagePickerController : ( UIImagePickerController * ) picker didFinishPickingMediaWithInfo : ( NSDictionary * ) info {
UIImage * selectedImage = [ info objectForKey : UIImagePickerControllerOriginalImage ] ;
if ( selectedImage ) {
2015-01-05 13:25:11 +00:00
[ self updateAvatarImage : selectedImage ] ;
isAvatarUpdated = YES ;
_saveUserInfoButton . enabled = YES ;
2014-11-24 09:38:23 +00:00
}
[ self dismissMediaPicker ] ;
}
- ( void ) imagePickerControllerDidCancel : ( UIImagePickerController * ) picker {
[ self dismissMediaPicker ] ;
}
- ( void ) dismissMediaPicker {
[ [ AppDelegate theDelegate ] . masterTabBarController dismissMediaPicker ] ;
}
@ end