NCUserStatus.swift 24 KB

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