NCUserStatus.swift 26 KB

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