NCShare.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. //
  2. // NCShare.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 17/07/2019.
  6. // Copyright © 2019 Marino Faggiana. All rights reserved.
  7. // Copyright © 2022 Henrik Storch. All rights reserved.
  8. //
  9. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  10. // Author Henrik Storch <henrik.storch@nextcloud.com>
  11. //
  12. // This program is free software: you can redistribute it and/or modify
  13. // it under the terms of the GNU General Public License as published by
  14. // the Free Software Foundation, either version 3 of the License, or
  15. // (at your option) any later version.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of the GNU General Public License
  23. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. //
  25. import UIKit
  26. import Parchment
  27. import DropDown
  28. import NCCommunication
  29. import MarqueeLabel
  30. class NCShare: UIViewController, NCShareNetworkingDelegate, NCSharePagingContent {
  31. @IBOutlet weak var viewContainerConstraint: NSLayoutConstraint!
  32. @IBOutlet weak var sharedWithYouByView: UIView!
  33. @IBOutlet weak var sharedWithYouByImage: UIImageView!
  34. @IBOutlet weak var sharedWithYouByLabel: UILabel!
  35. @IBOutlet weak var sharedWithYouByNoteImage: UIImageView!
  36. @IBOutlet weak var sharedWithYouByNote: MarqueeLabel!
  37. @IBOutlet weak var searchFieldTopConstraint: NSLayoutConstraint!
  38. @IBOutlet weak var searchField: UITextField!
  39. var textField: UITextField? { searchField }
  40. @IBOutlet weak var tableView: UITableView!
  41. weak var appDelegate = UIApplication.shared.delegate as? AppDelegate
  42. public var metadata: tableMetadata?
  43. public var sharingEnabled = true
  44. public var height: CGFloat = 0
  45. var shares: (firstShareLink: tableShare?, share: [tableShare]?) = (nil, nil)
  46. private var dropDown = DropDown()
  47. var networking: NCShareNetworking?
  48. // MARK: - View Life Cycle
  49. override func viewDidLoad() {
  50. super.viewDidLoad()
  51. view.backgroundColor = NCBrandColor.shared.systemBackground
  52. viewContainerConstraint.constant = height
  53. searchFieldTopConstraint.constant = 10
  54. searchField.placeholder = NSLocalizedString("_shareLinksearch_placeholder_", comment: "")
  55. tableView.dataSource = self
  56. tableView.delegate = self
  57. tableView.allowsSelection = false
  58. tableView.backgroundColor = NCBrandColor.shared.systemBackground
  59. tableView.register(UINib(nibName: "NCShareLinkCell", bundle: nil), forCellReuseIdentifier: "cellLink")
  60. tableView.register(UINib(nibName: "NCShareUserCell", bundle: nil), forCellReuseIdentifier: "cellUser")
  61. NotificationCenter.default.addObserver(self, selector: #selector(reloadData), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadDataNCShare), object: nil)
  62. guard let appDelegate = appDelegate, let metadata = metadata else { return }
  63. checkSharedWithYou()
  64. reloadData()
  65. networking = NCShareNetworking(metadata: metadata, urlBase: appDelegate.urlBase, view: self.view, delegate: self)
  66. if sharingEnabled {
  67. let isVisible = (self.navigationController?.topViewController as? NCSharePaging)?.indexPage == .sharing
  68. networking?.readShare(showLoadingIndicator: isVisible)
  69. }
  70. // changeTheming
  71. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeTheming), object: nil)
  72. changeTheming()
  73. }
  74. func makeNewLinkShare() {
  75. guard
  76. let advancePermission = UIStoryboard(name: "NCShare", bundle: nil).instantiateViewController(withIdentifier: "NCShareAdvancePermission") as? NCShareAdvancePermission,
  77. let navigationController = self.navigationController,
  78. let metadata = self.metadata else { return }
  79. self.checkEnforcedPassword { password in
  80. advancePermission.networking = self.networking
  81. advancePermission.share = NCTableShareOptions.shareLink(metadata: metadata, password: password)
  82. advancePermission.metadata = self.metadata
  83. navigationController.pushViewController(advancePermission, animated: true)
  84. }
  85. }
  86. // Shared with you by ...
  87. func checkSharedWithYou() {
  88. guard let appDelegate = self.appDelegate, let metadata = metadata, !metadata.ownerId.isEmpty, metadata.ownerId != appDelegate.userId else { return }
  89. searchFieldTopConstraint.constant = 65
  90. sharedWithYouByView.isHidden = false
  91. sharedWithYouByLabel.text = NSLocalizedString("_shared_with_you_by_", comment: "") + " " + metadata.ownerDisplayName
  92. sharedWithYouByImage.image = NCUtility.shared.loadUserImage(
  93. for: metadata.ownerId,
  94. displayName: metadata.ownerDisplayName,
  95. userBaseUrl: appDelegate)
  96. let shareAction = UITapGestureRecognizer(target: self, action: #selector(openShareProfile))
  97. sharedWithYouByImage.addGestureRecognizer(shareAction)
  98. let shareLabelAction = UITapGestureRecognizer(target: self, action: #selector(openShareProfile))
  99. sharedWithYouByLabel.addGestureRecognizer(shareLabelAction)
  100. if !metadata.note.isEmpty {
  101. searchFieldTopConstraint.constant = 95
  102. sharedWithYouByNoteImage.isHidden = false
  103. sharedWithYouByNoteImage.image = NCUtility.shared.loadImage(named: "note.text", color: .gray)
  104. sharedWithYouByNote.isHidden = false
  105. sharedWithYouByNote.text = metadata.note
  106. sharedWithYouByNote.textColor = NCBrandColor.shared.label
  107. sharedWithYouByNote.trailingBuffer = sharedWithYouByNote.frame.width
  108. } else {
  109. sharedWithYouByNoteImage.isHidden = true
  110. sharedWithYouByNote.isHidden = true
  111. }
  112. let fileName = appDelegate.userBaseUrl + "-" + metadata.ownerId + ".png"
  113. if NCManageDatabase.shared.getImageAvatarLoaded(fileName: fileName) == nil {
  114. let fileNameLocalPath = String(CCUtility.getDirectoryUserData()) + "/" + fileName
  115. let etag = NCManageDatabase.shared.getTableAvatar(fileName: fileName)?.etag
  116. NCCommunication.shared.downloadAvatar(
  117. user: metadata.ownerId,
  118. fileNameLocalPath: fileNameLocalPath,
  119. sizeImage: NCGlobal.shared.avatarSize,
  120. avatarSizeRounded: NCGlobal.shared.avatarSizeRounded,
  121. etag: etag) { _, imageAvatar, _, etag, errorCode, _ in
  122. if errorCode == 0, let etag = etag, let imageAvatar = imageAvatar {
  123. NCManageDatabase.shared.addAvatar(fileName: fileName, etag: etag)
  124. self.sharedWithYouByImage.image = imageAvatar
  125. } else if errorCode == NCGlobal.shared.errorNotModified, let imageAvatar = NCManageDatabase.shared.setAvatarLoaded(fileName: fileName) {
  126. self.sharedWithYouByImage.image = imageAvatar
  127. }
  128. }
  129. }
  130. }
  131. // MARK: - Notification Center
  132. @objc func openShareProfile() {
  133. guard let metadata = metadata else { return }
  134. self.showProfileMenu(userId: metadata.ownerId)
  135. }
  136. @objc func changeTheming() {
  137. tableView.reloadData()
  138. }
  139. // MARK: -
  140. @objc func reloadData() {
  141. if let metadata = metadata {
  142. shares = NCManageDatabase.shared.getTableShares(metadata: metadata)
  143. }
  144. tableView.reloadData()
  145. }
  146. // MARK: - IBAction
  147. @IBAction func searchFieldDidEndOnExit(textField: UITextField) {
  148. guard let searchString = textField.text, !searchString.isEmpty else { return }
  149. networking?.getSharees(searchString: searchString)
  150. }
  151. func checkEnforcedPassword(shareType: Int? = nil, completion: @escaping (String?) -> Void) {
  152. guard let metadata = self.metadata,
  153. NCManageDatabase.shared.getCapabilitiesServerBool(account: metadata.account, elements: NCElementsJSON.shared.capabilitiesFileSharingPubPasswdEnforced, exists: false),
  154. shareType == NCShareCommon.shared.SHARE_TYPE_USER || shareType == NCShareCommon.shared.SHARE_TYPE_EMAIL
  155. else { return completion(nil) }
  156. self.present(UIAlertController.sharePassword(completion: completion), animated: true)
  157. }
  158. // MARK: - NCShareNetworkingDelegate
  159. func readShareCompleted() {
  160. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataNCShare)
  161. }
  162. func shareCompleted() {
  163. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataNCShare)
  164. }
  165. func unShareCompleted() {
  166. self.reloadData()
  167. }
  168. func updateShareWithError(idShare: Int) {
  169. self.reloadData()
  170. }
  171. func getSharees(sharees: [NCCommunicationSharee]?) {
  172. guard let sharees = sharees, let appDelegate = appDelegate else { return }
  173. dropDown = DropDown()
  174. let appearance = DropDown.appearance()
  175. appearance.backgroundColor = NCBrandColor.shared.systemBackground
  176. appearance.cornerRadius = 10
  177. appearance.shadowColor = UIColor(white: 0.5, alpha: 1)
  178. appearance.shadowOpacity = 0.9
  179. appearance.shadowRadius = 25
  180. appearance.animationduration = 0.25
  181. appearance.textColor = .darkGray
  182. appearance.setupMaskedCorners([.layerMaxXMaxYCorner, .layerMinXMaxYCorner])
  183. for sharee in sharees {
  184. var label = sharee.label
  185. if sharee.shareType == NCShareCommon.shared.SHARE_TYPE_CIRCLE {
  186. label += " (\(sharee.circleInfo), \(sharee.circleOwner))"
  187. }
  188. dropDown.dataSource.append(label)
  189. }
  190. dropDown.anchorView = searchField
  191. dropDown.bottomOffset = CGPoint(x: 0, y: searchField.bounds.height)
  192. dropDown.width = searchField.bounds.width
  193. dropDown.direction = .bottom
  194. dropDown.cellNib = UINib(nibName: "NCSearchUserDropDownCell", bundle: nil)
  195. dropDown.customCellConfiguration = { (index: Index, _, cell: DropDownCell) -> Void in
  196. guard let cell = cell as? NCSearchUserDropDownCell else { return }
  197. let sharee = sharees[index]
  198. cell.setupCell(sharee: sharee, baseUrl: appDelegate)
  199. }
  200. dropDown.selectionAction = { index, _ in
  201. let sharee = sharees[index]
  202. guard
  203. let advancePermission = UIStoryboard(name: "NCShare", bundle: nil).instantiateViewController(withIdentifier: "NCShareAdvancePermission") as? NCShareAdvancePermission,
  204. let navigationController = self.navigationController,
  205. let metadata = self.metadata else { return }
  206. self.checkEnforcedPassword(shareType: sharee.shareType) { password in
  207. let shareOptions = NCTableShareOptions(sharee: sharee, metadata: metadata)
  208. if let password = password { shareOptions.password = password }
  209. advancePermission.share = shareOptions
  210. advancePermission.networking = self.networking
  211. advancePermission.metadata = metadata
  212. navigationController.pushViewController(advancePermission, animated: true)
  213. }
  214. }
  215. dropDown.show()
  216. }
  217. }
  218. // MARK: - UITableViewDelegate
  219. extension NCShare: UITableViewDelegate {
  220. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  221. if indexPath.section == 0, indexPath.row == 0 {
  222. // internal cell has description
  223. return 90
  224. }
  225. return 70
  226. }
  227. }
  228. // MARK: - UITableViewDataSource
  229. extension NCShare: UITableViewDataSource {
  230. func numberOfSections(in tableView: UITableView) -> Int {
  231. return 2
  232. }
  233. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  234. guard section != 0 else { return 2 }
  235. return shares.share?.count ?? 0
  236. }
  237. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  238. // Setup default share cells
  239. guard indexPath.section != 0 else {
  240. guard let cell = tableView.dequeueReusableCell(withIdentifier: "cellLink", for: indexPath) as? NCShareLinkCell
  241. else { return UITableViewCell() }
  242. cell.delegate = self
  243. if indexPath.row == 0 {
  244. cell.isInternalLink = true
  245. } else if shares.firstShareLink?.isInvalidated != true {
  246. cell.tableShare = shares.firstShareLink
  247. }
  248. cell.setupCellUI()
  249. return cell
  250. }
  251. guard let appDelegate = appDelegate, let tableShare = shares.share?[indexPath.row] else { return UITableViewCell() }
  252. // LINK
  253. if tableShare.shareType == NCShareCommon.shared.SHARE_TYPE_LINK {
  254. if let cell = tableView.dequeueReusableCell(withIdentifier: "cellLink", for: indexPath) as? NCShareLinkCell {
  255. cell.tableShare = tableShare
  256. cell.delegate = self
  257. cell.setupCellUI()
  258. return cell
  259. }
  260. } else {
  261. // USER / GROUP etc.
  262. if let cell = tableView.dequeueReusableCell(withIdentifier: "cellUser", for: indexPath) as? NCShareUserCell {
  263. cell.tableShare = tableShare
  264. cell.delegate = self
  265. cell.setupCellUI(userId: appDelegate.userId)
  266. let fileName = appDelegate.userBaseUrl + "-" + tableShare.shareWith + ".png"
  267. NCOperationQueue.shared.downloadAvatar(user: tableShare.shareWith, dispalyName: tableShare.shareWithDisplayname, fileName: fileName, cell: cell, view: tableView)
  268. return cell
  269. }
  270. }
  271. return UITableViewCell()
  272. }
  273. }