NCShareUserCell.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // NCShareUserCell.swift
  3. // Nextcloud
  4. //
  5. // Created by Henrik Storch on 15.11.2021.
  6. // Copyright © 2021 Henrik Storch. All rights reserved.
  7. //
  8. // Author Henrik Storch <henrik.storch@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. import UIKit
  23. import DropDown
  24. import NextcloudKit
  25. class NCShareUserCell: UITableViewCell, NCCellProtocol {
  26. @IBOutlet weak var imageItem: UIImageView!
  27. @IBOutlet weak var labelTitle: UILabel!
  28. @IBOutlet weak var buttonMenu: UIButton!
  29. @IBOutlet weak var imageStatus: UIImageView!
  30. @IBOutlet weak var status: UILabel!
  31. @IBOutlet weak var btnQuickStatus: UIButton!
  32. @IBOutlet weak var labelQuickStatus: UILabel!
  33. @IBOutlet weak var imageDownArrow: UIImageView!
  34. private var index = IndexPath()
  35. var tableShare: tableShare?
  36. let utility = NCUtility()
  37. weak var delegate: NCShareUserCellDelegate?
  38. var indexPath: IndexPath {
  39. get { return index }
  40. set { index = newValue }
  41. }
  42. var fileAvatarImageView: UIImageView? {
  43. return imageItem
  44. }
  45. var fileUser: String? {
  46. get { return tableShare?.shareWith }
  47. set {}
  48. }
  49. func setupCellUI(userId: String) {
  50. guard let tableShare = tableShare else {
  51. return
  52. }
  53. self.accessibilityCustomActions = [UIAccessibilityCustomAction(
  54. name: NSLocalizedString("_show_profile_", comment: ""),
  55. target: self,
  56. selector: #selector(tapAvatarImage))]
  57. let permissions = NCPermissions()
  58. labelTitle.text = tableShare.shareWithDisplayname
  59. labelTitle.textColor = NCBrandColor.shared.textColor
  60. isUserInteractionEnabled = true
  61. labelQuickStatus.isHidden = false
  62. imageDownArrow.isHidden = false
  63. buttonMenu.isHidden = false
  64. buttonMenu.accessibilityLabel = NSLocalizedString("_more_", comment: "")
  65. imageItem.image = NCShareCommon().getImageShareType(shareType: tableShare.shareType)
  66. let status = utility.getUserStatus(userIcon: tableShare.userIcon, userStatus: tableShare.userStatus, userMessage: tableShare.userMessage)
  67. imageStatus.image = status.statusImage
  68. self.status.text = status.statusMessage
  69. // If the initiator or the recipient is not the current user, show the list of sharees without any options to edit it.
  70. if tableShare.uidOwner != userId && tableShare.uidFileOwner != userId {
  71. isUserInteractionEnabled = false
  72. labelQuickStatus.isHidden = true
  73. imageDownArrow.isHidden = true
  74. buttonMenu.isHidden = true
  75. }
  76. btnQuickStatus.accessibilityHint = NSLocalizedString("_user_sharee_footer_", comment: "")
  77. btnQuickStatus.setTitle("", for: .normal)
  78. btnQuickStatus.contentHorizontalAlignment = .left
  79. if tableShare.permissions == permissions.permissionCreateShare {
  80. labelQuickStatus.text = NSLocalizedString("_share_file_drop_", comment: "")
  81. } else {
  82. // Read Only
  83. if permissions.isAnyPermissionToEdit(tableShare.permissions) {
  84. labelQuickStatus.text = NSLocalizedString("_share_editing_", comment: "")
  85. } else {
  86. labelQuickStatus.text = NSLocalizedString("_share_read_only_", comment: "")
  87. }
  88. }
  89. }
  90. override func awakeFromNib() {
  91. super.awakeFromNib()
  92. let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapAvatarImage))
  93. imageItem?.addGestureRecognizer(tapGesture)
  94. labelQuickStatus.textColor = NCBrandColor.shared.customer
  95. imageDownArrow.image = utility.loadImage(named: "arrowtriangle.down.circle", colors: [NCBrandColor.shared.customer])
  96. }
  97. @objc func tapAvatarImage(_ sender: UITapGestureRecognizer) {
  98. delegate?.showProfile(with: tableShare, sender: sender)
  99. }
  100. @IBAction func touchUpInsideMenu(_ sender: Any) {
  101. delegate?.tapMenu(with: tableShare, sender: sender)
  102. }
  103. @IBAction func quickStatusClicked(_ sender: Any) {
  104. delegate?.quickStatus(with: tableShare, sender: sender)
  105. }
  106. }
  107. protocol NCShareUserCellDelegate: AnyObject {
  108. func tapMenu(with tableShare: tableShare?, sender: Any)
  109. func showProfile(with tableComment: tableShare?, sender: Any)
  110. func quickStatus(with tableShare: tableShare?, sender: Any)
  111. }
  112. // MARK: - NCSearchUserDropDownCell
  113. class NCSearchUserDropDownCell: DropDownCell, NCCellProtocol {
  114. @IBOutlet weak var imageItem: UIImageView!
  115. @IBOutlet weak var imageStatus: UIImageView!
  116. @IBOutlet weak var status: UILabel!
  117. @IBOutlet weak var imageShareeType: UIImageView!
  118. @IBOutlet weak var centerTitle: NSLayoutConstraint!
  119. private var user: String = ""
  120. private var index = IndexPath()
  121. var indexPath: IndexPath {
  122. get { return index }
  123. set { index = newValue }
  124. }
  125. var fileAvatarImageView: UIImageView? {
  126. return imageItem
  127. }
  128. var fileUser: String? {
  129. get { return user }
  130. set { user = newValue ?? "" }
  131. }
  132. func setupCell(sharee: NKSharee, session: NCSession.Session) {
  133. let utility = NCUtility()
  134. imageItem.image = NCShareCommon().getImageShareType(shareType: sharee.shareType)
  135. imageShareeType.image = NCShareCommon().getImageShareType(shareType: sharee.shareType)
  136. let status = utility.getUserStatus(userIcon: sharee.userIcon, userStatus: sharee.userStatus, userMessage: sharee.userMessage)
  137. if let statusImage = status.statusImage {
  138. imageStatus.image = statusImage
  139. imageStatus.makeCircularBackground(withColor: .systemBackground)
  140. }
  141. self.status.text = status.statusMessage
  142. if self.status.text?.count ?? 0 > 0 {
  143. centerTitle.constant = -5
  144. } else {
  145. centerTitle.constant = 0
  146. }
  147. imageItem.image = utility.loadUserImage(for: sharee.shareWith, displayName: nil, urlBase: session.urlBase)
  148. let fileName = NCSession.shared.getFileName(urlBase: session.urlBase, user: sharee.shareWith)
  149. let results = NCManageDatabase.shared.getImageAvatarLoaded(fileName: fileName)
  150. if results.image == nil {
  151. let etag = NCManageDatabase.shared.getTableAvatar(fileName: fileName)?.etag
  152. NextcloudKit.shared.downloadAvatar(
  153. user: sharee.shareWith,
  154. fileNameLocalPath: NCUtilityFileSystem().directoryUserData + "/" + fileName,
  155. sizeImage: NCGlobal.shared.avatarSize,
  156. avatarSizeRounded: NCGlobal.shared.avatarSizeRounded,
  157. etag: etag, account: session.account) { _, imageAvatar, _, etag, _, error in
  158. if error == .success, let etag = etag, let imageAvatar = imageAvatar {
  159. NCManageDatabase.shared.addAvatar(fileName: fileName, etag: etag)
  160. self.imageItem.image = imageAvatar
  161. } else if error.errorCode == NCGlobal.shared.errorNotModified, let imageAvatar = NCManageDatabase.shared.setAvatarLoaded(fileName: fileName) {
  162. self.imageItem.image = imageAvatar
  163. }
  164. }
  165. }
  166. }
  167. }