NCUserStatus.swift 24 KB

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