NCShare.swift 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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. class NCSharePaging: UIViewController {
  26. private let pagingViewController = NCShareHeaderViewController()
  27. @objc var metadata: tableMetadata?
  28. override func viewDidLoad() {
  29. super.viewDidLoad()
  30. pagingViewController.metadata = metadata
  31. // Navigation Controller
  32. var image = CCGraphics.changeThemingColorImage(UIImage(named: "exit")!, width: 40, height: 40, color: UIColor.gray)
  33. image = image?.withRenderingMode(.alwaysOriginal)
  34. self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style:.plain, target: self, action: #selector(exitTapped))
  35. // Pagination
  36. addChild(pagingViewController)
  37. view.addSubview(pagingViewController.view)
  38. pagingViewController.didMove(toParent: self)
  39. pagingViewController.selectedTextColor = .black
  40. pagingViewController.indicatorColor = .black
  41. pagingViewController.indicatorOptions = .visible(
  42. height: 1,
  43. zIndex: Int.max,
  44. spacing: .zero,
  45. insets: .zero
  46. )
  47. // Contrain the paging view to all edges.
  48. pagingViewController.view.translatesAutoresizingMaskIntoConstraints = false
  49. NSLayoutConstraint.activate([
  50. pagingViewController.view.topAnchor.constraint(equalTo: view.topAnchor),
  51. pagingViewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
  52. pagingViewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  53. pagingViewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
  54. ])
  55. // Set our data source and delegate.
  56. pagingViewController.dataSource = self
  57. }
  58. @objc func exitTapped() {
  59. self.dismiss(animated: true, completion: nil)
  60. }
  61. }
  62. extension NCSharePaging: PagingViewControllerDataSource {
  63. func pagingViewController<T>(_ pagingViewController: PagingViewController<T>, viewControllerForIndex index: Int) -> UIViewController {
  64. let height = pagingViewController.options.menuHeight + NCSharePagingView.HeaderHeight
  65. switch index {
  66. case 0:
  67. let viewController = UIStoryboard(name: "NCActivity", bundle: nil).instantiateInitialViewController() as! NCActivity
  68. viewController.insets = UIEdgeInsets(top: height, left: 0, bottom: 0, right: 0)
  69. viewController.refreshControlEnable = false
  70. viewController.didSelectItemEnable = false
  71. viewController.filterFileID = metadata!.fileID
  72. return viewController
  73. case 1:
  74. let viewController = UIStoryboard(name: "NCShare", bundle: nil).instantiateViewController(withIdentifier: "comments") as! NCShareComments
  75. viewController.metadata = metadata!
  76. return viewController
  77. case 2:
  78. let viewController = UIStoryboard(name: "NCShare", bundle: nil).instantiateViewController(withIdentifier: "sharing") as! NCShare
  79. viewController.metadata = metadata!
  80. viewController.height = height
  81. return viewController
  82. default:
  83. return UIViewController()
  84. }
  85. }
  86. func pagingViewController<T>(_ pagingViewController: PagingViewController<T>, pagingItemForIndex index: Int) -> T {
  87. switch index {
  88. case 0:
  89. return PagingIndexItem(index: index, title: NSLocalizedString("_activity_", comment: "")) as! T
  90. case 1:
  91. return PagingIndexItem(index: index, title: NSLocalizedString("_comments_", comment: "")) as! T
  92. case 2:
  93. return PagingIndexItem(index: index, title: NSLocalizedString("_sharing_", comment: "")) as! T
  94. default:
  95. return PagingIndexItem(index: index, title: "") as! T
  96. }
  97. }
  98. func numberOfViewControllers<T>(in: PagingViewController<T>) -> Int{
  99. return 3
  100. }
  101. }
  102. class NCShareHeaderViewController: PagingViewController<PagingIndexItem> {
  103. public var image: UIImage?
  104. public var metadata: tableMetadata?
  105. override func loadView() {
  106. view = NCSharePagingView(
  107. options: options,
  108. collectionView: collectionView,
  109. pageView: pageViewController.view,
  110. metadata: metadata
  111. )
  112. }
  113. }
  114. class NCSharePagingView: PagingView {
  115. static let HeaderHeight: CGFloat = 200
  116. var metadata: tableMetadata?
  117. var headerHeightConstraint: NSLayoutConstraint?
  118. public init(options: Parchment.PagingOptions, collectionView: UICollectionView, pageView: UIView, metadata: tableMetadata?) {
  119. super.init(options: options, collectionView: collectionView, pageView: pageView)
  120. self.metadata = metadata
  121. }
  122. required init?(coder: NSCoder) {
  123. fatalError("init(coder:) has not been implemented")
  124. }
  125. override func setupConstraints() {
  126. let headerView = Bundle.main.loadNibNamed("NCShareHeaderView", owner: self, options: nil)?.first as! NCShareHeaderView
  127. if FileManager.default.fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(metadata!.fileID, fileNameView: metadata!.fileNameView)) {
  128. headerView.imageView.image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(metadata!.fileID, fileNameView: metadata!.fileNameView))
  129. } else {
  130. if metadata!.iconName.count > 0 {
  131. headerView.imageView.image = UIImage.init(named: metadata!.iconName)
  132. } else if metadata!.directory {
  133. let image = UIImage.init(named: "folder")!
  134. headerView.imageView.image = CCGraphics.changeThemingColorImage(image, width: image.size.width*2, height: image.size.height*2, color: NCBrandColor.sharedInstance.brandElement)
  135. } else {
  136. headerView.imageView.image = UIImage.init(named: "file")
  137. }
  138. }
  139. headerView.fileName.text = metadata?.fileNameView
  140. if metadata!.favorite {
  141. headerView.favorite.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "favorite"), width: 40, height: 40, color: NCBrandColor.sharedInstance.yellowFavorite), for: .normal)
  142. } else {
  143. headerView.favorite.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "favorite"), width: 40, height: 40, color: NCBrandColor.sharedInstance.textInfo), for: .normal)
  144. }
  145. headerView.info.text = CCUtility.transformedSize(metadata!.size) + ", " + CCUtility.dateDiff(metadata!.date as Date)
  146. addSubview(headerView)
  147. pageView.translatesAutoresizingMaskIntoConstraints = false
  148. collectionView.translatesAutoresizingMaskIntoConstraints = false
  149. headerView.translatesAutoresizingMaskIntoConstraints = false
  150. headerHeightConstraint = headerView.heightAnchor.constraint(
  151. equalToConstant: NCSharePagingView.HeaderHeight
  152. )
  153. headerHeightConstraint?.isActive = true
  154. NSLayoutConstraint.activate([
  155. collectionView.leadingAnchor.constraint(equalTo: leadingAnchor),
  156. collectionView.trailingAnchor.constraint(equalTo: trailingAnchor),
  157. collectionView.heightAnchor.constraint(equalToConstant: options.menuHeight),
  158. collectionView.topAnchor.constraint(equalTo: headerView.bottomAnchor),
  159. headerView.topAnchor.constraint(equalTo: topAnchor),
  160. headerView.leadingAnchor.constraint(equalTo: leadingAnchor),
  161. headerView.trailingAnchor.constraint(equalTo: trailingAnchor),
  162. pageView.leadingAnchor.constraint(equalTo: leadingAnchor),
  163. pageView.trailingAnchor.constraint(equalTo: trailingAnchor),
  164. pageView.bottomAnchor.constraint(equalTo: bottomAnchor),
  165. pageView.topAnchor.constraint(equalTo: topAnchor)
  166. ])
  167. }
  168. }
  169. class NCShareHeaderView: UIView {
  170. @IBOutlet weak var imageView: UIImageView!
  171. @IBOutlet weak var fileName: UILabel!
  172. @IBOutlet weak var info: UILabel!
  173. @IBOutlet weak var favorite: UIButton!
  174. }
  175. // MARK: - Comments
  176. class NCShareComments: UIViewController {
  177. var metadata: tableMetadata?
  178. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  179. override func viewDidLoad() {
  180. super.viewDidLoad()
  181. OCNetworking.sharedManager()?.getCommentsWithAccount(appDelegate.activeAccount, fileID: metadata?.fileID, completion: { (account, list, message, errorCode) in
  182. print("ciao")
  183. })
  184. }
  185. }
  186. // MARK: - Share
  187. class NCShare: UIViewController, UIGestureRecognizerDelegate, NCShareLinkCellDelegate, NCShareNetworkingDelegate {
  188. var metadata: tableMetadata?
  189. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  190. public var height: CGFloat = 0
  191. private var viewMenuShareLink: UIView?
  192. private var shareLinkMenuView: NCShareLinkMenuView?
  193. @IBOutlet weak var viewContainerConstraint: NSLayoutConstraint!
  194. @IBOutlet weak var searchField: UITextField!
  195. @IBOutlet weak var returnSearchButton: UIButton!
  196. @IBOutlet weak var shareLinkImage: UIImageView!
  197. @IBOutlet weak var shareLinkLabel: UILabel!
  198. @IBOutlet weak var buttonCopy: UIButton!
  199. @IBOutlet weak var buttonMenu: UIButton!
  200. @IBOutlet weak var tableView: UITableView!
  201. override func viewDidLoad() {
  202. super.viewDidLoad()
  203. viewContainerConstraint.constant = height
  204. searchField.placeholder = NSLocalizedString("_shareLinksearch_placeholder_", comment: "")
  205. returnSearchButton.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "arrowRight"), width: 100, height: 100, color: UIColor.gray), for: .normal)
  206. shareLinkLabel.text = NSLocalizedString("_share_link_", comment: "")
  207. shareLinkImage.image = NCShareCommon.sharedInstance.createLinkAvatar()
  208. buttonCopy.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "shareCopy"), width: 100, height: 100, color: UIColor.gray), for: .normal)
  209. tableView.dataSource = self
  210. tableView.delegate = self
  211. tableView.allowsSelection = false
  212. tableView.register(UINib.init(nibName: "NCShareLinkCell", bundle: nil), forCellReuseIdentifier: "cellLink")
  213. NotificationCenter.default.addObserver(self, selector: #selector(self.reloadData), name: NSNotification.Name(rawValue: "reloadDataNCShare"), object: nil)
  214. reloadData()
  215. let networking = NCShareNetworking.init(account: metadata!.account, activeUrl: appDelegate.activeUrl, view: nil, delegate: self)
  216. networking.readShare()
  217. }
  218. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  219. viewMenuShareLink?.removeFromSuperview()
  220. }
  221. @IBAction func touchUpInsideButtonCopy(_ sender: Any) {
  222. let shares = NCManageDatabase.sharedInstance.getTableShares(metadata: metadata!)
  223. tapCopy(with: shares.firstShareLink, sender: sender)
  224. }
  225. @IBAction func touchUpInsideButtonMenu(_ sender: Any) {
  226. let shares = NCManageDatabase.sharedInstance.getTableShares(metadata: metadata!)
  227. if shares.firstShareLink != nil {
  228. tapMenu(with: shares.firstShareLink!, sender: sender)
  229. } else {
  230. let networking = NCShareNetworking.init(account: metadata!.account, activeUrl: appDelegate.activeUrl, view: self.view, delegate: self)
  231. networking.share(metadata: metadata!, password: "", permission: 1, hideDownload: false)
  232. }
  233. }
  234. func tapCopy(with tableShare: tableShare?, sender: Any) {
  235. NCShareCommon.sharedInstance.copyLink(tableShare: tableShare, viewController: self)
  236. }
  237. func tapMenu(with tableShare: tableShare?, sender: Any) {
  238. NCShareCommon.sharedInstance.openViewMenuShareLink(view: self.view, height: height, tableShare: tableShare, metadata: metadata!)
  239. }
  240. @objc func reloadData() {
  241. let shares = NCManageDatabase.sharedInstance.getTableShares(metadata: metadata!)
  242. if shares.firstShareLink == nil {
  243. buttonMenu.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "shareAdd"), width: 100, height: 100, color: UIColor.gray), for: .normal)
  244. buttonCopy.isHidden = true
  245. } else {
  246. buttonMenu.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "shareMenu"), width: 100, height: 100, color: UIColor.gray), for: .normal)
  247. buttonCopy.isHidden = false
  248. }
  249. tableView.reloadData()
  250. }
  251. // NCShareNetworkingDelegate
  252. func readShareCompleted(errorCode: Int) {
  253. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadDataNCShare"), object: nil, userInfo: nil)
  254. }
  255. func shareCompleted(errorCode: Int) {
  256. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadDataNCShare"), object: nil, userInfo: nil)
  257. }
  258. func unShareCompleted() { }
  259. func updateShareWithError(idRemoteShared: Int) { }
  260. }
  261. extension NCShare: UITableViewDelegate {
  262. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  263. return 60
  264. }
  265. }
  266. extension NCShare: UITableViewDataSource {
  267. func numberOfSections(in tableView: UITableView) -> Int {
  268. return 1
  269. }
  270. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  271. var numOfRows = 0
  272. let shares = NCManageDatabase.sharedInstance.getTableShares(metadata: metadata!)
  273. if shares.share != nil {
  274. numOfRows = shares.share!.count
  275. }
  276. return numOfRows
  277. }
  278. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  279. let shares = NCManageDatabase.sharedInstance.getTableShares(metadata: metadata!)
  280. let tableShare = shares.share![indexPath.row]
  281. if tableShare.shareType == Int(shareTypeLink.rawValue) {
  282. if let cell = tableView.dequeueReusableCell(withIdentifier: "cellLink", for: indexPath) as? NCShareLinkCell {
  283. cell.tableShare = tableShare
  284. cell.delegate = self
  285. return cell
  286. }
  287. }
  288. return UITableViewCell()
  289. }
  290. }
  291. class NCShareLinkCell: UITableViewCell {
  292. private let iconShare: CGFloat = 200
  293. var tableShare: tableShare?
  294. var delegate: NCShareLinkCellDelegate?
  295. @IBOutlet weak var imageItem: UIImageView!
  296. @IBOutlet weak var labelTitle: UILabel!
  297. @IBOutlet weak var buttonCopy: UIButton!
  298. @IBOutlet weak var buttonMenu: UIButton!
  299. override func awakeFromNib() {
  300. super.awakeFromNib()
  301. imageItem.image = NCShareCommon.sharedInstance.createLinkAvatar()
  302. labelTitle.text = NSLocalizedString("_share_link_", comment: "")
  303. buttonCopy.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "shareCopy"), width:100, height: 100, color: UIColor.gray), for: .normal)
  304. buttonMenu.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "shareMenu"), width:100, height: 100, color: UIColor.gray), for: .normal)
  305. }
  306. @IBAction func touchUpInsideCopy(_ sender: Any) {
  307. delegate?.tapCopy(with: tableShare, sender: sender)
  308. }
  309. @IBAction func touchUpInsideMenu(_ sender: Any) {
  310. delegate?.tapMenu(with: tableShare, sender: sender)
  311. }
  312. }
  313. protocol NCShareLinkCellDelegate {
  314. func tapCopy(with tableShare: tableShare?, sender: Any)
  315. func tapMenu(with tableShare: tableShare?, sender: Any)
  316. }
  317. // MARK: - ShareLinkMenuView
  318. class NCShareLinkMenuView: UIView, UIGestureRecognizerDelegate, NCShareNetworkingDelegate {
  319. @IBOutlet weak var switchAllowEditing: UISwitch!
  320. @IBOutlet weak var labelAllowEditing: UILabel!
  321. @IBOutlet weak var switchHideDownload: UISwitch!
  322. @IBOutlet weak var labelHideDownload: UILabel!
  323. @IBOutlet weak var switchPasswordProtect: UISwitch!
  324. @IBOutlet weak var labelPasswordProtect: UILabel!
  325. @IBOutlet weak var fieldPasswordProtect: UITextField!
  326. @IBOutlet weak var buttonSendPasswordProtect: UIButton!
  327. @IBOutlet weak var switchSetExpirationDate: UISwitch!
  328. @IBOutlet weak var labelSetExpirationDate: UILabel!
  329. @IBOutlet weak var fieldSetExpirationDate: UITextField!
  330. @IBOutlet weak var imageNoteToRecipient: UIImageView!
  331. @IBOutlet weak var labelNoteToRecipient: UILabel!
  332. @IBOutlet weak var textViewNoteToRecipient: UITextView!
  333. @IBOutlet weak var buttonSendNoteToRecipient: UIButton!
  334. @IBOutlet weak var buttonDeleteShareLink: UIButton!
  335. @IBOutlet weak var labelDeleteShareLink: UILabel!
  336. @IBOutlet weak var imageDeleteShareLink: UIImageView!
  337. @IBOutlet weak var buttonAddAnotherLink: UIButton!
  338. @IBOutlet weak var labelAddAnotherLink: UILabel!
  339. @IBOutlet weak var imageAddAnotherLink: UIImageView!
  340. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  341. public let width: CGFloat = 250
  342. public let height: CGFloat = 470
  343. private var tableShare: tableShare?
  344. public var metadata: tableMetadata?
  345. public var viewWindow: UIView?
  346. override func awakeFromNib() {
  347. self.frame.size.width = width
  348. self.frame.size.height = height
  349. layer.borderColor = UIColor.lightGray.cgColor
  350. layer.borderWidth = 0.5
  351. layer.cornerRadius = 5
  352. layer.masksToBounds = false
  353. layer.shadowOffset = CGSize(width: 2, height: 2)
  354. layer.shadowOpacity = 0.2
  355. layer.cornerRadius = 5
  356. switchAllowEditing.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  357. switchHideDownload.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  358. switchPasswordProtect.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  359. switchSetExpirationDate.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  360. textViewNoteToRecipient.layer.borderColor = UIColor.lightGray.cgColor
  361. textViewNoteToRecipient.layer.borderWidth = 0.5
  362. textViewNoteToRecipient.layer.masksToBounds = false
  363. textViewNoteToRecipient.layer.cornerRadius = 5
  364. imageNoteToRecipient.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "file_txt"), width: 100, height: 100, color: UIColor.black)
  365. imageDeleteShareLink.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), width: 100, height: 100, color: UIColor.black)
  366. imageAddAnotherLink.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "add"), width: 100, height: 100, color: UIColor.black)
  367. }
  368. func addTap(view: UIView) {
  369. self.viewWindow = view
  370. let tap = UITapGestureRecognizer(target: self, action: #selector(tapHandler))
  371. tap.delegate = self
  372. viewWindow?.addGestureRecognizer(tap)
  373. }
  374. @objc func tapHandler(gesture: UITapGestureRecognizer) {
  375. viewWindow?.removeFromSuperview()
  376. }
  377. func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
  378. return gestureRecognizer.view == touch.view
  379. }
  380. func reloadData(idRemoteShared: Int) {
  381. tableShare = NCManageDatabase.sharedInstance.getTableShare(account: metadata!.account, idRemoteShared: idRemoteShared)
  382. // Allow editing
  383. if tableShare != nil && tableShare!.permissions > 1 {
  384. switchAllowEditing.setOn(true, animated: false)
  385. } else {
  386. switchAllowEditing.setOn(false, animated: false)
  387. }
  388. // Hide download
  389. if tableShare != nil && tableShare!.hideDownload {
  390. switchHideDownload.setOn(true, animated: false)
  391. } else {
  392. switchHideDownload.setOn(false, animated: false)
  393. }
  394. // Password protect
  395. if tableShare != nil && tableShare!.shareWith.count > 0 {
  396. switchPasswordProtect.setOn(true, animated: false)
  397. fieldPasswordProtect.isEnabled = true
  398. fieldPasswordProtect.text = tableShare!.shareWith
  399. buttonSendPasswordProtect.isEnabled = true
  400. } else {
  401. switchPasswordProtect.setOn(false, animated: false)
  402. fieldPasswordProtect.isEnabled = false
  403. fieldPasswordProtect.text = ""
  404. buttonSendPasswordProtect.isEnabled = false
  405. }
  406. // Set expiration date
  407. if tableShare != nil && tableShare!.expirationDate != nil {
  408. switchSetExpirationDate.setOn(true, animated: false)
  409. switchSetExpirationDate.isEnabled = true
  410. let dateFormatter = DateFormatter()
  411. dateFormatter.formatterBehavior = .behavior10_4
  412. dateFormatter.dateStyle = .short
  413. fieldSetExpirationDate.text = dateFormatter.string(from: tableShare!.expirationDate! as Date)
  414. } else {
  415. switchSetExpirationDate.setOn(false, animated: false)
  416. switchSetExpirationDate.isEnabled = false
  417. fieldSetExpirationDate.text = ""
  418. }
  419. // Note to recipient
  420. if tableShare != nil {
  421. textViewNoteToRecipient.text = tableShare!.note
  422. } else {
  423. textViewNoteToRecipient.text = ""
  424. }
  425. }
  426. @IBAction func switchAllowEditingChanged(sender: UISwitch) {
  427. guard let tableShare = self.tableShare else { return }
  428. var permission = 0
  429. if sender.isOn {
  430. permission = 3
  431. } else {
  432. permission = 1
  433. }
  434. let networking = NCShareNetworking.init(account: metadata!.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  435. networking.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: permission, note: nil, expirationTime: nil, hideDownload: tableShare.hideDownload)
  436. }
  437. @IBAction func switchHideDownloadChanged(sender: UISwitch) {
  438. guard let tableShare = self.tableShare else { return }
  439. let networking = NCShareNetworking.init(account: metadata!.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  440. networking.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: 0, note: nil, expirationTime: nil, hideDownload: sender.isOn)
  441. }
  442. @IBAction func switchPasswordProtectChanged(sender: UISwitch) {
  443. guard let tableShare = self.tableShare else { return }
  444. if sender.isOn {
  445. fieldPasswordProtect.isEnabled = true
  446. fieldPasswordProtect.text = tableShare.shareWith
  447. buttonSendPasswordProtect.isEnabled = true
  448. } else {
  449. let networking = NCShareNetworking.init(account: metadata!.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  450. networking.updateShare(idRemoteShared: tableShare.idRemoteShared, password: "", permission: 0, note: nil, expirationTime: nil, hideDownload: tableShare.hideDownload)
  451. }
  452. }
  453. @IBAction func buttonSendPasswordProtect(sender: UIButton) {
  454. guard let tableShare = self.tableShare else { return }
  455. if fieldPasswordProtect.text == nil || fieldPasswordProtect.text == "" { return }
  456. let networking = NCShareNetworking.init(account: metadata!.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  457. networking.updateShare(idRemoteShared: tableShare.idRemoteShared, password: fieldPasswordProtect.text, permission: 0, note: nil, expirationTime: nil, hideDownload: tableShare.hideDownload)
  458. }
  459. @IBAction func buttonSendNoteToRecipient(sender: UIButton) {
  460. guard let tableShare = self.tableShare else { return }
  461. if textViewNoteToRecipient.text == nil { return }
  462. let networking = NCShareNetworking.init(account: metadata!.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  463. networking.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: 0, note: textViewNoteToRecipient.text, expirationTime: nil, hideDownload: tableShare.hideDownload)
  464. }
  465. @IBAction func buttonDeleteShareLink(sender: UIButton) {
  466. guard let tableShare = self.tableShare else { return }
  467. let networking = NCShareNetworking.init(account: metadata!.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  468. networking.unShare(idRemoteShared: tableShare.idRemoteShared)
  469. }
  470. @IBAction func buttonAddAnotherLink(sender: UIButton) {
  471. let networking = NCShareNetworking.init(account: metadata!.account, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  472. networking.share(metadata: metadata!, password: "", permission: 1, hideDownload: false)
  473. }
  474. // delegate networking
  475. func readShareCompleted(errorCode: Int) {
  476. reloadData(idRemoteShared: tableShare?.idRemoteShared ?? 0)
  477. }
  478. func shareCompleted(errorCode: Int) {
  479. viewWindow?.removeFromSuperview()
  480. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadDataNCShare"), object: nil, userInfo: nil)
  481. }
  482. func unShareCompleted() {
  483. viewWindow?.removeFromSuperview()
  484. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadDataNCShare"), object: nil, userInfo: nil)
  485. }
  486. func updateShareWithError(idRemoteShared: Int) {
  487. reloadData(idRemoteShared: idRemoteShared)
  488. }
  489. }
  490. // --------------------------------------------------------------------------------------------
  491. // ===== Common =====
  492. // --------------------------------------------------------------------------------------------
  493. class NCShareCommon: NSObject {
  494. @objc static let sharedInstance: NCShareCommon = {
  495. let instance = NCShareCommon()
  496. return instance
  497. }()
  498. func createLinkAvatar() -> UIImage? {
  499. let size: CGFloat = 200
  500. let bottomImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "circle"), width: size, height: size, color: NCBrandColor.sharedInstance.brand)
  501. let topImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "sharebylink"), width: size, height: size, color: UIColor.white)
  502. UIGraphicsBeginImageContextWithOptions(CGSize(width: size, height: size), false, 0.0)
  503. bottomImage?.draw(in: CGRect(origin: CGPoint.zero, size: CGSize(width: size, height: size)))
  504. topImage?.draw(in: CGRect(origin: CGPoint(x: size/4, y: size/4), size: CGSize(width: size/2, height: size/2)))
  505. let image = UIGraphicsGetImageFromCurrentImageContext()
  506. UIGraphicsEndImageContext()
  507. return image
  508. }
  509. func openViewMenuShareLink(view: UIView, height: CGFloat, tableShare: tableShare?, metadata: tableMetadata) {
  510. let globalPoint = view.superview?.convert(view.frame.origin, to: nil)
  511. let window = UIApplication.shared.keyWindow!
  512. let viewWindow = UIView(frame: window.bounds)
  513. window.addSubview(viewWindow)
  514. let shareLinkMenuView = Bundle.main.loadNibNamed("NCShareLinkMenuView", owner: self, options: nil)?.first as? NCShareLinkMenuView
  515. shareLinkMenuView?.addTap(view: viewWindow)
  516. shareLinkMenuView?.metadata = metadata
  517. shareLinkMenuView?.reloadData(idRemoteShared: tableShare?.idRemoteShared ?? 0)
  518. let shareLinkMenuViewX = view.bounds.width - (shareLinkMenuView?.frame.width)! - 40 + globalPoint!.x
  519. var shareLinkMenuViewY = height + 10 + globalPoint!.y
  520. if (view.bounds.height - (height + 10)) < shareLinkMenuView!.height {
  521. shareLinkMenuViewY = shareLinkMenuViewY - height
  522. }
  523. shareLinkMenuView?.frame = CGRect(x: shareLinkMenuViewX, y: shareLinkMenuViewY, width: shareLinkMenuView!.width, height: shareLinkMenuView!.height)
  524. viewWindow.addSubview(shareLinkMenuView!)
  525. }
  526. func copyLink(tableShare: tableShare?, viewController: UIViewController) {
  527. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  528. var url: String = ""
  529. guard let tableShare = tableShare else { return }
  530. if tableShare.token.hasPrefix("http://") || tableShare.token.hasPrefix("https://") {
  531. url = tableShare.token
  532. } else if tableShare.url != "" {
  533. url = tableShare.url
  534. } else {
  535. url = appDelegate.activeUrl + "/" + k_share_link_middle_part_url_after_version_8 + tableShare.token
  536. }
  537. if let name = URL(string: url), !name.absoluteString.isEmpty {
  538. let objectsToShare = [name]
  539. let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
  540. viewController.present(activityVC, animated: true, completion: nil)
  541. }
  542. }
  543. }
  544. // --------------------------------------------------------------------------------------------
  545. // ===== Networking =====
  546. // --------------------------------------------------------------------------------------------
  547. class NCShareNetworking: NSObject {
  548. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  549. var account: String
  550. var activeUrl: String
  551. var delegate: NCShareNetworkingDelegate?
  552. var view: UIView?
  553. init(account: String, activeUrl: String, view: UIView?, delegate: NCShareNetworkingDelegate?) {
  554. self.account = account
  555. self.activeUrl = activeUrl
  556. self.view = view
  557. self.delegate = delegate
  558. super.init()
  559. }
  560. func readShare() {
  561. NCUtility.sharedInstance.startActivityIndicator(view: view, bottom: 0)
  562. OCNetworking.sharedManager()?.readShare(withAccount: account, completion: { (account, items, message, errorCode) in
  563. NCUtility.sharedInstance.stopActivityIndicator()
  564. if errorCode == 0 {
  565. let itemsOCSharedDto = items as! [OCSharedDto]
  566. NCManageDatabase.sharedInstance.addShare(account: self.account, activeUrl: self.activeUrl, items: itemsOCSharedDto)
  567. } else {
  568. self.appDelegate.messageNotification("_share_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  569. }
  570. self.delegate?.readShareCompleted(errorCode: errorCode)
  571. })
  572. }
  573. func share(metadata: tableMetadata, password: String, permission: Int, hideDownload: Bool) {
  574. NCUtility.sharedInstance.startActivityIndicator(view: view, bottom: 0)
  575. let fileName = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, activeUrl: activeUrl)!
  576. OCNetworking.sharedManager()?.share(withAccount: metadata.account, fileName: fileName, password: password, permission: permission, hideDownload: hideDownload, completion: { (account, message, errorCode) in
  577. if errorCode == 0 {
  578. OCNetworking.sharedManager()?.readShare(withAccount: account, completion: { (account, items, message, errorCode) in
  579. NCUtility.sharedInstance.stopActivityIndicator()
  580. if errorCode == 0 {
  581. let itemsOCSharedDto = items as! [OCSharedDto]
  582. NCManageDatabase.sharedInstance.addShare(account: self.account, activeUrl: self.activeUrl, items: itemsOCSharedDto)
  583. } else {
  584. self.appDelegate.messageNotification("_share_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  585. }
  586. self.delegate?.shareCompleted(errorCode: errorCode)
  587. })
  588. } else {
  589. NCUtility.sharedInstance.stopActivityIndicator()
  590. self.appDelegate.messageNotification("_share_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  591. }
  592. })
  593. }
  594. func unShare(idRemoteShared: Int) {
  595. NCUtility.sharedInstance.startActivityIndicator(view: view, bottom: 0)
  596. OCNetworking.sharedManager()?.unshareAccount(account, shareID: idRemoteShared, completion: { (account, message, errorCode) in
  597. NCUtility.sharedInstance.stopActivityIndicator()
  598. if errorCode == 0 {
  599. NCManageDatabase.sharedInstance.deleteTableShare(account: account!, idRemoteShared: idRemoteShared)
  600. self.delegate?.unShareCompleted()
  601. } else {
  602. self.appDelegate.messageNotification("_share_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  603. }
  604. })
  605. }
  606. func updateShare(idRemoteShared: Int, password: String?, permission: Int, note: String?, expirationTime: String?, hideDownload: Bool) {
  607. NCUtility.sharedInstance.startActivityIndicator(view: view, bottom: 0)
  608. OCNetworking.sharedManager()?.shareUpdateAccount(account, shareID: idRemoteShared, password: password, note:note, permission: permission, expirationTime: expirationTime, hideDownload: hideDownload, completion: { (account, message, errorCode) in
  609. NCUtility.sharedInstance.stopActivityIndicator()
  610. if errorCode == 0 {
  611. self.readShare()
  612. } else {
  613. self.appDelegate.messageNotification("_share_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  614. self.delegate?.updateShareWithError(idRemoteShared: idRemoteShared)
  615. }
  616. })
  617. }
  618. }
  619. protocol NCShareNetworkingDelegate {
  620. func readShareCompleted(errorCode: Int)
  621. func shareCompleted(errorCode: Int)
  622. func unShareCompleted()
  623. func updateShareWithError(idRemoteShared: Int)
  624. }