NCUserStatus.swift 13 KB

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