NCUserStatus.swift 24 KB

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