NCUserStatus.swift 24 KB

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