NCUserStatus.swift 23 KB

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