NCUserStatus.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. //
  2. // NCUserStatus.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 25/05/21.
  6. // Copyright © 2021 Marino Faggiana. All rights reserved.
  7. //
  8. //
  9. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  10. //
  11. // This program is free software: you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published by
  13. // the Free Software Foundation, either version 3 of the License, or
  14. // (at your option) any later version.
  15. //
  16. // This program is distributed in the hope that it will be useful,
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. // GNU General Public License for more details.
  20. //
  21. // You should have received a copy of the GNU General Public License
  22. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. //
  24. import UIKit
  25. import Foundation
  26. import NCCommunication
  27. @available(iOS 13.0, *)
  28. class NCUserStatus: UIViewController {
  29. @IBOutlet weak var onlineButton: UIButton!
  30. @IBOutlet weak var onlineImage: UIImageView!
  31. @IBOutlet weak var onlineLabel: UILabel!
  32. @IBOutlet weak var awayButton: UIButton!
  33. @IBOutlet weak var awayImage: UIImageView!
  34. @IBOutlet weak var awayLabel: UILabel!
  35. @IBOutlet weak var dndButton: UIButton!
  36. @IBOutlet weak var dndImage: UIImageView!
  37. @IBOutlet weak var dndLabel: UILabel!
  38. @IBOutlet weak var dndDescrLabel: UILabel!
  39. @IBOutlet weak var invisibleButton: UIButton!
  40. @IBOutlet weak var invisibleImage: UIImageView!
  41. @IBOutlet weak var invisibleLabel: UILabel!
  42. @IBOutlet weak var invisibleDescrLabel: UILabel!
  43. @IBOutlet weak var statusMessageLabel: UILabel!
  44. @IBOutlet weak var statusMessageEmojiTextField: emojiTextField!
  45. @IBOutlet weak var statusMessageTextField: UITextField!
  46. @IBOutlet weak var tableView: UITableView!
  47. @IBOutlet weak var clearStatusMessageAfterLabel: UILabel!
  48. @IBOutlet weak var clearStatusMessageAfterText: UILabel!
  49. @IBOutlet weak var clearStatusMessageButton: UIButton!
  50. @IBOutlet weak var setStatusMessageButton: UIButton!
  51. private var statusPredefinedStatuses: [NCCommunicationUserStatus] = []
  52. private var userStatusRetrieveStatuses: [NCCommunicationUserStatus] = []
  53. private let heightCell: CGFloat = 45
  54. // MARK: - View Life Cycle
  55. override func viewDidLoad() {
  56. super.viewDidLoad()
  57. self.navigationItem.title = NSLocalizedString("_online_status_", comment: "")
  58. onlineButton.layer.cornerRadius = 10
  59. onlineButton.layer.masksToBounds = true
  60. onlineButton.backgroundColor = NCBrandColor.shared.systemGray6
  61. //onlineLabel.layer.borderWidth = 0.5
  62. //onlineLabel.layer.borderColor = NCBrandColor.shared.brand.cgColor
  63. let onLine = NCUtility.shared.getUserStatus(userIcon: nil, userStatus: "online", userMessage: nil)
  64. onlineImage.image = onLine.onlineStatus
  65. onlineLabel.text = onLine.statusMessage
  66. onlineLabel.textColor = NCBrandColor.shared.label
  67. awayButton.layer.cornerRadius = 10
  68. awayButton.layer.masksToBounds = true
  69. awayButton.backgroundColor = NCBrandColor.shared.systemGray6
  70. //onlineLabel.layer.borderWidth = 0.5
  71. //onlineLabel.layer.borderColor = NCBrandColor.shared.brand.cgColor
  72. let away = NCUtility.shared.getUserStatus(userIcon: nil, userStatus: "away", userMessage: nil)
  73. awayImage.image = away.onlineStatus
  74. awayLabel.text = away.statusMessage
  75. awayLabel.textColor = NCBrandColor.shared.label
  76. dndButton.layer.cornerRadius = 10
  77. dndButton.layer.masksToBounds = true
  78. dndButton.backgroundColor = NCBrandColor.shared.systemGray6
  79. //onlineLabel.layer.borderWidth = 0.5
  80. //onlineLabel.layer.borderColor = NCBrandColor.shared.brand.cgColor
  81. let dnd = NCUtility.shared.getUserStatus(userIcon: nil, userStatus: "dnd", userMessage: nil)
  82. dndImage.image = dnd.onlineStatus
  83. dndLabel.text = dnd.statusMessage
  84. dndLabel.textColor = NCBrandColor.shared.label
  85. dndDescrLabel.text = dnd.descriptionMessage
  86. dndDescrLabel.textColor = .darkGray
  87. invisibleButton.layer.cornerRadius = 10
  88. invisibleButton.layer.masksToBounds = true
  89. invisibleButton.backgroundColor = NCBrandColor.shared.systemGray6
  90. //onlineLabel.layer.borderWidth = 0.5
  91. //onlineLabel.layer.borderColor = NCBrandColor.shared.brand.cgColor
  92. let offline = NCUtility.shared.getUserStatus(userIcon: nil, userStatus: "offline", userMessage: nil)
  93. invisibleImage.image = offline.onlineStatus
  94. invisibleLabel.text = offline.statusMessage
  95. invisibleLabel.textColor = NCBrandColor.shared.label
  96. invisibleDescrLabel.text = offline.descriptionMessage
  97. invisibleDescrLabel.textColor = .darkGray
  98. statusMessageLabel.text = NSLocalizedString("_status_message_", comment: "")
  99. statusMessageLabel.textColor = NCBrandColor.shared.label
  100. statusMessageEmojiTextField.delegate = self
  101. statusMessageEmojiTextField.backgroundColor = NCBrandColor.shared.systemGray6
  102. statusMessageTextField.placeholder = NSLocalizedString("_status_message_placehorder_", comment: "")
  103. statusMessageTextField.textColor = NCBrandColor.shared.label
  104. tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1))
  105. tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  106. clearStatusMessageButton.layer.cornerRadius = 15
  107. clearStatusMessageButton.layer.masksToBounds = true
  108. clearStatusMessageButton.layer.borderWidth = 0.5
  109. clearStatusMessageButton.layer.borderColor = UIColor.darkGray.cgColor
  110. clearStatusMessageButton.backgroundColor = NCBrandColor.shared.systemGray6
  111. clearStatusMessageButton.setTitle(NSLocalizedString("_clear_status_message_", comment: ""), for: .normal)
  112. clearStatusMessageButton.setTitleColor(NCBrandColor.shared.label, for: .normal)
  113. setStatusMessageButton.layer.cornerRadius = 15
  114. setStatusMessageButton.layer.masksToBounds = true
  115. setStatusMessageButton.backgroundColor = NCBrandColor.shared.brand
  116. setStatusMessageButton.setTitle(NSLocalizedString("_set_status_message_", comment: ""), for: .normal)
  117. setStatusMessageButton.setTitleColor(NCBrandColor.shared.brandText, for: .normal)
  118. getStatus()
  119. }
  120. // MARK: - Theming
  121. @objc func changeTheming() {
  122. view.backgroundColor = NCBrandColor.shared.secondarySystemBackground
  123. tableView.backgroundColor = NCBrandColor.shared.secondarySystemBackground
  124. tableView.reloadData()
  125. }
  126. // MARK: - Networking
  127. func getStatus() {
  128. NCCommunication.shared.getUserStatus { account, clearAt, icon, message, messageId, messageIsPredefined, status, statusIsUserDefined, userId, errorCode, errorDescription in
  129. if errorCode == 0 {
  130. NCCommunication.shared.getUserStatusPredefinedStatuses { account, userStatuses, errorCode, errorDescription in
  131. if errorCode == 0 {
  132. if let userStatuses = userStatuses {
  133. self.statusPredefinedStatuses = userStatuses
  134. }
  135. NCCommunication.shared.getUserStatusRetrieveStatuses(limit: 200, offset: 0) { account, userStatuses, errorCode, errorDescription in
  136. if errorCode == 0 {
  137. if let userStatuses = userStatuses {
  138. self.userStatusRetrieveStatuses = userStatuses
  139. }
  140. self.tableView.reloadData()
  141. } else {
  142. print("error")
  143. }
  144. }
  145. } else {
  146. print("error")
  147. }
  148. }
  149. } else {
  150. print("error")
  151. }
  152. }
  153. }
  154. }
  155. @available(iOS 13.0, *)
  156. extension NCUserStatus: UITextFieldDelegate {
  157. func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
  158. if textField is emojiTextField {
  159. if string.count == 0 {
  160. textField.text = "😀"
  161. return false
  162. }
  163. textField.text = string
  164. }
  165. return true
  166. }
  167. }
  168. @available(iOS 13.0, *)
  169. class emojiTextField: UITextField {
  170. // required for iOS 13
  171. override var textInputContextIdentifier: String? { "" } // return non-nil to show the Emoji keyboard ¯\_(ツ)_/¯
  172. override var textInputMode: UITextInputMode? {
  173. for mode in UITextInputMode.activeInputModes {
  174. if mode.primaryLanguage == "emoji" {
  175. return mode
  176. }
  177. }
  178. return nil
  179. }
  180. override init(frame: CGRect) {
  181. super.init(frame: frame)
  182. commonInit()
  183. }
  184. required init?(coder: NSCoder) {
  185. super.init(coder: coder)
  186. commonInit()
  187. }
  188. func commonInit() {
  189. NotificationCenter.default.addObserver(self, selector: #selector(inputModeDidChange), name: UITextInputMode.currentInputModeDidChangeNotification, object: nil)
  190. }
  191. @objc func inputModeDidChange(_ notification: Notification) {
  192. guard isFirstResponder else {
  193. return
  194. }
  195. DispatchQueue.main.async { [weak self] in
  196. self?.reloadInputViews()
  197. }
  198. }
  199. }
  200. @available(iOS 13.0, *)
  201. extension NCUserStatus: UITableViewDelegate {
  202. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  203. return heightCell
  204. }
  205. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  206. /*
  207. if indexPath.row == accounts.count {
  208. dismiss(animated: true)
  209. delegate?.accountRequestAddAccount()
  210. } else {
  211. let account = accounts[indexPath.row]
  212. if account.account != activeAccount?.account {
  213. dismiss(animated: true) {
  214. self.delegate?.accountRequestChangeAccount(account: account.account)
  215. }
  216. } else {
  217. dismiss(animated: true)
  218. }
  219. }
  220. */
  221. }
  222. }
  223. @available(iOS 13.0, *)
  224. extension NCUserStatus: UITableViewDataSource {
  225. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  226. return statusPredefinedStatuses.count
  227. }
  228. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  229. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
  230. cell.backgroundColor = tableView.backgroundColor
  231. let icon = cell.viewWithTag(10) as? UILabel
  232. let message = cell.viewWithTag(20) as? UILabel
  233. let status = statusPredefinedStatuses[indexPath.row]
  234. icon?.text = status.icon
  235. message?.text = status.message
  236. return cell
  237. }
  238. }