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.
		
		
		
		
		
			
		
			
	
	
		
			176 lines
		
	
	
		
			5.6 KiB
		
	
	
	
		
			Swift
		
	
		
		
			
		
	
	
			176 lines
		
	
	
		
			5.6 KiB
		
	
	
	
		
			Swift
		
	
| 
											7 years ago
										 | // | ||
| 
											7 years ago
										 | //  Copyright (c) 2019 Open Whisper Systems. All rights reserved. | ||
| 
											7 years ago
										 | // | ||
|  | 
 | ||
|  | import UIKit | ||
|  | 
 | ||
|  | @objc | ||
|  | public class BackupRestoreViewController: OWSTableViewController { | ||
|  | 
 | ||
| 
											7 years ago
										 |     private var hasBegunImport = false | ||
|  | 
 | ||
| 
											7 years ago
										 |     // MARK: - Dependencies | ||
|  | 
 | ||
| 
											7 years ago
										 |     private var backup: OWSBackup { | ||
|  |         return AppEnvironment.shared.backup | ||
|  |     } | ||
|  | 
 | ||
| 
											7 years ago
										 |     // MARK: - | ||
|  | 
 | ||
| 
											7 years ago
										 |     override public func loadView() { | ||
| 
											7 years ago
										 |         super.loadView() | ||
|  | 
 | ||
| 
											7 years ago
										 |         navigationItem.title = NSLocalizedString("SETTINGS_BACKUP", comment: "Label for the backup view in app settings.") | ||
| 
											7 years ago
										 | 
 | ||
|  |         navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(didPressCancelButton)) | ||
|  |     } | ||
|  | 
 | ||
|  |     override public func viewDidLoad() { | ||
|  |         super.viewDidLoad() | ||
|  | 
 | ||
|  |         NotificationCenter.default.addObserver(self, | ||
|  |                                                selector: #selector(backupStateDidChange), | ||
|  |                                                name: NSNotification.Name(NSNotificationNameBackupStateDidChange), | ||
|  |                                                object: nil) | ||
|  | 
 | ||
|  |         updateTableContents() | ||
|  |     } | ||
|  | 
 | ||
|  |     private func updateTableContents() { | ||
| 
											7 years ago
										 |         if hasBegunImport { | ||
|  |             updateProgressContents() | ||
|  |         } else { | ||
|  |             updateDecisionContents() | ||
|  |         } | ||
|  |     } | ||
|  | 
 | ||
|  |     private func updateDecisionContents() { | ||
|  |         let contents = OWSTableContents() | ||
|  | 
 | ||
|  |         let section = OWSTableSection() | ||
|  | 
 | ||
|  |         section.headerTitle = NSLocalizedString("BACKUP_RESTORE_DECISION_TITLE", comment: "Label for the backup restore decision section.") | ||
|  | 
 | ||
|  |         section.add(OWSTableItem.actionItem(withText: NSLocalizedString("CHECK_FOR_BACKUP_DO_NOT_RESTORE", | ||
|  |                                                                         comment: "The label for the 'do not restore backup' button."), actionBlock: { [weak self] in | ||
|  |                 guard let strongSelf = self else { | ||
|  |                     return | ||
|  |                 } | ||
|  |                 strongSelf.cancelAndDismiss() | ||
|  |         })) | ||
|  |         section.add(OWSTableItem.actionItem(withText: NSLocalizedString("CHECK_FOR_BACKUP_RESTORE", | ||
|  |                                                                         comment: "The label for the 'restore backup' button."), actionBlock: { [weak self] in | ||
|  |                 guard let strongSelf = self else { | ||
|  |                     return | ||
|  |                 } | ||
|  |                 strongSelf.startImport() | ||
|  |         })) | ||
|  | 
 | ||
|  |         contents.addSection(section) | ||
|  |         self.contents = contents | ||
|  |     } | ||
|  | 
 | ||
| 
											7 years ago
										 |     private var progressFormatter: NumberFormatter = { | ||
|  |         let numberFormatter = NumberFormatter() | ||
|  |         numberFormatter.numberStyle = .percent | ||
|  |         numberFormatter.maximumFractionDigits = 0 | ||
|  |         numberFormatter.multiplier = 1 | ||
|  |         return numberFormatter | ||
|  |     }() | ||
|  | 
 | ||
