NCShare.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. //
  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. //
  23. import Foundation
  24. import Parchment
  25. import DropDown
  26. class NCShare: UIViewController, UIGestureRecognizerDelegate, NCShareLinkCellDelegate, NCShareUserCellDelegate, NCShareNetworkingDelegate {
  27. @IBOutlet weak var viewContainerConstraint: NSLayoutConstraint!
  28. @IBOutlet weak var sharedWithYouByView: UIView!
  29. @IBOutlet weak var sharedWithYouByImage: UIImageView!
  30. @IBOutlet weak var sharedWithYouByLabel: UILabel!
  31. @IBOutlet weak var searchFieldTopConstraint: NSLayoutConstraint!
  32. @IBOutlet weak var searchField: UITextField!
  33. @IBOutlet weak var shareLinkImage: UIImageView!
  34. @IBOutlet weak var shareLinkLabel: UILabel!
  35. @IBOutlet weak var buttonCopy: UIButton!
  36. @IBOutlet weak var buttonMenu: UIButton!
  37. @IBOutlet weak var tableView: UITableView!
  38. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  39. var metadata: tableMetadata?
  40. public var height: CGFloat = 0
  41. private var shareLinkMenuView: NCShareLinkMenuView?
  42. private var shareUserMenuView: NCShareUserMenuView?
  43. private var shareMenuViewWindow: UIView?
  44. private var dropDown = DropDown()
  45. private var networking: NCShareNetworking?
  46. override func viewDidLoad() {
  47. super.viewDidLoad()
  48. viewContainerConstraint.constant = height
  49. searchFieldTopConstraint.constant = 10
  50. searchField.placeholder = NSLocalizedString("_shareLinksearch_placeholder_", comment: "")
  51. shareLinkLabel.text = NSLocalizedString("_share_link_", comment: "")
  52. shareLinkImage.image = NCShareCommon.sharedInstance.createLinkAvatar()
  53. buttonCopy.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "shareCopy"), width: 100, height: 100, color: UIColor.gray), for: .normal)
  54. tableView.dataSource = self
  55. tableView.delegate = self
  56. tableView.allowsSelection = false
  57. tableView.register(UINib.init(nibName: "NCShareLinkCell", bundle: nil), forCellReuseIdentifier: "cellLink")
  58. tableView.register(UINib.init(nibName: "NCShareUserCell", bundle: nil), forCellReuseIdentifier: "cellUser")
  59. NotificationCenter.default.addObserver(self, selector: #selector(self.reloadData), name: NSNotification.Name(rawValue: "reloadDataNCShare"), object: nil)
  60. // Shared with you by ...
  61. if metadata!.ownerId != self.appDelegate.activeUserID {
  62. searchFieldTopConstraint.constant = 65
  63. sharedWithYouByView.isHidden = false
  64. sharedWithYouByLabel.text = NSLocalizedString("_shared_with_you_by_", comment: "") + " " + metadata!.ownerDisplayName
  65. let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.activeUser, activeUrl: appDelegate.activeUrl) + "-" + metadata!.ownerId + ".png"
  66. if FileManager.default.fileExists(atPath: fileNameLocalPath) {
  67. if let image = UIImage(contentsOfFile: fileNameLocalPath) { sharedWithYouByImage.image = image }
  68. } else {
  69. let url = appDelegate.activeUrl + k_avatar + metadata!.ownerId + "/" + k_avatar_size
  70. let encodedString = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
  71. OCNetworking.sharedManager()?.downloadContents(ofUrl: encodedString, completion: { (data, message, errorCode) in
  72. if errorCode == 0 && UIImage(data: data!) != nil {
  73. do {
  74. try data!.write(to: NSURL(fileURLWithPath: fileNameLocalPath) as URL, options: .atomic)
  75. if let image = UIImage(contentsOfFile: fileNameLocalPath) { self.sharedWithYouByImage.image = image }
  76. } catch { return }
  77. } else {
  78. self.sharedWithYouByImage.image = UIImage(named: "avatar")
  79. }
  80. })
  81. }
  82. }
  83. reloadData()
  84. networking = NCShareNetworking.init(metadata: metadata!, activeUrl: appDelegate.activeUrl, view: self.view, delegate: self)
  85. networking?.readShare()
  86. }
  87. @objc func reloadData() {
  88. let shares = NCManageDatabase.sharedInstance.getTableShares(metadata: metadata!)
  89. if shares.firstShareLink == nil {
  90. buttonMenu.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "shareAdd"), width: 100, height: 100, color: UIColor.gray), for: .normal)
  91. buttonCopy.isHidden = true
  92. } else {
  93. buttonMenu.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "shareMenu"), width: 100, height: 100, color: UIColor.gray), for: .normal)
  94. buttonCopy.isHidden = false
  95. }
  96. tableView.reloadData()
  97. }
  98. // MARK: - IBAction
  99. @IBAction func searchFieldDidEndOnExit(textField: UITextField) {
  100. guard let searchString = textField.text else { return }
  101. networking?.getUserAndGroup(searchString: searchString)
  102. }
  103. @IBAction func touchUpInsideButtonCopy(_ sender: Any) {
  104. guard let metadata = self.metadata else { return }
  105. let shares = NCManageDatabase.sharedInstance.getTableShares(metadata: metadata)
  106. tapCopy(with: shares.firstShareLink, sender: sender)
  107. }
  108. @IBAction func touchUpInsideButtonMenu(_ sender: Any) {
  109. guard let metadata = self.metadata else { return }
  110. let shares = NCManageDatabase.sharedInstance.getTableShares(metadata: metadata)
  111. if shares.firstShareLink != nil {
  112. tapMenu(with: shares.firstShareLink!, sender: sender)
  113. } else {
  114. networking?.share(password: "", permission: 1, hideDownload: false)
  115. }
  116. }
  117. @objc func tapLinkMenuViewWindow(gesture: UITapGestureRecognizer) {
  118. shareLinkMenuView?.unLoad()
  119. shareLinkMenuView = nil
  120. shareUserMenuView?.unLoad()
  121. shareUserMenuView = nil
  122. }
  123. func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
  124. return gestureRecognizer.view == touch.view
  125. }
  126. func tapCopy(with tableShare: tableShare?, sender: Any) {
  127. NCShareCommon.sharedInstance.copyLink(tableShare: tableShare, viewController: self)
  128. }
  129. func switchCanEdit(with tableShare: tableShare?, switch: Bool, sender: UISwitch) {
  130. guard let tableShare = tableShare else { return }
  131. guard let metadata = self.metadata else { return }
  132. let canShare = UtilsFramework.isPermission(toCanShare: tableShare.permissions)
  133. var permission: Int = 0
  134. if sender.isOn {
  135. permission = UtilsFramework.getPermissionsValue(byCanEdit: true, andCanCreate: true, andCanChange: true, andCanDelete: true, andCanShare: canShare, andIsFolder: metadata.directory)
  136. } else {
  137. permission = UtilsFramework.getPermissionsValue(byCanEdit: false, andCanCreate: false, andCanChange: false, andCanDelete: false, andCanShare: canShare, andIsFolder: metadata.directory)
  138. }
  139. networking?.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: permission, note: nil, expirationTime: nil, hideDownload: tableShare.hideDownload)
  140. }
  141. func tapMenu(with tableShare: tableShare?, sender: Any) {
  142. guard let tableShare = tableShare else { return }
  143. if tableShare.shareType == Int(shareTypeLink.rawValue) {
  144. let views = NCShareCommon.sharedInstance.openViewMenuShareLink(shareViewController: self, tableShare: tableShare, metadata: metadata!)
  145. shareLinkMenuView = views.shareLinkMenuView
  146. shareMenuViewWindow = views.viewWindow
  147. let tap = UITapGestureRecognizer(target: self, action: #selector(tapLinkMenuViewWindow))
  148. tap.delegate = self
  149. shareMenuViewWindow?.addGestureRecognizer(tap)
  150. } else {
  151. let views = NCShareCommon.sharedInstance.openViewMenuUser(shareViewController: self, tableShare: tableShare, metadata: metadata!)
  152. shareUserMenuView = views.shareUserMenuView
  153. shareMenuViewWindow = views.viewWindow
  154. let tap = UITapGestureRecognizer(target: self, action: #selector(tapLinkMenuViewWindow))
  155. tap.delegate = self
  156. shareMenuViewWindow?.addGestureRecognizer(tap)
  157. }
  158. }
  159. /// MARK: - NCShareNetworkingDelegate
  160. func readShareCompleted() {
  161. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadDataNCShare"), object: nil, userInfo: nil)
  162. }
  163. func shareCompleted() {
  164. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadDataNCShare"), object: nil, userInfo: nil)
  165. }
  166. func unShareCompleted() { }
  167. func updateShareWithError(idRemoteShared: Int) { }
  168. func getUserAndGroup(items: [OCShareUser]?) {
  169. guard let items = items else { return }
  170. dropDown = DropDown()
  171. let appearance = DropDown.appearance()
  172. appearance.backgroundColor = .white
  173. appearance.cornerRadius = 10
  174. appearance.shadowColor = UIColor(white: 0.5, alpha: 1)
  175. appearance.shadowOpacity = 0.9
  176. appearance.shadowRadius = 25
  177. appearance.animationduration = 0.25
  178. appearance.textColor = .darkGray
  179. if #available(iOS 11.0, *) {
  180. appearance.setupMaskedCorners([.layerMaxXMaxYCorner, .layerMinXMaxYCorner])
  181. }
  182. for item in items {
  183. if item.displayName != nil && item.displayName != "" {
  184. dropDown.dataSource.append(item.displayName)
  185. } else {
  186. dropDown.dataSource.append(item.name)
  187. }
  188. }
  189. dropDown.anchorView = searchField
  190. dropDown.bottomOffset = CGPoint(x: 0, y: searchField.bounds.height)
  191. dropDown.width = searchField.bounds.width
  192. dropDown.direction = .bottom
  193. dropDown.cellNib = UINib(nibName: "NCShareUserDropDownCell", bundle: nil)
  194. dropDown.customCellConfiguration = { (index: Index, item: String, cell: DropDownCell) -> Void in
  195. guard let cell = cell as? NCShareUserDropDownCell else { return }
  196. cell.imageItem.image = UIImage(named: "avatar")
  197. let item = items[index]
  198. let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(self.appDelegate.activeUser, activeUrl: self.appDelegate.activeUrl) + "-" + item.name + ".png"
  199. if FileManager.default.fileExists(atPath: fileNameLocalPath) {
  200. if let image = UIImage(contentsOfFile: fileNameLocalPath) { cell.imageItem.image = image }
  201. } else {
  202. DispatchQueue.global().async {
  203. let url = self.appDelegate.activeUrl + k_avatar + item.name + "/" + k_avatar_size
  204. let encodedString = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
  205. OCNetworking.sharedManager()?.downloadContents(ofUrl: encodedString, completion: { (data, message, errorCode) in
  206. if errorCode == 0 && UIImage(data: data!) != nil {
  207. do {
  208. try data!.write(to: NSURL(fileURLWithPath: fileNameLocalPath) as URL, options: .atomic)
  209. if let image = UIImage(contentsOfFile: fileNameLocalPath) { cell.imageItem.image = image }
  210. } catch { return }
  211. } else {
  212. cell.imageItem.image = UIImage(named: "avatar")
  213. }
  214. })
  215. }
  216. }
  217. if item.shareeType == 0 { cell.imageShareeType.image = UIImage(named: "shareTypeUser")} // shareTypeUser
  218. if item.shareeType == 1 { cell.imageShareeType.image = UIImage(named: "shareTypeGroup")} // shareTypeGroup
  219. if item.shareeType == 3 { cell.imageShareeType.image = UIImage(named: "shareTypeLink")} // shareTypeLink
  220. if item.shareeType == 4 { cell.imageShareeType.image = UIImage(named: "shareTypeEmail")} // shareTypeEmail
  221. if item.shareeType == 5 { cell.imageShareeType.image = UIImage(named: "shareTypeUser")} // shareTypeContact
  222. if item.shareeType == 6 { cell.imageShareeType.image = UIImage(named: "shareTypeLink")} // shareTypeRemote
  223. }
  224. dropDown.selectionAction = { [weak self] (index, item) in
  225. let item = items[index]
  226. self!.networking?.shareUserAndGroup(name: item.name, shareeType: item.shareeType, metadata: self!.metadata!)
  227. }
  228. dropDown.show()
  229. }
  230. }
  231. // MARK: - UITableViewDelegate
  232. extension NCShare: UITableViewDelegate {
  233. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  234. return 60
  235. }
  236. }
  237. // MARK: - UITableViewDataSource
  238. extension NCShare: UITableViewDataSource {
  239. func numberOfSections(in tableView: UITableView) -> Int {
  240. return 1
  241. }
  242. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  243. var numOfRows = 0
  244. let shares = NCManageDatabase.sharedInstance.getTableShares(metadata: metadata!)
  245. if shares.share != nil {
  246. numOfRows = shares.share!.count
  247. }
  248. return numOfRows
  249. }
  250. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  251. let shares = NCManageDatabase.sharedInstance.getTableShares(metadata: metadata!)
  252. let tableShare = shares.share![indexPath.row]
  253. // LINK
  254. if tableShare.shareType == Int(shareTypeLink.rawValue) {
  255. if let cell = tableView.dequeueReusableCell(withIdentifier: "cellLink", for: indexPath) as? NCShareLinkCell {
  256. cell.tableShare = tableShare
  257. cell.delegate = self
  258. return cell
  259. }
  260. } else {
  261. // USER
  262. if let cell = tableView.dequeueReusableCell(withIdentifier: "cellUser", for: indexPath) as? NCShareUserCell {
  263. cell.tableShare = tableShare
  264. cell.delegate = self
  265. cell.labelTitle.text = tableShare.shareWith
  266. cell.isUserInteractionEnabled = true
  267. cell.switchCanEdit.isHidden = false
  268. cell.labelCanEdit.isHidden = false
  269. cell.buttonMenu.isHidden = false
  270. let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.activeUser, activeUrl: appDelegate.activeUrl) + "-" + tableShare.shareWith + ".png"
  271. if FileManager.default.fileExists(atPath: fileNameLocalPath) {
  272. if let image = UIImage(contentsOfFile: fileNameLocalPath) { cell.imageItem.image = image }
  273. } else {
  274. DispatchQueue.global().async {
  275. let url = self.appDelegate.activeUrl + k_avatar + tableShare.shareWith + "/" + k_avatar_size
  276. let encodedString = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
  277. OCNetworking.sharedManager()?.downloadContents(ofUrl: encodedString, completion: { (data, message, errorCode) in
  278. if errorCode == 0 && UIImage(data: data!) != nil {
  279. do {
  280. try data!.write(to: NSURL(fileURLWithPath: fileNameLocalPath) as URL, options: .atomic)
  281. if let image = UIImage(contentsOfFile: fileNameLocalPath) { cell.imageItem.image = image }
  282. } catch { return }
  283. } else {
  284. cell.imageItem.image = UIImage(named: "avatar")
  285. }
  286. })
  287. }
  288. }
  289. if UtilsFramework.isAnyPermission(toEdit: tableShare.permissions) {
  290. cell.switchCanEdit.setOn(true, animated: false)
  291. } else {
  292. cell.switchCanEdit.setOn(false, animated: false)
  293. }
  294. // If the initiator or the recipient is not the current user, show the list of sharees without any options to edit it.
  295. if tableShare.uidOwner != self.appDelegate.activeUserID && tableShare.uidFileOwner != self.appDelegate.activeUserID {
  296. cell.isUserInteractionEnabled = false
  297. cell.switchCanEdit.isHidden = true
  298. cell.labelCanEdit.isHidden = true
  299. cell.buttonMenu.isHidden = true
  300. }
  301. return cell
  302. }
  303. }
  304. return UITableViewCell()
  305. }
  306. }
  307. // MARK: - NCShareLinkCell
  308. class NCShareLinkCell: UITableViewCell {
  309. @IBOutlet weak var imageItem: UIImageView!
  310. @IBOutlet weak var labelTitle: UILabel!
  311. @IBOutlet weak var buttonCopy: UIButton!
  312. @IBOutlet weak var buttonMenu: UIButton!
  313. private let iconShare: CGFloat = 200
  314. var tableShare: tableShare?
  315. var delegate: NCShareLinkCellDelegate?
  316. override func awakeFromNib() {
  317. super.awakeFromNib()
  318. imageItem.image = NCShareCommon.sharedInstance.createLinkAvatar()
  319. labelTitle.text = NSLocalizedString("_share_link_", comment: "")
  320. buttonCopy.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "shareCopy"), width:100, height: 100, color: UIColor.gray), for: .normal)
  321. buttonMenu.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "shareMenu"), width:100, height: 100, color: UIColor.gray), for: .normal)
  322. }
  323. @IBAction func touchUpInsideCopy(_ sender: Any) {
  324. delegate?.tapCopy(with: tableShare, sender: sender)
  325. }
  326. @IBAction func touchUpInsideMenu(_ sender: Any) {
  327. delegate?.tapMenu(with: tableShare, sender: sender)
  328. }
  329. }
  330. protocol NCShareLinkCellDelegate {
  331. func tapCopy(with tableShare: tableShare?, sender: Any)
  332. func tapMenu(with tableShare: tableShare?, sender: Any)
  333. }
  334. // MARK: - NCShareUserCell
  335. class NCShareUserCell: UITableViewCell {
  336. @IBOutlet weak var imageItem: UIImageView!
  337. @IBOutlet weak var labelTitle: UILabel!
  338. @IBOutlet weak var labelCanEdit: UILabel!
  339. @IBOutlet weak var switchCanEdit: UISwitch!
  340. @IBOutlet weak var buttonMenu: UIButton!
  341. var tableShare: tableShare?
  342. var delegate: NCShareUserCellDelegate?
  343. override func awakeFromNib() {
  344. super.awakeFromNib()
  345. switchCanEdit.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  346. switchCanEdit.onTintColor = NCBrandColor.sharedInstance.brand
  347. labelCanEdit.text = NSLocalizedString("_share_permission_edit_", comment: "")
  348. buttonMenu.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "shareMenu"), width:100, height: 100, color: UIColor.gray), for: .normal)
  349. }
  350. @IBAction func switchCanEditChanged(sender: UISwitch) {
  351. delegate?.switchCanEdit(with: tableShare, switch: sender.isOn, sender: sender)
  352. }
  353. @IBAction func touchUpInsideMenu(_ sender: Any) {
  354. delegate?.tapMenu(with: tableShare, sender: sender)
  355. }
  356. }
  357. protocol NCShareUserCellDelegate {
  358. func switchCanEdit(with tableShare: tableShare?, switch: Bool, sender: UISwitch)
  359. func tapMenu(with tableShare: tableShare?, sender: Any)
  360. }
  361. // MARK: - NCShareUserDropDownCell
  362. class NCShareUserDropDownCell: DropDownCell {
  363. @IBOutlet weak var imageItem: UIImageView!
  364. @IBOutlet weak var imageShareeType: UIImageView!
  365. }