NCUserStatus.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 buttonCancel: UIBarButtonItem!
  30. @IBOutlet weak var onlineButton: UIButton!
  31. @IBOutlet weak var onlineImage: UIImageView!
  32. @IBOutlet weak var onlineLabel: UILabel!
  33. @IBOutlet weak var awayButton: UIButton!
  34. @IBOutlet weak var awayImage: UIImageView!
  35. @IBOutlet weak var awayLabel: UILabel!
  36. @IBOutlet weak var dndButton: UIButton!
  37. @IBOutlet weak var dndImage: UIImageView!
  38. @IBOutlet weak var dndLabel: UILabel!
  39. @IBOutlet weak var dndDescrLabel: UILabel!
  40. @IBOutlet weak var invisibleButton: UIButton!
  41. @IBOutlet weak var invisibleImage: UIImageView!
  42. @IBOutlet weak var invisibleLabel: UILabel!
  43. @IBOutlet weak var invisibleDescrLabel: UILabel!
  44. @IBOutlet weak var statusMessageLabel: UILabel!
  45. @IBOutlet weak var statusMessageEmojiTextField: emojiTextField!
  46. @IBOutlet weak var statusMessageTextField: UITextField!
  47. @IBOutlet weak var tableView: UITableView!
  48. @IBOutlet weak var clearStatusMessageAfterLabel: UILabel!
  49. @IBOutlet weak var clearStatusMessageAfterText: UILabel!
  50. @IBOutlet weak var clearStatusMessageButton: UIButton!
  51. @IBOutlet weak var setStatusMessageButton: UIButton!
  52. private var statusPredefinedStatuses: [NCCommunicationUserStatus] = []
  53. // MARK: - View Life Cycle
  54. override func viewDidLoad() {
  55. super.viewDidLoad()
  56. self.navigationItem.title = NSLocalizedString("_online_status_", comment: "")
  57. buttonCancel.title = NSLocalizedString("_cancel_", comment: "")
  58. onlineButton.layer.cornerRadius = 10
  59. onlineButton.layer.masksToBounds = true
  60. onlineButton.backgroundColor = NCBrandColor.shared.systemGray5
  61. //onlineLabel.layer.borderWidth = 0.5
  62. //onlineLabel.layer.borderColor = NCBrandColor.shared.brand.cgColor
  63. let onLine = NCUtility.shared.getUserStatus(userIcon: nil, userStatus: "online", userMessage: nil)
  64. onlineImage.image = onLine.onlineStatus
  65. onlineLabel.text = onLine.statusMessage
  66. onlineLabel.textColor = NCBrandColor.shared.label
  67. awayButton.layer.cornerRadius = 10
  68. awayButton.layer.masksToBounds = true
  69. awayButton.backgroundColor = NCBrandColor.shared.systemGray5
  70. let away = NCUtility.shared.getUserStatus(userIcon: nil, userStatus: "away", userMessage: nil)
  71. awayImage.image = away.onlineStatus
  72. awayLabel.text = away.statusMessage
  73. awayLabel.textColor = NCBrandColor.shared.label
  74. dndButton.layer.cornerRadius = 10
  75. dndButton.layer.masksToBounds = true
  76. dndButton.backgroundColor = NCBrandColor.shared.systemGray5
  77. let dnd = NCUtility.shared.getUserStatus(userIcon: nil, userStatus: "dnd", userMessage: nil)
  78. dndImage.image = dnd.onlineStatus
  79. dndLabel.text = dnd.statusMessage
  80. dndLabel.textColor = NCBrandColor.shared.label
  81. dndDescrLabel.text = dnd.descriptionMessage
  82. dndDescrLabel.textColor = .darkGray
  83. invisibleButton.layer.cornerRadius = 10
  84. invisibleButton.layer.masksToBounds = true
  85. invisibleButton.backgroundColor = NCBrandColor.shared.systemGray5
  86. let offline = NCUtility.shared.getUserStatus(userIcon: nil, userStatus: "offline", userMessage: nil)
  87. invisibleImage.image = offline.onlineStatus
  88. invisibleLabel.text = offline.statusMessage
  89. invisibleLabel.textColor = NCBrandColor.shared.label
  90. invisibleDescrLabel.text = offline.descriptionMessage
  91. invisibleDescrLabel.textColor = .darkGray
  92. statusMessageLabel.text = NSLocalizedString("_status_message_", comment: "")
  93. statusMessageLabel.textColor = NCBrandColor.shared.label
  94. statusMessageEmojiTextField.delegate = self
  95. statusMessageEmojiTextField.backgroundColor = NCBrandColor.shared.systemGray5
  96. statusMessageTextField.placeholder = NSLocalizedString("_status_message_placehorder_", comment: "")
  97. statusMessageTextField.textColor = NCBrandColor.shared.label
  98. tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1))
  99. tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  100. clearStatusMessageAfterLabel.text = NSLocalizedString("_clear_status_message_after_", comment: "")
  101. clearStatusMessageAfterLabel.textColor = NCBrandColor.shared.label
  102. clearStatusMessageAfterText.layer.cornerRadius = 5
  103. clearStatusMessageAfterText.layer.masksToBounds = true
  104. clearStatusMessageAfterText.layer.borderWidth = 0.2
  105. clearStatusMessageAfterText.layer.borderColor = UIColor.lightGray.cgColor
  106. clearStatusMessageAfterText.text = NSLocalizedString("_dont_clear_", comment: "")
  107. clearStatusMessageAfterText.textColor = .lightGray
  108. clearStatusMessageButton.layer.cornerRadius = 15
  109. clearStatusMessageButton.layer.masksToBounds = true
  110. clearStatusMessageButton.layer.borderWidth = 0.5
  111. clearStatusMessageButton.layer.borderColor = UIColor.darkGray.cgColor
  112. clearStatusMessageButton.backgroundColor = NCBrandColor.shared.systemGray5
  113. clearStatusMessageButton.setTitle(NSLocalizedString("_clear_status_message_", comment: ""), for: .normal)
  114. clearStatusMessageButton.setTitleColor(NCBrandColor.shared.label, for: .normal)
  115. setStatusMessageButton.layer.cornerRadius = 15
  116. setStatusMessageButton.layer.masksToBounds = true
  117. setStatusMessageButton.backgroundColor = NCBrandColor.shared.brand
  118. setStatusMessageButton.setTitle(NSLocalizedString("_set_status_message_", comment: ""), for: .normal)
  119. setStatusMessageButton.setTitleColor(NCBrandColor.shared.brandText, for: .normal)
  120. changeTheming()
  121. getStatus()
  122. }
  123. func dismissWithError(_ errorCode: Int, errorDescription: String) {
  124. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  125. self.dismiss(animated: true) {
  126. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode, forced: true)
  127. }
  128. }
  129. }
  130. // MARK: - Theming
  131. @objc func changeTheming() {
  132. view.backgroundColor = NCBrandColor.shared.systemBackground
  133. tableView.backgroundColor = NCBrandColor.shared.systemBackground
  134. tableView.reloadData()
  135. }
  136. // MARK: ACTION
  137. @IBAction func actionCancel(_ sender: UIBarButtonItem) {
  138. self.dismiss(animated: true, completion: nil)
  139. }
  140. // MARK: - Networking
  141. func getStatus() {
  142. NCCommunication.shared.getUserStatus { account, clearAt, icon, message, messageId, messageIsPredefined, status, statusIsUserDefined, userId, errorCode, errorDescription in
  143. if errorCode == 0 {
  144. self.statusMessageEmojiTextField.text = icon
  145. self.statusMessageTextField.text = message
  146. self.clearStatusMessageAfterText.text = " " + CCUtility.getTitleSectionDate(clearAt! as Date)
  147. switch status {
  148. case "online":
  149. self.onlineButton.layer.borderWidth = 1.5
  150. self.onlineButton.layer.borderColor = NCBrandColor.shared.brand.cgColor
  151. case "away":
  152. self.awayButton.layer.borderWidth = 1.5
  153. self.awayButton.layer.borderColor = NCBrandColor.shared.brand.cgColor
  154. case "dnd":
  155. self.dndButton.layer.borderWidth = 1.5
  156. self.dndButton.layer.borderColor = NCBrandColor.shared.brand.cgColor
  157. case "invisible":
  158. self.invisibleButton.layer.borderWidth = 1.5
  159. self.invisibleButton.layer.borderColor = NCBrandColor.shared.brand.cgColor
  160. default:
  161. print("No status")
  162. }
  163. NCCommunication.shared.getUserStatusPredefinedStatuses { account, userStatuses, errorCode, errorDescription in
  164. if errorCode == 0 {
  165. if let userStatuses = userStatuses {
  166. self.statusPredefinedStatuses = userStatuses
  167. }
  168. self.tableView.reloadData()
  169. } else {
  170. self.dismissWithError(errorCode, errorDescription: errorDescription)
  171. }
  172. }
  173. } else {
  174. self.dismissWithError(errorCode, errorDescription: errorDescription)
  175. }
  176. }
  177. }
  178. }
  179. @available(iOS 13.0, *)
  180. extension NCUserStatus: UITextFieldDelegate {
  181. func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
  182. if textField is emojiTextField {
  183. if string.count == 0 {
  184. textField.text = "😀"
  185. return false
  186. }
  187. textField.text = string
  188. }
  189. return true
  190. }
  191. }
  192. @available(iOS 13.0, *)
  193. class emojiTextField: UITextField {
  194. // required for iOS 13
  195. override var textInputContextIdentifier: String? { "" } // return non-nil to show the Emoji keyboard ¯\_(ツ)_/¯
  196. override var textInputMode: UITextInputMode? {
  197. for mode in UITextInputMode.activeInputModes {
  198. if mode.primaryLanguage == "emoji" {
  199. return mode
  200. }
  201. }
  202. return nil
  203. }
  204. override init(frame: CGRect) {
  205. super.init(frame: frame)
  206. commonInit()
  207. }
  208. required init?(coder: NSCoder) {
  209. super.init(coder: coder)
  210. commonInit()
  211. }
  212. func commonInit() {
  213. NotificationCenter.default.addObserver(self, selector: #selector(inputModeDidChange), name: UITextInputMode.currentInputModeDidChangeNotification, object: nil)
  214. }
  215. @objc func inputModeDidChange(_ notification: Notification) {
  216. guard isFirstResponder else {
  217. return
  218. }
  219. DispatchQueue.main.async { [weak self] in
  220. self?.reloadInputViews()
  221. }
  222. }
  223. }
  224. @available(iOS 13.0, *)
  225. extension NCUserStatus: UITableViewDelegate {
  226. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  227. return 45
  228. }
  229. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  230. let status = statusPredefinedStatuses[indexPath.row]
  231. }
  232. }
  233. @available(iOS 13.0, *)
  234. extension NCUserStatus: UITableViewDataSource {
  235. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  236. return statusPredefinedStatuses.count
  237. }
  238. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  239. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
  240. cell.backgroundColor = tableView.backgroundColor
  241. let status = statusPredefinedStatuses[indexPath.row]
  242. let icon = cell.viewWithTag(10) as! UILabel
  243. let message = cell.viewWithTag(20) as! UILabel
  244. icon.text = status.icon
  245. var timeString = getPredefinedClearStatusText(clearAt: status.clearAt, clearAtTime: status.clearAtTime, clearAtType: status.clearAtType)
  246. if let messageText = status.message {
  247. message.text = messageText
  248. timeString = " - " + timeString
  249. let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: messageText + timeString)
  250. attributedString.setColor(color: .lightGray, font: UIFont.systemFont(ofSize: 15), forText: timeString)
  251. message.attributedText = attributedString
  252. }
  253. return cell
  254. }
  255. func getPredefinedClearStatusText(clearAt: NSDate?, clearAtTime: String?, clearAtType: String?) -> String {
  256. // Date
  257. if clearAt != nil {
  258. return CCUtility.getTitleSectionDate(clearAt! as Date)
  259. }
  260. // Period
  261. if clearAtTime != nil && clearAtType == "period" {
  262. switch clearAtTime {
  263. case "3600":
  264. return NSLocalizedString("_an_hour_", comment: "")
  265. case "1800":
  266. return NSLocalizedString("_30_minutes_", comment: "")
  267. default:
  268. return NSLocalizedString("_dont_clear_", comment: "")
  269. }
  270. }
  271. // End of
  272. if clearAtTime != nil && clearAtType == "end-of" {
  273. return NSLocalizedString(clearAtTime!, comment: "")
  274. }
  275. return NSLocalizedString("_dont_clear_", comment: "")
  276. }
  277. }