SafeSetupViewController.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2018-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. import UIKit
  22. class SafeSetupViewController: ThemedViewController {
  23. @IBOutlet weak var safeImageView: UIImageView!
  24. @IBOutlet weak var safeImageCircle: UIView!
  25. @IBOutlet weak var safeSwitchLabel: UILabel!
  26. @IBOutlet weak var safeSwitch: UISwitch!
  27. @IBOutlet weak var safeActivatedContainer: UIView!
  28. @IBOutlet weak var safeDeactivatedContainer: UIView!
  29. @IBOutlet weak var safeSeparatorView: UIView!
  30. var safeManager: SafeManager
  31. var mdmSetup: MDMSetup
  32. required init?(coder aDecoder: NSCoder) {
  33. let safeConfigManager = SafeConfigManager()
  34. let safeStore = SafeStore(safeConfigManager: safeConfigManager, serverApiConnector: ServerAPIConnector())
  35. self.safeManager = SafeManager(safeConfigManager: safeConfigManager, safeStore: safeStore, safeApiService: SafeApiService())
  36. self.mdmSetup = MDMSetup(setup: false)
  37. super.init(coder: aDecoder)
  38. }
  39. override func viewDidLoad() {
  40. super.viewDidLoad()
  41. self.title = "Threema Safe"
  42. self.safeImageCircle.layer.cornerRadius = self.safeImageCircle.frame.height / 2
  43. self.safeSwitch.isEnabled = !self.mdmSetup.isSafeBackupForce()
  44. }
  45. override func viewWillAppear(_ animated: Bool) {
  46. refreshView()
  47. setupColor()
  48. }
  49. //MARK: - Navigation
  50. override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
  51. guard identifier == "SafeSetupPassword",
  52. sender is UISwitch else {
  53. return true
  54. }
  55. if self.safeManager.isActivated {
  56. //confirm to delete backup
  57. UIAlertTemplate.showConfirm(owner: self, popOverSource: self.safeSwitch!, title: NSLocalizedString("safe_deactivate", comment: ""), message: NSLocalizedString("safe_deactivate_explain", comment: ""), titleOk: NSLocalizedString("deactivate", comment: ""), actionOk: { (action) in
  58. self.safeManager.deactivate()
  59. self.refreshView()
  60. }, titleCancel: NSLocalizedString("cancel", comment: ""), actionCancel: { (action) in self.safeSwitch.isOn = true })
  61. return false
  62. }
  63. return true
  64. }
  65. func refreshView() {
  66. guard self.safeManager.isActivated else {
  67. self.safeActivatedContainer.isHidden = true
  68. self.safeDeactivatedContainer.isHidden = false
  69. self.safeSwitch.isOn = false
  70. return
  71. }
  72. self.safeActivatedContainer.isHidden = false
  73. // refresh safe activated container view controller and start backup
  74. safeActivatedViewController()?.refreshView(updateCell: true)
  75. self.safeDeactivatedContainer.isHidden = true
  76. self.safeSwitch.isOn = true
  77. }
  78. @objc override func refresh() {
  79. super.refresh()
  80. for childViewController in self.children {
  81. if let viewController = childViewController as? SafeActivatedViewController {
  82. viewController.refresh()
  83. }
  84. if let viewController = childViewController as? SafeDeactivatedViewController {
  85. viewController.refresh()
  86. }
  87. }
  88. setupColor()
  89. }
  90. // MARK: Private functions
  91. private func safeActivatedViewController() -> SafeActivatedViewController? {
  92. for childViewController in self.children {
  93. if let viewController = childViewController as? SafeActivatedViewController {
  94. return viewController
  95. }
  96. }
  97. return nil
  98. }
  99. private func setupColor() {
  100. safeSeparatorView.backgroundColor = Colors.hairline()
  101. safeImageCircle.backgroundColor = UIColor(red: 235.0/255.0, green: 235.0/255.0, blue: 235.0/255.0, alpha: 1.0)
  102. safeImageView.bringSubviewToFront(self.view)
  103. }
  104. }
  105. extension SafeSetupViewController {
  106. @IBAction func doneSafeSetupPassword(_ segue: UIStoryboardSegue) {
  107. guard segue.source is SafeSetupPasswordViewController else {
  108. return
  109. }
  110. safeActivatedViewController()?.backupNow()
  111. self.refreshView()
  112. }
  113. }