NCShareAdvancePermission.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 isNewShare {
  48. let serverUrl = metadata.serverUrl + "/" + metadata.fileName
  49. if share.shareType != NCShareCommon.shared.SHARE_TYPE_LINK, metadata.e2eEncrypted,
  50. NCGlobal.shared.capabilityE2EEApiVersion == NCGlobal.shared.e2eeVersionV20 {
  51. if NCNetworkingE2EE.shared.isInUpload(account: metadata.account, serverUrl: serverUrl) {
  52. let error = NKError(errorCode: NCGlobal.shared.errorE2EEUploadInProgress, errorDescription: NSLocalizedString("_e2e_in_upload_", comment: ""))
  53. return NCContentPresenter.shared.showInfo(error: error)
  54. }
  55. let error = await NCNetworkingE2EE().uploadMetadata(account: metadata.account, serverUrl: serverUrl, userId: metadata.userId, addUserId: share.shareWith, removeUserId: nil)
  56. if error != .success {
  57. NCContentPresenter.shared.showError(error: error)
  58. return
  59. }
  60. }
  61. networking?.createShare(option: share)
  62. } else {
  63. networking?.updateShare(option: share)
  64. }
  65. }
  66. navigationController?.popViewController(animated: true)
  67. }
  68. var oldTableShare: tableShare?
  69. var share: NCTableShareable!
  70. var isNewShare: Bool { share is NCTableShareOptions }
  71. var metadata: tableMetadata!
  72. var shareConfig: NCShareConfig!
  73. var networking: NCShareNetworking?
  74. override func viewDidLoad() {
  75. super.viewDidLoad()
  76. self.shareConfig = NCShareConfig(parentMetadata: metadata, share: share)
  77. tableView.estimatedRowHeight = tableView.rowHeight
  78. tableView.rowHeight = UITableView.automaticDimension
  79. self.setNavigationTitle()
  80. self.navigationItem.hidesBackButton = true
  81. // disbale pull to dimiss
  82. isModalInPresentation = true
  83. }
  84. override func viewWillLayoutSubviews() {
  85. super.viewWillLayoutSubviews()
  86. guard tableView.tableHeaderView == nil, tableView.tableFooterView == nil else { return }
  87. setupHeaderView()
  88. setupFooterView()
  89. }
  90. func setupFooterView() {
  91. guard let footerView = (Bundle.main.loadNibNamed("NCShareAdvancePermissionFooter", owner: self, options: nil)?.first as? NCShareAdvancePermissionFooter) else { return }
  92. footerView.setupUI(delegate: self)
  93. // tableFooterView can't use auto layout directly
  94. let container = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 120))
  95. container.addSubview(footerView)
  96. tableView.tableFooterView = container
  97. footerView.translatesAutoresizingMaskIntoConstraints = false
  98. footerView.bottomAnchor.constraint(equalTo: container.bottomAnchor).isActive = true
  99. footerView.heightAnchor.constraint(equalTo: container.heightAnchor).isActive = true
  100. footerView.widthAnchor.constraint(equalTo: container.widthAnchor).isActive = true
  101. }
  102. func setupHeaderView() {
  103. guard let headerView = (Bundle.main.loadNibNamed("NCShareAdvancePermissionHeader", owner: self, options: nil)?.first as? NCShareAdvancePermissionHeader) else { return }
  104. headerView.setupUI(with: metadata)
  105. let container = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 220))
  106. container.addSubview(headerView)
  107. tableView.tableHeaderView = container
  108. headerView.translatesAutoresizingMaskIntoConstraints = false
  109. headerView.topAnchor.constraint(equalTo: container.topAnchor).isActive = true
  110. headerView.heightAnchor.constraint(equalTo: container.heightAnchor).isActive = true
  111. headerView.widthAnchor.constraint(equalTo: container.widthAnchor).isActive = true
  112. }
  113. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  114. if section == 0 {
  115. return NSLocalizedString("_permissions_", comment: "")
  116. } else if section == 1 {
  117. return NSLocalizedString("_advanced_", comment: "")
  118. } else { return nil }
  119. }
  120. override func numberOfSections(in tableView: UITableView) -> Int {
  121. return 2
  122. }
  123. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  124. if section == 0 {
  125. // check reshare permission, if restricted add note
  126. let maxPermission = metadata.directory ? NCGlobal.shared.permissionMaxFolderShare : NCGlobal.shared.permissionMaxFileShare
  127. return shareConfig.resharePermission != maxPermission ? shareConfig.permissions.count + 1 : shareConfig.permissions.count
  128. } else if section == 1 {
  129. return shareConfig.advanced.count
  130. } else { return 0 }
  131. }
  132. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  133. guard let cell = shareConfig.cellFor(indexPath: indexPath) else {
  134. let noteCell = UITableViewCell(style: .subtitle, reuseIdentifier: "noteCell")
  135. noteCell.detailTextLabel?.text = NSLocalizedString("_share_reshare_restricted_", comment: "")
  136. noteCell.detailTextLabel?.isEnabled = false
  137. noteCell.isUserInteractionEnabled = false
  138. noteCell.detailTextLabel?.numberOfLines = 0
  139. return noteCell
  140. }
  141. if let cell = cell as? NCShareDateCell { cell.onReload = tableView.reloadData }
  142. return cell
  143. }
  144. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  145. tableView.deselectRow(at: indexPath, animated: true)
  146. guard let cellConfig = shareConfig.config(for: indexPath) else { return }
  147. guard let cellConfig = cellConfig as? NCShareDetails else {
  148. cellConfig.didSelect(for: share)
  149. tableView.reloadData()
  150. return
  151. }
  152. switch cellConfig {
  153. case .hideDownload:
  154. share.hideDownload.toggle()
  155. tableView.reloadData()
  156. case .expirationDate:
  157. let cell = tableView.cellForRow(at: indexPath) as? NCShareDateCell
  158. cell?.textField.becomeFirstResponder()
  159. cell?.checkMaximumDate(account: metadata.account)
  160. case .password:
  161. guard share.password.isEmpty else {
  162. share.password = ""
  163. tableView.reloadData()
  164. return
  165. }
  166. let alertController = UIAlertController.password(titleKey: "_share_password_") { password in
  167. self.share.password = password ?? ""
  168. tableView.reloadData()
  169. }
  170. self.present(alertController, animated: true)
  171. case .note:
  172. let storyboard = UIStoryboard(name: "NCShare", bundle: nil)
  173. guard let viewNewUserComment = storyboard.instantiateViewController(withIdentifier: "NCShareNewUserAddComment") as? NCShareNewUserAddComment else { return }
  174. viewNewUserComment.metadata = self.metadata
  175. viewNewUserComment.share = self.share
  176. viewNewUserComment.onDismiss = tableView.reloadData
  177. self.navigationController?.pushViewController(viewNewUserComment, animated: true)
  178. case .label:
  179. let alertController = UIAlertController.withTextField(titleKey: "_share_link_name_") { textField in
  180. textField.placeholder = cellConfig.title
  181. textField.text = self.share.label
  182. } completion: { newValue in
  183. self.share.label = newValue ?? ""
  184. self.setNavigationTitle()
  185. tableView.reloadData()
  186. }
  187. self.present(alertController, animated: true)
  188. }
  189. }
  190. }