2014-10-02 15:02:47 +00:00
/ *
2015-04-13 16:00:39 +00:00
Copyright 2015 OpenMarket Ltd
2014-10-02 15:02:47 +00:00
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 .
* /
2014-10-03 17:26:39 +00:00
# import "SettingsViewController.h"
2014-10-02 15:02:47 +00:00
2015-04-13 16:00:39 +00:00
# import "RageShakeManager.h"
2014-10-08 16:55:26 +00:00
# import "AppDelegate.h"
2014-12-12 13:18:34 +00:00
# import "APNSHandler.h"
2014-10-08 16:55:26 +00:00
2015-01-29 17:33:39 +00:00
# import "ContactManager.h"
2014-10-29 16:48:11 +00:00
# import "SettingsTableViewCell.h"
2015-01-23 17:45:13 +00:00
# define SETTINGS_SECTION _LINKED _EMAILS _INDEX 0
# define SETTINGS_SECTION _NOTIFICATIONS _INDEX 1
2015-01-26 17:06:13 +00:00
# define SETTINGS_SECTION _CONTACTS _INDEX 2
# define SETTINGS_SECTION _ROOMS _INDEX 3
# define SETTINGS_SECTION _CONFIGURATION _INDEX 4
# define SETTINGS_SECTION _COMMANDS _INDEX 5
# define SETTINGS_SECTION _COUNT 6
2014-10-29 16:48:11 +00:00
2015-04-10 15:42:06 +00:00
// TODO Restore room event settings
2015-04-15 12:46:54 +00:00
# define SETTINGS_SECTION _ROOMS _DISPLAY _ALL _EVENTS _INDEX 0
# define SETTINGS_SECTION _ROOMS _SHOW _REDACTIONS _INDEX 1
# define SETTINGS_SECTION _ROOMS _SHOW _UNSUPPORTED _EVENTS _INDEX 2
# define SETTINGS_SECTION _ROOMS _SORT _MEMBERS _INDEX 3
# define SETTINGS_SECTION _ROOMS _DISPLAY _LEFT _MEMBERS _INDEX 4
# define SETTINGS_SECTION _ROOMS _SET _CACHE _SIZE _INDEX 5
# define SETTINGS_SECTION _ROOMS _CLEAR _CACHE _INDEX 6
# define SETTINGS_SECTION _ROOMS _INDEX _COUNT 7
2015-04-10 15:42:06 +00:00
2014-12-19 09:02:09 +00:00
2015-03-13 12:35:22 +00:00
NSString * const kUserInfoNotificationRulesText = @ "To configure global notification settings (like rules), go find a webclient and hit Settings > Notifications." ;
2015-04-22 12:52:51 +00:00
NSString * const kConfigurationFormatText = @ "Console version: %@\r\nMatrixKit version: %@\r\nMatrixSDK version: %@\r\n%@\r\nHome server: %@\r\nIdentity server: %@\r\nUser ID: %@" ;
2015-02-04 17:18:38 +00:00
NSString * const kBuildFormatText = @ "Build: %@\r\n" ;
2014-10-29 16:48:11 +00:00
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" ;
2014-10-18 02:03:07 +00:00
@ interface SettingsViewController ( ) {
2015-01-26 15:51:34 +00:00
NSMutableArray * alertsArray ;
2015-01-23 17:45:13 +00:00
// Navigation Bar button
UIButton * logoutBtn ;
2014-10-24 10:06:35 +00:00
2015-01-23 17:45:13 +00:00
// User ' s profile
2015-04-02 11:50:11 +00:00
MXKMediaLoader * imageLoader ;
2014-10-18 02:03:07 +00:00
NSString * currentDisplayName ;
NSString * currentPictureURL ;
2015-01-14 17:30:00 +00:00
NSString * currentPictureThumbURL ;
2014-11-10 14:26:37 +00:00
NSString * uploadedPictureURL ;
2015-01-26 15:51:34 +00:00
// Local changes
BOOL isAvatarUpdated ;
BOOL isSavingInProgress ;
2015-05-20 19:41:34 +00:00
blockMXKAccountDetailsViewController_onReadyToLeave onReadyToLeaveHandler ;
2015-01-26 15:51:34 +00:00
// Listen user ' s profile changes
2014-12-11 13:52:04 +00:00
id userUpdateListener ;
2015-01-23 17:45:13 +00:00
// Linked emails
NSMutableArray * linkedEmails ;
2015-05-05 07:56:25 +00:00
MXK3PID * submittedEmail ;
2015-01-27 16:24:55 +00:00
SettingsCellWithTextFieldAndButton * submittedEmailCell ;
2015-01-27 09:25:36 +00:00
SettingsCellWithLabelTextFieldAndButton * emailTokenCell ;
// Dynamic rows in the Linked emails section
2015-01-30 18:06:36 +00:00
NSInteger submittedEmailRowIndex ;
NSInteger emailTokenRowIndex ;
2015-01-26 17:06:13 +00:00
2015-01-23 17:45:13 +00:00
// Notifications
2014-12-12 13:18:34 +00:00
UISwitch * apnsNotificationsSwitch ;
UISwitch * inAppNotificationsSwitch ;
2015-01-23 17:45:13 +00:00
// Dynamic rows in the Notifications section
2015-01-30 18:06:36 +00:00
NSInteger enablePushNotifRowIndex ;
NSInteger enableInAppNotifRowIndex ;
2015-03-13 12:35:22 +00:00
NSInteger userInfoNotifRowIndex ;
2015-01-30 18:06:36 +00:00
// Contacts
UISwitch * contactsSyncSwitch ;
// Country codes management
NSArray * countryCodes ;
NSString * countryCode ;
NSString * selectedCountryCode ;
BOOL isSelectingCountryCode ;
// Dynamic rows in Contacts section
NSInteger syncLocalContactsRowIndex ;
NSInteger countryCodeRowIndex ;
2015-01-23 17:45:13 +00:00
// Rooms settings
2014-10-29 16:48:11 +00:00
UISwitch * allEventsSwitch ;
2015-02-20 13:43:11 +00:00
UISwitch * redactionsSwitch ;
2015-01-28 17:39:23 +00:00
UISwitch * unsupportedEventsSwitch ;
2014-11-05 11:57:24 +00:00
UISwitch * sortMembersSwitch ;
2014-12-17 13:40:39 +00:00
UISwitch * displayLeftMembersSwitch ;
2015-01-23 17:45:13 +00:00
SettingsCellWithLabelAndSlider * maxCacheSizeCell ;
2015-02-24 16:41:10 +00:00
NSUInteger minimumCacheSize ;
2014-10-18 02:03:07 +00:00
}
2015-04-13 16:00:39 +00:00
// @ property ( strong , nonatomic ) IBOutlet UITableView * tableView ;
2014-10-08 16:55:26 +00:00
@ property ( weak , nonatomic ) IBOutlet UIView * tableHeader ;
2015-01-23 17:45:13 +00:00
@ property ( weak , nonatomic ) IBOutlet UIButton * userPictureButton ;
2014-10-08 16:55:26 +00:00
@ property ( weak , nonatomic ) IBOutlet UITextField * userDisplayName ;
2015-01-05 13:25:11 +00:00
@ property ( weak , nonatomic ) IBOutlet UIButton * saveUserInfoButton ;
2015-04-13 17:02:02 +00:00
@ property ( strong , nonatomic ) IBOutlet UIView * profileActivityIndicatorBgView ;
@ property ( strong , nonatomic ) IBOutlet UIActivityIndicatorView * profileActivityIndicator ;
2014-10-08 16:55:26 +00:00
- ( IBAction ) onButtonPressed : ( id ) sender ;
2014-10-02 15:02:47 +00:00
2014-10-03 17:26:39 +00:00
@ end
2014-10-02 15:02:47 +00:00
2014-10-03 17:26:39 +00:00
@ implementation SettingsViewController
2014-10-02 15:02:47 +00:00
2014-10-03 17:26:39 +00:00
- ( void ) viewDidLoad {
[ super viewDidLoad ] ;
// Do any additional setup after loading the view , typically from a nib .
2014-10-24 10:06:35 +00:00
2015-04-15 12:46:54 +00:00
// Consider the standard settings by default
_settings = [ MXKAppSettings standardAppSettings ] ;
2014-10-29 16:48:11 +00:00
// 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
2015-01-23 17:45:13 +00:00
[ [ self . userPictureButton imageView ] setContentMode : UIViewContentModeScaleAspectFill ] ;
[ [ self . userPictureButton imageView ] setClipsToBounds : YES ] ;
2014-12-16 07:47:48 +00:00
2015-01-26 15:51:34 +00:00
alertsArray = [ NSMutableArray array ] ;
2015-02-24 16:41:10 +00:00
// Initialize the minimum cache size with the current value
2015-04-13 16:00:39 +00:00
minimumCacheSize = self . minCachesSize ;
2015-01-05 13:25:11 +00:00
isAvatarUpdated = NO ;
2015-01-26 15:51:34 +00:00
isSavingInProgress = NO ;
2015-01-05 15:53:41 +00:00
2015-01-05 13:25:11 +00:00
_saveUserInfoButton . enabled = NO ;
2015-04-13 17:02:02 +00:00
_profileActivityIndicatorBgView . hidden = YES ;
2015-01-29 17:33:39 +00:00
// country selection
NSString * path = [ [ NSBundle mainBundle ] pathForResource : @ "countryCodes" ofType : @ "plist" ] ;
countryCodes = [ NSArray arrayWithContentsOfFile : path ] ;
isSelectingCountryCode = NO ;
2015-04-13 16:00:39 +00:00
// Setup ` MXKRoomMemberListViewController` properties
self . rageShakeManager = [ RageShakeManager sharedManager ] ;
2014-10-03 17:26:39 +00:00
}
2014-10-02 15:02:47 +00:00
2014-10-03 17:26:39 +00:00
- ( void ) didReceiveMemoryWarning {
[ super didReceiveMemoryWarning ] ;
// Dispose of any resources that can be recreated .
2014-10-24 10:06:35 +00:00
if ( imageLoader ) {
2015-01-08 20:23:49 +00:00
[ imageLoader cancel ] ;
2014-10-24 10:06:35 +00:00
imageLoader = nil ;
}
}
- ( void ) dealloc {
2014-12-11 13:52:04 +00:00
[ self reset ] ;
2015-01-26 15:51:34 +00:00
alertsArray = nil ;
2014-10-29 16:48:11 +00:00
logoutBtn = nil ;
2014-10-03 17:26:39 +00:00
}
2014-10-02 15:02:47 +00:00
2014-10-18 02:03:07 +00:00
- ( void ) viewWillAppear : ( BOOL ) animated {
[ super viewWillAppear : animated ] ;
2014-12-11 13:52:04 +00:00
2015-04-15 12:46:54 +00:00
if ( ! _settings ) {
// Consider the standard settings by default
_settings = [ MXKAppSettings standardAppSettings ] ;
}
selectedCountryCode = countryCode = [ _settings phonebookCountryCode ] ;
2015-01-29 17:33:39 +00:00
2015-02-24 16:41:10 +00:00
// Update the minimum cache size with the current value
// Dispatch this operation to not freeze the app
dispatch_async ( dispatch_get _main _queue ( ) , ^ {
2015-04-13 16:00:39 +00:00
minimumCacheSize = self . minCachesSize ;
2015-02-24 16:41:10 +00:00
} ) ;
2014-12-11 13:52:04 +00:00
// Refresh display
[ self configureView ] ;
2014-12-12 13:18:34 +00:00
[ [ NSNotificationCenter defaultCenter ] addObserver : self selector : @ selector ( onAPNSHandlerHasBeenUpdated ) name : kAPNSHandlerHasBeenUpdated object : nil ] ;
2014-11-10 14:26:37 +00:00
}
- ( void ) viewWillDisappear : ( BOOL ) animated {
[ super viewWillDisappear : animated ] ;
2015-02-12 10:16:28 +00:00
2015-04-13 17:02:02 +00:00
[ self stopActivityIndicator ] ;
2015-01-29 17:33:39 +00:00
// if country has been updated
// update the contact phonenumbers
// and check if they match now to Matrix Users
if ( ! [ countryCode isEqualToString : selectedCountryCode ] ) {
2015-04-15 12:46:54 +00:00
[ _settings setPhonebookCountryCode : selectedCountryCode ] ;
2015-01-29 17:33:39 +00:00
countryCode = selectedCountryCode ;
[ [ ContactManager sharedManager ] internationalizePhoneNumbers : countryCode ] ;
[ [ ContactManager sharedManager ] fullRefresh ] ;
}
2015-04-15 12:46:54 +00:00
countryCode = [ _settings phonebookCountryCode ] ;
2015-01-05 15:53:41 +00:00
2014-12-12 13:18:34 +00:00
[ [ NSNotificationCenter defaultCenter ] removeObserver : self name : kAPNSHandlerHasBeenUpdated object : nil ] ;
2014-11-10 14:26:37 +00:00
}
2015-01-26 15:51:34 +00:00
- ( BOOL ) shouldLeave : ( blockSettings_onReadyToLeave ) handler {
// Check whether some local changes have not been saved
if ( _saveUserInfoButton . enabled ) {
2015-01-05 15:53:41 +00:00
dispatch_async ( dispatch_get _main _queue ( ) , ^ {
2015-04-02 09:33:41 +00:00
MXKAlert * alert = [ [ MXKAlert alloc ] initWithTitle : nil message : @ "Changes will be discarded" style : MXKAlertStyleAlert ] ;
2015-01-26 15:51:34 +00:00
[ alertsArray addObject : alert ] ;
2015-04-02 09:33:41 +00:00
alert . cancelButtonIndex = [ alert addActionWithTitle : @ "Discard" style : MXKAlertActionStyleDefault handler : ^ ( MXKAlert * alert ) {
2015-01-26 15:51:34 +00:00
[ alertsArray removeObject : alert ] ;
// Discard changes
self . userDisplayName . text = currentDisplayName ;
2015-04-13 16:00:39 +00:00
[ self updateUserPicture : self . mxSession . myUser . avatarUrl force : YES ] ;
2015-01-26 15:51:34 +00:00
// Ready to leave
if ( handler ) {
handler ( ) ;
}
2015-01-05 15:53:41 +00:00
} ] ;
2015-04-02 09:33:41 +00:00
[ alert addActionWithTitle : @ "Save" style : MXKAlertActionStyleDefault handler : ^ ( MXKAlert * alert ) {
2015-01-26 15:51:34 +00:00
[ alertsArray removeObject : alert ] ;
2015-05-20 19:41:34 +00:00
// Start saving ( Report handler to leave at the end ) .
onReadyToLeaveHandler = handler ;
2015-01-26 15:51:34 +00:00
[ self saveUserInfo ] ;
2015-01-05 15:53:41 +00:00
} ] ;
2015-01-26 15:51:34 +00:00
[ alert showInViewController : self ] ;
2015-01-05 15:53:41 +00:00
} ) ;
2015-05-20 19:41:34 +00:00
return NO ;
} else if ( isSavingInProgress ) {
// Report handler to leave at the end of saving
onReadyToLeaveHandler = handler ;
2015-01-26 15:51:34 +00:00
return NO ;
2015-01-05 15:53:41 +00:00
}
2015-05-20 19:41:34 +00:00
2015-01-26 15:51:34 +00:00
return YES ;
2015-01-05 15:53:41 +00:00
}
2015-04-13 17:02:02 +00:00
# pragma mark - overridden MXKTableViewController methods
- ( void ) setMxSession : ( MXSession * ) session {
[ super setMxSession : session ] ;
[ self configureView ] ;
}
- ( void ) didMatrixSessionStateChange {
[ super didMatrixSessionStateChange ] ;
[ self configureView ] ;
}
- ( void ) startActivityIndicator {
if ( _profileActivityIndicatorBgView . hidden ) {
_profileActivityIndicatorBgView . hidden = NO ;
[ _profileActivityIndicator startAnimating ] ;
}
_userPictureButton . enabled = NO ;
_userDisplayName . enabled = NO ;
_saveUserInfoButton . enabled = NO ;
}
- ( void ) stopActivityIndicator {
if ( ! isSavingInProgress ) {
if ( ! _profileActivityIndicatorBgView . hidden ) {
_profileActivityIndicatorBgView . hidden = YES ;
[ _profileActivityIndicator stopAnimating ] ;
}
_userPictureButton . enabled = YES ;
_userDisplayName . enabled = YES ;
[ self updateSaveUserInfoButtonStatus ] ;
}
}
2014-11-10 14:26:37 +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 ] ;
}
2014-11-10 14:26:37 +00:00
- ( void ) reset {
2015-01-08 17:20:34 +00:00
// Remove observers
[ [ NSNotificationCenter defaultCenter ] removeObserver : self ] ;
2014-11-10 14:26:37 +00:00
// Cancel picture loader ( if any )
if ( imageLoader ) {
2015-01-08 20:23:49 +00:00
[ imageLoader cancel ] ;
2014-11-10 14:26:37 +00:00
imageLoader = nil ;
}
2014-10-18 02:03:07 +00:00
2015-01-26 15:51:34 +00:00
// Cancel potential alerts
2015-04-02 09:33:41 +00:00
for ( MXKAlert * alert in alertsArray ) {
2014-11-10 14:26:37 +00:00
[ alert dismiss : NO ] ;
}
2014-12-11 13:52:04 +00:00
// Remove listener
if ( userUpdateListener ) {
2015-04-13 16:00:39 +00:00
[ self . mxSession . myUser removeListener : userUpdateListener ] ;
2014-12-11 13:52:04 +00:00
userUpdateListener = nil ;
}
2014-11-10 14:26:37 +00:00
currentPictureURL = nil ;
2015-01-14 17:30:00 +00:00
currentPictureThumbURL = nil ;
2014-11-10 14:26:37 +00:00
uploadedPictureURL = nil ;
2015-01-26 15:51:34 +00:00
isAvatarUpdated = NO ;
2015-01-23 17:45:13 +00:00
[ self updateUserPictureButton : [ UIImage imageNamed : @ "default-profile" ] ] ;
2014-11-10 14:26:37 +00:00
currentDisplayName = nil ;
self . userDisplayName . text = nil ;
2015-01-23 17:45:13 +00:00
2015-01-26 15:51:34 +00:00
_saveUserInfoButton . enabled = NO ;
2015-01-23 17:45:13 +00:00
linkedEmails = nil ;
2015-01-27 16:24:55 +00:00
submittedEmail = nil ;
submittedEmailCell = nil ;
emailTokenCell = nil ;
contactsSyncSwitch = nil ;
apnsNotificationsSwitch = nil ;
inAppNotificationsSwitch = nil ;
allEventsSwitch = nil ;
2015-01-28 17:39:23 +00:00
unsupportedEventsSwitch = nil ;
2015-01-27 16:24:55 +00:00
sortMembersSwitch = nil ;
displayLeftMembersSwitch = nil ;
maxCacheSizeCell = nil ;
2015-05-20 19:41:34 +00:00
onReadyToLeaveHandler = nil ;
2014-11-10 14:26:37 +00:00
}
2014-12-11 13:52:04 +00:00
- ( void ) configureView {
2015-01-26 15:51:34 +00:00
// Ignore any refresh when saving is in progress
if ( isSavingInProgress ) {
2015-01-05 15:53:41 +00:00
return ;
}
2014-11-10 14:26:37 +00:00
// Disable user ' s interactions
2015-01-23 17:45:13 +00:00
_userPictureButton . enabled = NO ;
2014-11-10 14:26:37 +00:00
_userDisplayName . enabled = NO ;
2015-04-13 16:00:39 +00:00
if ( ! self . mxSession ) {
[ self reset ] ;
} else if ( self . mxSession . state = = MXSessionStateRunning ) {
2014-12-11 13:52:04 +00:00
if ( ! userUpdateListener ) {
// Set current user ' s information and add observers
2015-04-13 16:00:39 +00:00
[ self updateUserPicture : self . mxSession . myUser . avatarUrl force : YES ] ;
currentDisplayName = self . mxSession . myUser . displayname ;
2014-12-11 13:52:04 +00:00
self . userDisplayName . text = currentDisplayName ;
2014-11-10 14:26:37 +00:00
2014-12-11 13:52:04 +00:00
// Register listener to update user ' s information
2015-04-13 16:00:39 +00:00
userUpdateListener = [ self . mxSession . myUser listenToUserUpdate : ^ ( MXEvent * event ) {
2014-12-11 13:52:04 +00:00
// Update displayName
2015-04-13 16:00:39 +00:00
if ( ! [ currentDisplayName isEqualToString : self . mxSession . myUser . displayname ] ) {
currentDisplayName = self . mxSession . myUser . displayname ;
self . userDisplayName . text = self . mxSession . myUser . displayname ;
2014-12-11 13:52:04 +00:00
}
// Update user ' s avatar
2015-04-13 16:00:39 +00:00
[ self updateUserPicture : self . mxSession . myUser . avatarUrl force : NO ] ;
2015-01-26 15:51:34 +00:00
// Update button management
[ self updateSaveUserInfoButtonStatus ] ;
2015-01-05 13:25:11 +00:00
2014-12-11 13:52:04 +00:00
// TODO display user ' s presence
} ] ;
}
2015-04-13 16:00:39 +00:00
} else if ( self . mxSession . state = = MXSessionStateStoreDataReady || self . mxSession . state = = MXSessionStateSyncInProgress ) {
2015-02-12 13:39:08 +00:00
// Remove listener ( if any ) , this action is required to handle correctly matrix sdk handler reload ( see clear cache )
if ( userUpdateListener ) {
2015-04-13 16:00:39 +00:00
[ self . mxSession . myUser removeListener : userUpdateListener ] ;
2015-02-12 13:39:08 +00:00
userUpdateListener = nil ;
}
2015-01-05 14:11:40 +00:00
// Set local user ' s information ( the data may not be up - to - date )
2015-04-13 16:00:39 +00:00
[ self updateUserPicture : self . mxSession . myUser . avatarUrl force : NO ] ;
currentDisplayName = self . mxSession . myUser . displayname ;
2015-01-05 14:11:40 +00:00
self . userDisplayName . text = currentDisplayName ;
2014-12-11 13:52:04 +00:00
}
2014-11-10 14:26:37 +00:00
2015-01-26 15:51:34 +00:00
// Restore user ' s interactions
_userPictureButton . enabled = YES ;
_userDisplayName . enabled = YES ;
2014-11-10 14:26:37 +00:00
[ self . tableView reloadData ] ;
2014-10-30 09:54:57 +00:00
}
2015-01-26 15:51:34 +00:00
- ( void ) saveUserInfo {
2015-04-13 17:02:02 +00:00
[ self startActivityIndicator ] ;
2015-01-26 15:51:34 +00:00
isSavingInProgress = YES ;
2014-10-31 17:54:32 +00:00
// 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-10-31 17:54:32 +00:00
// Save display name
2015-04-13 16:00:39 +00:00
[ self . mxSession . myUser setDisplayName : displayname success : ^ {
2015-01-26 15:51:34 +00:00
// Update the current displayname
2014-10-31 17:54:32 +00:00
currentDisplayName = displayname ;
2015-01-26 15:51:34 +00:00
// Go to the next change saving step
[ self saveUserInfo ] ;
2014-10-31 17:54:32 +00:00
} failure : ^ ( NSError * error ) {
2015-02-24 12:49:05 +00:00
NSLog ( @ "[SettingsVC] Failed to set displayName: %@" , error ) ;
2014-10-31 17:54:32 +00:00
// Alert user
2014-11-10 14:26:37 +00:00
NSString * title = [ error . userInfo valueForKey : NSLocalizedFailureReasonErrorKey ] ;
if ( ! title ) {
title = @ "Display name change failed" ;
}
NSString * msg = [ error . userInfo valueForKey : NSLocalizedDescriptionKey ] ;
2015-04-02 09:33:41 +00:00
MXKAlert * alert = [ [ MXKAlert alloc ] initWithTitle : title message : msg style : MXKAlertStyleAlert ] ;
2015-01-26 15:51:34 +00:00
[ alertsArray addObject : alert ] ;
2015-04-02 09:33:41 +00:00
alert . cancelButtonIndex = [ alert addActionWithTitle : @ "Abort" style : MXKAlertActionStyleDefault handler : ^ ( MXKAlert * alert ) {
2015-01-26 15:51:34 +00:00
[ alertsArray removeObject : alert ] ;
// Discard changes
2014-11-10 14:26:37 +00:00
self . userDisplayName . text = currentDisplayName ;
2015-04-13 16:00:39 +00:00
[ self updateUserPicture : self . mxSession . myUser . avatarUrl force : YES ] ;
2015-01-26 15:51:34 +00:00
// Loop to end saving
[ self saveUserInfo ] ;
2014-11-10 14:26:37 +00:00
} ] ;
2015-04-02 09:33:41 +00:00
[ alert addActionWithTitle : @ "Retry" style : MXKAlertActionStyleDefault handler : ^ ( MXKAlert * alert ) {
2015-01-26 15:51:34 +00:00
[ alertsArray removeObject : alert ] ;
// Loop to retry saving
[ self saveUserInfo ] ;
2014-11-10 14:26:37 +00:00
} ] ;
[ alert showInViewController : self ] ;
2014-10-31 17:54:32 +00:00
} ] ;
2015-01-26 15:51:34 +00:00
return ;
2014-10-31 17:54:32 +00:00
}
2014-11-10 14:26:37 +00:00
2015-01-26 15:51:34 +00:00
// Check whether avatar has been updated
if ( isAvatarUpdated ) {
if ( uploadedPictureURL = = nil ) {
2015-02-19 15:58:17 +00:00
// Retrieve the current picture and make sure its orientation is up
2015-04-02 09:40:09 +00:00
UIImage * updatedPicture = [ MXKTools forceImageOrientationUp : [ self . userPictureButton imageForState : UIControlStateNormal ] ] ;
2015-02-19 15:58:17 +00:00
2015-01-26 15:51:34 +00:00
// Upload picture
2015-04-13 16:00:39 +00:00
MXKMediaLoader * uploader = [ MXKMediaManager prepareUploaderWithMatrixSession : self . mxSession initialRange : 0 andRange : 1.0 ] ;
2015-02-19 15:58:17 +00:00
[ uploader uploadData : UIImageJPEGRepresentation ( updatedPicture , 0.5 ) mimeType : @ "image/jpeg" success : ^ ( NSString * url ) {
2015-01-26 15:51:34 +00:00
// Store uploaded picture url and trigger picture saving
uploadedPictureURL = url ;
[ self saveUserInfo ] ;
} failure : ^ ( NSError * error ) {
2015-02-24 12:49:05 +00:00
NSLog ( @ "[SettingsVC] Failed to upload image: %@" , error ) ;
2015-01-26 15:51:34 +00:00
[ self handleErrorDuringPictureSaving : error ] ;
} ] ;
} else {
2015-04-13 16:00:39 +00:00
[ self . mxSession . myUser setAvatarUrl : uploadedPictureURL
2015-01-26 15:51:34 +00:00
success : ^ {
// uploadedPictureURL becomes the user ' s picture
[ self updateUserPicture : uploadedPictureURL force : YES ] ;
// Loop to end saving
[ self saveUserInfo ] ;
} failure : ^ ( NSError * error ) {
2015-02-24 12:49:05 +00:00
NSLog ( @ "[SettingsVC] Failed to set avatar url: %@" , error ) ;
2015-01-26 15:51:34 +00:00
[ self handleErrorDuringPictureSaving : error ] ;
} ] ;
}
return ;
}
// Backup is complete
isSavingInProgress = NO ;
2015-04-13 17:02:02 +00:00
// Stop activity indicator except if matrix session is working
if ( self . mxSession . state ! = MXSessionStateSyncInProgress && self . mxSession . state ! = MXSessionStateInitialised ) {
[ self stopActivityIndicator ] ;
}
2015-05-20 19:41:34 +00:00
// Ready to leave
if ( onReadyToLeaveHandler ) {
onReadyToLeaveHandler ( ) ;
onReadyToLeaveHandler = nil ;
}
2014-10-31 17:54:32 +00:00
}
2014-11-10 14:26:37 +00:00
- ( void ) handleErrorDuringPictureSaving : ( NSError * ) error {
NSString * title = [ error . userInfo valueForKey : NSLocalizedFailureReasonErrorKey ] ;
if ( ! title ) {
title = @ "Picture change failed" ;
2014-10-24 10:06:35 +00:00
}
2014-11-10 14:26:37 +00:00
NSString * msg = [ error . userInfo valueForKey : NSLocalizedDescriptionKey ] ;
2014-10-24 10:06:35 +00:00
2015-04-02 09:33:41 +00:00
MXKAlert * alert = [ [ MXKAlert alloc ] initWithTitle : title message : msg style : MXKAlertStyleAlert ] ;
2015-01-26 15:51:34 +00:00
[ alertsArray addObject : alert ] ;
2015-04-02 09:33:41 +00:00
alert . cancelButtonIndex = [ alert addActionWithTitle : @ "Abort" style : MXKAlertActionStyleDefault handler : ^ ( MXKAlert * alert ) {
2015-01-26 15:51:34 +00:00
[ alertsArray removeObject : alert ] ;
2014-11-10 14:26:37 +00:00
// Remove change
2015-01-26 15:51:34 +00:00
self . userDisplayName . text = currentDisplayName ;
2015-04-13 16:00:39 +00:00
[ self updateUserPicture : self . mxSession . myUser . avatarUrl force : YES ] ;
2015-01-26 15:51:34 +00:00
// Loop to end saving
[ self saveUserInfo ] ;
2014-11-10 14:26:37 +00:00
} ] ;
2015-04-02 09:33:41 +00:00
[ alert addActionWithTitle : @ "Retry" style : MXKAlertActionStyleDefault handler : ^ ( MXKAlert * alert ) {
2015-01-26 15:51:34 +00:00
[ alertsArray removeObject : alert ] ;
// Loop to retry saving
[ self saveUserInfo ] ;
2014-11-10 14:26:37 +00:00
} ] ;
2014-10-24 10:06:35 +00:00
2014-11-10 14:26:37 +00:00
[ alert showInViewController : self ] ;
2014-10-24 10:06:35 +00:00
}
2015-01-26 15:51:34 +00:00
- ( void ) updateUserPicture : ( NSString * ) avatar_url force : ( BOOL ) force {
if ( force || currentPictureURL = = nil || [ currentPictureURL isEqualToString : avatar_url ] = = NO ) {
2015-01-08 17:20:34 +00:00
// Remove any pending observers
[ [ NSNotificationCenter defaultCenter ] removeObserver : self ] ;
2014-10-30 09:54:57 +00:00
// Cancel previous loader ( if any )
if ( imageLoader ) {
2015-01-08 20:23:49 +00:00
[ imageLoader cancel ] ;
2014-10-30 09:54:57 +00:00
imageLoader = nil ;
}
2015-01-26 15:51:34 +00:00
// Cancel any local change
isAvatarUpdated = NO ;
uploadedPictureURL = nil ;
2014-10-30 09:54:57 +00:00
currentPictureURL = [ avatar_url isEqual : [ NSNull null ] ] ? nil : avatar_url ;
if ( currentPictureURL ) {
2015-01-14 17:30:00 +00:00
// Suppose this url is a matrix content uri , we use SDK to get the well adapted thumbnail from server
2015-04-13 16:00:39 +00:00
currentPictureThumbURL = [ self . mxSession . matrixRestClient urlOfContentThumbnail : currentPictureURL toFitViewSize : self . userPictureButton . frame . size withMethod : MXThumbnailingMethodCrop ] ;
2015-04-02 11:50:11 +00:00
NSString * cacheFilePath = [ MXKMediaManager cachePathForMediaWithURL : currentPictureThumbURL inFolder : kMXKMediaManagerAvatarThumbnailFolder ] ;
2015-01-14 17:30:00 +00:00
2015-01-08 17:20:34 +00:00
// Check whether the image download is in progress
2015-04-02 11:50:11 +00:00
id loader = [ MXKMediaManager existingDownloaderWithOutputFilePath : cacheFilePath ] ;
2015-01-08 17:20:34 +00:00
if ( loader ) {
// Add observers
2015-04-02 11:50:11 +00:00
[ [ NSNotificationCenter defaultCenter ] addObserver : self selector : @ selector ( onMediaDownloadEnd : ) name : kMXKMediaDownloadDidFinishNotification object : nil ] ;
[ [ NSNotificationCenter defaultCenter ] addObserver : self selector : @ selector ( onMediaDownloadEnd : ) name : kMXKMediaDownloadDidFailNotification object : nil ] ;
2015-01-08 17:20:34 +00:00
} else {
// Retrieve the image from cache
2015-04-02 11:50:11 +00:00
UIImage * image = [ MXKMediaManager loadPictureFromFilePath : cacheFilePath ] ;
2015-01-08 17:20:34 +00:00
if ( image ) {
2015-01-23 17:45:13 +00:00
[ self updateUserPictureButton : image ] ;
2015-01-08 17:20:34 +00:00
} else {
// Cancel potential download in progress
if ( imageLoader ) {
2015-01-08 20:23:49 +00:00
[ imageLoader cancel ] ;
2015-01-08 17:20:34 +00:00
}
// Add observers
2015-04-02 11:50:11 +00:00
[ [ NSNotificationCenter defaultCenter ] addObserver : self selector : @ selector ( onMediaDownloadEnd : ) name : kMXKMediaDownloadDidFinishNotification object : nil ] ;
[ [ NSNotificationCenter defaultCenter ] addObserver : self selector : @ selector ( onMediaDownloadEnd : ) name : kMXKMediaDownloadDidFailNotification object : nil ] ;
imageLoader = [ MXKMediaManager downloadMediaFromURL : currentPictureThumbURL andSaveAtFilePath : cacheFilePath ] ;
2015-01-08 17:20:34 +00:00
}
}
2014-10-30 09:54:57 +00:00
} else {
// Set placeholder
2015-01-23 17:45:13 +00:00
[ self updateUserPictureButton : [ UIImage imageNamed : @ "default-profile" ] ] ;
}
}
}
- ( void ) updateUserPictureButton : ( UIImage * ) image {
[ self . userPictureButton setImage : image forState : UIControlStateNormal ] ;
[ self . userPictureButton setImage : image forState : UIControlStateHighlighted ] ;
[ self . userPictureButton setImage : image forState : UIControlStateDisabled ] ;
}
2015-01-26 15:51:34 +00:00
- ( void ) updateSaveUserInfoButtonStatus {
// Check whether display name has been changed
2015-01-23 17:45:13 +00:00
NSString * displayname = self . userDisplayName . text ;
2015-01-26 15:51:34 +00:00
BOOL isDisplayNameUpdated = ( ( displayname . length || currentDisplayName . length ) && [ displayname isEqualToString : currentDisplayName ] = = NO ) ;
2015-01-23 17:45:13 +00:00
2015-05-20 19:41:34 +00:00
_saveUserInfoButton . enabled = ( isDisplayNameUpdated || isAvatarUpdated ) && ! isSavingInProgress ;
2015-01-23 17:45:13 +00:00
}
2015-01-08 17:20:34 +00:00
- ( void ) onMediaDownloadEnd : ( NSNotification * ) notif {
// sanity check
if ( [ notif . object isKindOfClass : [ NSString class ] ] ) {
NSString * url = notif . object ;
2015-04-02 11:50:11 +00:00
NSString * cacheFilePath = notif . userInfo [ kMXKMediaLoaderFilePathKey ] ;
2015-01-08 17:20:34 +00:00
2015-01-14 17:30:00 +00:00
if ( [ url isEqualToString : currentPictureThumbURL ] ) {
2015-01-08 17:20:34 +00:00
// update the image
2015-04-02 11:50:11 +00:00
UIImage * image = nil ;
if ( cacheFilePath . length ) {
image = [ MXKMediaManager loadPictureFromFilePath : cacheFilePath ] ;
}
2015-01-08 17:20:34 +00:00
if ( image = = nil ) {
image = [ UIImage imageNamed : @ "default-profile" ] ;
}
2015-01-23 17:45:13 +00:00
[ self updateUserPictureButton : image ] ;
2015-01-08 17:20:34 +00:00
// remove the observers
[ [ NSNotificationCenter defaultCenter ] removeObserver : self ] ;
imageLoader = nil ;
2015-04-02 11:50:11 +00:00
if ( [ notif . name isEqualToString : kMXKMediaDownloadDidFailNotification ] ) {
2015-01-08 17:20:34 +00:00
// Reset picture URL in order to try next time
currentPictureURL = nil ;
}
}
}
}
2014-10-29 16:48:11 +00:00
# pragma mark - Actions
2014-10-24 10:06:35 +00:00
2014-10-08 16:55:26 +00:00
- ( IBAction ) onButtonPressed : ( id ) sender {
2014-10-24 10:06:35 +00:00
[ self dismissKeyboard ] ;
2015-01-05 13:25:11 +00:00
if ( sender = = _saveUserInfoButton ) {
2015-01-26 15:51:34 +00:00
[ self saveUserInfo ] ;
2015-01-23 17:45:13 +00:00
} else if ( sender = = _userPictureButton ) {
2014-10-31 17:54:32 +00:00
// Open picture gallery
2014-11-10 14:26:37 +00:00
UIImagePickerController * mediaPicker = [ [ UIImagePickerController alloc ] init ] ;
mediaPicker . delegate = self ;
mediaPicker . sourceType = UIImagePickerControllerSourceTypePhotoLibrary ;
mediaPicker . allowsEditing = NO ;
[ [ AppDelegate theDelegate ] . masterTabBarController presentMediaPicker : mediaPicker ] ;
2014-10-29 16:48:11 +00:00
} else if ( sender = = logoutBtn ) {
2014-10-08 16:55:26 +00:00
[ [ AppDelegate theDelegate ] logout ] ;
2015-01-27 16:24:55 +00:00
} else if ( sender = = submittedEmailCell . settingButton ) {
if ( ! submittedEmail || ! [ submittedEmail . address isEqualToString : submittedEmailCell . settingTextField . text ] ) {
2015-05-05 07:56:25 +00:00
submittedEmail = [ [ MXK3PID alloc ] initWithMedium : kMX3PIDMediumEmail andAddress : submittedEmailCell . settingTextField . text ] ;
2015-01-27 16:24:55 +00:00
}
submittedEmailCell . settingButton . enabled = NO ;
2015-05-04 17:18:55 +00:00
[ submittedEmail requestValidationTokenWithMatrixRestClient : self . mxSession . matrixRestClient success : ^ {
2015-01-27 16:24:55 +00:00
// Reset email field
submittedEmailCell . settingTextField . text = nil ;
[ self . tableView reloadData ] ;
} failure : ^ ( NSError * error ) {
2015-02-24 12:49:05 +00:00
NSLog ( @ "[SettingsVC] Failed to request email token: %@" , error ) ;
2015-01-27 16:24:55 +00:00
// Alert user
[ [ AppDelegate theDelegate ] showErrorAsAlert : error ] ;
submittedEmailCell . settingButton . enabled = YES ;
} ] ;
} else if ( sender = = emailTokenCell . settingButton ) {
emailTokenCell . settingButton . enabled = NO ;
[ submittedEmail validateWithToken : emailTokenCell . settingTextField . text success : ^ ( BOOL success ) {
if ( success ) {
// The email has been "Authenticated"
// Link the email with user ' s account
2015-04-13 16:00:39 +00:00
[ submittedEmail bindWithUserId : self . mxSession . myUser . userId success : ^ {
2015-01-27 16:24:55 +00:00
// Add new linked email
if ( ! linkedEmails ) {
linkedEmails = [ NSMutableArray array ] ;
}
[ linkedEmails addObject : submittedEmail . address ] ;
// Release pending email and refresh table to remove related cell
submittedEmail = nil ;
[ self . tableView reloadData ] ;
} failure : ^ ( NSError * error ) {
2015-02-24 12:49:05 +00:00
NSLog ( @ "[SettingsVC] Failed to link email: %@" , error ) ;
2015-01-27 16:24:55 +00:00
// Alert user
[ [ AppDelegate theDelegate ] showErrorAsAlert : error ] ;
// Release the pending email ( even if it is Authenticated )
submittedEmail = nil ;
[ self . tableView reloadData ] ;
} ] ;
} else {
2015-02-24 12:49:05 +00:00
NSLog ( @ "[SettingsVC] Failed to link email" ) ;
2015-04-02 09:33:41 +00:00
MXKAlert * alert = [ [ MXKAlert alloc ] initWithTitle : nil message : @ "Failed to link email" style : MXKAlertStyleAlert ] ;
2015-01-27 16:24:55 +00:00
[ alertsArray addObject : alert ] ;
2015-04-02 09:33:41 +00:00
alert . cancelButtonIndex = [ alert addActionWithTitle : @ "OK" style : MXKAlertActionStyleDefault handler : ^ ( MXKAlert * alert ) {
2015-01-27 16:24:55 +00:00
[ alertsArray removeObject : alert ] ;
} ] ;
[ alert showInViewController : self ] ;
// Reset wrong token
emailTokenCell . settingTextField . text = nil ;
}
} failure : ^ ( NSError * error ) {
2015-02-24 12:49:05 +00:00
NSLog ( @ "[SettingsVC] Failed to submit email token: %@" , error ) ;
2015-01-27 16:24:55 +00:00
// Alert user
[ [ AppDelegate theDelegate ] showErrorAsAlert : error ] ;
emailTokenCell . settingButton . enabled = YES ;
} ] ;
2014-12-12 13:18:34 +00:00
} else if ( sender = = apnsNotificationsSwitch ) {
[ APNSHandler sharedHandler ] . isActive = apnsNotificationsSwitch . on ;
2015-01-29 17:06:37 +00:00
apnsNotificationsSwitch . enabled = NO ;
2014-12-12 13:18:34 +00:00
} else if ( sender = = inAppNotificationsSwitch ) {
2015-04-15 12:46:54 +00:00
_settings . enableInAppNotifications = inAppNotificationsSwitch . on ;
2015-01-09 14:58:15 +00:00
[ self . tableView reloadData ] ;
2014-10-29 16:48:11 +00:00
} else if ( sender = = allEventsSwitch ) {
2015-04-15 12:46:54 +00:00
_settings . showAllEventsInRoomHistory = allEventsSwitch . on ;
2015-02-20 13:43:11 +00:00
} else if ( sender = = redactionsSwitch ) {
2015-04-15 12:46:54 +00:00
_settings . showRedactionsInRoomHistory = redactionsSwitch . on ;
2015-01-28 17:39:23 +00:00
} else if ( sender = = unsupportedEventsSwitch ) {
2015-04-15 12:46:54 +00:00
_settings . showUnsupportedEventsInRoomHistory = unsupportedEventsSwitch . on ;
2014-11-05 11:57:24 +00:00
} else if ( sender = = sortMembersSwitch ) {
2015-04-15 12:46:54 +00:00
_settings . sortRoomMembersUsingLastSeenTime = sortMembersSwitch . on ;
2014-12-17 13:40:39 +00:00
} else if ( sender = = displayLeftMembersSwitch ) {
2015-04-15 12:46:54 +00:00
_settings . showLeftMembersInRoomMemberList = displayLeftMembersSwitch . on ;
2015-01-26 17:06:13 +00:00
} else if ( sender = = contactsSyncSwitch ) {
2015-04-15 12:46:54 +00:00
_settings . syncLocalContacts = contactsSyncSwitch . on ;
2015-01-29 17:33:39 +00:00
isSelectingCountryCode = NO ;
dispatch_async ( dispatch_get _main _queue ( ) , ^ {
[ self . tableView reloadData ] ;
} ) ;
2014-10-08 16:55:26 +00:00
}
}
2015-01-19 07:58:45 +00:00
- ( IBAction ) onSliderValueChange : ( id ) sender {
if ( sender = = maxCacheSizeCell . settingSlider ) {
2015-01-21 14:08:29 +00:00
UISlider * slider = maxCacheSizeCell . settingSlider ;
// check if the upper bounds have been updated
2015-04-13 16:00:39 +00:00
if ( slider . maximumValue ! = self . maxAllowedCachesSize ) {
slider . maximumValue = self . maxAllowedCachesSize ;
2015-01-21 14:08:29 +00:00
}
// check if the value does not exceed the bounds
2015-02-24 16:41:10 +00:00
if ( slider . value < minimumCacheSize ) {
slider . value = minimumCacheSize ;
2015-01-21 14:08:29 +00:00
}
2015-04-13 16:00:39 +00:00
[ self setCurrentMaxCachesSize : slider . value ] ;
2015-01-21 14:08:29 +00:00
2015-04-13 16:00:39 +00:00
maxCacheSizeCell . settingLabel . text = [ NSString stringWithFormat : @ "Maximum cache size (%@)" , [ NSByteCountFormatter stringFromByteCount : self . currentMaxCachesSize countStyle : NSByteCountFormatterCountStyleFile ] ] ;
2015-01-19 07:58:45 +00:00
}
}
2014-10-08 16:55:26 +00:00
# pragma mark - keyboard
2014-12-16 23:59:57 +00:00
- ( void ) dismissKeyboard {
2015-01-09 14:58:15 +00:00
if ( [ _userDisplayName isFirstResponder ] ) {
// Hide the keyboard
[ _userDisplayName resignFirstResponder ] ;
2015-01-26 15:51:34 +00:00
[ self updateSaveUserInfoButtonStatus ] ;
2015-01-27 16:24:55 +00:00
} else if ( [ submittedEmailCell . settingTextField isFirstResponder ] ) {
[ submittedEmailCell . settingTextField resignFirstResponder ] ;
} else if ( [ emailTokenCell . settingTextField isFirstResponder ] ) {
[ emailTokenCell . settingTextField resignFirstResponder ] ;
2015-01-09 14:58:15 +00:00
}
2014-10-08 16:55:26 +00:00
}
# pragma mark - UITextField delegate
2015-01-05 13:25:11 +00:00
- ( BOOL ) textFieldShouldReturn : ( UITextField * ) textField {
2015-01-23 17:45:13 +00:00
// "Done" key has been pressed
[ self dismissKeyboard ] ;
2014-10-08 16:55:26 +00:00
return YES ;
}
2015-01-05 13:25:11 +00:00
- ( IBAction ) textFieldDidChange : ( id ) sender {
if ( sender = = _userDisplayName ) {
2015-01-26 15:51:34 +00:00
[ self updateSaveUserInfoButtonStatus ] ;
2015-01-27 16:24:55 +00:00
} else if ( sender = = submittedEmailCell . settingTextField ) {
submittedEmailCell . settingButton . enabled = ( submittedEmailCell . settingTextField . text . length ! = 0 ) ;
} else if ( sender = = emailTokenCell . settingTextField ) {
emailTokenCell . settingButton . enabled = ( emailTokenCell . settingTextField . text . length ! = 0 ) ;
2015-01-05 13:25:11 +00:00
}
}
2015-01-23 17:45:13 +00:00
# pragma mark - UITableView data source
2014-10-29 16:48:11 +00:00
- ( NSInteger ) numberOfSectionsInTableView : ( UITableView * ) tableView {
2015-01-23 17:45:13 +00:00
return SETTINGS_SECTION _COUNT ;
2014-10-29 16:48:11 +00:00
}
- ( NSInteger ) tableView : ( UITableView * ) tableView numberOfRowsInSection : ( NSInteger ) section {
2015-01-27 16:24:55 +00:00
NSInteger count = 0 ;
2015-01-23 17:45:13 +00:00
if ( section = = SETTINGS_SECTION _LINKED _EMAILS _INDEX ) {
2015-01-27 16:24:55 +00:00
submittedEmailRowIndex = emailTokenRowIndex = -1 ;
2015-01-09 14:58:15 +00:00
2015-01-27 16:24:55 +00:00
count = linkedEmails . count ;
submittedEmailRowIndex = count + + ;
2015-05-05 07:56:25 +00:00
if ( submittedEmail && submittedEmail . validationState >= MXK3PIDAuthStateTokenReceived ) {
2015-01-27 16:24:55 +00:00
emailTokenRowIndex = count + + ;
} else {
emailTokenCell = nil ;
}
} else if ( section = = SETTINGS_SECTION _NOTIFICATIONS _INDEX ) {
2015-03-13 12:35:22 +00:00
enableInAppNotifRowIndex = enablePushNotifRowIndex = userInfoNotifRowIndex = -1 ;
2015-01-09 14:58:15 +00:00
2015-01-27 09:25:36 +00:00
if ( [ APNSHandler sharedHandler ] . isAvailable ) {
enablePushNotifRowIndex = count + + ;
2015-01-09 14:58:15 +00:00
}
2015-01-27 09:25:36 +00:00
enableInAppNotifRowIndex = count + + ;
2015-03-13 12:35:22 +00:00
userInfoNotifRowIndex = count + + ;
2015-01-26 17:06:13 +00:00
} else if ( section = = SETTINGS_SECTION _CONTACTS _INDEX ) {
2015-01-29 17:33:39 +00:00
countryCodeRowIndex = syncLocalContactsRowIndex = -1 ;
// init row index
syncLocalContactsRowIndex = count + + ;
2015-04-15 12:46:54 +00:00
if ( [ _settings syncLocalContacts ] ) {
2015-01-29 17:33:39 +00:00
countryCodeRowIndex = count + + ;
}
2014-11-05 11:57:24 +00:00
} else if ( section = = SETTINGS_SECTION _ROOMS _INDEX ) {
2015-01-27 16:24:55 +00:00
count = SETTINGS_SECTION _ROOMS _INDEX _COUNT ;
2014-10-29 16:48:11 +00:00
} else if ( section = = SETTINGS_SECTION _CONFIGURATION _INDEX ) {
2015-01-27 16:24:55 +00:00
count = 1 ;
2014-10-29 16:48:11 +00:00
} else if ( section = = SETTINGS_SECTION _COMMANDS _INDEX ) {
2015-01-27 16:24:55 +00:00
count = 1 ;
2014-10-29 16:48:11 +00:00
}
2015-01-27 16:24:55 +00:00
return count ;
2014-10-29 16:48:11 +00:00
}
- ( CGFloat ) tableView : ( UITableView * ) tableView heightForRowAtIndexPath : ( NSIndexPath * ) indexPath {
2015-01-23 17:45:13 +00:00
if ( indexPath . section = = SETTINGS_SECTION _LINKED _EMAILS _INDEX ) {
2015-01-27 16:24:55 +00:00
if ( indexPath . row = = emailTokenRowIndex ) {
return 70 ;
}
2015-01-23 17:45:13 +00:00
return 44 ;
} else if ( indexPath . section = = SETTINGS_SECTION _NOTIFICATIONS _INDEX ) {
2015-03-13 12:35:22 +00:00
if ( indexPath . row = = userInfoNotifRowIndex ) {
UITextView * textView = [ [ UITextView alloc ] initWithFrame : CGRectMake ( 0 , 0 , tableView . frame . size . width , MAXFLOAT ) ] ;
textView . font = [ UIFont systemFontOfSize : 14 ] ;
textView . text = kUserInfoNotificationRulesText ;
CGSize contentSize = [ textView sizeThatFits : textView . frame . size ] ;
return contentSize . height + 1 ;
}
2014-10-29 16:48:11 +00:00
return 44 ;
2015-01-26 17:06:13 +00:00
} else if ( indexPath . section = = SETTINGS_SECTION _CONTACTS _INDEX ) {
2015-01-29 17:33:39 +00:00
if ( ( indexPath . row = = countryCodeRowIndex ) && isSelectingCountryCode ) {
return 164 ;
}
2015-01-26 17:06:13 +00:00
return 44 ;
2014-11-05 11:57:24 +00:00
} else if ( indexPath . section = = SETTINGS_SECTION _ROOMS _INDEX ) {
2015-01-19 07:58:45 +00:00
if ( indexPath . row = = SETTINGS_SECTION _ROOMS _SET _CACHE _SIZE _INDEX ) {
2015-01-21 14:08:29 +00:00
return 88 ;
2015-01-19 07:58:45 +00:00
}
2014-10-29 16:48:11 +00:00
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 ] ;
2015-02-04 17:18:38 +00:00
NSString * appVersion = [ AppDelegate theDelegate ] . appVersion ;
NSString * build = [ AppDelegate theDelegate ] . build ;
if ( build . length ) {
build = [ NSString stringWithFormat : kBuildFormatText , build ] ;
}
2015-04-22 12:52:51 +00:00
textView . text = [ NSString stringWithFormat : kConfigurationFormatText , appVersion , MatrixKitVersion , MatrixSDKVersion , build , self . mxSession . matrixRestClient . homeserver , self . mxSession . matrixRestClient . identityServer , self . mxSession . myUser . userId ] ;
2014-10-29 16:48:11 +00:00
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-10-29 16:48:11 +00:00
return 30 ;
}
2014-12-12 13:18:34 +00:00
- ( CGFloat ) tableView : ( UITableView * ) tableView heightForFooterInSection : ( NSInteger ) section {
2014-10-29 16:48:11 +00:00
return 1 ;
}
2014-12-12 13:18:34 +00:00
- ( UIView * ) tableView : ( UITableView * ) tableView viewForHeaderInSection : ( NSInteger ) section {
2014-10-29 16:48:11 +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 ] ;
2015-01-23 17:45:13 +00:00
if ( section = = SETTINGS_SECTION _LINKED _EMAILS _INDEX ) {
sectionHeader . text = @ " Linked emails" ;
} else if ( section = = SETTINGS_SECTION _NOTIFICATIONS _INDEX ) {
2014-10-29 16:48:11 +00:00
sectionHeader . text = @ " Notifications" ;
2015-01-26 17:06:13 +00:00
} else if ( section = = SETTINGS_SECTION _CONTACTS _INDEX ) {
sectionHeader . text = @ " Contacts" ;
2014-11-05 11:57:24 +00:00
} else if ( section = = SETTINGS_SECTION _ROOMS _INDEX ) {
sectionHeader . text = @ " Rooms" ;
2014-10-29 16:48:11 +00:00
} 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-10-29 16:48:11 +00:00
2015-01-23 17:45:13 +00:00
if ( indexPath . section = = SETTINGS_SECTION _LINKED _EMAILS _INDEX ) {
2015-01-27 16:24:55 +00:00
if ( indexPath . row < linkedEmails . count ) {
cell = [ [ UITableViewCell alloc ] initWithStyle : UITableViewCellStyleDefault reuseIdentifier : @ "LinkedEmailCell" ] ;
if ( ! cell ) {
cell = [ [ UITableViewCell alloc ] initWithStyle : UITableViewCellStyleDefault reuseIdentifier : @ "LinkedEmailCell" ] ;
}
cell . selectionStyle = UITableViewCellSelectionStyleNone ;
cell . textLabel . text = [ linkedEmails objectAtIndex : indexPath . row ] ;
} else if ( indexPath . row = = submittedEmailRowIndex ) {
// Report the current email value ( if any )
NSString * currentEmail = nil ;
if ( submittedEmailCell ) {
currentEmail = submittedEmailCell . settingTextField . text ;
}
submittedEmailCell = [ tableView dequeueReusableCellWithIdentifier : @ "SettingsCellWithTextFieldAndButton" forIndexPath : indexPath ] ;
submittedEmailCell . settingTextField . text = currentEmail ;
submittedEmailCell . settingButton . enabled = ( currentEmail . length ! = 0 ) ;
[ submittedEmailCell . settingButton setTitle : @ "Link Email" forState : UIControlStateNormal ] ;
[ submittedEmailCell . settingButton setTitle : @ "Link Email" forState : UIControlStateHighlighted ] ;
if ( emailTokenRowIndex ! = -1 ) {
// Hide the separator
CGSize screenSize = [ [ UIScreen mainScreen ] bounds ] . size ;
CGFloat rightInset = ( screenSize . width < screenSize . height ) ? screenSize . height : screenSize . width ;
submittedEmailCell . separatorInset = UIEdgeInsetsMake ( 0. f , 0. f , 0. f , rightInset ) ;
}
cell = submittedEmailCell ;
} else if ( indexPath . row = = emailTokenRowIndex ) {
// Report the current token value ( if any )
NSString * currentToken = nil ;
if ( emailTokenCell ) {
currentToken = emailTokenCell . settingTextField . text ;
}
emailTokenCell = [ tableView dequeueReusableCellWithIdentifier : @ "SettingsCellWithLabelTextFieldAndButton" forIndexPath : indexPath ] ;
emailTokenCell . settingLabel . text = [ NSString stringWithFormat : @ "Enter validation token for %@:" , submittedEmail . address ] ;
emailTokenCell . settingTextField . text = currentToken ;
emailTokenCell . settingButton . enabled = ( currentToken . length ! = 0 ) ;
[ emailTokenCell . settingButton setTitle : @ "Submit code" forState : UIControlStateNormal ] ;
[ emailTokenCell . settingButton setTitle : @ "Submit code" forState : UIControlStateHighlighted ] ;
cell = emailTokenCell ;
2015-01-23 17:45:13 +00:00
}
} else if ( indexPath . section = = SETTINGS_SECTION _NOTIFICATIONS _INDEX ) {
2015-03-13 12:35:22 +00:00
if ( indexPath . row = = userInfoNotifRowIndex ) {
SettingsCellWithTextView * userInfoCell = [ tableView dequeueReusableCellWithIdentifier : @ "SettingsCellWithTextView" forIndexPath : indexPath ] ;
userInfoCell . settingTextView . text = kUserInfoNotificationRulesText ;
cell = userInfoCell ;
} else {
SettingsCellWithSwitch * notificationsCell = [ tableView dequeueReusableCellWithIdentifier : @ "SettingsCellWithSwitch" forIndexPath : indexPath ] ;
if ( indexPath . row = = enableInAppNotifRowIndex ) {
notificationsCell . settingLabel . text = @ "Enable In-App notifications" ;
2015-04-15 12:46:54 +00:00
notificationsCell . settingSwitch . on = [ _settings enableInAppNotifications ] ;
2015-03-13 12:35:22 +00:00
inAppNotificationsSwitch = notificationsCell . settingSwitch ;
} else / * enablePushNotifRowIndex * / {
notificationsCell . settingLabel . text = @ "Enable push notifications" ;
notificationsCell . settingSwitch . on = [ [ APNSHandler sharedHandler ] isActive ] ;
notificationsCell . settingSwitch . enabled = YES ;
apnsNotificationsSwitch = notificationsCell . settingSwitch ;
}
cell = notificationsCell ;
2014-12-12 13:18:34 +00:00
}
2015-01-26 17:06:13 +00:00
} else if ( indexPath . section = = SETTINGS_SECTION _CONTACTS _INDEX ) {
2015-01-29 17:33:39 +00:00
if ( indexPath . row = = syncLocalContactsRowIndex ) {
SettingsCellWithSwitch * contactsCell = [ tableView dequeueReusableCellWithIdentifier : @ "SettingsCellWithSwitch" forIndexPath : indexPath ] ;
contactsCell . settingLabel . text = @ "Sync local contacts" ;
2015-04-15 12:46:54 +00:00
contactsCell . settingSwitch . on = [ _settings syncLocalContacts ] ;
2015-01-29 17:33:39 +00:00
contactsSyncSwitch = contactsCell . settingSwitch ;
cell = contactsCell ;
} else if ( indexPath . row = = countryCodeRowIndex ) {
int index = 0 ;
NSString * countryName = @ "" ;
for ( NSDictionary * dict in countryCodes ) {
if ( [ [ dict valueForKey : @ "id" ] isEqualToString : selectedCountryCode ] ) {
countryName = [ dict valueForKey : @ "country" ] ;
break ;
}
index + + ;
}
2015-01-26 17:06:13 +00:00
2015-01-29 17:33:39 +00:00
// there is no country code selection
if ( ! isSelectingCountryCode ) {
SettingsCellWithLabelAndSubLabel * countryCell = [ tableView dequeueReusableCellWithIdentifier : @ "SettingsCellWithLabelAndSubLabel" forIndexPath : indexPath ] ;
countryCell . label . text = @ "Select your country" ;
countryCell . sublabel . text = countryName ;
countryCell . accessoryType = UITableViewCellAccessoryDisclosureIndicator ;
cell = countryCell ;
} else {
// there is a selection in progress
SettingsCellWithPicker * pickerCell = [ tableView dequeueReusableCellWithIdentifier : @ "SettingsCellWithPicker" forIndexPath : indexPath ] ;
// display a picker
pickerCell . pickerView . delegate = self ;
pickerCell . pickerView . dataSource = self ;
if ( countryName . length > 0 ) {
dispatch_async ( dispatch_get _main _queue ( ) , ^ {
[ pickerCell . pickerView selectRow : index inComponent : 0 animated : NO ] ;
} ) ;
}
cell = pickerCell ;
}
}
2015-01-26 17:06:13 +00:00
2014-11-05 11:57:24 +00:00
} 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 ) {
2015-03-13 12:35:22 +00:00
cell = [ tableView dequeueReusableCellWithIdentifier : @ "ClearCacheCell" ] ;
2014-12-19 09:02:09 +00:00
if ( ! cell ) {
cell = [ [ UITableViewCell alloc ] initWithStyle : UITableViewCellStyleDefault reuseIdentifier : @ "ClearCacheCell" ] ;
}
2015-04-13 16:00:39 +00:00
cell . textLabel . text = [ NSString stringWithFormat : @ "Clear Cache (%@)" , [ NSByteCountFormatter stringFromByteCount : self . cachesSize countStyle : NSByteCountFormatterCountStyleFile ] ] ;
2014-12-19 09:02:09 +00:00
cell . textLabel . textAlignment = NSTextAlignmentCenter ;
cell . textLabel . textColor = [ AppDelegate theDelegate ] . masterTabBarController . tabBar . tintColor ;
2015-01-19 07:58:45 +00:00
} else if ( indexPath . row = = SETTINGS_SECTION _ROOMS _SET _CACHE _SIZE _INDEX ) {
2015-01-27 16:24:55 +00:00
maxCacheSizeCell = [ tableView dequeueReusableCellWithIdentifier : @ "SettingsCellWithLabelAndSilder" forIndexPath : indexPath ] ;
2015-01-21 14:08:29 +00:00
maxCacheSizeCell . settingSlider . minimumValue = 0 ;
2015-04-13 16:00:39 +00:00
maxCacheSizeCell . settingSlider . maximumValue = self . maxAllowedCachesSize ;
maxCacheSizeCell . settingSlider . value = self . currentMaxCachesSize ;
2015-01-19 07:58:45 +00:00
2015-01-21 14:08:29 +00:00
[ self onSliderValueChange : maxCacheSizeCell . settingSlider ] ;
2015-01-27 16:24:55 +00:00
cell = maxCacheSizeCell ;
2014-12-17 13:40:39 +00:00
} else {
2015-01-23 17:45:13 +00:00
SettingsCellWithSwitch * roomsSettingCell = [ tableView dequeueReusableCellWithIdentifier : @ "SettingsCellWithSwitch" forIndexPath : indexPath ] ;
2014-12-19 09:02:09 +00:00
if ( indexPath . row = = SETTINGS_SECTION _ROOMS _DISPLAY _ALL _EVENTS _INDEX ) {
roomsSettingCell . settingLabel . text = @ "Display all events" ;
2015-04-15 12:46:54 +00:00
roomsSettingCell . settingSwitch . on = [ _settings showAllEventsInRoomHistory ] ;
2014-12-19 09:02:09 +00:00
allEventsSwitch = roomsSettingCell . settingSwitch ;
2015-04-10 15:31:47 +00:00
} else if ( indexPath . row = = SETTINGS_SECTION _ROOMS _SHOW _REDACTIONS _INDEX ) {
roomsSettingCell . settingLabel . text = @ "Show redactions" ;
2015-04-15 12:46:54 +00:00
roomsSettingCell . settingSwitch . on = [ _settings showRedactionsInRoomHistory ] ;
2015-02-20 13:43:11 +00:00
redactionsSwitch = roomsSettingCell . settingSwitch ;
2015-04-10 14:55:28 +00:00
} else if ( indexPath . row = = SETTINGS_SECTION _ROOMS _SHOW _UNSUPPORTED _EVENTS _INDEX ) {
roomsSettingCell . settingLabel . text = @ "Show unsupported events" ;
2015-04-15 12:46:54 +00:00
roomsSettingCell . settingSwitch . on = [ _settings showUnsupportedEventsInRoomHistory ] ;
2015-01-28 17:39:23 +00:00
unsupportedEventsSwitch = roomsSettingCell . settingSwitch ;
2014-12-19 09:02:09 +00:00
} else if ( indexPath . row = = SETTINGS_SECTION _ROOMS _SORT _MEMBERS _INDEX ) {
roomsSettingCell . settingLabel . text = @ "Sort members by last seen time" ;
2015-04-15 12:46:54 +00:00
roomsSettingCell . settingSwitch . on = [ _settings sortRoomMembersUsingLastSeenTime ] ;
2014-12-19 09:02:09 +00:00
sortMembersSwitch = roomsSettingCell . settingSwitch ;
} else if ( indexPath . row = = SETTINGS_SECTION _ROOMS _DISPLAY _LEFT _MEMBERS _INDEX ) {
roomsSettingCell . settingLabel . text = @ "Display left members" ;
2015-04-15 12:46:54 +00:00
roomsSettingCell . settingSwitch . on = [ _settings showLeftMembersInRoomMemberList ] ;
2014-12-19 09:02:09 +00:00
displayLeftMembersSwitch = roomsSettingCell . settingSwitch ;
}
cell = roomsSettingCell ;
2014-11-05 11:57:24 +00:00
}
2014-10-29 16:48:11 +00:00
} else if ( indexPath . section = = SETTINGS_SECTION _CONFIGURATION _INDEX ) {
2015-02-04 10:06:56 +00:00
SettingsCellWithTextView * configurationCell = [ tableView dequeueReusableCellWithIdentifier : @ "SettingsCellWithTextView" forIndexPath : indexPath ] ;
2015-02-04 17:18:38 +00:00
NSString * appVersion = [ AppDelegate theDelegate ] . appVersion ;
NSString * build = [ AppDelegate theDelegate ] . build ;
if ( build . length ) {
build = [ NSString stringWithFormat : kBuildFormatText , build ] ;
}
2015-04-22 12:52:51 +00:00
configurationCell . settingTextView . text = [ NSString stringWithFormat : kConfigurationFormatText , appVersion , MatrixKitVersion , MatrixSDKVersion , build , self . mxSession . matrixRestClient . homeserver , self . mxSession . matrixRestClient . identityServer , self . mxSession . myUser . userId ] ;
2015-01-27 16:24:55 +00:00
cell = configurationCell ;
2014-10-29 16:48:11 +00:00
} else if ( indexPath . section = = SETTINGS_SECTION _COMMANDS _INDEX ) {
2015-02-04 10:06:56 +00:00
SettingsCellWithTextView * commandsCell = [ tableView dequeueReusableCellWithIdentifier : @ "SettingsCellWithTextView" forIndexPath : indexPath ] ;
commandsCell . settingTextView . text = kCommandsDescriptionText ;
2014-10-29 16:48:11 +00:00
cell = commandsCell ;
}
return cell ;
}
2015-01-23 17:45:13 +00:00
# pragma mark - UITableView 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 ) ) {
// clear caches
2015-04-30 14:19:12 +00:00
[ [ AppDelegate theDelegate ] reloadMatrixSessions : YES ] ;
2015-01-23 17:45:13 +00:00
}
2015-01-29 17:33:39 +00:00
else if ( indexPath . section = = SETTINGS_SECTION _CONTACTS _INDEX ) {
if ( indexPath . row = = countryCodeRowIndex ) {
isSelectingCountryCode = YES ;
dispatch_async ( dispatch_get _main _queue ( ) , ^ {
[ self . tableView reloadData ] ;
} ) ;
}
}
2015-01-23 17:45:13 +00:00
[ aTableView deselectRowAtIndexPath : indexPath animated : YES ] ;
}
}
2014-10-31 17:54:32 +00:00
# pragma mark - UIImagePickerControllerDelegate
- ( void ) imagePickerController : ( UIImagePickerController * ) picker didFinishPickingMediaWithInfo : ( NSDictionary * ) info {
UIImage * selectedImage = [ info objectForKey : UIImagePickerControllerOriginalImage ] ;
if ( selectedImage ) {
2015-01-23 17:45:13 +00:00
[ self updateUserPictureButton : selectedImage ] ;
2015-01-05 13:25:11 +00:00
isAvatarUpdated = YES ;
_saveUserInfoButton . enabled = YES ;
2014-10-31 17:54:32 +00:00
}
[ self dismissMediaPicker ] ;
}
- ( void ) imagePickerControllerDidCancel : ( UIImagePickerController * ) picker {
[ self dismissMediaPicker ] ;
}
- ( void ) dismissMediaPicker {
2014-11-10 14:26:37 +00:00
[ [ AppDelegate theDelegate ] . masterTabBarController dismissMediaPicker ] ;
2014-10-31 17:54:32 +00:00
}
2015-01-29 17:33:39 +00:00
# pragma mark UIPickerViewDataSource
- ( NSInteger ) numberOfComponentsInPickerView : ( UIPickerView * ) pickerView {
return 1 ;
}
- ( NSInteger ) pickerView : ( UIPickerView * ) pickerView numberOfRowsInComponent : ( NSInteger ) component {
return [ countryCodes count ] ;
}
# pragma mark UIPickerViewDelegate
- ( NSString * ) pickerView : ( UIPickerView * ) pickerView titleForRow : ( NSInteger ) row forComponent : ( NSInteger ) component {
return [ [ countryCodes objectAtIndex : row ] valueForKey : @ "country" ] ;
}
- ( void ) pickerView : ( UIPickerView * ) pickerView didSelectRow : ( NSInteger ) row inComponent : ( NSInteger ) component {
// sanity check
if ( ( row >= 0 ) && ( row < countryCodes . count ) ) {
NSDictionary * dict = [ countryCodes objectAtIndex : row ] ;
selectedCountryCode = [ dict valueForKey : @ "id" ] ;
}
isSelectingCountryCode = NO ;
dispatch_async ( dispatch_get _main _queue ( ) , ^ {
[ self . tableView reloadData ] ;
} ) ;
}
2015-04-13 16:00:39 +00:00
# pragma mark Cache handling
// return the MX cache size in bytes
- ( NSUInteger ) MXCacheSize {
if ( self . mxSession . store && [ self . mxSession . store isKindOfClass : [ MXFileStore class ] ] ) {
MXFileStore * fileStore = ( MXFileStore * ) self . mxSession . store ;
return fileStore . diskUsage ;
}
return 0 ;
}
// return the sum of the caches ( MX cache + media cache . . . ) in bytes
- ( NSUInteger ) cachesSize {
return self . MXCacheSize + [ MXKMediaManager cacheSize ] ;
}
// defines the min allow cache size in bytes
- ( NSUInteger ) minCachesSize {
// add a 50 MB margin to avoid cache file deletion
return self . MXCacheSize + [ MXKMediaManager minCacheSize ] + 50 * 1024 * 1024 ;
}
// defines the current max caches size in bytes
- ( NSUInteger ) currentMaxCachesSize {
return self . MXCacheSize + [ MXKMediaManager currentMaxCacheSize ] ;
}
- ( void ) setCurrentMaxCachesSize : ( NSUInteger ) maxCachesSize {
[ MXKMediaManager setCurrentMaxCacheSize : maxCachesSize - self . MXCacheSize ] ;
}
// defines the max allowed caches size in bytes
- ( NSUInteger ) maxAllowedCachesSize {
return self . MXCacheSize + [ MXKMediaManager maxAllowedCacheSize ] ;
}
2014-10-03 17:26:39 +00:00
@ end