NCShareAdvancePermission.swift 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // NCShareAdvancePermission.swift
  3. // Nextcloud
  4. //
  5. // Created by T-systems on 09/08/21.
  6. // Copyright © 2022 Henrik Storch. All rights reserved.
  7. //
  8. // Author Henrik Storch <henrik.storch@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 UIKit
  24. import NextcloudKit
  25. import SVGKit
  26. import CloudKit
  27. class NCShareAdvancePermission: UITableViewController, NCShareAdvanceFotterDelegate, NCShareDetail {
  28. func dismissShareAdvanceView(shouldSave: Bool) {
  29. guard shouldSave else {
  30. guard oldTableShare?.hasChanges(comparedTo: share) != false else {
  31. navigationController?.popViewController(animated: true)
  32. return
  33. }
  34. let alert = UIAlertController(
  35. title: NSLocalizedString("_cancel_request_", comment: ""),
  36. message: NSLocalizedString("_discard_changes_info_", comment: ""),
  37. preferredStyle: .alert)
  38. alert.addAction(UIAlertAction(
  39. title: NSLocalizedString("_discard_changes_", comment: ""),
  40. style: .destructive,
  41. handler: { _ in self.navigationController?.popViewController(animated: true) }))
  42. alert.addAction(UIAlertAction(title: NSLocalizedString("_continue_editing_", comment: ""), style: .default))
  43. self.present(alert, animated: true)
  44. return
  45. }
  46. Task {
  47. if metadata.e2eEncrypted && NCGlobal.shared.capabilityE2EEApiVersion == NCGlobal.shared.e2eeVersionV20 {
  48. let serverUrl = metadata.serverUrl + "/" + metadata.fileName
  49. let error = await NCNetworkingE2EE.shared.uploadMetadata(account: metadata.account, serverUrl: serverUrl, userId: metadata.userId, addUserId: share.shareWith)
  50. if error != .success {
  51. NCContentPresenter.shared.showError(error: error)
  52. }
  53. }
  54. if isNewShare {
  55. networking?.createShare(option: share)
  56. } else {
  57. networking?.updateShare(option: share)
  58. }
  59. }
  60. navigationController?.popViewController(animated: true)
  61. }
  62. var oldTableShare: tableShare?
  63. var share: NCTableShareable!
  64. var isNewShare: Bool { share is NCTableShareOptions }
  65. var metadata: tableMetadata!
  66. var shareConfig: NCShareConfig!
  67. var networking: NCShareNetworking?
  68. override func viewDidLoad() {
  69. super.viewDidLoad()
  70. self.shareConfig = NCShareConfig(parentMetadata: metadata, share: share)
  71. tableView.estimatedRowHeight = tableView.rowHeight
  72. tableView.rowHeight = UITableView.automaticDimension
  73. self.setNavigationTitle()
  74. self.navigationItem.hidesBackButton = true
  75. // disbale pull to dimiss
  76. isModalInPresentation = true
  77. }
  78. override func viewWillLayoutSubviews() {
  79. super.viewWillLayoutSubviews()
  80. guard tableView.tableHeaderView == nil, tableView.tableFooterView == nil else { return }
  81. setupHeaderView()
  82. setupFooterView()
  83. }
  84. func setupFooterView() {
  85. guard let footerView = (Bundle.main.loadNibNamed("NCShareAdvancePermissionFooter", owner: self, options: nil)?.first as? NCShareAdvancePermissionFooter) else { return }
  86. footerView.setupUI(delegate: self)
  87. // tableFooterView can't use auto layout directly
  88. let container = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 120))
  89. container.addSubview(footerView)
  90. tableView.tableFooterView = container
  91. footerView.translatesAutoresizingMaskIntoConstraints = false
  92. footerView.bottomAnchor.constraint(equalTo: container.bottomAnchor).isActive = true
  93. footerView.heightAnchor.constraint(equalTo: container.heightAnchor).isActive = true
  94. footerView.widthAnchor.constraint(equalTo: container.widthAnchor).isActive = true
  95. }
  96. func setupHeaderView() {
  97. guard let headerView = (Bundle.main.loadNibNamed("NCShareAdvancePermissionHeader", owner: self, options: nil)?.first as? NCShareAdvancePermissionHeader) else { return }
  98. headerView.setupUI(with: metadata)
  99. let container = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 220))
  100. container.addSubview(headerView)
  101. tableView.tableHeaderView = container
  102. headerView.translatesAutoresizingMaskIntoConstraints = false
  103. headerView.topAnchor.constraint(equalTo: container.topAnchor).isActive = true
  104. headerView.heightAnchor.constraint(equalTo: container.heightAnchor).isActive = true
  105. headerView.widthAnchor.constraint(equalTo: container.widthAnchor).isActive = true
  106. }
  107. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  108. if section == 0 {
  109. return NSLocalizedString("_permissions_", comment: "")
  110. } else if section == 1 {
  111. return NSLocalizedString("_advanced_", comment: "")
  112. } else { return nil }
  113. }
  114. override func numberOfSections(in tableView: UITableView) -> Int {
  115. return 2
  116. }
  117. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  118. if section == 0 {
  119. // check reshare permission, if restricted add note
  120. let maxPermission = metadata.directory ? NCGlobal.shared.permissionMaxFolderShare : NCGlobal.shared.permissionMaxFileShare
  121. return shareConfig.resharePermission != maxPermission ? shareConfig.permissions.count + 1 : shareConfig.permissions.count
  122. } else if section == 1 {
  123. return shareConfig.advanced.count
  124. } else { return 0 }
  125. }
  126. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  127. guard let cell = shareConfig.cellFor(indexPath: indexPath) else {
  128. let noteCell = UITableViewCell(style: .subtitle, reuseIdentifier: "noteCell")
  129. noteCell.detailTextLabel?.text = NSLocalizedString("_share_reshare_restricted_", comment: "")
  130. noteCell.detailTextLabel?.isEnabled = false
  131. noteCell.isUserInteractionEnabled = false
  132. noteCell.detailTextLabel?.numberOfLines = 0
  133. return noteCell
  134. }
  135. if let cell = cell as? NCShareDateCell { cell.onReload = tableView.reloadData }
  136. return cell
  137. }
  138. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  139. tableView.deselectRow(at: indexPath, animated: true)
  140. guard let cellConfig = shareConfig.config(for: indexPath) else { return }
  141. guard let cellConfig = cellConfig as? NCShareDetails else {
  142. cellConfig.didSelect(for: share)
  143. tableView.reloadData()
  144. return
  145. }
  146. switch cellConfig {
  147. case .hideDownload:
  148. share.hideDownload.toggle()
  149. tableView.reloadData()
  150. case .expirationDate:
  151. let cell = tableView.cellForRow(at: indexPath) as? NCShareDateCell
  152. cell?.textField.becomeFirstResponder()
  153. cell?.checkMaximumDate(account: metadata.account)
  154. case .password:
  155. guard share.password.isEmpty else {
  156. share.password = ""
  157. tableView.reloadData()
  158. return
  159. }
  160. let alertController = UIAlertController.password(titleKey: "_share_password_") { password in
  161. self.share.password = password ?? ""
  162. tableView.reloadData()
  163. }
  164. self.present(alertController, animated: true)
  165. case .note:
  166. let storyboard = UIStoryboard(name: "NCShare", bundle: nil)
  167. guard let viewNewUserComment = storyboard.instantiateViewController(withIdentifier: "NCShareNewUserAddComment") as? NCShareNewUserAddComment else { return }
  168. viewNewUserComment.metadata = self.metadata
  169. viewNewUserComment.share = self.share
  170. viewNewUserComment.onDismiss = tableView.reloadData
  171. self.navigationController?.pushViewController(viewNewUserComment, animated: true)
  172. case .label:
  173. let alertController = UIAlertController.withTextField(titleKey: "_share_link_name_") { textField in
  174. textField.placeholder = cellConfig.title
  175. textField.text = self.share.label
  176. } completion: { newValue in
  177. self.share.label = newValue ?? ""
  178. self.setNavigationTitle()
  179. tableView.reloadData()
  180. }
  181. self.present(alertController, animated: true)
  182. }
  183. }
  184. }