WebClientSessionCell.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. class WebClientSessionCell: UITableViewCell {
  22. @IBOutlet weak var browserIcon: UIImageView!
  23. @IBOutlet weak var loadingIndicator: UIActivityIndicatorView!
  24. @IBOutlet weak var browserLabel: UILabel!
  25. @IBOutlet weak var infoLabel: UILabel!
  26. var webClientSession: WebClientSession!
  27. var viewController: ThreemaWebViewController!
  28. func setupCell() {
  29. loadingIndicator.isHidden = true
  30. loadingIndicator.stopAnimating()
  31. switch Colors.getTheme() {
  32. case ColorThemeDark, ColorThemeDarkWork:
  33. loadingIndicator.style = .white
  34. case ColorThemeLight, ColorThemeLightWork, ColorThemeUndefined:
  35. loadingIndicator.style = .gray
  36. default:
  37. loadingIndicator.style = .gray
  38. }
  39. if webClientSession.name != nil {
  40. browserLabel.text = webClientSession.name
  41. } else {
  42. browserLabel.text = NSLocalizedString("webClientSession_unnamed", comment: "")
  43. }
  44. var info: String?
  45. if let lastConenction = webClientSession.lastConnection {
  46. info = String(format: "%@: %@\n%@", NSLocalizedString("webClientSession_lastUse", comment: ""), DateFormatter.shortStyleDateTime(lastConenction), (webClientSession.permanent?.boolValue)! ? NSLocalizedString("webClientSession_saved", comment: "") : NSLocalizedString("webClientSession_notSaved", comment: ""))
  47. } else {
  48. info = String(format: "\n%@", (webClientSession.permanent?.boolValue)! ? NSLocalizedString("webClientSession_saved", comment: "") : NSLocalizedString("webClientSession_notSaved", comment: ""))
  49. }
  50. infoLabel.text = info
  51. switch webClientSession.browserName {
  52. case "chrome":
  53. browserIcon.image = UIImage(named: "Chrome")
  54. case "safari":
  55. browserIcon.image = UIImage(named: "Safari")
  56. case "firefox":
  57. browserIcon.image = UIImage(named: "FireFox")
  58. case "edge":
  59. browserIcon.image = UIImage(named: "Edge")
  60. case "opera":
  61. browserIcon.image = UIImage(named: "Opera")
  62. default:
  63. if webClientSession.isConnecting {
  64. browserIcon.image = nil
  65. loadingIndicator.startAnimating()
  66. loadingIndicator.isHidden = false
  67. } else {
  68. browserIcon.image = UIImage(named: "ExclamationMark")
  69. }
  70. }
  71. var cellAccessibility: String! = browserLabel.text
  72. var cellAccessibilityActions = [UIAccessibilityCustomAction]()
  73. if let sessionInfo = info {
  74. cellAccessibility.append(". ")
  75. cellAccessibility.append(sessionInfo)
  76. }
  77. if (webClientSession.active?.boolValue)! {
  78. accessoryType = .none
  79. accessoryView = UIImageView(image: UIImage(named: "WebClientConnection", in: Colors.main()))
  80. cellAccessibility.append(". ")
  81. cellAccessibility.append(NSLocalizedString("status_loggedin", comment: ""))
  82. cellAccessibilityActions.append(UIAccessibilityCustomAction(name: NSLocalizedString("webClientSession_actionSheet_stopSession", comment: ""), target: self, selector: #selector(handleStopSession)))
  83. } else {
  84. accessoryView = nil
  85. accessoryType = UIAccessibility.isVoiceOverRunning ? .none : .disclosureIndicator
  86. }
  87. self.accessibilityLabel = cellAccessibility
  88. cellAccessibilityActions.append(UIAccessibilityCustomAction(name: NSLocalizedString("webClientSession_actionSheet_renameSession", comment: ""), target: self, selector: #selector(handleRenameSession)))
  89. self.accessibilityCustomActions = cellAccessibilityActions
  90. }
  91. @objc private func handleStopSession() {
  92. ValidationLogger.shared().logString("Threema Web: Disconnect webclient userStoppedSession")
  93. WCSessionManager.shared.stopSession(webClientSession)
  94. }
  95. @objc private func handleRenameSession() {
  96. let renameAlert = UIAlertController(title: NSLocalizedString("webClientSession_sessionName", comment: ""), message: nil, preferredStyle: .alert)
  97. renameAlert.addTextField { textfield in
  98. if let sessionName = self.webClientSession.name {
  99. textfield.text = sessionName
  100. } else {
  101. textfield.placeholder = NSLocalizedString("webClientSession_unnamed", comment: "")
  102. }
  103. }
  104. let saveAction = UIAlertAction(title: NSLocalizedString("save", comment: ""), style: .default) { alertAction in
  105. let textField = renameAlert.textFields![0]
  106. WebClientSessionStore.shared.updateWebClientSession(session: self.webClientSession, sessionName: textField.text)
  107. }
  108. renameAlert.addAction(saveAction)
  109. let cancelAction = UIAlertAction(title: NSLocalizedString("cancel", comment: ""), style: .cancel)
  110. renameAlert.addAction(cancelAction)
  111. viewController.present(renameAlert, animated: true)
  112. }
  113. @objc private func handleDeleteSession() {
  114. WCSessionManager.shared.stopAndDeleteSession(webClientSession)
  115. if viewController.fetchedResultsController!.fetchedObjects!.count == 0 {
  116. UserSettings.shared().threemaWeb = false
  117. }
  118. }
  119. }