| 
											7 years ago
										 |     private func updateProgressContents() { | ||
| 
											7 years ago
										 |         let contents = OWSTableContents() | ||
|  | 
 | ||
|  |         let section = OWSTableSection() | ||
|  | 
 | ||
|  |         section.add(OWSTableItem.label(withText: NSLocalizedString("BACKUP_RESTORE_STATUS", comment: "Label for the backup restore status."), accessoryText: NSStringForBackupImportState(backup.backupImportState))) | ||
|  | 
 | ||
|  |         if backup.backupImportState == .inProgress { | ||
|  |             if let backupImportDescription = backup.backupImportDescription { | ||
|  |                 section.add(OWSTableItem.label(withText: NSLocalizedString("BACKUP_RESTORE_DESCRIPTION", comment: "Label for the backup restore description."), accessoryText: backupImportDescription)) | ||
|  |             } | ||
|  | 
 | ||
|  |             if let backupImportProgress = backup.backupImportProgress { | ||
|  |                 let progressInt = backupImportProgress.floatValue * 100 | ||
| 
											7 years ago
										 |                 if let progressString = progressFormatter.string(from: NSNumber(value: progressInt)) { | ||
| 
											7 years ago
										 |                     section.add(OWSTableItem.label(withText: NSLocalizedString("BACKUP_RESTORE_PROGRESS", comment: "Label for the backup restore progress."), accessoryText: progressString)) | ||
|  |                 } else { | ||
|  |                     owsFailDebug("Could not format progress: \(progressInt)") | ||
|  |                 } | ||
|  |             } | ||
|  |         } | ||
|  | 
 | ||
|  |         contents.addSection(section) | ||
|  |         self.contents = contents | ||
|  | 
 | ||
|  |         // TODO: Add cancel button. | ||
|  |     } | ||
|  | 
 | ||
|  |     // MARK: Helpers | ||
|  | 
 | ||
|  |     @objc | ||
|  |     private func didPressCancelButton(sender: UIButton) { | ||
|  |         Logger.info("") | ||
|  | 
 | ||
|  |         // TODO: Cancel import. | ||
|  | 
 | ||
| 
											7 years ago
										 |         cancelAndDismiss() | ||
|  |     } | ||
|  | 
 | ||
|  |     @objc | ||
|  |     private func cancelAndDismiss() { | ||
|  |         Logger.info("") | ||
|  | 
 | ||
|  |         backup.setHasPendingRestoreDecision(false) | ||
|  | 
 | ||
| 
											7 years ago
										 |         showHomeView() | ||
| 
											7 years ago
										 |     } | ||
|  | 
 | ||
| 
											7 years ago
										 |     @objc | ||
|  |     private func startImport() { | ||
|  |         Logger.info("") | ||
|  | 
 | ||
| 
											7 years ago
										 |         hasBegunImport = true | ||
|  | 
 | ||
| 
											7 years ago
										 |         backup.tryToImport() | ||
|  |     } | ||
|  | 
 | ||
| 
											7 years ago
										 |     private func showHomeView() { | ||
| 
											7 years ago
										 |         // In production, this view will never be presented in a modal. | ||
|  |         // During testing (debug UI, etc.), it may be a modal. | ||
| 
											7 years ago
										 |         let isModal = navigationController?.presentingViewController != nil | ||
|  |         if isModal { | ||
|  |             dismiss(animated: true, completion: { | ||
|  |                 SignalApp.shared().showHomeView() | ||
|  |             }) | ||
|  |         } else { | ||
|  |             SignalApp.shared().showHomeView() | ||
|  |         } | ||
| 
											7 years ago
										 | 
 | ||
|  |         NotificationCenter.default.removeObserver(self) | ||
| 
											7 years ago
										 |     } | ||
|  | 
 | ||
|  |     // MARK: - Notifications | ||
|  | 
 | ||
|  |     @objc func backupStateDidChange() { | ||
|  |         AssertIsOnMainThread() | ||
|  | 
 | ||
| 
											7 years ago
										 |         Logger.verbose("backup.backupImportState: \(NSStringForBackupImportState(backup.backupImportState))") | ||
|  |         Logger.flush() | ||
|  | 
 | ||
| 
											7 years ago
										 |         if backup.backupImportState == .succeeded { | ||
| 
											7 years ago
										 |             backup.setHasPendingRestoreDecision(false) | ||
|  | 
 | ||
| 
											7 years ago
										 |             showHomeView() | ||
|  |         } else { | ||
|  |             updateTableContents() | ||
|  |         } | ||
|  |     } | ||
| 
											7 years ago
										 | 
 | ||
|  |     // MARK: Orientation | ||
|  | 
 | ||
|  |     public override var supportedInterfaceOrientations: UIInterfaceOrientationMask { | ||
| 
											7 years ago
										 |         return .portrait | ||
| 
											7 years ago
										 |     } | ||
| 
											7 years ago
										 | } |