NCShareUserCell.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. weak var delegate: NCShareUserCellDelegate?
  37. var indexPath: IndexPath {
  38. get { return index }
  39. set { index = newValue }
  40. }
  41. var fileAvatarImageView: UIImageView? {
  42. return imageItem
  43. }
  44. var fileUser: String? {
  45. get { return tableShare?.shareWith }
  46. set {}
  47. }
  48. func setupCellUI(userId: String) {
  49. guard let tableShare = tableShare else {
  50. return
  51. }
  52. self.accessibilityCustomActions = [UIAccessibilityCustomAction(
  53. name: NSLocalizedString("_show_profile_", comment: ""),
  54. target: self,
  55. selector: #selector(tapAvatarImage))]
  56. labelTitle.text = tableShare.shareWithDisplayname
  57. labelTitle.textColor = .label
  58. isUserInteractionEnabled = true
  59. labelQuickStatus.isHidden = false
  60. imageDownArrow.isHidden = false
  61. buttonMenu.isHidden = false
  62. buttonMenu.accessibilityLabel = NSLocalizedString("_more_", comment: "")
  63. imageItem.image = NCShareCommon.shared.getImageShareType(shareType: tableShare.shareType)
  64. let status = NCUtility.shared.getUserStatus(userIcon: tableShare.userIcon, userStatus: tableShare.userStatus, userMessage: tableShare.userMessage)
  65. imageStatus.image = status.onlineStatus
  66. self.status.text = status.statusMessage
  67. // If the initiator or the recipient is not the current user, show the list of sharees without any options to edit it.
  68. if tableShare.uidOwner != userId && tableShare.uidFileOwner != userId {
  69. isUserInteractionEnabled = false
  70. labelQuickStatus.isHidden = true
  71. imageDownArrow.isHidden = true
  72. buttonMenu.isHidden = true
  73. }
  74. btnQuickStatus.accessibilityHint = NSLocalizedString("_user_sharee_footer_", comment: "")
  75. btnQuickStatus.setTitle("", for: .normal)
  76. btnQuickStatus.contentHorizontalAlignment = .left
  77. if tableShare.permissions == NCGlobal.shared.permissionCreateShare {
  78. labelQuickStatus.text = NSLocalizedString("_share_file_drop_", comment: "")
  79. } else {
  80. // Read Only
  81. if CCUtility.isAnyPermission(toEdit: tableShare.permissions) {
  82. labelQuickStatus.text = NSLocalizedString("_share_editing_", comment: "")
  83. } else {
  84. labelQuickStatus.text = NSLocalizedString("_share_read_only_", comment: "")
  85. }
  86. }
  87. }
  88. override func awakeFromNib() {
  89. super.awakeFromNib()
  90. let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapAvatarImage))
  91. imageItem?.addGestureRecognizer(tapGesture)
  92. buttonMenu.setImage(UIImage(named: "shareMenu")?.image(color: .gray, size: 50), for: .normal)
  93. labelQuickStatus.textColor = NCBrandColor.shared.customer
  94. imageDownArrow.image = NCUtility.shared.loadImage(named: "arrowtriangle.down.fill", color: NCBrandColor.shared.customer)
  95. }
  96. @objc func tapAvatarImage(_ sender: UITapGestureRecognizer) {
  97. delegate?.showProfile(with: tableShare, sender: sender)
  98. }
  99. @IBAction func touchUpInsideMenu(_ sender: Any) {
  100. delegate?.tapMenu(with: tableShare, sender: sender)
  101. }
  102. @IBAction func quickStatusClicked(_ sender: Any) {
  103. delegate?.quickStatus(with: tableShare, sender: sender)
  104. }
  105. }
  106. protocol NCShareUserCellDelegate: AnyObject {
  107. func tapMenu(with tableShare: tableShare?, sender: Any)
  108. func showProfile(with tableComment: tableShare?, sender: Any)
  109. func quickStatus(with tableShare: tableShare?, sender: Any)
  110. }
  111. // MARK: - NCSearchUserDropDownCell
  112. class NCSearchUserDropDownCell: DropDownCell, NCCellProtocol {
  113. @IBOutlet weak var imageItem: UIImageView!
  114. @IBOutlet weak var imageStatus: UIImageView!
  115. @IBOutlet weak var status: UILabel!
  116. @IBOutlet weak var imageShareeType: UIImageView!
  117. @IBOutlet weak var centerTitle: NSLayoutConstraint!
  118. private var user: String = ""
  119. private var index = IndexPath()
  120. var indexPath: IndexPath {
  121. get { return index }
  122. set { index = newValue }
  123. }
  124. var fileAvatarImageView: UIImageView? {
  125. return imageItem
  126. }
  127. var fileUser: String? {
  128. get { return user }
  129. set { user = newValue ?? "" }
  130. }
  131. func setupCell(sharee: NKSharee, baseUrl: NCUserBaseUrl) {
  132. imageItem.image = NCShareCommon.shared.getImageShareType(shareType: sharee.shareType)
  133. imageShareeType.image = NCShareCommon.shared.getImageShareType(shareType: sharee.shareType)
  134. let status = NCUtility.shared.getUserStatus(userIcon: sharee.userIcon, userStatus: sharee.userStatus, userMessage: sharee.userMessage)
  135. imageStatus.image = status.onlineStatus
  136. self.status.text = status.statusMessage
  137. if self.status.text?.count ?? 0 > 0 {
  138. centerTitle.constant = -5
  139. } else {
  140. centerTitle.constant = 0
  141. }
  142. imageItem.image = NCUtility.shared.loadUserImage(
  143. for: sharee.shareWith,
  144. displayName: nil,
  145. userBaseUrl: baseUrl)
  146. let fileName = baseUrl.userBaseUrl + "-" + sharee.shareWith + ".png"
  147. if NCManageDatabase.shared.getImageAvatarLoaded(fileName: fileName) == nil {
  148. let fileNameLocalPath = String(CCUtility.getDirectoryUserData()) + "/" + fileName
  149. let etag = NCManageDatabase.shared.getTableAvatar(fileName: fileName)?.etag
  150. NextcloudKit.shared.downloadAvatar(
  151. user: sharee.shareWith,
  152. fileNameLocalPath: fileNameLocalPath,
  153. sizeImage: NCGlobal.shared.avatarSize,
  154. avatarSizeRounded: NCGlobal.shared.avatarSizeRounded,
  155. etag: etag) { _, imageAvatar, _, etag, error in
  156. if error == .success, let etag = etag, let imageAvatar = imageAvatar {
  157. NCManageDatabase.shared.addAvatar(fileName: fileName, etag: etag)
  158. self.imageItem.image = imageAvatar
  159. } else if error.errorCode == NCGlobal.shared.errorNotModified, let imageAvatar = NCManageDatabase.shared.setAvatarLoaded(fileName: fileName) {
  160. self.imageItem.image = imageAvatar
  161. }
  162. }
  163. }
  164. }
  165. }