NCUserStatus.swift 26 KB

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