NCUserStatus.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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 clearAtTimestamp: Double = 0 // Unix Timestamp representing the time to clear the status
  55. private let borderWidthButton: CGFloat = 1.5
  56. private let borderColorButton: CGColor = NCBrandColor.shared.brand.cgColor
  57. // MARK: - View Life Cycle
  58. override func viewDidLoad() {
  59. super.viewDidLoad()
  60. self.navigationItem.title = NSLocalizedString("_online_status_", comment: "")
  61. buttonCancel.title = NSLocalizedString("_close_", comment: "")
  62. onlineButton.layer.cornerRadius = 10
  63. onlineButton.layer.masksToBounds = true
  64. onlineButton.backgroundColor = NCBrandColor.shared.systemGray5
  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 invisible = NCUtility.shared.getUserStatus(userIcon: nil, userStatus: "invisible", userMessage: nil)
  89. invisibleImage.image = invisible.onlineStatus
  90. invisibleLabel.text = invisible.statusMessage
  91. invisibleLabel.textColor = NCBrandColor.shared.label
  92. invisibleDescrLabel.text = invisible.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.delegate = self
  99. statusMessageTextField.placeholder = NSLocalizedString("_status_message_placehorder_", comment: "")
  100. statusMessageTextField.textColor = NCBrandColor.shared.label
  101. tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1))
  102. tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  103. clearStatusMessageAfterLabel.text = NSLocalizedString("_clear_status_message_after_", comment: "")
  104. clearStatusMessageAfterLabel.textColor = NCBrandColor.shared.label
  105. clearStatusMessageAfterText.layer.cornerRadius = 5
  106. clearStatusMessageAfterText.layer.masksToBounds = true
  107. clearStatusMessageAfterText.layer.borderWidth = 0.2
  108. clearStatusMessageAfterText.layer.borderColor = UIColor.lightGray.cgColor
  109. clearStatusMessageAfterText.text = NSLocalizedString("_dont_clear_", comment: "")
  110. if traitCollection.userInterfaceStyle == .dark {
  111. clearStatusMessageAfterText.backgroundColor = .black
  112. clearStatusMessageAfterText.textColor = .white
  113. } else {
  114. clearStatusMessageAfterText.backgroundColor = .white
  115. clearStatusMessageAfterText.textColor = .black
  116. }
  117. let tap = UITapGestureRecognizer(target: self, action: #selector(self.actionClearStatusMessageAfterText(sender:)))
  118. clearStatusMessageAfterText.isUserInteractionEnabled = true
  119. clearStatusMessageAfterText.addGestureRecognizer(tap)
  120. clearStatusMessageAfterText.text = " " + NSLocalizedString("_dont_clear_", comment: "")
  121. clearStatusMessageButton.layer.cornerRadius = 15
  122. clearStatusMessageButton.layer.masksToBounds = true
  123. clearStatusMessageButton.layer.borderWidth = 0.5
  124. clearStatusMessageButton.layer.borderColor = UIColor.darkGray.cgColor
  125. clearStatusMessageButton.backgroundColor = NCBrandColor.shared.systemGray5
  126. clearStatusMessageButton.setTitle(NSLocalizedString("_clear_status_message_", comment: ""), for: .normal)
  127. clearStatusMessageButton.setTitleColor(NCBrandColor.shared.label, for: .normal)
  128. setStatusMessageButton.layer.cornerRadius = 15
  129. setStatusMessageButton.layer.masksToBounds = true
  130. setStatusMessageButton.backgroundColor = NCBrandColor.shared.brand
  131. setStatusMessageButton.setTitle(NSLocalizedString("_set_status_message_", comment: ""), for: .normal)
  132. setStatusMessageButton.setTitleColor(NCBrandColor.shared.brandText, for: .normal)
  133. getStatus()
  134. }
  135. override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
  136. super.traitCollectionDidChange(previousTraitCollection)
  137. changeTheming()
  138. }
  139. override func viewWillDisappear(_ animated: Bool) {
  140. super.viewWillDisappear(animated)
  141. NCCommunication.shared.getUserStatus { account, clearAt, icon, message, messageId, messageIsPredefined, status, statusIsUserDefined, _, errorCode, _ in
  142. if errorCode == 0 {
  143. NCManageDatabase.shared.setAccountUserStatus(userStatusClearAt: clearAt, userStatusIcon: icon, userStatusMessage: message, userStatusMessageId: messageId, userStatusMessageIsPredefined: messageIsPredefined, userStatusStatus: status, userStatusStatusIsUserDefined: statusIsUserDefined, account: account)
  144. }
  145. }
  146. }
  147. func dismissIfError(_ errorCode: Int, errorDescription: String) {
  148. if errorCode != 0 && errorCode != NCGlobal.shared.errorResourceNotFound {
  149. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  150. self.dismiss(animated: true) {
  151. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  152. }
  153. }
  154. }
  155. }
  156. // MARK: - Theming
  157. @objc func changeTheming() {
  158. view.backgroundColor = NCBrandColor.shared.systemBackground
  159. tableView.backgroundColor = NCBrandColor.shared.systemBackground
  160. tableView.reloadData()
  161. }
  162. // MARK: ACTION
  163. @IBAction func actionCancel(_ sender: UIBarButtonItem) {
  164. self.dismiss(animated: true, completion: nil)
  165. }
  166. @IBAction func actionOnline(_ sender: UIButton) {
  167. self.onlineButton.layer.borderWidth = self.borderWidthButton
  168. self.onlineButton.layer.borderColor = self.borderColorButton
  169. self.awayButton.layer.borderWidth = 0
  170. self.awayButton.layer.borderColor = nil
  171. self.dndButton.layer.borderWidth = 0
  172. self.dndButton.layer.borderColor = nil
  173. self.invisibleButton.layer.borderWidth = 0
  174. self.invisibleButton.layer.borderColor = nil
  175. NCCommunication.shared.setUserStatus(status: "online") { _, errorCode, errorDescription in
  176. self.dismissIfError(errorCode, errorDescription: errorDescription)
  177. }
  178. }
  179. @IBAction func actionAway(_ sender: UIButton) {
  180. self.onlineButton.layer.borderWidth = 0
  181. self.onlineButton.layer.borderColor = nil
  182. self.awayButton.layer.borderWidth = self.borderWidthButton
  183. self.awayButton.layer.borderColor = self.borderColorButton
  184. self.dndButton.layer.borderWidth = 0
  185. self.dndButton.layer.borderColor = nil
  186. self.invisibleButton.layer.borderWidth = 0
  187. self.invisibleButton.layer.borderColor = nil
  188. NCCommunication.shared.setUserStatus(status: "away") { _, errorCode, errorDescription in
  189. self.dismissIfError(errorCode, errorDescription: errorDescription)
  190. }
  191. }
  192. @IBAction func actionDnd(_ sender: UIButton) {
  193. self.onlineButton.layer.borderWidth = 0
  194. self.onlineButton.layer.borderColor = nil
  195. self.awayButton.layer.borderWidth = 0
  196. self.awayButton.layer.borderColor = nil
  197. self.dndButton.layer.borderWidth = self.borderWidthButton
  198. self.dndButton.layer.borderColor = self.borderColorButton
  199. self.invisibleButton.layer.borderWidth = 0
  200. self.invisibleButton.layer.borderColor = nil
  201. NCCommunication.shared.setUserStatus(status: "dnd") { _, errorCode, errorDescription in
  202. self.dismissIfError(errorCode, errorDescription: errorDescription)
  203. }
  204. }
  205. @IBAction func actionInvisible(_ sender: UIButton) {
  206. self.onlineButton.layer.borderWidth = 0
  207. self.onlineButton.layer.borderColor = nil
  208. self.awayButton.layer.borderWidth = 0
  209. self.awayButton.layer.borderColor = nil
  210. self.dndButton.layer.borderWidth = 0
  211. self.dndButton.layer.borderColor = nil
  212. self.invisibleButton.layer.borderWidth = self.borderWidthButton
  213. self.invisibleButton.layer.borderColor = self.borderColorButton
  214. NCCommunication.shared.setUserStatus(status: "invisible") { _, errorCode, errorDescription in
  215. self.dismissIfError(errorCode, errorDescription: errorDescription)
  216. }
  217. }
  218. @objc func actionClearStatusMessageAfterText(sender: UITapGestureRecognizer) {
  219. let dropDown = DropDown()
  220. let appearance = DropDown.appearance()
  221. let clearStatusMessageAfterTextBackup = clearStatusMessageAfterText.text
  222. if traitCollection.userInterfaceStyle == .dark {
  223. appearance.backgroundColor = .black
  224. appearance.textColor = .white
  225. } else {
  226. appearance.backgroundColor = .white
  227. appearance.textColor = .black
  228. }
  229. appearance.cornerRadius = 5
  230. appearance.shadowRadius = 0
  231. appearance.animationEntranceOptions = .transitionCurlUp
  232. appearance.animationduration = 0.25
  233. appearance.setupMaskedCorners([.layerMaxXMaxYCorner, .layerMinXMaxYCorner])
  234. dropDown.dataSource.append(NSLocalizedString("_dont_clear_", comment: ""))
  235. dropDown.dataSource.append(NSLocalizedString("_30_minutes_", comment: ""))
  236. dropDown.dataSource.append(NSLocalizedString("_1_hour_", comment: ""))
  237. dropDown.dataSource.append(NSLocalizedString("_4_hours_", comment: ""))
  238. dropDown.dataSource.append(NSLocalizedString("day", comment: ""))
  239. dropDown.dataSource.append(NSLocalizedString("_this_week_", comment: ""))
  240. dropDown.anchorView = clearStatusMessageAfterText
  241. dropDown.topOffset = CGPoint(x: 0, y: -clearStatusMessageAfterText.bounds.height)
  242. dropDown.width = clearStatusMessageAfterText.bounds.width
  243. dropDown.direction = .top
  244. dropDown.selectionAction = { _, item in
  245. self.clearAtTimestamp = self.getClearAt(item)
  246. self.clearStatusMessageAfterText.text = " " + item
  247. }
  248. dropDown.cancelAction = { [unowned self] in
  249. clearStatusMessageAfterText.text = clearStatusMessageAfterTextBackup
  250. }
  251. clearStatusMessageAfterText.text = " " + NSLocalizedString("_select_option_", comment: "")
  252. dropDown.show()
  253. }
  254. @IBAction func actionClearStatusMessage(_ sender: UIButton) {
  255. NCCommunication.shared.clearMessage { _, errorCode, errorDescription in
  256. if errorCode != 0 {
  257. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  258. }
  259. self.dismiss(animated: true)
  260. }
  261. }
  262. @IBAction func actionSetStatusMessage(_ sender: UIButton) {
  263. guard let message = statusMessageTextField.text else { return }
  264. NCCommunication.shared.setCustomMessageUserDefined(statusIcon: statusMessageEmojiTextField.text, message: message, clearAt: clearAtTimestamp) { _, errorCode, errorDescription in
  265. if errorCode != 0 {
  266. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  267. }
  268. self.dismiss(animated: true)
  269. }
  270. }
  271. // MARK: - Networking
  272. func getStatus() {
  273. NCCommunication.shared.getUserStatus { _, clearAt, icon, message, _, _, status, _, _, errorCode, errorDescription in
  274. if errorCode == 0 || errorCode == NCGlobal.shared.errorResourceNotFound {
  275. if icon != nil {
  276. self.statusMessageEmojiTextField.text = icon
  277. }
  278. if message != nil {
  279. self.statusMessageTextField.text = message
  280. }
  281. if clearAt != nil {
  282. self.clearStatusMessageAfterText.text = " " + self.getPredefinedClearStatusText(clearAt: clearAt, clearAtTime: nil, clearAtType: nil)
  283. }
  284. switch status {
  285. case "online":
  286. self.onlineButton.layer.borderWidth = self.borderWidthButton
  287. self.onlineButton.layer.borderColor = self.borderColorButton
  288. case "away":
  289. self.awayButton.layer.borderWidth = self.borderWidthButton
  290. self.awayButton.layer.borderColor = self.borderColorButton
  291. case "dnd":
  292. self.dndButton.layer.borderWidth = self.borderWidthButton
  293. self.dndButton.layer.borderColor = self.borderColorButton
  294. case "invisible", "offline":
  295. self.invisibleButton.layer.borderWidth = self.borderWidthButton
  296. self.invisibleButton.layer.borderColor = self.borderColorButton
  297. default:
  298. print("No status")
  299. }
  300. NCCommunication.shared.getUserStatusPredefinedStatuses { _, userStatuses, errorCode, errorDescription in
  301. if errorCode == 0 {
  302. if let userStatuses = userStatuses {
  303. self.statusPredefinedStatuses = userStatuses
  304. }
  305. self.tableView.reloadData()
  306. }
  307. self.dismissIfError(errorCode, errorDescription: errorDescription)
  308. }
  309. }
  310. self.dismissIfError(errorCode, errorDescription: errorDescription)
  311. }
  312. }
  313. // MARK: - Algorithms
  314. func getClearAt(_ clearAtString: String) -> Double {
  315. let now = Date()
  316. let calendar = Calendar.current
  317. let gregorian = Calendar(identifier: .gregorian)
  318. let midnight = calendar.startOfDay(for: now)
  319. guard let tomorrow = calendar.date(byAdding: .day, value: 1, to: midnight) else { return 0 }
  320. guard let startweek = gregorian.date(from: gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: now)) else { return 0 }
  321. guard let endweek = gregorian.date(byAdding: .day, value: 6, to: startweek) else { return 0 }
  322. switch clearAtString {
  323. case NSLocalizedString("_dont_clear_", comment: ""):
  324. return 0
  325. case NSLocalizedString("_30_minutes_", comment: ""):
  326. let date = now.addingTimeInterval(1800)
  327. return date.timeIntervalSince1970
  328. case NSLocalizedString("_1_hour_", comment: ""), NSLocalizedString("_an_hour_", comment: ""):
  329. let date = now.addingTimeInterval(3600)
  330. return date.timeIntervalSince1970
  331. case NSLocalizedString("_4_hours_", comment: ""):
  332. let date = now.addingTimeInterval(14400)
  333. return date.timeIntervalSince1970
  334. case NSLocalizedString("day", comment: ""):
  335. return tomorrow.timeIntervalSince1970
  336. case NSLocalizedString("_this_week_", comment: ""):
  337. return endweek.timeIntervalSince1970
  338. default:
  339. return 0
  340. }
  341. }
  342. func getPredefinedClearStatusText(clearAt: NSDate?, clearAtTime: String?, clearAtType: String?) -> String {
  343. // Date
  344. if clearAt != nil {
  345. let from = Date()
  346. let to = clearAt! as Date
  347. let day = Calendar.current.dateComponents([.day], from: from, to: to).day ?? 0
  348. let hour = Calendar.current.dateComponents([.hour], from: from, to: to).hour ?? 0
  349. let minute = Calendar.current.dateComponents([.minute], from: from, to: to).minute ?? 0
  350. if day > 0 {
  351. if day == 1 { return NSLocalizedString("day", comment: "") }
  352. return "\(day) " + NSLocalizedString("_days_", comment: "")
  353. }
  354. if hour > 0 {
  355. if hour == 1 { return NSLocalizedString("_an_hour_", comment: "") }
  356. if hour == 4 { return NSLocalizedString("_4_hour_", comment: "") }
  357. return "\(hour) " + NSLocalizedString("_hours_", comment: "")
  358. }
  359. if minute > 0 {
  360. if minute >= 25 && minute <= 30 { return NSLocalizedString("_30_minutes_", comment: "") }
  361. if minute > 30 { return NSLocalizedString("_an_hour_", comment: "") }
  362. return "\(minute) " + NSLocalizedString("_minutes_", comment: "")
  363. }
  364. }
  365. // Period
  366. if clearAtTime != nil && clearAtType == "period" {
  367. switch clearAtTime {
  368. case "3600":
  369. return NSLocalizedString("_an_hour_", comment: "")
  370. case "1800":
  371. return NSLocalizedString("_30_minutes_", comment: "")
  372. default:
  373. return NSLocalizedString("_dont_clear_", comment: "")
  374. }
  375. }
  376. // End of
  377. if clearAtTime != nil && clearAtType == "end-of" {
  378. return NSLocalizedString(clearAtTime!, comment: "")
  379. }
  380. return NSLocalizedString("_dont_clear_", comment: "")
  381. }
  382. }
  383. @available(iOS 13.0, *)
  384. extension NCUserStatus: UITextFieldDelegate {
  385. func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
  386. if textField is emojiTextField {
  387. if string.count == 0 {
  388. textField.text = "😀"
  389. return false
  390. }
  391. textField.text = string
  392. textField.endEditing(true)
  393. }
  394. return true
  395. }
  396. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  397. textField.resignFirstResponder()
  398. return false
  399. }
  400. }
  401. @available(iOS 13.0, *)
  402. class emojiTextField: UITextField {
  403. // required for iOS 13
  404. override var textInputContextIdentifier: String? { "" } // return non-nil to show the Emoji keyboard ¯\_(ツ)_/¯
  405. override var textInputMode: UITextInputMode? {
  406. for mode in UITextInputMode.activeInputModes {
  407. if mode.primaryLanguage == "emoji" {
  408. return mode
  409. }
  410. }
  411. return nil
  412. }
  413. override init(frame: CGRect) {
  414. super.init(frame: frame)
  415. commonInit()
  416. }
  417. required init?(coder: NSCoder) {
  418. super.init(coder: coder)
  419. commonInit()
  420. }
  421. func commonInit() {
  422. NotificationCenter.default.addObserver(self, selector: #selector(inputModeDidChange), name: UITextInputMode.currentInputModeDidChangeNotification, object: nil)
  423. }
  424. @objc func inputModeDidChange(_ notification: Notification) {
  425. guard isFirstResponder else {
  426. return
  427. }
  428. DispatchQueue.main.async { [weak self] in
  429. self?.reloadInputViews()
  430. }
  431. }
  432. }
  433. @available(iOS 13.0, *)
  434. extension NCUserStatus: UITableViewDelegate {
  435. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  436. return 45
  437. }
  438. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  439. guard let cell = tableView.cellForRow(at: indexPath) else { return }
  440. let status = statusPredefinedStatuses[indexPath.row]
  441. if let messageId = status.id {
  442. NCCommunication.shared.setCustomMessagePredefined(messageId: messageId, clearAt: 0) { _, errorCode, errorDescription in
  443. cell.isSelected = false
  444. if errorCode == 0 {
  445. let clearAtTimestampString = self.getPredefinedClearStatusText(clearAt: status.clearAt, clearAtTime: status.clearAtTime, clearAtType: status.clearAtType)
  446. self.statusMessageEmojiTextField.text = status.icon
  447. self.statusMessageTextField.text = status.message
  448. self.clearStatusMessageAfterText.text = " " + clearAtTimestampString
  449. self.clearAtTimestamp = self.getClearAt(clearAtTimestampString)
  450. }
  451. self.dismissIfError(errorCode, errorDescription: errorDescription)
  452. }
  453. }
  454. }
  455. }
  456. @available(iOS 13.0, *)
  457. extension NCUserStatus: UITableViewDataSource {
  458. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  459. return statusPredefinedStatuses.count
  460. }
  461. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  462. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
  463. cell.backgroundColor = tableView.backgroundColor
  464. let status = statusPredefinedStatuses[indexPath.row]
  465. let icon = cell.viewWithTag(10) as! UILabel
  466. let message = cell.viewWithTag(20) as! UILabel
  467. icon.text = status.icon
  468. var timeString = getPredefinedClearStatusText(clearAt: status.clearAt, clearAtTime: status.clearAtTime, clearAtType: status.clearAtType)
  469. if let messageText = status.message {
  470. message.text = messageText
  471. timeString = " - " + timeString
  472. let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: messageText + timeString)
  473. attributedString.setColor(color: .lightGray, font: UIFont.systemFont(ofSize: 15), forText: timeString)
  474. message.attributedText = attributedString
  475. }
  476. return cell
  477. }
  478. }