NCShare.swift 14 KB

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