NCUserStatus.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. //
  2. // NCUserStatus.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 25/05/21.
  6. // Copyright © 2021 Marino Faggiana. All rights reserved.
  7. //
  8. //
  9. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  10. //
  11. // This program is free software: you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published by
  13. // the Free Software Foundation, either version 3 of the License, or
  14. // (at your option) any later version.
  15. //
  16. // This program is distributed in the hope that it will be useful,
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. // GNU General Public License for more details.
  20. //
  21. // You should have received a copy of the GNU General Public License
  22. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. //
  24. import UIKit
  25. import Foundation
  26. import NCCommunication
  27. @available(iOS 13.0, *)
  28. class NCUserStatus: UIViewController {
  29. @IBOutlet weak var onlineButton: UIButton!
  30. @IBOutlet weak var onlineImage: UIImageView!
  31. @IBOutlet weak var onlineLabel: UILabel!
  32. @IBOutlet weak var awayButton: UIButton!
  33. @IBOutlet weak var awayImage: UIImageView!
  34. @IBOutlet weak var awayLabel: UILabel!
  35. @IBOutlet weak var dndButton: UIButton!
  36. @IBOutlet weak var dndImage: UIImageView!
  37. @IBOutlet weak var dndLabel: UILabel!
  38. @IBOutlet weak var dndDescrLabel: UILabel!
  39. @IBOutlet weak var invisibleButton: UIButton!
  40. @IBOutlet weak var invisibleImage: UIImageView!
  41. @IBOutlet weak var invisibleLabel: UILabel!
  42. @IBOutlet weak var invisibleDescrLabel: UILabel!
  43. @IBOutlet weak var statusMessageLabel: UILabel!
  44. @IBOutlet weak var statusMessageEmojiTextField: emojiTextField!
  45. @IBOutlet weak var statusMessageTextField: UITextField!
  46. @IBOutlet weak var tableView: UITableView!
  47. @IBOutlet weak var clearStatusMessageAfterLabel: UILabel!
  48. @IBOutlet weak var clearStatusMessageAfterText: UILabel!
  49. @IBOutlet weak var clearStatusMessageButton: UIButton!
  50. @IBOutlet weak var setStatusMessageButton: UIButton!
  51. private var statusPredefinedStatuses: [NCCommunicationUserStatus] = []
  52. // MARK: - View Life Cycle
  53. override func viewDidLoad() {
  54. super.viewDidLoad()
  55. self.navigationItem.title = NSLocalizedString("_online_status_", comment: "")
  56. onlineButton.layer.cornerRadius = 10
  57. onlineButton.layer.masksToBounds = true
  58. onlineButton.backgroundColor = NCBrandColor.shared.systemGray5
  59. //onlineLabel.layer.borderWidth = 0.5
  60. //onlineLabel.layer.borderColor = NCBrandColor.shared.brand.cgColor
  61. let onLine = NCUtility.shared.getUserStatus(userIcon: nil, userStatus: "online", userMessage: nil)
  62. onlineImage.image = onLine.onlineStatus
  63. onlineLabel.text = onLine.statusMessage
  64. onlineLabel.textColor = NCBrandColor.shared.label
  65. awayButton.layer.cornerRadius = 10
  66. awayButton.layer.masksToBounds = true
  67. awayButton.backgroundColor = NCBrandColor.shared.systemGray5
  68. let away = NCUtility.shared.getUserStatus(userIcon: nil, userStatus: "away", userMessage: nil)
  69. awayImage.image = away.onlineStatus
  70. awayLabel.text = away.statusMessage
  71. awayLabel.textColor = NCBrandColor.shared.label
  72. dndButton.layer.cornerRadius = 10
  73. dndButton.layer.masksToBounds = true
  74. dndButton.backgroundColor = NCBrandColor.shared.systemGray5
  75. let dnd = NCUtility.shared.getUserStatus(userIcon: nil, userStatus: "dnd", userMessage: nil)
  76. dndImage.image = dnd.onlineStatus
  77. dndLabel.text = dnd.statusMessage
  78. dndLabel.textColor = NCBrandColor.shared.label
  79. dndDescrLabel.text = dnd.descriptionMessage
  80. dndDescrLabel.textColor = .darkGray
  81. invisibleButton.layer.cornerRadius = 10
  82. invisibleButton.layer.masksToBounds = true
  83. invisibleButton.backgroundColor = NCBrandColor.shared.systemGray5
  84. let offline = NCUtility.shared.getUserStatus(userIcon: nil, userStatus: "offline", userMessage: nil)
  85. invisibleImage.image = offline.onlineStatus
  86. invisibleLabel.text = offline.statusMessage
  87. invisibleLabel.textColor = NCBrandColor.shared.label
  88. invisibleDescrLabel.text = offline.descriptionMessage
  89. invisibleDescrLabel.textColor = .darkGray
  90. statusMessageLabel.text = NSLocalizedString("_status_message_", comment: "")
  91. statusMessageLabel.textColor = NCBrandColor.shared.label
  92. statusMessageEmojiTextField.delegate = self
  93. statusMessageEmojiTextField.backgroundColor = NCBrandColor.shared.systemGray5
  94. statusMessageTextField.placeholder = NSLocalizedString("_status_message_placehorder_", comment: "")
  95. statusMessageTextField.textColor = NCBrandColor.shared.label
  96. tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1))
  97. tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  98. clearStatusMessageAfterLabel.text = NSLocalizedString("_clear_status_message_after_", comment: "")
  99. clearStatusMessageAfterLabel.textColor = NCBrandColor.shared.label
  100. clearStatusMessageAfterText.layer.cornerRadius = 5
  101. clearStatusMessageAfterText.layer.masksToBounds = true
  102. clearStatusMessageAfterText.layer.borderWidth = 0.2
  103. clearStatusMessageAfterText.layer.borderColor = UIColor.lightGray.cgColor
  104. clearStatusMessageAfterText.text = NSLocalizedString("_dont_clear_", comment: "")
  105. clearStatusMessageAfterText.textColor = .lightGray
  106. clearStatusMessageButton.layer.cornerRadius = 15
  107. clearStatusMessageButton.layer.masksToBounds = true
  108. clearStatusMessageButton.layer.borderWidth = 0.5
  109. clearStatusMessageButton.layer.borderColor = UIColor.darkGray.cgColor
  110. clearStatusMessageButton.backgroundColor = NCBrandColor.shared.systemGray5
  111. clearStatusMessageButton.setTitle(NSLocalizedString("_clear_status_message_", comment: ""), for: .normal)
  112. clearStatusMessageButton.setTitleColor(NCBrandColor.shared.label, for: .normal)
  113. setStatusMessageButton.layer.cornerRadius = 15
  114. setStatusMessageButton.layer.masksToBounds = true
  115. setStatusMessageButton.backgroundColor = NCBrandColor.shared.brand
  116. setStatusMessageButton.setTitle(NSLocalizedString("_set_status_message_", comment: ""), for: .normal)
  117. setStatusMessageButton.setTitleColor(NCBrandColor.shared.brandText, for: .normal)
  118. changeTheming()
  119. getStatus()
  120. }
  121. func dismissWithError(_ errorCode: Int, errorDescription: String) {
  122. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  123. self.dismiss(animated: true) {
  124. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode, forced: true)
  125. }
  126. }
  127. }
  128. // MARK: - Theming
  129. @objc func changeTheming() {
  130. view.backgroundColor = NCBrandColor.shared.systemBackground
  131. tableView.backgroundColor = NCBrandColor.shared.systemBackground
  132. tableView.reloadData()
  133. }
  134. // MARK: - Networking
  135. func getStatus() {
  136. NCCommunication.shared.getUserStatus { account, clearAt, icon, message, messageId, messageIsPredefined, status, statusIsUserDefined, userId, errorCode, errorDescription in
  137. if errorCode == 0 {
  138. self.statusMessageEmojiTextField.text = icon
  139. self.statusMessageTextField.text = message
  140. self.clearStatusMessageAfterText.text = " " + CCUtility.getTitleSectionDate(clearAt! as Date)
  141. switch status {
  142. case "online":
  143. self.onlineButton.layer.borderWidth = 1.5
  144. self.onlineButton.layer.borderColor = NCBrandColor.shared.brand.cgColor
  145. case "away":
  146. self.awayButton.layer.borderWidth = 1.5
  147. self.awayButton.layer.borderColor = NCBrandColor.shared.brand.cgColor
  148. case "dnd":
  149. self.dndButton.layer.borderWidth = 1.5
  150. self.dndButton.layer.borderColor = NCBrandColor.shared.brand.cgColor
  151. case "invisible":
  152. self.invisibleButton.layer.borderWidth = 1.5
  153. self.invisibleButton.layer.borderColor = NCBrandColor.shared.brand.cgColor
  154. default:
  155. print("No status")
  156. }
  157. NCCommunication.shared.getUserStatusPredefinedStatuses { account, userStatuses, errorCode, errorDescription in
  158. if errorCode == 0 {
  159. if let userStatuses = userStatuses {
  160. self.statusPredefinedStatuses = userStatuses
  161. }
  162. self.tableView.reloadData()
  163. } else {
  164. self.dismissWithError(errorCode, errorDescription: errorDescription)
  165. }
  166. }
  167. } else {
  168. self.dismissWithError(errorCode, errorDescription: errorDescription)
  169. }
  170. }
  171. }
  172. }
  173. @available(iOS 13.0, *)
  174. extension NCUserStatus: UITextFieldDelegate {
  175. func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
  176. if textField is emojiTextField {
  177. if string.count == 0 {
  178. textField.text = "😀"
  179. return false
  180. }
  181. textField.text = string
  182. }
  183. return true
  184. }
  185. }
  186. @available(iOS 13.0, *)
  187. class emojiTextField: UITextField {
  188. // required for iOS 13
  189. override var textInputContextIdentifier: String? { "" } // return non-nil to show the Emoji keyboard ¯\_(ツ)_/¯
  190. override var textInputMode: UITextInputMode? {
  191. for mode in UITextInputMode.activeInputModes {
  192. if mode.primaryLanguage == "emoji" {
  193. return mode
  194. }
  195. }
  196. return nil
  197. }
  198. override init(frame: CGRect) {
  199. super.init(frame: frame)
  200. commonInit()
  201. }
  202. required init?(coder: NSCoder) {
  203. super.init(coder: coder)
  204. commonInit()
  205. }
  206. func commonInit() {
  207. NotificationCenter.default.addObserver(self, selector: #selector(inputModeDidChange), name: UITextInputMode.currentInputModeDidChangeNotification, object: nil)
  208. }
  209. @objc func inputModeDidChange(_ notification: Notification) {
  210. guard isFirstResponder else {
  211. return
  212. }
  213. DispatchQueue.main.async { [weak self] in
  214. self?.reloadInputViews()
  215. }
  216. }
  217. }
  218. @available(iOS 13.0, *)
  219. extension NCUserStatus: UITableViewDelegate {
  220. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  221. return 45
  222. }
  223. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  224. let status = statusPredefinedStatuses[indexPath.row]
  225. }
  226. }
  227. @available(iOS 13.0, *)
  228. extension NCUserStatus: UITableViewDataSource {
  229. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  230. return statusPredefinedStatuses.count
  231. }
  232. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  233. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
  234. cell.backgroundColor = tableView.backgroundColor
  235. let status = statusPredefinedStatuses[indexPath.row]
  236. let icon = cell.viewWithTag(10) as! UILabel
  237. let message = cell.viewWithTag(20) as! UILabel
  238. icon.text = status.icon
  239. var timeString = getPredefinedClearStatusText(clearAt: status.clearAt, clearAtTime: status.clearAtTime, clearAtType: status.clearAtType)
  240. if let messageText = status.message {
  241. message.text = messageText
  242. timeString = " - " + timeString
  243. let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: messageText + timeString)
  244. attributedString.setColor(color: .lightGray, font: UIFont.systemFont(ofSize: 15), forText: timeString)
  245. message.attributedText = attributedString
  246. }
  247. return cell
  248. }
  249. func getPredefinedClearStatusText(clearAt: NSDate?, clearAtTime: String?, clearAtType: String?) -> String {
  250. // Date
  251. if clearAt != nil {
  252. return CCUtility.getTitleSectionDate(clearAt! as Date)
  253. }
  254. // Period
  255. if clearAt == nil && clearAtType == "period" {
  256. switch clearAtTime {
  257. case "3600":
  258. return NSLocalizedString("_1_hour_", comment: "")
  259. case "1800":
  260. return NSLocalizedString("_30_minutes_", comment: "")
  261. default:
  262. return NSLocalizedString("_dont_clear_", comment: "")
  263. }
  264. }
  265. // End of
  266. if clearAt == nil && clearAtTime != nil && clearAtType == "end-of" {
  267. return NSLocalizedString(clearAtTime!, comment: "")
  268. }
  269. return NSLocalizedString("_dont_clear_", comment: "")
  270. }
  271. }