mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			214 lines
		
	
	
		
			8.1 KiB
		
	
	
	
		
			Matlab
		
	
		
		
			
		
	
	
			214 lines
		
	
	
		
			8.1 KiB
		
	
	
	
		
			Matlab
		
	
| 
											8 years ago
										 | // | ||
| 
											7 years ago
										 | //  Copyright (c) 2019 Open Whisper Systems. All rights reserved. | ||
| 
											8 years ago
										 | // | ||
|  | 
 | ||
|  | #import "OWSBackupSettingsViewController.h" | ||
| 
											8 years ago
										 | #import "OWSBackup.h" | ||
| 
											7 years ago
										 | #import "Session-Swift.h" | ||
| 
											5 years ago
										 | 
 | ||
| 
											7 years ago
										 | #import <PromiseKit/AnyPromise.h> | ||
| 
											5 years ago
										 | #import <SessionMessagingKit/Environment.h> | ||
| 
											5 years ago
										 | #import <SignalUtilitiesKit/SignalUtilitiesKit-Swift.h> | ||
|  | #import <SignalUtilitiesKit/UIColor+OWS.h> | ||
|  | #import <SignalUtilitiesKit/UIFont+OWS.h> | ||
| 
											5 years ago
										 | #import <SessionUtilitiesKit/UIView+OWS.h> | ||
| 
											5 years ago
										 | #import <SessionUtilitiesKit/MIMETypeUtil.h> | ||
| 
											8 years ago
										 | 
 | ||
|  | NS_ASSUME_NONNULL_BEGIN | ||
|  | 
 | ||
|  | @interface OWSBackupSettingsViewController () | ||
|  | 
 | ||
| 
											7 years ago
										 | @property (nonatomic, nullable) NSError *iCloudError; | ||
|  | 
 | ||
| 
											8 years ago
										 | @end | ||
|  | 
 | ||
|  | #pragma mark - | ||
|  | 
 | ||
|  | @implementation OWSBackupSettingsViewController | ||
|  | 
 | ||
| 
											7 years ago
										 | #pragma mark - Dependencies | ||
|  | 
 | ||
|  | - (OWSBackup *)backup | ||
|  | { | ||
|  |     OWSAssertDebug(AppEnvironment.shared.backup); | ||
|  | 
 | ||
|  |     return AppEnvironment.shared.backup; | ||
|  | } | ||
|  | 
 | ||
|  | #pragma mark - | ||
|  | 
 | ||
| 
											8 years ago
										 | - (void)viewDidLoad | ||
|  | { | ||
|  |     [super viewDidLoad]; | ||
|  | 
 | ||
|  |     self.title = NSLocalizedString(@"SETTINGS_BACKUP", @"Label for the backup view in app settings."); | ||
|  | 
 | ||
|  |     [[NSNotificationCenter defaultCenter] addObserver:self | ||
|  |                                              selector:@selector(backupStateDidChange:) | ||
|  |                                                  name:NSNotificationNameBackupStateDidChange | ||
|  |                                                object:nil]; | ||
| 
											7 years ago
										 |     [[NSNotificationCenter defaultCenter] addObserver:self | ||
|  |                                              selector:@selector(applicationDidBecomeActive:) | ||
|  |                                                  name:OWSApplicationDidBecomeActiveNotification | ||
|  |                                                object:nil]; | ||
| 
											8 years ago
										 | 
 | ||
|  |     [self updateTableContents]; | ||
|  | } | ||
|  | 
 | ||
|  | - (void)dealloc | ||
|  | { | ||
|  |     [[NSNotificationCenter defaultCenter] removeObserver:self]; | ||
|  | } | ||
|  | 
 | ||
|  | - (void)viewDidAppear:(BOOL)animated | ||
|  | { | ||
| 
											7 years ago
										 |     [super viewDidAppear:animated]; | ||
| 
											7 years ago
										 | 
 | ||
| 
											8 years ago
										 |     [self updateTableContents]; | ||
| 
											7 years ago
										 |     [self updateICloudStatus]; | ||
|  | } | ||
|  | 
 | ||
