From 94101a1ff57cfce48d06d84ab634ad33f0f4b9b1 Mon Sep 17 00:00:00 2001 From: giomfo Date: Wed, 23 Nov 2016 13:14:59 +0100 Subject: [PATCH] User Settings: List user's devices - support the device deletion. - support the device renaming. --- Vector.xcodeproj/project.pbxproj | 18 + Vector/Assets/en.lproj/Vector.strings | 11 + .../ViewController/SettingsViewController.h | 4 +- .../ViewController/SettingsViewController.m | 178 +++++++- Vector/Views/DeviceView/DeviceView.h | 62 +++ Vector/Views/DeviceView/DeviceView.m | 416 ++++++++++++++++++ Vector/Views/DeviceView/DeviceView.xib | 117 +++++ 7 files changed, 795 insertions(+), 11 deletions(-) create mode 100644 Vector/Views/DeviceView/DeviceView.h create mode 100644 Vector/Views/DeviceView/DeviceView.m create mode 100644 Vector/Views/DeviceView/DeviceView.xib diff --git a/Vector.xcodeproj/project.pbxproj b/Vector.xcodeproj/project.pbxproj index d32816e9a..01aab69bd 100644 --- a/Vector.xcodeproj/project.pbxproj +++ b/Vector.xcodeproj/project.pbxproj @@ -274,6 +274,8 @@ F094AA351B78E42600B1FBBF /* RecentsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F094AA231B78E42600B1FBBF /* RecentsViewController.m */; }; F094AA371B78E42600B1FBBF /* RoomViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F094AA271B78E42600B1FBBF /* RoomViewController.m */; }; F094AA381B78E42600B1FBBF /* SettingsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F094AA291B78E42600B1FBBF /* SettingsViewController.m */; }; + F09617B71DE49BAD00093E9D /* DeviceView.m in Sources */ = {isa = PBXBuildFile; fileRef = F09617B51DE49BAD00093E9D /* DeviceView.m */; }; + F09617B81DE49BAD00093E9D /* DeviceView.xib in Resources */ = {isa = PBXBuildFile; fileRef = F09617B61DE49BAD00093E9D /* DeviceView.xib */; }; F0989F4E1CD7769000FA6EAC /* ForgotPasswordInputsView.m in Sources */ = {isa = PBXBuildFile; fileRef = F0989F4C1CD7769000FA6EAC /* ForgotPasswordInputsView.m */; }; F0989F4F1CD7769000FA6EAC /* ForgotPasswordInputsView.xib in Resources */ = {isa = PBXBuildFile; fileRef = F0989F4D1CD7769000FA6EAC /* ForgotPasswordInputsView.xib */; }; F09E24ED1C6DE24900D39503 /* RoomMemberTitleView.m in Sources */ = {isa = PBXBuildFile; fileRef = F09E24EB1C6DE24900D39503 /* RoomMemberTitleView.m */; }; @@ -725,6 +727,9 @@ F094AA271B78E42600B1FBBF /* RoomViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RoomViewController.m; sourceTree = ""; }; F094AA281B78E42600B1FBBF /* SettingsViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SettingsViewController.h; sourceTree = ""; }; F094AA291B78E42600B1FBBF /* SettingsViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SettingsViewController.m; sourceTree = ""; }; + F09617B41DE49BAD00093E9D /* DeviceView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DeviceView.h; sourceTree = ""; }; + F09617B51DE49BAD00093E9D /* DeviceView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DeviceView.m; sourceTree = ""; }; + F09617B61DE49BAD00093E9D /* DeviceView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = DeviceView.xib; sourceTree = ""; }; F0989F4B1CD7769000FA6EAC /* ForgotPasswordInputsView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ForgotPasswordInputsView.h; sourceTree = ""; }; F0989F4C1CD7769000FA6EAC /* ForgotPasswordInputsView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ForgotPasswordInputsView.m; sourceTree = ""; }; F0989F4D1CD7769000FA6EAC /* ForgotPasswordInputsView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = ForgotPasswordInputsView.xib; sourceTree = ""; }; @@ -993,6 +998,7 @@ F001D7521B8207C000A162C3 /* Views */ = { isa = PBXGroup; children = ( + F09617B31DE49BAD00093E9D /* DeviceView */, F00ACC5C1DDB11CE0093F646 /* EncryptionInfoView */, F083C4591D9E9C2400E5246C /* Search */, F0CC4DBC1C4E26FA003BBE45 /* MediaAlbum */, @@ -1563,6 +1569,16 @@ path = ViewController; sourceTree = ""; }; + F09617B31DE49BAD00093E9D /* DeviceView */ = { + isa = PBXGroup; + children = ( + F09617B41DE49BAD00093E9D /* DeviceView.h */, + F09617B51DE49BAD00093E9D /* DeviceView.m */, + F09617B61DE49BAD00093E9D /* DeviceView.xib */, + ); + path = DeviceView; + sourceTree = ""; + }; F09E24E91C6DE24900D39503 /* RoomMember */ = { isa = PBXGroup; children = ( @@ -1954,6 +1970,7 @@ F03BF6AC1D8BF5B1002EF6A7 /* placeholder@3x.png in Resources */, F094A9B91B78D8F000B1FBBF /* LaunchScreen.xib in Resources */, F03BF6A51D8BF5B1002EF6A7 /* notifications@2x.png in Resources */, + F09617B81DE49BAD00093E9D /* DeviceView.xib in Resources */, F083C4951D9EAFC500E5246C /* file_photo_icon@3x.png in Resources */, F083C4901D9EAFC500E5246C /* file_music_icon.png in Resources */, F03BF6C11D8BF5B1002EF6A7 /* search_bg@3x.png in Resources */, @@ -2150,6 +2167,7 @@ F09EAFA11DD2109B009C7EFB /* RoomIncomingEncryptedTextMsgWithoutSenderNameBubbleCell.m in Sources */, F09EAF9F1DD2109B009C7EFB /* RoomIncomingEncryptedTextMsgWithoutSenderInfoBubbleCell.m in Sources */, F0AC734A1DA2A6130011DAEE /* RoomFilesSearchViewController.m in Sources */, + F09617B71DE49BAD00093E9D /* DeviceView.m in Sources */, F083C4581D9E982900E5246C /* FilesSearchCellData.m in Sources */, F083C45D1D9E9C2400E5246C /* FilesSearchTableViewCell.m in Sources */, F05895001B8B7E6600B73E85 /* RoomBubbleCellData.m in Sources */, diff --git a/Vector/Assets/en.lproj/Vector.strings b/Vector/Assets/en.lproj/Vector.strings index 5a5b3ff0b..b588ab030 100644 --- a/Vector/Assets/en.lproj/Vector.strings +++ b/Vector/Assets/en.lproj/Vector.strings @@ -43,6 +43,7 @@ "active_call" = "Active Call"; "active_call_details" = "Active Call (%@)"; "later" = "Later"; +"rename" = "Rename"; // Authentication "auth_login" = "Log in"; @@ -308,6 +309,16 @@ "settings_crypto_device_id" = "\nDevice ID: "; "settings_crypto_device_key" = "\nDevice key: "; +// Devices +"device_details_title" = "Device information\n"; +"device_details_name" = "Name\n"; +"device_details_identifier" = "Device ID\n"; +"device_details_last_seen" = "Last seen\n"; +"device_details_last_seen_format" = "%@ @ %@\n"; +"device_details_rename_prompt_message" = "Device name:"; +"device_details_delete_prompt_title" = "Authentication"; +"device_details_delete_prompt_message" = "This operation requires additional authentication.\nTo continue, please enter your password."; + // Room Details "room_details_title" = "Room Details"; "room_details_people" = "Members"; diff --git a/Vector/ViewController/SettingsViewController.h b/Vector/ViewController/SettingsViewController.h index 9d4e9229b..e7cfd9731 100644 --- a/Vector/ViewController/SettingsViewController.h +++ b/Vector/ViewController/SettingsViewController.h @@ -16,9 +16,11 @@ #import +#import "DeviceView.h" + #import "MediaPickerViewController.h" -@interface SettingsViewController : MXKViewController +@interface SettingsViewController : MXKViewController @property (nonatomic) IBOutlet UITableView *tableView; diff --git a/Vector/ViewController/SettingsViewController.m b/Vector/ViewController/SettingsViewController.m index b34230ee4..eab8de5cc 100644 --- a/Vector/ViewController/SettingsViewController.m +++ b/Vector/ViewController/SettingsViewController.m @@ -39,6 +39,7 @@ enum { SETTINGS_SECTION_CONTACTS_INDEX, #endif SETTINGS_SECTION_LABS_INDEX, + SETTINGS_SECTION_DEVICES_INDEX, SETTINGS_SECTION_CRYPTOGRAPHY_INDEX, SETTINGS_SECTION_COUNT }; @@ -122,6 +123,10 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); NSInteger userSettingsNightModeSepIndex; NSInteger userSettingsNightModeIndex; + // Devices + NSMutableArray *devicesArray; + DeviceView *deviceView; + // Observe kAppDelegateDidTapStatusBarNotification to handle tap on clock status bar. id kAppDelegateDidTapStatusBarNotificationObserver; @@ -304,6 +309,9 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); // Refresh the current device information in parallel [self loadCurrentDeviceInformation]; + // Refresh devices in parallel + [self loadDevices]; + // Observe kAppDelegateDidTapStatusBarNotificationObserver. kAppDelegateDidTapStatusBarNotificationObserver = [[NSNotificationCenter defaultCenter] addObserverForName:kAppDelegateDidTapStatusBarNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notif) { @@ -387,6 +395,12 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); [[NSNotificationCenter defaultCenter] removeObserver:self]; onReadyToDestroyHandler = nil; + + if (deviceView) + { + [deviceView removeFromSuperview]; + deviceView = nil; + } } -(void)setNewEmailEditingEnabled:(BOOL)newEmailEditingEnabled @@ -590,6 +604,108 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); return cryptoInformationString; } +- (void)loadDevices +{ + // Refresh the account devices list + MXKAccount* account = [MXKAccountManager sharedManager].activeAccounts.firstObject; + [account.mxRestClient devices:^(NSArray *devices) { + + if (devices) + { + devicesArray = [NSMutableArray arrayWithArray:devices]; + + // Sort devices according to the last seen date. + NSComparator comparator = ^NSComparisonResult(MXDevice *deviceA, MXDevice *deviceB) { + + if (deviceA.lastSeenTs > deviceB.lastSeenTs) + { + return NSOrderedAscending; + } + if (deviceA.lastSeenTs < deviceB.lastSeenTs) + { + return NSOrderedDescending; + } + + return NSOrderedSame; + }; + + // Sort devices list + [devicesArray sortUsingComparator:comparator]; + } + else + { + devicesArray = nil; + + } + + // Refresh all the table (A slide down animation is observed when we limit the refresh to the concerned section). + // Note: The use of 'reloadData' handles the case where the account has been logged out. + [self.tableView reloadData]; + + } failure:^(NSError *error) { + + // Display the data that has been loaded last time + // Note: The use of 'reloadData' handles the case where the account has been logged out. + [self.tableView reloadData]; + + }]; +} + +- (void)showDeviceDetails:(MXDevice *)device +{ + [self dismissKeyboard]; + + deviceView = [[DeviceView alloc] initWithDevice:device andMatrixSession:self.mainSession]; + deviceView.delegate = self; + + // Add the view and define edge constraints + [self.view addSubview:deviceView]; + + [self.view addConstraint:[NSLayoutConstraint constraintWithItem:deviceView + attribute:NSLayoutAttributeTop + relatedBy:NSLayoutRelationEqual + toItem:self.topLayoutGuide + attribute:NSLayoutAttributeBottom + multiplier:1.0f + constant:0.0f]]; + + [self.view addConstraint:[NSLayoutConstraint constraintWithItem:deviceView + attribute:NSLayoutAttributeBottom + relatedBy:NSLayoutRelationEqual + toItem:self.bottomLayoutGuide + attribute:NSLayoutAttributeTop + multiplier:1.0f + constant:0.0f]]; + + [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view + attribute:NSLayoutAttributeLeading + relatedBy:NSLayoutRelationEqual + toItem:deviceView + attribute:NSLayoutAttributeLeading + multiplier:1.0f + constant:0.0f]]; + + [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view + attribute:NSLayoutAttributeTrailing + relatedBy:NSLayoutRelationEqual + toItem:deviceView + attribute:NSLayoutAttributeTrailing + multiplier:1.0f + constant:0.0f]]; + [self.view setNeedsUpdateConstraints]; +} + +- (void)deviceView:(DeviceView*)deviceView presentMXKAlert:(MXKAlert*)alert +{ + [self dismissKeyboard]; + [alert showInViewController:self]; +} + +- (void)deviceViewDidUpdate:(DeviceView*)deviceView +{ + [self loadDevices]; +} + #pragma mark - Segues - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender @@ -669,6 +785,10 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); { count = LABS_COUNT; } + else if (section == SETTINGS_SECTION_DEVICES_INDEX) + { + count = devicesArray.count; + } else if (section == SETTINGS_SECTION_CRYPTOGRAPHY_INDEX) { // Check whether this section is visible. @@ -958,8 +1078,8 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); if (!privacyPolicyCell) { privacyPolicyCell = [[MXKTableViewCell alloc] init]; - privacyPolicyCell.textLabel.font = [UIFont systemFontOfSize:17]; } + privacyPolicyCell.textLabel.font = [UIFont systemFontOfSize:17]; NSString *ignoredUserId; if (indexPath.row < session.ignoredUsers.count) @@ -992,8 +1112,8 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); if (!configCell) { configCell = [[MXKTableViewCell alloc] init]; - configCell.textLabel.font = [UIFont systemFontOfSize:17]; } + configCell.textLabel.font = [UIFont systemFontOfSize:17]; NSString *configFormat = [NSString stringWithFormat:@"%@\n%@\n%@", [NSBundle mxk_localizedStringForKey:@"settings_config_user_id"], [NSBundle mxk_localizedStringForKey:@"settings_config_home_server"], [NSBundle mxk_localizedStringForKey:@"settings_config_identity_server"]]; @@ -1011,8 +1131,8 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); if (!versionCell) { versionCell = [[MXKTableViewCell alloc] init]; - versionCell.textLabel.font = [UIFont systemFontOfSize:17]; } + versionCell.textLabel.font = [UIFont systemFontOfSize:17]; NSString* appVersion = [AppDelegate theDelegate].appVersion; NSString* build = [AppDelegate theDelegate].build; @@ -1028,8 +1148,8 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); if (!versionCell) { versionCell = [[MXKTableViewCell alloc] init]; - versionCell.textLabel.font = [UIFont systemFontOfSize:17]; } + versionCell.textLabel.font = [UIFont systemFontOfSize:17]; versionCell.textLabel.text = [NSString stringWithFormat:NSLocalizedStringFromTable(@"settings_olm_version", @"Vector", nil), OLMKitVersionString()]; versionCell.textLabel.textColor = kVectorTextColorBlack; @@ -1042,8 +1162,8 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); if (!termAndConditionCell) { termAndConditionCell = [[MXKTableViewCell alloc] init]; - termAndConditionCell.textLabel.font = [UIFont systemFontOfSize:17]; } + termAndConditionCell.textLabel.font = [UIFont systemFontOfSize:17]; termAndConditionCell.textLabel.text = NSLocalizedStringFromTable(@"settings_term_conditions", @"Vector", nil); termAndConditionCell.textLabel.textColor = kVectorTextColorBlack; @@ -1056,8 +1176,8 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); if (!copyrightCell) { copyrightCell = [[MXKTableViewCell alloc] init]; - copyrightCell.textLabel.font = [UIFont systemFontOfSize:17]; } + copyrightCell.textLabel.font = [UIFont systemFontOfSize:17]; copyrightCell.textLabel.text = NSLocalizedStringFromTable(@"settings_copyright", @"Vector", nil); copyrightCell.textLabel.textColor = kVectorTextColorBlack; @@ -1070,8 +1190,8 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); if (!privacyPolicyCell) { privacyPolicyCell = [[MXKTableViewCell alloc] init]; - privacyPolicyCell.textLabel.font = [UIFont systemFontOfSize:17]; } + privacyPolicyCell.textLabel.font = [UIFont systemFontOfSize:17]; privacyPolicyCell.textLabel.text = NSLocalizedStringFromTable(@"settings_privacy_policy", @"Vector", nil); privacyPolicyCell.textLabel.textColor = kVectorTextColorBlack; @@ -1084,8 +1204,8 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); if (!thirdPartyCell) { thirdPartyCell = [[MXKTableViewCell alloc] init]; - thirdPartyCell.textLabel.font = [UIFont systemFontOfSize:17]; } + thirdPartyCell.textLabel.font = [UIFont systemFontOfSize:17]; thirdPartyCell.textLabel.text = NSLocalizedStringFromTable(@"settings_third_party_notices", @"Vector", nil); thirdPartyCell.textLabel.textColor = kVectorTextColorBlack; @@ -1166,6 +1286,34 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); cell = labelAndSwitchCell; } } + else if (section == SETTINGS_SECTION_DEVICES_INDEX) + { + MXKTableViewCell *deviceCell = [tableView dequeueReusableCellWithIdentifier:[MXKTableViewCell defaultReuseIdentifier]]; + if (!deviceCell) + { + deviceCell = [[MXKTableViewCell alloc] init]; + } + + if (row < devicesArray.count) + { + NSString *name = devicesArray[row].displayName; + NSString *deviceId = devicesArray[row].deviceId; + deviceCell.textLabel.text = (name.length ? [NSString stringWithFormat:@"%@ [%@]", name, deviceId] : [NSString stringWithFormat:@"[%@]", deviceId]); + deviceCell.textLabel.numberOfLines = 0; + + if ([deviceId isEqualToString:self.mainSession.matrixRestClient.credentials.deviceId]) + { + deviceCell.textLabel.font = [UIFont boldSystemFontOfSize:17]; + } + else + { + deviceCell.textLabel.font = [UIFont systemFontOfSize:17]; + } + } + deviceCell.textLabel.textColor = kVectorTextColorBlack; + + cell = deviceCell; + } else if (section == SETTINGS_SECTION_CRYPTOGRAPHY_INDEX) { MXKTableViewCell *cryptoCell = [tableView dequeueReusableCellWithIdentifier:[MXKTableViewCell defaultReuseIdentifier]]; @@ -1221,6 +1369,10 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); { return NSLocalizedStringFromTable(@"settings_labs", @"Vector", nil); } + else if (section == SETTINGS_SECTION_DEVICES_INDEX) + { + return NSLocalizedStringFromTable(@"settings_devices", @"Vector", nil); + } else if (section == SETTINGS_SECTION_CRYPTOGRAPHY_INDEX) { // Check whether this section is visible @@ -1375,8 +1527,7 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); [currentAlert showInViewController:self]; } } - else - if (section == SETTINGS_SECTION_OTHER_INDEX) + else if (section == SETTINGS_SECTION_OTHER_INDEX) { if (row == OTHER_COPYRIGHT_INDEX) { @@ -1417,6 +1568,13 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); self.newEmailEditingEnabled = YES; } } + else if (section == SETTINGS_SECTION_DEVICES_INDEX) + { + if (row < devicesArray.count) + { + [self showDeviceDetails:devicesArray[row]]; + } + } [aTableView deselectRowAtIndexPath:indexPath animated:YES]; } diff --git a/Vector/Views/DeviceView/DeviceView.h b/Vector/Views/DeviceView/DeviceView.h new file mode 100644 index 000000000..f28d4e263 --- /dev/null +++ b/Vector/Views/DeviceView/DeviceView.h @@ -0,0 +1,62 @@ +/* + Copyright 2016 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 + +@class DeviceView; +@protocol DeviceViewDelegate + +/** + Tells the delegate that a MXKAlert must be presented. + + @param deviceView the device view. + @param alert the alert to present. + */ +- (void)deviceView:(DeviceView*)deviceView presentMXKAlert:(MXKAlert*)alert; + +@optional + +/** + Tells the delegate that the device was updated (renamed, removed...). + + @param deviceView the device view. + */ +- (void)deviceViewDidUpdate:(DeviceView*)deviceView; + +@end + +@interface DeviceView : UIView + +@property (weak, nonatomic) IBOutlet UIView *bgView; +@property (weak, nonatomic) IBOutlet UIView *containerView; +@property (weak, nonatomic) IBOutlet UITextView *textView; +@property (weak, nonatomic) IBOutlet UIButton *cancelButton; +@property (weak, nonatomic) IBOutlet UIButton *renameButton; +@property (weak, nonatomic) IBOutlet UIButton *deleteButton; +@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator; + +/** + Initialize a device view to display the information of a user's device. + */ +- (instancetype)initWithDevice:(MXDevice*)device andMatrixSession:(MXSession*)session; + +/** + The delegate. + */ +@property (nonatomic) id delegate; + +@end + diff --git a/Vector/Views/DeviceView/DeviceView.m b/Vector/Views/DeviceView/DeviceView.m new file mode 100644 index 000000000..e021dc8b5 --- /dev/null +++ b/Vector/Views/DeviceView/DeviceView.m @@ -0,0 +1,416 @@ +/* + Copyright 2016 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 "DeviceView.h" + +#import "AppDelegate.h" + +#import "VectorDesignValues.h" + +static NSAttributedString *verticalWhitespace = nil; + +@interface DeviceView () +{ + /** + The displayed device + */ + MXDevice *mxDevice; + + /** + The matrix session. + */ + MXSession *mxSession; + + /** + The current alert + */ + MXKAlert *currentAlert; +} +@end + +@implementation DeviceView + +- (void)awakeFromNib +{ + [super awakeFromNib]; + + // Add tap recognizer to discard the view on bg view tap + UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onBgViewTap:)]; + [tap setNumberOfTouchesRequired:1]; + [tap setNumberOfTapsRequired:1]; + [tap setDelegate:self]; + [self.bgView addGestureRecognizer:tap]; + + // Add shadow on event details view + _containerView.layer.cornerRadius = 5; + _containerView.layer.shadowOffset = CGSizeMake(0, 1); + _containerView.layer.shadowOpacity = 0.5f; + + // Localize string + [_cancelButton setTitle:[NSBundle mxk_localizedStringForKey:@"ok"] forState:UIControlStateNormal]; + [_cancelButton setTitle:[NSBundle mxk_localizedStringForKey:@"ok"] forState:UIControlStateHighlighted]; + + [_renameButton setTitle:[NSBundle mxk_localizedStringForKey:@"rename"] forState:UIControlStateNormal]; + [_renameButton setTitle:[NSBundle mxk_localizedStringForKey:@"rename"] forState:UIControlStateHighlighted]; + + [_deleteButton setTitle:[NSBundle mxk_localizedStringForKey:@"delete"] forState:UIControlStateNormal]; + [_deleteButton setTitle:[NSBundle mxk_localizedStringForKey:@"delete"] forState:UIControlStateHighlighted]; +} + +- (void)removeFromSuperview +{ + if (currentAlert) + { + [currentAlert dismiss:NO]; + currentAlert = nil; + } + + [super removeFromSuperview]; +} + +- (instancetype)initWithDevice:(MXDevice*)device andMatrixSession:(MXSession*)session +{ + NSArray *nibViews = [[NSBundle bundleForClass:[DeviceView class]] loadNibNamed:NSStringFromClass([DeviceView class]) + owner:nil + options:nil]; + self = nibViews.firstObject; + if (self) + { + mxDevice = device; + mxSession = session; + + [self setTranslatesAutoresizingMaskIntoConstraints: NO]; + + if (mxDevice) + { + // Device information + NSMutableAttributedString *deviceInformationString = [[NSMutableAttributedString alloc] + initWithString:NSLocalizedStringFromTable(@"device_details_title", @"Vector", nil) + attributes:@{NSForegroundColorAttributeName : kVectorTextColorBlack, + NSFontAttributeName: [UIFont boldSystemFontOfSize:15]}]; + [deviceInformationString appendAttributedString:[DeviceView verticalWhitespace]]; + + [deviceInformationString appendAttributedString:[[NSMutableAttributedString alloc] + initWithString:NSLocalizedStringFromTable(@"device_details_name", @"Vector", nil) + attributes:@{NSForegroundColorAttributeName : kVectorTextColorBlack, + NSFontAttributeName: [UIFont boldSystemFontOfSize:14]}]]; + [deviceInformationString appendAttributedString:[[NSMutableAttributedString alloc] + initWithString:device.displayName.length ? device.displayName : @"" + attributes:@{NSForegroundColorAttributeName : kVectorTextColorBlack, + NSFontAttributeName: [UIFont systemFontOfSize:14]}]]; + [deviceInformationString appendAttributedString:[DeviceView verticalWhitespace]]; + + [deviceInformationString appendAttributedString:[[NSMutableAttributedString alloc] + initWithString:NSLocalizedStringFromTable(@"device_details_identifier", @"Vector", nil) + attributes:@{NSForegroundColorAttributeName : kVectorTextColorBlack, + NSFontAttributeName: [UIFont boldSystemFontOfSize:14]}]]; + [deviceInformationString appendAttributedString:[[NSMutableAttributedString alloc] + initWithString:device.deviceId + attributes:@{NSForegroundColorAttributeName : kVectorTextColorBlack, + NSFontAttributeName: [UIFont systemFontOfSize:14]}]]; + [deviceInformationString appendAttributedString:[DeviceView verticalWhitespace]]; + + [deviceInformationString appendAttributedString:[[NSMutableAttributedString alloc] + initWithString:NSLocalizedStringFromTable(@"device_details_last_seen", @"Vector", nil) + attributes:@{NSForegroundColorAttributeName : kVectorTextColorBlack, + NSFontAttributeName: [UIFont boldSystemFontOfSize:14]}]]; + + NSDate *lastSeenDate = [NSDate dateWithTimeIntervalSince1970:device.lastSeenTs/1000]; + + NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; + [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:[[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0]]]; + [dateFormatter setDateStyle:NSDateFormatterShortStyle]; + [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; + [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; + + NSString *lastSeen = [NSString stringWithFormat:NSLocalizedStringFromTable(@"device_details_last_seen_format", @"Vector", nil), device.lastSeenIp, [dateFormatter stringFromDate:lastSeenDate]]; + + [deviceInformationString appendAttributedString:[[NSMutableAttributedString alloc] + initWithString:lastSeen + attributes:@{NSForegroundColorAttributeName : kVectorTextColorBlack, + NSFontAttributeName: [UIFont systemFontOfSize:14]}]]; + [deviceInformationString appendAttributedString:[DeviceView verticalWhitespace]]; + + self.textView.attributedText = deviceInformationString; + } + else + { + _textView.text = nil; + } + + // Hide potential activity indicator + [_activityIndicator stopAnimating]; + } + + return self; +} + +- (void)dealloc +{ + mxDevice = nil; + mxSession = nil; +} + ++ (NSAttributedString *)verticalWhitespace +{ + if (verticalWhitespace == nil) + { + verticalWhitespace = [[NSAttributedString alloc] initWithString:@"\n\n" attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:4]}]; + } + return verticalWhitespace; +} + +#pragma mark - Actions + +- (IBAction)onBgViewTap:(UITapGestureRecognizer*)sender +{ + [self removeFromSuperview]; +} + +- (IBAction)onButtonPressed:(id)sender +{ + if (sender == _cancelButton) + { + [self removeFromSuperview]; + } + else if (sender == _renameButton) + { + [self renameDevice]; + } + else if (sender == _deleteButton) + { + [self deleteDevice]; + } +} + +#pragma mark - + +- (void)renameDevice +{ + if (!self.delegate) + { + // Ignore + NSLog(@"[DeviceView] Rename device failed, delegate is missing"); + return; + } + + [currentAlert dismiss:NO]; + + __weak typeof(self) weakSelf = self; + + // Prompt the user to enter a device name. + currentAlert = [[MXKAlert alloc] initWithTitle:nil message:NSLocalizedStringFromTable(@"device_details_rename_prompt_message", @"Vector", nil) style:MXKAlertStyleAlert]; + + [currentAlert addTextFieldWithConfigurationHandler:^(UITextField *textField) { + + textField.secureTextEntry = NO; + textField.placeholder = nil; + textField.keyboardType = UIKeyboardTypeDefault; + if (weakSelf) + { + __strong __typeof(weakSelf)strongSelf = weakSelf; + textField.text = strongSelf->mxDevice.displayName; + } + }]; + + currentAlert.cancelButtonIndex = [currentAlert addActionWithTitle:[NSBundle mxk_localizedStringForKey:@"cancel"] style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) { + + if (weakSelf) + { + __strong __typeof(weakSelf)strongSelf = weakSelf; + strongSelf->currentAlert = nil; + } + + }]; + + [currentAlert addActionWithTitle:[NSBundle mxk_localizedStringForKey:@"ok"] style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) { + + if (weakSelf) + { + UITextField *textField = [alert textFieldAtIndex:0]; + + __strong __typeof(weakSelf)strongSelf = weakSelf; + strongSelf->currentAlert = nil; + + [strongSelf.activityIndicator startAnimating]; + + [strongSelf->mxSession.matrixRestClient setDeviceName:textField.text forDeviceId:strongSelf->mxDevice.deviceId success:^{ + + if (weakSelf) + { + __strong __typeof(weakSelf)strongSelf = weakSelf; + + [strongSelf.activityIndicator stopAnimating]; + + if (strongSelf.delegate && [strongSelf.delegate respondsToSelector:@selector(deviceViewDidUpdate:)]) + { + [strongSelf.delegate deviceViewDidUpdate:strongSelf]; + } + + [strongSelf removeFromSuperview]; + } + + } failure:^(NSError *error) { + + // Notify user + [[AppDelegate theDelegate] showErrorAsAlert:error]; + + if (weakSelf) + { + __strong __typeof(weakSelf)strongSelf = weakSelf; + + NSLog(@"[DeviceView] Rename device (%@) failed", strongSelf->mxDevice.deviceId); + + [strongSelf.activityIndicator stopAnimating]; + [strongSelf removeFromSuperview]; + } + + }]; + } + }]; + + [self.delegate deviceView:self presentMXKAlert:currentAlert]; +} + +- (void)deleteDevice +{ + if (!self.delegate) + { + // Ignore + NSLog(@"[DeviceView] Delete device failed, delegate is missing"); + return; + } + + // Get an authentication session to prepare device deletion + [self.activityIndicator startAnimating]; + + [mxSession.matrixRestClient getSessionToDeleteDeviceByDeviceId:mxDevice.deviceId success:^(MXAuthenticationSession *authSession) { + + // Check whether the password based type is supported + BOOL isPasswordBasedTypeSupported = NO; + for (MXLoginFlow *loginFlow in authSession.flows) + { + if ([loginFlow.type isEqualToString:kMXLoginFlowTypePassword] || [loginFlow.stages indexOfObject:kMXLoginFlowTypePassword] != NSNotFound) + { + isPasswordBasedTypeSupported = YES; + break; + } + } + + if (isPasswordBasedTypeSupported && authSession.session) + { + // Prompt for a password + [currentAlert dismiss:NO]; + + __weak typeof(self) weakSelf = self; + + // Prompt the user to enter a device name. + currentAlert = [[MXKAlert alloc] initWithTitle:NSLocalizedStringFromTable(@"device_details_delete_prompt_title", @"Vector", nil) message:NSLocalizedStringFromTable(@"device_details_delete_prompt_message", @"Vector", nil) style:MXKAlertStyleAlert]; + + [currentAlert addTextFieldWithConfigurationHandler:^(UITextField *textField) { + + textField.secureTextEntry = YES; + textField.placeholder = nil; + textField.keyboardType = UIKeyboardTypeDefault; + }]; + + currentAlert.cancelButtonIndex = [currentAlert addActionWithTitle:[NSBundle mxk_localizedStringForKey:@"cancel"] style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) { + + if (weakSelf) + { + __strong __typeof(weakSelf)strongSelf = weakSelf; + strongSelf->currentAlert = nil; + + [strongSelf.activityIndicator stopAnimating]; + } + + }]; + + [currentAlert addActionWithTitle:[NSBundle mxk_localizedStringForKey:@"submit"] style:MXKAlertActionStyleDefault handler:^(MXKAlert *alert) { + + if (weakSelf) + { + UITextField *textField = [alert textFieldAtIndex:0]; + + __strong __typeof(weakSelf)strongSelf = weakSelf; + strongSelf->currentAlert = nil; + + NSString *userId = strongSelf->mxSession.myUser.userId; + NSDictionary *authParams; + + // Sanity check + if (userId) + { + authParams = @{@"session":authSession.session, + @"user": userId, + @"password": textField.text, + @"type": kMXLoginFlowTypePassword}; + + } + + [strongSelf->mxSession.matrixRestClient deleteDeviceByDeviceId:strongSelf->mxDevice.deviceId authParams:authParams success:^{ + + if (weakSelf) + { + __strong __typeof(weakSelf)strongSelf = weakSelf; + + [strongSelf.activityIndicator stopAnimating]; + + if (strongSelf.delegate && [strongSelf.delegate respondsToSelector:@selector(deviceViewDidUpdate:)]) + { + [strongSelf.delegate deviceViewDidUpdate:strongSelf]; + } + + [strongSelf removeFromSuperview]; + } + + } failure:^(NSError *error) { + + // Notify user + [[AppDelegate theDelegate] showErrorAsAlert:error]; + + if (weakSelf) + { + __strong __typeof(weakSelf)strongSelf = weakSelf; + + NSLog(@"[DeviceView] Delete device (%@) failed", strongSelf->mxDevice.deviceId); + + [strongSelf.activityIndicator stopAnimating]; + [strongSelf removeFromSuperview]; + } + + }]; + } + }]; + + [self.delegate deviceView:self presentMXKAlert:currentAlert]; + } + else + { + NSLog(@"[DeviceView] Delete device (%@) failed, auth session flow type is not supported", mxDevice.deviceId); + [self.activityIndicator stopAnimating]; + } + + } failure:^(NSError *error) { + + NSLog(@"[DeviceView] Delete device (%@) failed, unable to get auth session", mxDevice.deviceId); + [self.activityIndicator stopAnimating]; + + [[AppDelegate theDelegate] showErrorAsAlert:error]; + }]; +} + +@end \ No newline at end of file diff --git a/Vector/Views/DeviceView/DeviceView.xib b/Vector/Views/DeviceView/DeviceView.xib new file mode 100644 index 000000000..7973e6a4e --- /dev/null +++ b/Vector/Views/DeviceView/DeviceView.xib @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +