NCUserStatus.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. 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. getStatus()
  121. }
  122. override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
  123. super.traitCollectionDidChange(previousTraitCollection)
  124. changeTheming()
  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: ACTION
  140. @IBAction func actionCancel(_ sender: UIBarButtonItem) {
  141. self.dismiss(animated: true, completion: nil)
  142. }
  143. @IBAction func actionOnline(_ sender: UIButton) {
  144. self.onlineButton.layer.borderWidth = self.borderWidthButton
  145. self.onlineButton.layer.borderColor = self.borderColorButton
  146. self.awayButton.layer.borderWidth = 0
  147. self.awayButton.layer.borderColor = nil
  148. self.dndButton.layer.borderWidth = 0
  149. self.dndButton.layer.borderColor = nil
  150. self.invisibleButton.layer.borderWidth = 0
  151. self.invisibleButton.layer.borderColor = nil
  152. }
  153. @IBAction func actionAway(_ sender: UIButton) {
  154. self.onlineButton.layer.borderWidth = 0
  155. self.onlineButton.layer.borderColor = nil
  156. self.awayButton.layer.borderWidth = self.borderWidthButton
  157. self.awayButton.layer.borderColor = self.borderColorButton
  158. self.dndButton.layer.borderWidth = 0
  159. self.dndButton.layer.borderColor = nil
  160. self.invisibleButton.layer.borderWidth = 0
  161. self.invisibleButton.layer.borderColor = nil
  162. }
  163. @IBAction func actionDnd(_ sender: UIButton) {
  164. self.onlineButton.layer.borderWidth = 0
  165. self.onlineButton.layer.borderColor = nil
  166. self.awayButton.layer.borderWidth = 0
  167. self.awayButton.layer.borderColor = nil
  168. self.dndButton.layer.borderWidth = self.borderWidthButton
  169. self.dndButton.layer.borderColor = self.borderColorButton
  170. self.invisibleButton.layer.borderWidth = 0
  171. self.invisibleButton.layer.borderColor = nil
  172. }
  173. @IBAction func actionInvisible(_ sender: UIButton) {
  174. self.onlineButton.layer.borderWidth = 0
  175. self.onlineButton.layer.borderColor = nil
  176. self.awayButton.layer.borderWidth = 0
  177. self.awayButton.layer.borderColor = nil
  178. self.dndButton.layer.borderWidth = 0
  179. self.dndButton.layer.borderColor = nil
  180. self.invisibleButton.layer.borderWidth = self.borderWidthButton
  181. self.invisibleButton.layer.borderColor = self.borderColorButton
  182. }
  183. @IBAction func actionClearStatusMessage(_ sender: UIButton) {
  184. }
  185. @IBAction func actionSetStatusMessage(_ sender: UIButton) {
  186. }
  187. // MARK: - Networking
  188. func getStatus() {
  189. NCCommunication.shared.getUserStatus { account, clearAt, icon, message, messageId, messageIsPredefined, status, statusIsUserDefined, userId, errorCode, errorDescription in
  190. if errorCode == 0 {
  191. self.statusMessageEmojiTextField.text = icon
  192. self.statusMessageTextField.text = message
  193. self.clearStatusMessageAfterText.text = " " + CCUtility.getTitleSectionDate(clearAt! as Date)
  194. switch status {
  195. case "online":
  196. self.onlineButton.layer.borderWidth = self.borderWidthButton
  197. self.onlineButton.layer.borderColor = self.borderColorButton
  198. case "away":
  199. self.awayButton.layer.borderWidth = self.borderWidthButton
  200. self.awayButton.layer.borderColor = self.borderColorButton
  201. case "dnd":
  202. self.dndButton.layer.borderWidth = self.borderWidthButton
  203. self.dndButton.layer.borderColor = self.borderColorButton
  204. case "invisible":
  205. self.invisibleButton.layer.borderWidth = self.borderWidthButton
  206. self.invisibleButton.layer.borderColor = self.borderColorButton
  207. default:
  208. print("No status")
  209. }
  210. NCCommunication.shared.getUserStatusPredefinedStatuses { account, userStatuses, errorCode, errorDescription in
  211. if errorCode == 0 {
  212. if let userStatuses = userStatuses {
  213. self.statusPredefinedStatuses = userStatuses
  214. }
  215. self.tableView.reloadData()
  216. } else {
  217. self.dismissWithError(errorCode, errorDescription: errorDescription)
  218. }
  219. }
  220. } else {
  221. self.dismissWithError(errorCode, errorDescription: errorDescription)
  222. }
  223. }
  224. }
  225. }
  226. @available(iOS 13.0, *)
  227. extension NCUserStatus: UITextFieldDelegate {
  228. func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
  229. if textField is emojiTextField {
  230. if string.count == 0 {
  231. textField.text = "😀"
  232. return false
  233. }
  234. textField.text = string
  235. }
  236. return true
  237. }
  238. }
  239. @available(iOS 13.0, *)
  240. class emojiTextField: UITextField {
  241. // required for iOS 13
  242. override var textInputContextIdentifier: String? { "" } // return non-nil to show the Emoji keyboard ¯\_(ツ)_/¯
  243. override var textInputMode: UITextInputMode? {
  244. for mode in UITextInputMode.activeInputModes {
  245. if mode.primaryLanguage == "emoji" {
  246. return mode
  247. }
  248. }
  249. return nil
  250. }
  251. override init(frame: CGRect) {
  252. super.init(frame: frame)
  253. commonInit()
  254. }
  255. required init?(coder: NSCoder) {
  256. super.init(coder: coder)
  257. commonInit()
  258. }
  259. func commonInit() {
  260. NotificationCenter.default.addObserver(self, selector: #selector(inputModeDidChange), name: UITextInputMode.currentInputModeDidChangeNotification, object: nil)
  261. }
  262. @objc func inputModeDidChange(_ notification: Notification) {
  263. guard isFirstResponder else {
  264. return
  265. }
  266. DispatchQueue.main.async { [weak self] in
  267. self?.reloadInputViews()
  268. }
  269. }
  270. }
  271. @available(iOS 13.0, *)
  272. extension NCUserStatus: UITableViewDelegate {
  273. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  274. return 45
  275. }
  276. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  277. let status = statusPredefinedStatuses[indexPath.row]
  278. }
  279. }
  280. @available(iOS 13.0, *)
  281. extension NCUserStatus: UITableViewDataSource {
  282. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  283. return statusPredefinedStatuses.count
  284. }
  285. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  286. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
  287. cell.backgroundColor = tableView.backgroundColor
  288. let status = statusPredefinedStatuses[indexPath.row]
  289. let icon = cell.viewWithTag(10) as! UILabel
  290. let message = cell.viewWithTag(20) as! UILabel
  291. icon.text = status.icon
  292. var timeString = getPredefinedClearStatusText(clearAt: status.clearAt, clearAtTime: status.clearAtTime, clearAtType: status.clearAtType)
  293. if let messageText = status.message {
  294. message.text = messageText
  295. timeString = " - " + timeString
  296. let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: messageText + timeString)
  297. attributedString.setColor(color: .lightGray, font: UIFont.systemFont(ofSize: 15), forText: timeString)
  298. message.attributedText = attributedString
  299. }
  300. return cell
  301. }
  302. func getPredefinedClearStatusText(clearAt: NSDate?, clearAtTime: String?, clearAtType: String?) -> String {
  303. // Date
  304. if clearAt != nil {
  305. return CCUtility.getTitleSectionDate(clearAt! as Date)
  306. }
  307. // Period
  308. if clearAtTime != nil && clearAtType == "period" {
  309. switch clearAtTime {
  310. case "3600":
  311. return NSLocalizedString("_an_hour_", comment: "")
  312. case "1800":
  313. return NSLocalizedString("_30_minutes_", comment: "")
  314. default:
  315. return NSLocalizedString("_dont_clear_", comment: "")
  316. }
  317. }
  318. // End of
  319. if clearAtTime != nil && clearAtType == "end-of" {
  320. return NSLocalizedString(clearAtTime!, comment: "")
  321. }
  322. return NSLocalizedString("_dont_clear_", comment: "")
  323. }
  324. }