|  | - (void)updateICloudStatus | ||
|  | { | ||
|  |     __weak OWSBackupSettingsViewController *weakSelf = self; | ||
| 
											7 years ago
										 |     [[self.backup ensureCloudKitAccess] | ||
| 
											7 years ago
										 |             .then(^{ | ||
| 
											7 years ago
										 |                 OWSAssertIsOnMainThread(); | ||
|  | 
 | ||
|  |                 weakSelf.iCloudError = nil; | ||
|  |                 [weakSelf updateTableContents]; | ||
| 
											7 years ago
										 |             }) | ||
|  |             .catch(^(NSError *error) { | ||
| 
											7 years ago
										 |                 OWSAssertIsOnMainThread(); | ||
|  | 
 | ||
|  |                 weakSelf.iCloudError = error; | ||
|  |                 [weakSelf updateTableContents]; | ||
| 
											7 years ago
										 |             }) retainUntilComplete]; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
|  | #pragma mark - Table Contents | ||
|  | 
 | ||
|  | - (void)updateTableContents | ||
|  | { | ||
|  |     OWSTableContents *contents = [OWSTableContents new]; | ||
|  | 
 | ||
|  |     BOOL isBackupEnabled = [OWSBackup.sharedManager isBackupEnabled]; | ||
|  | 
 | ||
| 
											7 years ago
										 |     if (self.iCloudError) { | ||
|  |         OWSTableSection *iCloudSection = [OWSTableSection new]; | ||
|  |         iCloudSection.headerTitle = NSLocalizedString( | ||
|  |             @"SETTINGS_BACKUP_ICLOUD_STATUS", @"Label for iCloud status row in the in the backup settings view."); | ||
|  |         [iCloudSection | ||
|  |             addItem:[OWSTableItem | ||
|  |                         longDisclosureItemWithText:[OWSBackupAPI errorMessageForCloudKitAccessError:self.iCloudError] | ||
|  |                                        actionBlock:^{ | ||
|  |                                            [[UIApplication sharedApplication] | ||
|  |                                                openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; | ||
|  |                                        }]]; | ||
|  |         [contents addSection:iCloudSection]; | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     // TODO: This UI is temporary. | ||
|  |     // Enabling backup will involve entering and registering a PIN. | ||
|  |     OWSTableSection *enableSection = [OWSTableSection new]; | ||
|  |     enableSection.headerTitle = NSLocalizedString(@"SETTINGS_BACKUP", @"Label for the backup view in app settings."); | ||
|  |     [enableSection | ||
|  |         addItem:[OWSTableItem switchItemWithText: | ||
|  |                                   NSLocalizedString(@"SETTINGS_BACKUP_ENABLING_SWITCH", | ||
|  |                                       @"Label for switch in settings that controls whether or not backup is enabled.") | ||
| 
											7 years ago
										 |                                        isOnBlock:^{ | ||
|  |                                            return [OWSBackup.sharedManager isBackupEnabled]; | ||
|  |                                        } | ||
| 
											8 years ago
										 |                                           target:self | ||
|  |                                         selector:@selector(isBackupEnabledDidChange:)]]; | ||
|  |     [contents addSection:enableSection]; | ||
|  | 
 | ||
| 
											8 years ago
										 |     if (isBackupEnabled) { | ||
|  |         // TODO: This UI is temporary. | ||
|  |         // Enabling backup will involve entering and registering a PIN. | ||
|  |         OWSTableSection *progressSection = [OWSTableSection new]; | ||
|  |         [progressSection | ||
| 
											7 years ago
										 |             addItem:[OWSTableItem | ||
|  |                         labelItemWithText:NSLocalizedString(@"SETTINGS_BACKUP_STATUS", | ||
| 
											7 years ago
										 |                                               @"Label for backup status row in the in the backup settings view.") | ||
| 
											7 years ago
										 |                             accessoryText:NSStringForBackupExportState(OWSBackup.sharedManager.backupExportState)]]; | ||
| 
											8 years ago
										 |         if (OWSBackup.sharedManager.backupExportState == OWSBackupState_InProgress) { | ||
|  |             if (OWSBackup.sharedManager.backupExportDescription) { | ||
|  |                 [progressSection | ||
|  |                     addItem:[OWSTableItem | ||
|  |                                 labelItemWithText:NSLocalizedString(@"SETTINGS_BACKUP_PHASE", | ||
|  |                                                       @"Label for phase row in the in the backup settings view.") | ||
|  |                                     accessoryText:OWSBackup.sharedManager.backupExportDescription]]; | ||
|  |                 if (OWSBackup.sharedManager.backupExportProgress) { | ||
|  |                     NSUInteger progressPercent | ||
|  |                         = (NSUInteger)round(OWSBackup.sharedManager.backupExportProgress.floatValue * 100); | ||
| 
											8 years ago
										 |                     NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; | ||
|  |                     [numberFormatter setNumberStyle:NSNumberFormatterPercentStyle]; | ||
|  |                     [numberFormatter setMaximumFractionDigits:0]; | ||
|  |                     [numberFormatter setMultiplier:@1]; | ||
|  |                     NSString *progressString = [numberFormatter stringFromNumber:@(progressPercent)]; | ||
| 
											8 years ago
										 |                     [progressSection | ||
|  |                         addItem:[OWSTableItem | ||
|  |                                     labelItemWithText:NSLocalizedString(@"SETTINGS_BACKUP_PROGRESS", | ||
|  |                                                           @"Label for phase row in the in the backup settings view.") | ||
| 
											8 years ago
										 |                                         accessoryText:progressString]]; | ||
| 
											8 years ago
										 |                 } | ||
|  |             } | ||
|  |         } | ||
|  | 
 | ||
|  |         switch (OWSBackup.sharedManager.backupExportState) { | ||
|  |             case OWSBackupState_Idle: | ||
|  |             case OWSBackupState_Failed: | ||
|  |             case OWSBackupState_Succeeded: | ||
|  |                 [progressSection | ||
|  |                     addItem:[OWSTableItem disclosureItemWithText: | ||
|  |                                               NSLocalizedString(@"SETTINGS_BACKUP_BACKUP_NOW", | ||
|  |                                                   @"Label for 'backup now' button in the backup settings view.") | ||
|  |                                                      actionBlock:^{ | ||
|  |                                                          [OWSBackup.sharedManager tryToExportBackup]; | ||
|  |                                                      }]]; | ||
|  |                 break; | ||
|  |             case OWSBackupState_InProgress: | ||
|  |                 [progressSection | ||
|  |                     addItem:[OWSTableItem disclosureItemWithText: | ||
|  |                                               NSLocalizedString(@"SETTINGS_BACKUP_CANCEL_BACKUP", | ||
|  |                                                   @"Label for 'cancel backup' button in the backup settings view.") | ||
|  |                                                      actionBlock:^{ | ||
|  |                                                          [OWSBackup.sharedManager cancelExportBackup]; | ||
|  |                                                      }]]; | ||
|  |                 break; | ||
|  |         } | ||
|  | 
 | ||
|  |         [contents addSection:progressSection]; | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     self.contents = contents; | ||
|  | } | ||
|  | 
 | ||
|  | - (void)isBackupEnabledDidChange:(UISwitch *)sender | ||
|  | { | ||
|  |     [OWSBackup.sharedManager setIsBackupEnabled:sender.isOn]; | ||
| 
											7 years ago
										 | 
 | ||
|  |     [self updateTableContents]; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
|  | #pragma mark - Events | ||
|  | 
 | ||
|  | - (void)backupStateDidChange:(NSNotification *)notification | ||
|  | { | ||
| 
											7 years ago
										 |     OWSAssertIsOnMainThread(); | ||
|  | 
 | ||
| 
											8 years ago
										 |     [self updateTableContents]; | ||
|  | } | ||
|  | 
 | ||
| 
											7 years ago
										 | - (void)applicationDidBecomeActive:(NSNotification *)notification | ||
|  | { | ||
|  |     OWSAssertIsOnMainThread(); | ||
|  | 
 | ||
|  |     [self updateICloudStatus]; | ||
|  | } | ||
|  | 
 | ||
| 
											8 years ago
										 | @end | ||
|  | 
 | ||
|  | NS_ASSUME_NONNULL_END |