2018-05-30 14:59:18 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 New Vector 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 "DeactivateAccountViewController.h"
|
|
|
|
|
2019-01-11 09:32:56 +00:00
|
|
|
#import "ThemeService.h"
|
2018-11-30 16:10:28 +00:00
|
|
|
#import "Riot-Swift.h"
|
2018-05-30 14:59:18 +00:00
|
|
|
|
|
|
|
#pragma mark - Defines & Constants
|
|
|
|
|
|
|
|
static CGFloat const kButtonCornerRadius = 5.0;
|
|
|
|
static CGFloat const kTextFontSize = 15.0;
|
|
|
|
|
|
|
|
#pragma mark - Private Interface
|
|
|
|
|
|
|
|
@interface DeactivateAccountViewController ()
|
|
|
|
|
|
|
|
#pragma mark - Outlets
|
|
|
|
|
|
|
|
@property (weak, nonatomic) IBOutlet UILabel *deactivateAccountInfosLabel;
|
|
|
|
|
|
|
|
@property (weak, nonatomic) IBOutlet UILabel *forgetMessagesInfoLabel;
|
|
|
|
@property (weak, nonatomic) IBOutlet UIButton *forgetMessageButton;
|
|
|
|
|
|
|
|
@property (weak, nonatomic) IBOutlet UIButton *deactivateAcccountButton;
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark - Private Properties
|
|
|
|
|
|
|
|
@property (strong, nonatomic) NSDictionary *normalStringAttributes;
|
|
|
|
@property (strong, nonatomic) NSDictionary *emphasizeStringAttributes;
|
|
|
|
|
|
|
|
@property (strong, nonatomic) MXKErrorAlertPresentation *errorPresentation;
|
|
|
|
|
2018-05-31 10:14:13 +00:00
|
|
|
@property (weak, nonatomic) id <NSObject> themeDidChangeNotificationObserver;
|
|
|
|
|
2018-05-30 14:59:18 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
#pragma mark - Implementation
|
|
|
|
|
|
|
|
@implementation DeactivateAccountViewController
|
|
|
|
|
|
|
|
#pragma mark - Setup & Teardown
|
|
|
|
|
|
|
|
+ (DeactivateAccountViewController*)instantiateWithMatrixSession:(MXSession*)matrixSession
|
|
|
|
{
|
|
|
|
DeactivateAccountViewController* viewController = [[UIStoryboard storyboardWithName:NSStringFromClass([DeactivateAccountViewController class]) bundle:[NSBundle mainBundle]] instantiateInitialViewController];
|
|
|
|
[viewController addMatrixSession:matrixSession];
|
|
|
|
return viewController;
|
|
|
|
}
|
|
|
|
|
2018-05-31 10:14:13 +00:00
|
|
|
- (void)destroy
|
|
|
|
{
|
|
|
|
id<NSObject> notificationObserver = self.themeDidChangeNotificationObserver;
|
|
|
|
|
|
|
|
if (notificationObserver)
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:notificationObserver];
|
|
|
|
}
|
|
|
|
|
|
|
|
[super destroy];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-30 14:59:18 +00:00
|
|
|
#pragma mark - View life cycle
|
|
|
|
|
|
|
|
- (void)viewDidLoad
|
|
|
|
{
|
|
|
|
[super viewDidLoad];
|
|
|
|
// Do any additional setup after loading the view.
|
|
|
|
|
2021-09-28 05:40:01 +00:00
|
|
|
self.title = [VectorL10n deactivateAccountTitle];
|
2020-04-15 17:02:13 +00:00
|
|
|
|
|
|
|
[self setupViews];
|
2018-05-30 14:59:18 +00:00
|
|
|
|
|
|
|
self.errorPresentation = [[MXKErrorAlertPresentation alloc] init];
|
2018-05-31 10:14:13 +00:00
|
|
|
[self registerThemeNotification];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewWillAppear:(BOOL)animated
|
|
|
|
{
|
|
|
|
[super viewWillAppear:animated];
|
2020-04-15 15:45:46 +00:00
|
|
|
|
|
|
|
[self userInterfaceThemeDidChange];
|
2018-05-31 10:14:13 +00:00
|
|
|
|
|
|
|
// Screen tracking
|
2018-06-27 07:55:06 +00:00
|
|
|
[[Analytics sharedInstance] trackScreen:@"DeactivateAccount"];
|
2018-05-30 14:59:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewDidLayoutSubviews
|
|
|
|
{
|
|
|
|
[super viewDidLayoutSubviews];
|
|
|
|
|
|
|
|
[self.deactivateAcccountButton.layer setCornerRadius:kButtonCornerRadius];
|
|
|
|
}
|
|
|
|
|
2018-05-31 10:14:13 +00:00
|
|
|
- (UIStatusBarStyle)preferredStatusBarStyle
|
|
|
|
{
|
2019-01-11 10:45:27 +00:00
|
|
|
return ThemeService.shared.theme.statusBarStyle;
|
2018-05-31 10:14:13 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 14:59:18 +00:00
|
|
|
#pragma mark - Private
|
|
|
|
|
2018-05-31 10:14:13 +00:00
|
|
|
- (void)registerThemeNotification
|
|
|
|
{
|
2019-01-11 09:32:56 +00:00
|
|
|
self.themeDidChangeNotificationObserver = [[NSNotificationCenter defaultCenter] addObserverForName:kThemeServiceDidChangeThemeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notif) {
|
2018-05-31 10:14:13 +00:00
|
|
|
[self userInterfaceThemeDidChange];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)userInterfaceThemeDidChange
|
|
|
|
{
|
2019-01-11 10:45:27 +00:00
|
|
|
[ThemeService.shared.theme applyStyleOnNavigationBar:self.navigationController.navigationBar];
|
2018-11-29 16:31:41 +00:00
|
|
|
|
2019-01-11 10:45:27 +00:00
|
|
|
self.activityIndicator.backgroundColor = ThemeService.shared.theme.overlayBackgroundColor;
|
2020-04-15 15:45:46 +00:00
|
|
|
|
|
|
|
self.view.backgroundColor = ThemeService.shared.theme.backgroundColor;
|
2020-07-09 16:58:44 +00:00
|
|
|
|
|
|
|
self.forgetMessageButton.tintColor = ThemeService.shared.theme.tintColor;
|
2020-04-15 15:45:46 +00:00
|
|
|
|
2020-04-15 17:02:13 +00:00
|
|
|
[self updateStringAttributes];
|
|
|
|
[self updateNavigationBar];
|
|
|
|
[self updateDeactivateAcccountButton];
|
|
|
|
[self updateDeactivateAccountInfosLabel];
|
|
|
|
[self updateForgetMessagesInfoLabel];
|
2020-04-08 10:58:12 +00:00
|
|
|
[self setNeedsStatusBarAppearanceUpdate];
|
2018-05-31 10:14:13 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 17:02:13 +00:00
|
|
|
- (void)updateStringAttributes
|
2018-05-30 14:59:18 +00:00
|
|
|
{
|
|
|
|
self.normalStringAttributes = @{
|
|
|
|
NSFontAttributeName: [UIFont systemFontOfSize:kTextFontSize],
|
2019-01-11 10:45:27 +00:00
|
|
|
NSForegroundColorAttributeName: ThemeService.shared.theme.textPrimaryColor
|
2018-05-30 14:59:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
self.emphasizeStringAttributes = @{
|
|
|
|
NSFontAttributeName: [UIFont systemFontOfSize:kTextFontSize weight:UIFontWeightBold],
|
2019-01-11 10:45:27 +00:00
|
|
|
NSForegroundColorAttributeName: ThemeService.shared.theme.textPrimaryColor
|
2018-05-30 14:59:18 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setupViews
|
|
|
|
{
|
2020-04-15 17:02:13 +00:00
|
|
|
// Cancel bar button
|
2021-09-28 05:40:01 +00:00
|
|
|
UIBarButtonItem *cancelBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[VectorL10n cancel] style:UIBarButtonItemStylePlain target:self action:@selector(cancelButtonAction:)];
|
2018-05-30 14:59:18 +00:00
|
|
|
self.navigationItem.rightBarButtonItem = cancelBarButtonItem;
|
|
|
|
|
2020-04-15 17:02:13 +00:00
|
|
|
// Deactivate button
|
2018-05-30 14:59:18 +00:00
|
|
|
// Adjust button font size for small devices
|
|
|
|
self.deactivateAcccountButton.titleLabel.adjustsFontSizeToFitWidth = YES;
|
|
|
|
self.deactivateAcccountButton.titleLabel.minimumScaleFactor = 0.5;
|
|
|
|
self.deactivateAcccountButton.titleLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
|
|
|
|
self.deactivateAcccountButton.layer.masksToBounds = YES;
|
2021-09-28 05:40:01 +00:00
|
|
|
[self.deactivateAcccountButton setTitle:[VectorL10n deactivateAccountValidateAction] forState:UIControlStateNormal];
|
2020-04-15 17:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateNavigationBar
|
|
|
|
{
|
|
|
|
self.navigationController.navigationBar.titleTextAttributes = @{ NSForegroundColorAttributeName: ThemeService.shared.theme.warningColor };
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateDeactivateAcccountButton
|
|
|
|
{
|
2019-01-11 10:45:27 +00:00
|
|
|
self.deactivateAcccountButton.backgroundColor = ThemeService.shared.theme.tintColor;
|
|
|
|
[self.deactivateAcccountButton setTitleColor:ThemeService.shared.theme.headerTextSecondaryColor forState:UIControlStateDisabled];
|
2018-05-30 14:59:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 17:02:13 +00:00
|
|
|
- (void)updateDeactivateAccountInfosLabel
|
2018-05-30 14:59:18 +00:00
|
|
|
{
|
|
|
|
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
|
|
|
|
|
2021-09-28 05:40:01 +00:00
|
|
|
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:[VectorL10n deactivateAccountInformationsPart1] attributes:self.normalStringAttributes]];
|
2018-05-30 14:59:18 +00:00
|
|
|
|
2021-09-28 05:40:01 +00:00
|
|
|
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:[VectorL10n deactivateAccountInformationsPart2Emphasize] attributes:self.emphasizeStringAttributes]];
|
2018-05-30 14:59:18 +00:00
|
|
|
|
2021-09-28 05:40:01 +00:00
|
|
|
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:[VectorL10n deactivateAccountInformationsPart3] attributes:self.normalStringAttributes]];
|
2018-05-30 14:59:18 +00:00
|
|
|
|
2021-09-28 05:40:01 +00:00
|
|
|
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:[VectorL10n deactivateAccountInformationsPart4Emphasize] attributes:self.emphasizeStringAttributes]];
|
2018-05-30 14:59:18 +00:00
|
|
|
|
2021-09-28 05:40:01 +00:00
|
|
|
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:[VectorL10n deactivateAccountInformationsPart5] attributes:self.normalStringAttributes]];
|
2018-05-30 14:59:18 +00:00
|
|
|
|
|
|
|
[self.deactivateAccountInfosLabel setAttributedText:attributedString];
|
|
|
|
}
|
|
|
|
|
2020-04-15 17:02:13 +00:00
|
|
|
- (void)updateForgetMessagesInfoLabel
|
2018-05-30 14:59:18 +00:00
|
|
|
{
|
|
|
|
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
|
|
|
|
|
2021-09-28 05:40:01 +00:00
|
|
|
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:[VectorL10n deactivateAccountForgetMessagesInformationPart1] attributes:self.normalStringAttributes]];
|
2018-05-30 14:59:18 +00:00
|
|
|
|
2021-09-28 05:40:01 +00:00
|
|
|
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:[VectorL10n deactivateAccountForgetMessagesInformationPart2Emphasize] attributes:self.emphasizeStringAttributes]];
|
2018-05-30 14:59:18 +00:00
|
|
|
|
2021-09-28 05:40:01 +00:00
|
|
|
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:[VectorL10n deactivateAccountForgetMessagesInformationPart3] attributes:self.normalStringAttributes]];
|
2018-05-30 14:59:18 +00:00
|
|
|
|
|
|
|
[self.forgetMessagesInfoLabel setAttributedText:attributedString];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)enableUserActions:(BOOL)enableUserActions
|
|
|
|
{
|
|
|
|
self.navigationItem.rightBarButtonItem.enabled = enableUserActions;
|
2018-06-01 14:01:21 +00:00
|
|
|
self.forgetMessageButton.userInteractionEnabled = enableUserActions;
|
2018-05-30 14:59:18 +00:00
|
|
|
self.deactivateAcccountButton.enabled = enableUserActions;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)presentPasswordRequiredAlertWithSubmitHandler:(void (^)(NSString *password))submitHandler
|
|
|
|
cancelHandler:(dispatch_block_t)cancelHandler
|
|
|
|
{
|
2021-09-28 05:40:01 +00:00
|
|
|
UIAlertController *alert = [UIAlertController alertControllerWithTitle:[VectorL10n deactivateAccountPasswordAlertTitle]
|
|
|
|
message:[VectorL10n deactivateAccountPasswordAlertMessage] preferredStyle:UIAlertControllerStyleAlert];
|
2018-05-30 14:59:18 +00:00
|
|
|
|
|
|
|
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
|
|
|
|
textField.secureTextEntry = YES;
|
|
|
|
textField.placeholder = nil;
|
|
|
|
textField.keyboardType = UIKeyboardTypeDefault;
|
|
|
|
}];
|
|
|
|
|
2021-09-28 05:40:01 +00:00
|
|
|
[alert addAction:[UIAlertAction actionWithTitle:[MatrixKitL10n cancel]
|
2018-05-30 14:59:18 +00:00
|
|
|
style:UIAlertActionStyleCancel
|
|
|
|
handler:^(UIAlertAction * action) {
|
|
|
|
if (cancelHandler)
|
|
|
|
{
|
|
|
|
cancelHandler();
|
|
|
|
}
|
|
|
|
}]];
|
|
|
|
|
|
|
|
__weak typeof(self) weakSelf = self;
|
|
|
|
|
2021-09-28 05:40:01 +00:00
|
|
|
[alert addAction:[UIAlertAction actionWithTitle:[MatrixKitL10n cancel]
|
2018-05-30 14:59:18 +00:00
|
|
|
style:UIAlertActionStyleDefault
|
|
|
|
handler:^(UIAlertAction * action) {
|
|
|
|
UITextField *textField = alert.textFields.firstObject;
|
|
|
|
|
|
|
|
typeof(weakSelf) strongSelf = weakSelf;
|
|
|
|
|
|
|
|
if (strongSelf)
|
|
|
|
{
|
|
|
|
NSString *password = textField.text;
|
|
|
|
|
|
|
|
if (submitHandler)
|
|
|
|
{
|
|
|
|
submitHandler(password);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}]];
|
|
|
|
|
|
|
|
[self presentViewController:alert animated:YES completion:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)deactivateAccountWithUserId:(NSString*)userId
|
|
|
|
andPassword:(NSString*)password
|
|
|
|
eraseAllMessages:(BOOL)eraseAllMessages
|
|
|
|
{
|
|
|
|
if (password && userId)
|
|
|
|
{
|
|
|
|
[self enableUserActions:NO];
|
|
|
|
[self startActivityIndicator];
|
|
|
|
|
|
|
|
// This assumes that the homeserver requires password UI auth
|
|
|
|
// for this endpoint. In reality it could be any UI auth.
|
|
|
|
|
|
|
|
__weak typeof(self) weakSelf = self;
|
|
|
|
|
|
|
|
NSDictionary *authParameters = @{@"user": userId,
|
|
|
|
@"password": password,
|
|
|
|
@"type": kMXLoginFlowTypePassword};
|
|
|
|
|
|
|
|
[self.mainSession deactivateAccountWithAuthParameters:authParameters eraseAccount:eraseAllMessages success:^{
|
2021-06-03 08:30:07 +00:00
|
|
|
MXLogDebug(@"[SettingsViewController] Deactivate account with success");
|
2018-05-30 14:59:18 +00:00
|
|
|
|
2018-06-01 14:01:21 +00:00
|
|
|
typeof(weakSelf) strongSelf = weakSelf;
|
|
|
|
|
|
|
|
if (strongSelf)
|
|
|
|
{
|
|
|
|
[strongSelf stopActivityIndicator];
|
|
|
|
[strongSelf enableUserActions:YES];
|
|
|
|
[strongSelf.delegate deactivateAccountViewControllerDidDeactivateWithSuccess:strongSelf];
|
|
|
|
}
|
|
|
|
|
2018-05-30 14:59:18 +00:00
|
|
|
} failure:^(NSError *error) {
|
|
|
|
|
2021-06-03 08:30:07 +00:00
|
|
|
MXLogDebug(@"[SettingsViewController] Failed to deactivate account");
|
2018-05-30 14:59:18 +00:00
|
|
|
|
|
|
|
typeof(weakSelf) strongSelf = weakSelf;
|
|
|
|
|
|
|
|
if (strongSelf)
|
|
|
|
{
|
|
|
|
[strongSelf stopActivityIndicator];
|
|
|
|
[strongSelf enableUserActions:YES];
|
|
|
|
[strongSelf.errorPresentation presentErrorFromViewController:strongSelf forError:error animated:YES handler:nil];
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-03 08:30:07 +00:00
|
|
|
MXLogDebug(@"[SettingsViewController] Failed to deactivate account");
|
2018-05-30 14:59:18 +00:00
|
|
|
[self.errorPresentation presentGenericErrorFromViewController:self animated:YES handler:nil];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Actions
|
|
|
|
|
|
|
|
- (void)cancelButtonAction:(id)sender
|
|
|
|
{
|
|
|
|
[self.delegate deactivateAccountViewControllerDidCancel:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)forgetMessagesButtonAction:(UIButton*)sender
|
|
|
|
{
|
|
|
|
self.forgetMessageButton.selected = !self.forgetMessageButton.selected;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)deactivateAccountButtonAction:(id)sender
|
|
|
|
{
|
|
|
|
__weak typeof(self) weakSelf = self;
|
|
|
|
|
|
|
|
[self presentPasswordRequiredAlertWithSubmitHandler:^(NSString *password) {
|
|
|
|
|
|
|
|
typeof(weakSelf) strongSelf = weakSelf;
|
|
|
|
|
|
|
|
if (strongSelf)
|
|
|
|
{
|
|
|
|
NSString *userId = strongSelf.mainSession.myUser.userId;
|
|
|
|
[strongSelf deactivateAccountWithUserId:userId andPassword:password eraseAllMessages:strongSelf.forgetMessageButton.isEnabled];
|
|
|
|
}
|
|
|
|
|
2018-05-31 11:33:17 +00:00
|
|
|
} cancelHandler:nil];
|
2018-05-30 14:59:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|