NCShare.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. // Author Henrik Storch <henrik.storch@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 Parchment
  26. import DropDown
  27. import NCCommunication
  28. import MarqueeLabel
  29. class NCShare: UIViewController, UIGestureRecognizerDelegate, NCShareNetworkingDelegate, NCSharePagingContent {
  30. @IBOutlet weak var viewContainerConstraint: NSLayoutConstraint!
  31. @IBOutlet weak var sharedWithYouByView: UIView!
  32. @IBOutlet weak var sharedWithYouByImage: UIImageView!
  33. @IBOutlet weak var sharedWithYouByLabel: UILabel!
  34. @IBOutlet weak var sharedWithYouByNoteImage: UIImageView!
  35. @IBOutlet weak var sharedWithYouByNote: MarqueeLabel!
  36. @IBOutlet weak var searchFieldTopConstraint: NSLayoutConstraint!
  37. @IBOutlet weak var searchField: UITextField!
  38. var textField: UITextField? { searchField }
  39. @IBOutlet weak var tableView: UITableView!
  40. weak var appDelegate = UIApplication.shared.delegate as? AppDelegate
  41. public var metadata: tableMetadata?
  42. public var sharingEnabled = true
  43. public var height: CGFloat = 0
  44. var shares: (firstShareLink: tableShare?, share: [tableShare]?) = (nil, nil)
  45. var shareLinkMenuView: NCShareLinkMenuView?
  46. var shareUserMenuView: NCShareUserMenuView?
  47. var shareMenuViewWindow: UIView?
  48. private var dropDown = DropDown()
  49. var networking: NCShareNetworking?
  50. // MARK: - View Life Cycle
  51. override func viewDidLoad() {
  52. super.viewDidLoad()
  53. view.backgroundColor = NCBrandColor.shared.systemBackground
  54. viewContainerConstraint.constant = height
  55. searchFieldTopConstraint.constant = 10
  56. searchField.placeholder = NSLocalizedString("_shareLinksearch_placeholder_", comment: "")
  57. tableView.dataSource = self
  58. tableView.delegate = self
  59. tableView.allowsSelection = false
  60. tableView.backgroundColor = NCBrandColor.shared.systemBackground
  61. tableView.register(UINib(nibName: "NCShareLinkCell", bundle: nil), forCellReuseIdentifier: "cellLink")
  62. tableView.register(UINib(nibName: "NCShareUserCell", bundle: nil), forCellReuseIdentifier: "cellUser")
  63. NotificationCenter.default.addObserver(self, selector: #selector(reloadData), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadDataNCShare), object: nil)
  64. guard let appDelegate = appDelegate, let metadata = metadata else { return }
  65. checkSharedWithYou()
  66. reloadData()
  67. networking = NCShareNetworking(metadata: metadata, urlBase: appDelegate.urlBase, view: self.view, delegate: self)
  68. if sharingEnabled {
  69. let isVisible = (self.navigationController?.topViewController as? NCSharePaging)?.indexPage == .sharing
  70. networking?.readShare(showLoadingIndicator: isVisible)
  71. }
  72. // changeTheming
  73. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeTheming), object: nil)
  74. NotificationCenter.default.addObserver(self, selector: #selector(changePermissions(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterShareChangePermissions), object: nil)
  75. changeTheming()
  76. }
  77. // Shared with you by ...
  78. func checkSharedWithYou() {
  79. guard let appDelegate = self.appDelegate, let metadata = metadata, !metadata.ownerId.isEmpty, metadata.ownerId != appDelegate.userId else { return }
  80. searchFieldTopConstraint.constant = 65
  81. sharedWithYouByView.isHidden = false
  82. sharedWithYouByLabel.text = NSLocalizedString("_shared_with_you_by_", comment: "") + " " + metadata.ownerDisplayName
  83. sharedWithYouByImage.image = NCUtility.shared.loadUserImage(
  84. for: metadata.ownerId,
  85. displayName: metadata.ownerDisplayName,
  86. userBaseUrl: appDelegate)
  87. let shareAction = UITapGestureRecognizer(target: self, action: #selector(openShareProfile))
  88. sharedWithYouByImage.addGestureRecognizer(shareAction)
  89. let shareLabelAction = UITapGestureRecognizer(target: self, action: #selector(openShareProfile))
  90. sharedWithYouByLabel.addGestureRecognizer(shareLabelAction)
  91. if !metadata.note.isEmpty {
  92. searchFieldTopConstraint.constant = 95
  93. sharedWithYouByNoteImage.isHidden = false
  94. sharedWithYouByNoteImage.image = NCUtility.shared.loadImage(named: "note.text", color: .gray)
  95. sharedWithYouByNote.isHidden = false
  96. sharedWithYouByNote.text = metadata.note
  97. sharedWithYouByNote.textColor = NCBrandColor.shared.label
  98. sharedWithYouByNote.trailingBuffer = sharedWithYouByNote.frame.width
  99. } else {
  100. sharedWithYouByNoteImage.isHidden = true
  101. sharedWithYouByNote.isHidden = true
  102. }
  103. let fileName = appDelegate.userBaseUrl + "-" + metadata.ownerId + ".png"
  104. if NCManageDatabase.shared.getImageAvatarLoaded(fileName: fileName) == nil {
  105. let fileNameLocalPath = String(CCUtility.getDirectoryUserData()) + "/" + fileName
  106. let etag = NCManageDatabase.shared.getTableAvatar(fileName: fileName)?.etag
  107. NCCommunication.shared.downloadAvatar(
  108. user: metadata.ownerId,
  109. fileNameLocalPath: fileNameLocalPath,
  110. sizeImage: NCGlobal.shared.avatarSize,
  111. avatarSizeRounded: NCGlobal.shared.avatarSizeRounded,
  112. etag: etag) { _, imageAvatar, _, etag, errorCode, _ in
  113. if errorCode == 0, let etag = etag, let imageAvatar = imageAvatar {
  114. NCManageDatabase.shared.addAvatar(fileName: fileName, etag: etag)
  115. self.sharedWithYouByImage.image = imageAvatar
  116. } else if errorCode == NCGlobal.shared.errorNotModified, let imageAvatar = NCManageDatabase.shared.setAvatarLoaded(fileName: fileName) {
  117. self.sharedWithYouByImage.image = imageAvatar
  118. }
  119. }
  120. }
  121. }
  122. // MARK: - Notification Center
  123. @objc func openShareProfile() {
  124. guard let metadata = metadata else { return }
  125. self.showProfileMenu(userId: metadata.ownerId)
  126. }
  127. @objc func changeTheming() {
  128. tableView.reloadData()
  129. }
  130. @objc func changePermissions(_ notification: NSNotification) {
  131. if let userInfo = notification.userInfo as NSDictionary? {
  132. if let idShare = userInfo["idShare"] as? Int, let permissions = userInfo["permissions"] as? Int, let hideDownload = userInfo["hideDownload"] as? Bool {
  133. networking?.updateShare(idShare: idShare, password: nil, permissions: permissions, note: nil, label: nil, expirationDate: nil, hideDownload: hideDownload)
  134. }
  135. }
  136. }
  137. // MARK: -
  138. @objc func reloadData() {
  139. if let metadata = metadata {
  140. shares = NCManageDatabase.shared.getTableShares(metadata: metadata)
  141. }
  142. tableView.reloadData()
  143. }
  144. // MARK: - IBAction
  145. @IBAction func searchFieldDidEndOnExit(textField: UITextField) {
  146. guard let searchString = textField.text, !searchString.isEmpty else { return }
  147. networking?.getSharees(searchString: searchString)
  148. }
  149. func checkEnforcedPassword(callback: @escaping (String?) -> Void) {
  150. guard let metadata = self.metadata,
  151. NCManageDatabase.shared.getCapabilitiesServerBool(account: metadata.account, elements: NCElementsJSON.shared.capabilitiesFileSharingPubPasswdEnforced, exists: false)
  152. else { return callback(nil) }
  153. let alertController = UIAlertController(title: NSLocalizedString("_enforce_password_protection_", comment: ""), message: "", preferredStyle: .alert)
  154. alertController.addTextField { textField in
  155. textField.isSecureTextEntry = true
  156. }
  157. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .default) { _ in })
  158. let okAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default) { _ in
  159. let password = alertController.textFields?.first?.text
  160. callback(password)
  161. }
  162. alertController.addAction(okAction)
  163. self.present(alertController, animated: true, completion: nil)
  164. }
  165. @objc func tapLinkMenuViewWindow(gesture: UITapGestureRecognizer) {
  166. shareLinkMenuView?.unLoad()
  167. shareLinkMenuView = nil
  168. shareUserMenuView?.unLoad()
  169. shareUserMenuView = nil
  170. }
  171. func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
  172. return gestureRecognizer.view == touch.view
  173. }
  174. // MARK: - NCShareNetworkingDelegate
  175. func readShareCompleted() {
  176. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataNCShare)
  177. }
  178. func shareCompleted() {
  179. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataNCShare)
  180. }
  181. func unShareCompleted() { }
  182. func updateShareWithError(idShare: Int) {
  183. self.reloadData()
  184. }
  185. func getSharees(sharees: [NCCommunicationSharee]?) {
  186. guard let sharees = sharees, let appDelegate = appDelegate else { return }
  187. dropDown = DropDown()
  188. let appearance = DropDown.appearance()
  189. appearance.backgroundColor = NCBrandColor.shared.systemBackground
  190. appearance.cornerRadius = 10
  191. appearance.shadowColor = UIColor(white: 0.5, alpha: 1)
  192. appearance.shadowOpacity = 0.9
  193. appearance.shadowRadius = 25
  194. appearance.animationduration = 0.25
  195. appearance.textColor = .darkGray
  196. appearance.setupMaskedCorners([.layerMaxXMaxYCorner, .layerMinXMaxYCorner])
  197. for sharee in sharees {
  198. var label = sharee.label
  199. if sharee.shareType == NCShareCommon.shared.SHARE_TYPE_CIRCLE {
  200. label += " (\(sharee.circleInfo), \(sharee.circleOwner))"
  201. }
  202. dropDown.dataSource.append(label)
  203. }
  204. dropDown.anchorView = searchField
  205. dropDown.bottomOffset = CGPoint(x: 0, y: searchField.bounds.height)
  206. dropDown.width = searchField.bounds.width
  207. dropDown.direction = .bottom
  208. dropDown.cellNib = UINib(nibName: "NCSearchUserDropDownCell", bundle: nil)
  209. dropDown.customCellConfiguration = { (index: Index, _, cell: DropDownCell) -> Void in
  210. guard let cell = cell as? NCSearchUserDropDownCell else { return }
  211. let sharee = sharees[index]
  212. cell.setupCell(sharee: sharee, baseUrl: appDelegate)
  213. }
  214. dropDown.selectionAction = { index, _ in
  215. let sharee = sharees[index]
  216. self.checkEnforcedPassword { password in
  217. guard let metadata = self.metadata else { return }
  218. self.networking?.createShare(shareWith: sharee.shareWith, shareType: sharee.shareType, password: password, metadata: metadata)
  219. }
  220. }
  221. dropDown.show()
  222. }
  223. }
  224. // MARK: - UITableViewDelegate
  225. extension NCShare: UITableViewDelegate {
  226. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  227. if indexPath.section == 0, indexPath.row == 0 {
  228. // internal cell has description
  229. return 90
  230. }
  231. return 70
  232. }
  233. }
  234. // MARK: - UITableViewDataSource
  235. extension NCShare: UITableViewDataSource {
  236. func numberOfSections(in tableView: UITableView) -> Int {
  237. return 2
  238. }
  239. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  240. guard section != 0 else { return 2 }
  241. return shares.share?.count ?? 0
  242. }
  243. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  244. // Setup default share cells
  245. guard indexPath.section != 0 else {
  246. guard let cell = tableView.dequeueReusableCell(withIdentifier: "cellLink", for: indexPath) as? NCShareLinkCell
  247. else { return UITableViewCell() }
  248. cell.delegate = self
  249. if indexPath.row == 0 {
  250. cell.isInternalLink = true
  251. } else {
  252. cell.tableShare = shares.firstShareLink
  253. }
  254. cell.setupCellUI()
  255. return cell
  256. }
  257. guard let appDelegate = appDelegate, let tableShare = shares.share?[indexPath.row] else { return UITableViewCell() }
  258. // LINK
  259. if tableShare.shareType == 3 {
  260. if let cell = tableView.dequeueReusableCell(withIdentifier: "cellLink", for: indexPath) as? NCShareLinkCell {
  261. cell.tableShare = tableShare
  262. cell.delegate = self
  263. cell.setupCellUI()
  264. return cell
  265. }
  266. } else {
  267. // USER
  268. if let cell = tableView.dequeueReusableCell(withIdentifier: "cellUser", for: indexPath) as? NCShareUserCell {
  269. cell.tableShare = tableShare
  270. cell.delegate = self
  271. cell.setupCellUI(userId: appDelegate.userId)
  272. let fileName = appDelegate.userBaseUrl + "-" + tableShare.shareWith + ".png"
  273. NCOperationQueue.shared.downloadAvatar(user: tableShare.shareWith, dispalyName: tableShare.shareWithDisplayname, fileName: fileName, cell: cell, view: tableView)
  274. return cell
  275. }
  276. }
  277. return UITableViewCell()
  278. }
  279. }