NCUserStatus.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. import DropDown
  28. @available(iOS 13.0, *)
  29. class NCUserStatus: UIViewController {
  30. @IBOutlet weak var buttonCancel: UIBarButtonItem!
  31. @IBOutlet weak var onlineButton: UIButton!
  32. @IBOutlet weak var onlineImage: UIImageView!
  33. @IBOutlet weak var onlineLabel: UILabel!
  34. @IBOutlet weak var awayButton: UIButton!
  35. @IBOutlet weak var awayImage: UIImageView!
  36. @IBOutlet weak var awayLabel: UILabel!
  37. @IBOutlet weak var dndButton: UIButton!
  38. @IBOutlet weak var dndImage: UIImageView!
  39. @IBOutlet weak var dndLabel: UILabel!
  40. @IBOutlet weak var dndDescrLabel: UILabel!
  41. @IBOutlet weak var invisibleButton: UIButton!
  42. @IBOutlet weak var invisibleImage: UIImageView!
  43. @IBOutlet weak var invisibleLabel: UILabel!
  44. @IBOutlet weak var invisibleDescrLabel: UILabel!
  45. @IBOutlet weak var statusMessageLabel: UILabel!
  46. @IBOutlet weak var statusMessageEmojiTextField: emojiTextField!
  47. @IBOutlet weak var statusMessageTextField: UITextField!
  48. @IBOutlet weak var tableView: UITableView!
  49. @IBOutlet weak var clearStatusMessageAfterLabel: UILabel!
  50. @IBOutlet weak var clearStatusMessageAfterText: UILabel!
  51. @IBOutlet weak var clearStatusMessageButton: UIButton!
  52. @IBOutlet weak var setStatusMessageButton: UIButton!
  53. private var statusPredefinedStatuses: [NCCommunicationUserStatus] = []
  54. private var clearAt: NSDate?
  55. private var icon: String?
  56. private var message: String?
  57. private var messageId: String?
  58. private var messageIsPredefined: Bool?
  59. private var status: String?
  60. private var statusIsUserDefined: Bool?
  61. private var userId: String?
  62. private let borderWidthButton: CGFloat = 1.5
  63. private let borderColorButton: CGColor = NCBrandColor.shared.brand.cgColor
  64. // MARK: - View Life Cycle
  65. override func viewDidLoad() {
  66. super.viewDidLoad()
  67. self.navigationItem.title = NSLocalizedString("_online_status_", comment: "")
  68. buttonCancel.title = NSLocalizedString("_cancel_", comment: "")
  69. onlineButton.layer.cornerRadius = 10
  70. onlineButton.layer.masksToBounds = true
  71. onlineButton.backgroundColor = NCBrandColor.shared.systemGray5
  72. let onLine = NCUtility.shared.getUserStatus(userIcon: nil, userStatus: "online", userMessage: nil)
  73. onlineImage.image = onLine.onlineStatus
  74. onlineLabel.text = onLine.statusMessage
  75. onlineLabel.textColor = NCBrandColor.shared.label
  76. awayButton.layer.cornerRadius = 10
  77. awayButton.layer.masksToBounds = true
  78. awayButton.backgroundColor = NCBrandColor.shared.systemGray5
  79. let away = NCUtility.shared.getUserStatus(userIcon: nil, userStatus: "away", userMessage: nil)
  80. awayImage.image = away.onlineStatus
  81. awayLabel.text = away.statusMessage
  82. awayLabel.textColor = NCBrandColor.shared.label
  83. dndButton.layer.cornerRadius = 10
  84. dndButton.layer.masksToBounds = true
  85. dndButton.backgroundColor = NCBrandColor.shared.systemGray5
  86. let dnd = NCUtility.shared.getUserStatus(userIcon: nil, userStatus: "dnd", userMessage: nil)
  87. dndImage.image = dnd.onlineStatus
  88. dndLabel.text = dnd.statusMessage
  89. dndLabel.textColor = NCBrandColor.shared.label
  90. dndDescrLabel.text = dnd.descriptionMessage
  91. dndDescrLabel.textColor = .darkGray
  92. invisibleButton.layer.cornerRadius = 10
  93. invisibleButton.layer.masksToBounds = true
  94. invisibleButton.backgroundColor = NCBrandColor.shared.systemGray5
  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.delegate = self
  106. statusMessageTextField.placeholder = NSLocalizedString("_status_message_placehorder_", comment: "")
  107. statusMessageTextField.textColor = NCBrandColor.shared.label
  108. tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1))
  109. tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  110. clearStatusMessageAfterLabel.text = NSLocalizedString("_clear_status_message_after_", comment: "")
  111. clearStatusMessageAfterLabel.textColor = NCBrandColor.shared.label
  112. clearStatusMessageAfterText.layer.cornerRadius = 5
  113. clearStatusMessageAfterText.layer.masksToBounds = true
  114. clearStatusMessageAfterText.layer.borderWidth = 0.2
  115. clearStatusMessageAfterText.layer.borderColor = UIColor.lightGray.cgColor
  116. clearStatusMessageAfterText.text = NSLocalizedString("_dont_clear_", comment: "")
  117. clearStatusMessageAfterText.textColor = .lightGray
  118. let tap = UITapGestureRecognizer(target: self, action: #selector(self.actionClearStatusMessageAfterText(sender:)))
  119. clearStatusMessageAfterText.isUserInteractionEnabled = true
  120. clearStatusMessageAfterText.addGestureRecognizer(tap)
  121. clearStatusMessageAfterText.text = " " + NSLocalizedString("_dont_clear_", comment: "")
  122. clearStatusMessageButton.layer.cornerRadius = 15
  123. clearStatusMessageButton.layer.masksToBounds = true
  124. clearStatusMessageButton.layer.borderWidth = 0.5
  125. clearStatusMessageButton.layer.borderColor = UIColor.darkGray.cgColor
  126. clearStatusMessageButton.backgroundColor = NCBrandColor.shared.systemGray5
  127. clearStatusMessageButton.setTitle(NSLocalizedString("_clear_status_message_", comment: ""), for: .normal)
  128. clearStatusMessageButton.setTitleColor(NCBrandColor.shared.label, for: .normal)
  129. setStatusMessageButton.layer.cornerRadius = 15
  130. setStatusMessageButton.layer.masksToBounds = true
  131. setStatusMessageButton.backgroundColor = NCBrandColor.shared.brand
  132. setStatusMessageButton.setTitle(NSLocalizedString("_set_status_message_", comment: ""), for: .normal)
  133. setStatusMessageButton.setTitleColor(NCBrandColor.shared.brandText, for: .normal)
  134. getStatus()
  135. }
  136. override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
  137. super.traitCollectionDidChange(previousTraitCollection)
  138. changeTheming()
  139. }
  140. func dismissIfError(_ errorCode: Int, errorDescription: String) {
  141. if errorCode != 0 {
  142. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  143. self.dismiss(animated: true) {
  144. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode, forced: true)
  145. }
  146. }
  147. }
  148. }
  149. // MARK: - Theming
  150. @objc func changeTheming() {
  151. view.backgroundColor = NCBrandColor.shared.systemBackground
  152. tableView.backgroundColor = NCBrandColor.shared.systemBackground
  153. tableView.reloadData()
  154. }
  155. // MARK: ACTION
  156. @IBAction func actionCancel(_ sender: UIBarButtonItem) {
  157. self.dismiss(animated: true, completion: nil)
  158. }
  159. @IBAction func actionOnline(_ sender: UIButton) {
  160. self.onlineButton.layer.borderWidth = self.borderWidthButton
  161. self.onlineButton.layer.borderColor = self.borderColorButton
  162. self.awayButton.layer.borderWidth = 0
  163. self.awayButton.layer.borderColor = nil
  164. self.dndButton.layer.borderWidth = 0
  165. self.dndButton.layer.borderColor = nil
  166. self.invisibleButton.layer.borderWidth = 0
  167. self.invisibleButton.layer.borderColor = nil
  168. NCCommunication.shared.setUserStatus(status: "online") { account, errorCode, errorDescription in
  169. self.dismissIfError(errorCode, errorDescription: errorDescription)
  170. }
  171. }
  172. @IBAction func actionAway(_ sender: UIButton) {
  173. self.onlineButton.layer.borderWidth = 0
  174. self.onlineButton.layer.borderColor = nil
  175. self.awayButton.layer.borderWidth = self.borderWidthButton
  176. self.awayButton.layer.borderColor = self.borderColorButton
  177. self.dndButton.layer.borderWidth = 0
  178. self.dndButton.layer.borderColor = nil
  179. self.invisibleButton.layer.borderWidth = 0
  180. self.invisibleButton.layer.borderColor = nil
  181. NCCommunication.shared.setUserStatus(status: "away") { account, errorCode, errorDescription in
  182. self.dismissIfError(errorCode, errorDescription: errorDescription)
  183. }
  184. }
  185. @IBAction func actionDnd(_ sender: UIButton) {
  186. self.onlineButton.layer.borderWidth = 0
  187. self.onlineButton.layer.borderColor = nil
  188. self.awayButton.layer.borderWidth = 0
  189. self.awayButton.layer.borderColor = nil
  190. self.dndButton.layer.borderWidth = self.borderWidthButton
  191. self.dndButton.layer.borderColor = self.borderColorButton
  192. self.invisibleButton.layer.borderWidth = 0
  193. self.invisibleButton.layer.borderColor = nil
  194. NCCommunication.shared.setUserStatus(status: "dnd") { account, errorCode, errorDescription in
  195. self.dismissIfError(errorCode, errorDescription: errorDescription)
  196. }
  197. }
  198. @IBAction func actionInvisible(_ sender: UIButton) {
  199. self.onlineButton.layer.borderWidth = 0
  200. self.onlineButton.layer.borderColor = nil
  201. self.awayButton.layer.borderWidth = 0
  202. self.awayButton.layer.borderColor = nil
  203. self.dndButton.layer.borderWidth = 0
  204. self.dndButton.layer.borderColor = nil
  205. self.invisibleButton.layer.borderWidth = self.borderWidthButton
  206. self.invisibleButton.layer.borderColor = self.borderColorButton
  207. NCCommunication.shared.setUserStatus(status: "invisible") { account, errorCode, errorDescription in
  208. self.dismissIfError(errorCode, errorDescription: errorDescription)
  209. }
  210. }
  211. @objc func actionClearStatusMessageAfterText(sender:UITapGestureRecognizer) {
  212. let dropDown = DropDown()
  213. let appearance = DropDown.appearance()
  214. let clearStatusMessageAfterTextBackup = clearStatusMessageAfterText.text
  215. appearance.backgroundColor = NCBrandColor.shared.systemBackground
  216. appearance.cornerRadius = 5
  217. appearance.shadowColor = UIColor(white: 0.5, alpha: 1)
  218. appearance.shadowOpacity = 0.9
  219. appearance.shadowRadius = 25
  220. appearance.animationduration = 0.25
  221. appearance.textColor = .darkGray
  222. appearance.setupMaskedCorners([.layerMaxXMaxYCorner, .layerMinXMaxYCorner])
  223. dropDown.dataSource.append(NSLocalizedString("_dont_clear_", comment: ""))
  224. dropDown.dataSource.append(NSLocalizedString("_30_minutes_", comment: ""))
  225. dropDown.dataSource.append(NSLocalizedString("_1_hour_", comment: ""))
  226. dropDown.dataSource.append(NSLocalizedString("_4_hours_", comment: ""))
  227. dropDown.dataSource.append(NSLocalizedString("day", comment: ""))
  228. dropDown.dataSource.append(NSLocalizedString("_this_week_", comment: ""))
  229. dropDown.anchorView = clearStatusMessageAfterText
  230. dropDown.topOffset = CGPoint(x: 0, y: -clearStatusMessageAfterText.bounds.height)
  231. dropDown.width = clearStatusMessageAfterText.bounds.width
  232. dropDown.direction = .top
  233. dropDown.selectionAction = { [weak self] (index, item) in
  234. print("")
  235. }
  236. dropDown.cancelAction = { [unowned self] in
  237. clearStatusMessageAfterText.text = clearStatusMessageAfterTextBackup
  238. }
  239. clearStatusMessageAfterText.text = " " + NSLocalizedString("_select_option_", comment: "")
  240. dropDown.show()
  241. }
  242. @IBAction func actionClearStatusMessage(_ sender: UIButton) {
  243. NCCommunication.shared.clearMessage { account, errorCode, errorDescription in
  244. if errorCode != 0 {
  245. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode, forced: true)
  246. }
  247. self.dismiss(animated: true)
  248. }
  249. }
  250. @IBAction func actionSetStatusMessage(_ sender: UIButton) {
  251. }
  252. // MARK: - Networking
  253. func getStatus() {
  254. NCCommunication.shared.getUserStatus { account, clearAt, icon, message, messageId, messageIsPredefined, status, statusIsUserDefined, userId, errorCode, errorDescription in
  255. if errorCode == 0 {
  256. self.clearAt = clearAt
  257. self.icon = icon
  258. self.message = message
  259. self.messageId = messageId
  260. self.messageIsPredefined = messageIsPredefined
  261. self.status = status
  262. self.statusIsUserDefined = statusIsUserDefined
  263. self.userId = userId
  264. if icon != nil {
  265. self.statusMessageEmojiTextField.text = icon
  266. }
  267. if message != nil {
  268. self.statusMessageTextField.text = message
  269. }
  270. if clearAt != nil {
  271. self.clearStatusMessageAfterText.text = " " + CCUtility.getTitleSectionDate(clearAt! as Date)
  272. }
  273. switch status {
  274. case "online":
  275. self.onlineButton.layer.borderWidth = self.borderWidthButton
  276. self.onlineButton.layer.borderColor = self.borderColorButton
  277. case "away":
  278. self.awayButton.layer.borderWidth = self.borderWidthButton
  279. self.awayButton.layer.borderColor = self.borderColorButton
  280. case "dnd":
  281. self.dndButton.layer.borderWidth = self.borderWidthButton
  282. self.dndButton.layer.borderColor = self.borderColorButton
  283. case "invisible":
  284. self.invisibleButton.layer.borderWidth = self.borderWidthButton
  285. self.invisibleButton.layer.borderColor = self.borderColorButton
  286. default:
  287. print("No status")
  288. }
  289. NCCommunication.shared.getUserStatusPredefinedStatuses { account, userStatuses, errorCode, errorDescription in
  290. if errorCode == 0 {
  291. if let userStatuses = userStatuses {
  292. self.statusPredefinedStatuses = userStatuses
  293. }
  294. self.tableView.reloadData()
  295. }
  296. self.dismissIfError(errorCode, errorDescription: errorDescription)
  297. }
  298. }
  299. self.dismissIfError(errorCode, errorDescription: errorDescription)
  300. }
  301. }
  302. }
  303. @available(iOS 13.0, *)
  304. extension NCUserStatus: UITextFieldDelegate {
  305. func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
  306. if textField is emojiTextField {
  307. if string.count == 0 {
  308. textField.text = "😀"
  309. return false
  310. }
  311. textField.text = string
  312. textField.endEditing(true)
  313. }
  314. return true
  315. }
  316. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  317. textField.resignFirstResponder()
  318. return false
  319. }
  320. }
  321. @available(iOS 13.0, *)
  322. class emojiTextField: UITextField {
  323. // required for iOS 13
  324. override var textInputContextIdentifier: String? { "" } // return non-nil to show the Emoji keyboard ¯\_(ツ)_/¯
  325. override var textInputMode: UITextInputMode? {
  326. for mode in UITextInputMode.activeInputModes {
  327. if mode.primaryLanguage == "emoji" {
  328. return mode
  329. }
  330. }
  331. return nil
  332. }
  333. override init(frame: CGRect) {
  334. super.init(frame: frame)
  335. commonInit()
  336. }
  337. required init?(coder: NSCoder) {
  338. super.init(coder: coder)
  339. commonInit()
  340. }
  341. func commonInit() {
  342. NotificationCenter.default.addObserver(self, selector: #selector(inputModeDidChange), name: UITextInputMode.currentInputModeDidChangeNotification, object: nil)
  343. }
  344. @objc func inputModeDidChange(_ notification: Notification) {
  345. guard isFirstResponder else {
  346. return
  347. }
  348. DispatchQueue.main.async { [weak self] in
  349. self?.reloadInputViews()
  350. }
  351. }
  352. }
  353. @available(iOS 13.0, *)
  354. extension NCUserStatus: UITableViewDelegate {
  355. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  356. return 45
  357. }
  358. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  359. guard let cell = tableView.cellForRow(at: indexPath) else { return }
  360. let status = statusPredefinedStatuses[indexPath.row]
  361. if let messageId = status.id {
  362. NCCommunication.shared.setCustomMessagePredefined(messageId: messageId, clearAt: 0) { account, errorCode, errorDescription in
  363. cell.isSelected = false
  364. if errorCode == 0 {
  365. self.statusMessageEmojiTextField.text = status.icon
  366. self.statusMessageTextField.text = status.message
  367. }
  368. self.dismissIfError(errorCode, errorDescription: errorDescription)
  369. }
  370. }
  371. }
  372. }
  373. @available(iOS 13.0, *)
  374. extension NCUserStatus: UITableViewDataSource {
  375. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  376. return statusPredefinedStatuses.count
  377. }
  378. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  379. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
  380. cell.backgroundColor = tableView.backgroundColor
  381. let status = statusPredefinedStatuses[indexPath.row]
  382. let icon = cell.viewWithTag(10) as! UILabel
  383. let message = cell.viewWithTag(20) as! UILabel
  384. icon.text = status.icon
  385. var timeString = getPredefinedClearStatusText(clearAt: status.clearAt, clearAtTime: status.clearAtTime, clearAtType: status.clearAtType)
  386. if let messageText = status.message {
  387. message.text = messageText
  388. timeString = " - " + timeString
  389. let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: messageText + timeString)
  390. attributedString.setColor(color: .lightGray, font: UIFont.systemFont(ofSize: 15), forText: timeString)
  391. message.attributedText = attributedString
  392. }
  393. return cell
  394. }
  395. func getPredefinedClearStatusText(clearAt: NSDate?, clearAtTime: String?, clearAtType: String?) -> String {
  396. // Date
  397. if clearAt != nil {
  398. return CCUtility.getTitleSectionDate(clearAt! as Date)
  399. }
  400. // Period
  401. if clearAtTime != nil && clearAtType == "period" {
  402. switch clearAtTime {
  403. case "3600":
  404. return NSLocalizedString("_an_hour_", comment: "")
  405. case "1800":
  406. return NSLocalizedString("_30_minutes_", comment: "")
  407. default:
  408. return NSLocalizedString("_dont_clear_", comment: "")
  409. }
  410. }
  411. // End of
  412. if clearAtTime != nil && clearAtType == "end-of" {
  413. return NSLocalizedString(clearAtTime!, comment: "")
  414. }
  415. return NSLocalizedString("_dont_clear_", comment: "")
  416. }
  417. }