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.
		
		
		
		
		
			
		
			
	
	
		
			90 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			Swift
		
	
		
		
			
		
	
	
			90 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			Swift
		
	
| 
											6 years ago
										 | 
 | ||
|  | @objc(LKSeedModal) | ||
|  | final class SeedModal : Modal { | ||
|  |      | ||
|  |     private let mnemonic: String = { | ||
|  |         let identityManager = OWSIdentityManager.shared() | ||
|  |         let databaseConnection = identityManager.value(forKey: "dbConnection") as! YapDatabaseConnection | ||
|  |         var hexEncodedSeed: String! = databaseConnection.object(forKey: "LKLokiSeed", inCollection: OWSPrimaryStorageIdentityKeyStoreCollection) as! String? | ||
|  |         if hexEncodedSeed == nil { | ||
|  |             hexEncodedSeed = identityManager.identityKeyPair()!.hexEncodedPrivateKey // Legacy account | ||
|  |         } | ||
|  |         return Mnemonic.encode(hexEncodedString: hexEncodedSeed) | ||
|  |     }() | ||
|  |      | ||
|  |     // MARK: Lifecycle | ||
|  |     override func populateContentView() { | ||
|  |         // Set up title label | ||
|  |         let titleLabel = UILabel() | ||
|  |         titleLabel.textColor = Colors.text | ||
|  |         titleLabel.font = .boldSystemFont(ofSize: Values.mediumFontSize) | ||
| 
											5 years ago
										 |         titleLabel.text = NSLocalizedString("modal_seed_title", comment: "") | ||
| 
											6 years ago
										 |         titleLabel.numberOfLines = 0 | ||
|  |         titleLabel.lineBreakMode = .byWordWrapping | ||
|  |         titleLabel.textAlignment = .center | ||
|  |         // Set up mnemonic label | ||
|  |         let mnemonicLabel = UILabel() | ||
|  |         mnemonicLabel.textColor = Colors.text | ||
| 
											5 years ago
										 |         mnemonicLabel.font = Fonts.spaceMono(ofSize: Values.smallFontSize) | ||
| 
											6 years ago
										 |         mnemonicLabel.text = mnemonic | ||
|  |         mnemonicLabel.numberOfLines = 0 | ||
|  |         mnemonicLabel.lineBreakMode = .byWordWrapping | ||
|  |         mnemonicLabel.textAlignment = .center | ||
| 
											5 years ago
										 |         // Set up mnemonic label container | ||
|  |         let mnemonicLabelContainer = UIView() | ||
|  |         mnemonicLabelContainer.addSubview(mnemonicLabel) | ||
|  |         mnemonicLabel.pin(to: mnemonicLabelContainer, withInset: isIPhone6OrSmaller ? 4 : Values.smallSpacing) | ||
|  |         mnemonicLabelContainer.layer.cornerRadius = Values.textFieldCornerRadius | ||
|  |         mnemonicLabelContainer.layer.borderWidth = Values.borderThickness | ||
|  |         mnemonicLabelContainer.layer.borderColor = Colors.text.cgColor | ||
| 
											6 years ago
										 |         // Set up explanation label | ||
|  |         let explanationLabel = UILabel() | ||
|  |         explanationLabel.textColor = Colors.text.withAlphaComponent(Values.unimportantElementOpacity) | ||
|  |         explanationLabel.font = .systemFont(ofSize: Values.smallFontSize) | ||
| 
											5 years ago
										 |         explanationLabel.text = NSLocalizedString("modal_seed_explanation", comment: "") | ||
| 
											6 years ago
										 |         explanationLabel.numberOfLines = 0 | ||
|  |         explanationLabel.lineBreakMode = .byWordWrapping | ||
|  |         explanationLabel.textAlignment = .center | ||
|  |         // Set up copy button | ||
|  |         let copyButton = UIButton() | ||
|  |         copyButton.set(.height, to: Values.mediumButtonHeight) | ||
|  |         copyButton.layer.cornerRadius = Values.modalButtonCornerRadius | ||
|  |         copyButton.backgroundColor = Colors.buttonBackground | ||
|  |         copyButton.titleLabel!.font = .systemFont(ofSize: Values.smallFontSize) | ||
|  |         copyButton.setTitleColor(Colors.text, for: UIControl.State.normal) | ||
| 
											5 years ago
										 |         copyButton.setTitle(NSLocalizedString("copy", comment: ""), for: UIControl.State.normal) | ||
| 
											6 years ago
										 |         copyButton.addTarget(self, action: #selector(copySeed), for: UIControl.Event.touchUpInside) | ||
|  |         // Set up button stack view | ||
|  |         let buttonStackView = UIStackView(arrangedSubviews: [ cancelButton, copyButton ]) | ||
|  |         buttonStackView.axis = .horizontal | ||
|  |         buttonStackView.spacing = Values.mediumSpacing | ||
|  |         buttonStackView.distribution = .fillEqually | ||
| 
											5 years ago
										 |         // Set up explanation label | ||
|  |         let disclaimerLabel = UILabel() | ||
| 
											5 years ago
										 |         disclaimerLabel.textColor = Colors.text.withAlphaComponent(Values.unimportantElementOpacity) | ||
| 
											5 years ago
										 |         disclaimerLabel.font = .systemFont(ofSize: 10) | ||
| 
											5 years ago
										 |         disclaimerLabel.text = "It is not possible to use the same Session ID on multiple devices simultaneously" | ||
| 
											5 years ago
										 |         disclaimerLabel.numberOfLines = 0 | ||
|  |         disclaimerLabel.lineBreakMode = .byWordWrapping | ||
|  |         disclaimerLabel.textAlignment = .center | ||
| 
											6 years ago
										 |         // Set up stack view | ||
| 
											5 years ago
										 |         let stackView = UIStackView(arrangedSubviews: [ titleLabel, mnemonicLabelContainer, explanationLabel, buttonStackView, disclaimerLabel ]) | ||
| 
											6 years ago
										 |         stackView.axis = .vertical | ||
|  |         stackView.spacing = Values.largeSpacing | ||
|  |         contentView.addSubview(stackView) | ||
|  |         stackView.pin(.leading, to: .leading, of: contentView, withInset: Values.largeSpacing) | ||
|  |         stackView.pin(.top, to: .top, of: contentView, withInset: Values.largeSpacing) | ||
|  |         contentView.pin(.trailing, to: .trailing, of: stackView, withInset: Values.largeSpacing) | ||
|  |         contentView.pin(.bottom, to: .bottom, of: stackView, withInset: Values.largeSpacing) | ||
| 
											6 years ago
										 |         // Mark seed as viewed | ||
| 
											6 years ago
										 |         UserDefaults.standard[.hasViewedSeed] = true | ||
| 
											6 years ago
										 |         NotificationCenter.default.post(name: .seedViewed, object: nil) | ||
| 
											6 years ago
										 |     } | ||
|  |      | ||
|  |     // MARK: Interaction | ||
|  |     @objc private func copySeed() { | ||
|  |         UIPasteboard.general.string = mnemonic | ||
|  |         dismiss(animated: true, completion: nil) | ||
|  |     } | ||
|  | } |