SafeActivatedViewController.swift 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 UIKit
  21. class SafeActivatedViewController: ThemedTableViewController {
  22. let refreshViewNotificationName = Notification.Name(kSafeBackupUIRefresh)
  23. @IBOutlet weak var backupNowCell: UITableViewCell!
  24. @IBOutlet weak var serverNameLabel: UILabel!
  25. @IBOutlet weak var serverNameValueLabel: UILabel!
  26. @IBOutlet weak var maxBackupBytesLabel: UILabel!
  27. @IBOutlet weak var maxBackupBytesValueLabel: UILabel!
  28. @IBOutlet weak var retentionDaysLabel: UILabel!
  29. @IBOutlet weak var retentionDaysValueLabel: UILabel!
  30. @IBOutlet weak var lastBackupLabel: UILabel!
  31. @IBOutlet weak var lastBackupValueLabel: UILabel!
  32. @IBOutlet weak var backupSizeLabel: UILabel!
  33. @IBOutlet weak var backupSizeValueLabel: UILabel!
  34. @IBOutlet weak var lastResultLabel: UILabel!
  35. @IBOutlet weak var lastResultValueLabel: UILabel!
  36. @IBOutlet weak var explainButton: UIButton!
  37. @IBOutlet weak var backupNowButtonLabel: UILabel!
  38. @IBOutlet weak var changePasswordButtonLabel: UILabel!
  39. @IBOutlet weak var activityIndicator: UIActivityIndicatorView!
  40. var tapGestureRecognizer: UITapGestureRecognizer!
  41. private var backupTimer: Timer?
  42. private var mdmSetup: MDMSetup
  43. required init?(coder aDecoder: NSCoder) {
  44. self.mdmSetup = MDMSetup(setup: false)
  45. super.init(coder: aDecoder)
  46. }
  47. override func viewDidLoad() {
  48. super.viewDidLoad()
  49. self.serverNameLabel.text = NSLocalizedString("safe_server_name", comment: "")
  50. self.maxBackupBytesLabel.text = NSLocalizedString("safe_max_backup_size", comment: "")
  51. self.retentionDaysLabel.text = NSLocalizedString("safe_retention", comment: "")
  52. self.lastBackupLabel.text = NSLocalizedString("safe_last_backup", comment: "")
  53. self.backupSizeLabel.text = NSLocalizedString("safe_size", comment: "")
  54. self.lastResultLabel.text = NSLocalizedString("safe_result", comment: "")
  55. self.backupNowButtonLabel.text = NSLocalizedString("safe_backup_now", comment: "")
  56. self.changePasswordButtonLabel.text = NSLocalizedString("safe_change_password", comment: "")
  57. self.activityIndicator.hidesWhenStopped = true
  58. NotificationCenter.default.addObserver(self, selector: #selector(self.refreshViewNotification(notification:)), name: self.refreshViewNotificationName, object: nil)
  59. }
  60. deinit {
  61. NotificationCenter.default.removeObserver(self, name: self.refreshViewNotificationName, object: nil)
  62. }
  63. // MARK: - Public
  64. func backupNow() {
  65. // object: 0 -> 0s backup delay and force it
  66. // show activity on backup cell
  67. self.activityIndicator.startAnimating()
  68. self.backupNowButtonLabel.isHidden = true
  69. backupNowCell.isUserInteractionEnabled = false
  70. // start timer to hide activity on backup cell if backup is to fast
  71. backupTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(maybeHideActivityIndicator), userInfo: nil, repeats: false)
  72. NotificationCenter.default.post(name: NSNotification.Name(kSafeBackupTrigger), object: 0)
  73. }
  74. @objc func refreshViewNotification(notification: Notification) {
  75. refreshView(updateCell: true)
  76. }
  77. @objc override func refresh() {
  78. super.refresh()
  79. setupColors()
  80. }
  81. @objc func refreshView(updateCell: Bool) {
  82. let hyphen: String = "-"
  83. let safeConfigManager = SafeConfigManager()
  84. let safeStore = SafeStore(safeConfigManager: safeConfigManager, serverApiConnector: ServerAPIConnector())
  85. let safeManager = SafeManager(safeConfigManager: safeConfigManager, safeStore: safeStore, safeApiService: SafeApiService())
  86. if updateCell {
  87. if safeManager.isBackupRunning {
  88. self.activityIndicator.startAnimating()
  89. self.backupNowButtonLabel.isHidden = true
  90. backupNowCell.isUserInteractionEnabled = false
  91. } else {
  92. if backupTimer == nil {
  93. self.activityIndicator.stopAnimating()
  94. self.backupNowButtonLabel.isHidden = false
  95. backupNowCell.isUserInteractionEnabled = true
  96. }
  97. }
  98. }
  99. guard safeConfigManager.getKey() != nil else {
  100. self.serverNameValueLabel.text = hyphen
  101. self.maxBackupBytesValueLabel.text = hyphen
  102. self.retentionDaysValueLabel.text = hyphen
  103. self.lastBackupValueLabel.text = hyphen
  104. self.lastResultValueLabel.text = hyphen
  105. return
  106. }
  107. self.serverNameValueLabel.text = safeStore.getSafeServerToDisplay()
  108. self.maxBackupBytesValueLabel.text = safeConfigManager.getMaxBackupBytes() != nil ? String.localizedStringWithFormat("%1.2f KB", Float(safeConfigManager.getMaxBackupBytes()!) / 1024) : hyphen
  109. self.retentionDaysValueLabel.text = safeConfigManager.getRetentionDays() != nil ? String.localizedStringWithFormat(NSLocalizedString("number_of_days", comment: ""), safeConfigManager.getRetentionDays()!) : hyphen
  110. self.backupSizeValueLabel.text = safeConfigManager.getBackupSize() != nil ? String.localizedStringWithFormat(NSLocalizedString("%1.2f KB", comment: ""), Float(safeConfigManager.getBackupSize()!) / 1024) : hyphen
  111. self.lastBackupValueLabel.text = safeConfigManager.getLastBackup() != nil ? DateFormatter.mediumStyleDateShortStyleTime(safeConfigManager.getLastBackup()!) : hyphen
  112. self.lastResultValueLabel.text = safeConfigManager.getLastResult() != nil ? safeConfigManager.getLastResult()! : hyphen
  113. self.tableView.reloadData()
  114. setupColors()
  115. }
  116. @objc private func maybeHideActivityIndicator() {
  117. let safeConfigManager = SafeConfigManager()
  118. let safeStore = SafeStore(safeConfigManager: safeConfigManager, serverApiConnector: ServerAPIConnector())
  119. let safeManager = SafeManager(safeConfigManager: safeConfigManager, safeStore: safeStore, safeApiService: SafeApiService())
  120. if !safeManager.isBackupRunning {
  121. self.activityIndicator.stopAnimating()
  122. self.backupNowButtonLabel.isHidden = false
  123. backupNowCell.isUserInteractionEnabled = true
  124. }
  125. backupTimer = nil
  126. }
  127. private func setupColors() {
  128. let explainImage = self.explainButton.imageView?.image!.withTint(Colors.main())
  129. self.explainButton.setImage(explainImage, for: .normal)
  130. serverNameValueLabel.textColor = Colors.fontLight()
  131. maxBackupBytesValueLabel.textColor = Colors.fontLight()
  132. retentionDaysValueLabel.textColor = Colors.fontLight()
  133. lastBackupValueLabel.textColor = Colors.fontLight()
  134. backupSizeValueLabel.textColor = Colors.fontLight()
  135. lastResultValueLabel.textColor = Colors.fontLight()
  136. switch Colors.getTheme() {
  137. case ColorThemeDark, ColorThemeDarkWork:
  138. activityIndicator.style = .white
  139. break
  140. case ColorThemeUndefined, ColorThemeLight, ColorThemeLightWork:
  141. activityIndicator.style = .gray
  142. break
  143. default:
  144. activityIndicator.style = .gray
  145. break
  146. }
  147. if self.lastResultValueLabel.text == NSLocalizedString("safe_successful", comment: "") {
  148. self.lastResultValueLabel.textColor = Colors.green()
  149. } else if self.lastResultValueLabel.text != "-" {
  150. self.lastResultValueLabel.textColor = UIColor.red
  151. }
  152. }
  153. // MARK: UITableViewDelegates
  154. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  155. switch indexPath.section {
  156. case 2:
  157. return indexPath.row == 2 && self.mdmSetup.isSafeBackupPasswordPreset() ? 0.0 : UITableView.automaticDimension
  158. default:
  159. return UITableView.automaticDimension
  160. }
  161. }
  162. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  163. if indexPath.section == 2 {
  164. switch indexPath.row {
  165. case 0:
  166. UIAlertTemplate.showAlert(owner: self, title: "Threema Safe", message: NSLocalizedString("safe_enable_explain", comment: ""))
  167. break
  168. case 1:
  169. backupNow()
  170. break
  171. case 2:
  172. if !self.mdmSetup.isSafeBackupPasswordPreset(),
  173. let safeSetupViewController = self.parent as? SafeSetupViewController {
  174. safeSetupViewController .performSegue(withIdentifier: "SafeSetupPassword", sender: nil)
  175. }
  176. break
  177. default: break
  178. }
  179. tableView.deselectRow(at: indexPath, animated: true)
  180. }
  181. }
  182. override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  183. Colors.update(cell)
  184. setupColors()
  185. }
  186. }
  187. extension SafeActivatedViewController {
  188. @IBAction func touchDownExplainButton(_ sender: UIButton, forEvent event: UIEvent) {
  189. UIAlertTemplate.showAlert(owner: self, title: "Threema Safe", message: NSLocalizedString("safe_enable_explain", comment: ""))
  190. }
  191. }