NCUserStatus.swift 14 KB

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