NCShareAdvancePermission.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 NCCommunication
  25. import SVGKit
  26. import CloudKit
  27. class NCShareAdvancePermission: UITableViewController, NCShareAdvanceFotterDelegate, NCShareDetail {
  28. func dismissShareAdvanceView(shouldSave: Bool) {
  29. defer { navigationController?.popViewController(animated: true) }
  30. guard shouldSave else { return }
  31. if isNewShare {
  32. networking?.createShare(option: share)
  33. } else {
  34. networking?.updateShare(option: share)
  35. }
  36. }
  37. var share: NCTableShareable!
  38. var isNewShare: Bool { NCManageDatabase.shared.getTableShare(account: share.account, idShare: share.idShare) == nil }
  39. var metadata: tableMetadata!
  40. var shareConfig: NCShareConfig!
  41. var networking: NCShareNetworking?
  42. override func viewDidLoad() {
  43. super.viewDidLoad()
  44. self.shareConfig = NCShareConfig(parentMetadata: metadata, share: share)
  45. self.setNavigationTitle()
  46. if #available(iOS 13.0, *) {
  47. // disbale pull to dimiss
  48. isModalInPresentation = true
  49. }
  50. }
  51. override func viewWillLayoutSubviews() {
  52. super.viewWillLayoutSubviews()
  53. guard tableView.tableHeaderView == nil, tableView.tableFooterView == nil else { return }
  54. setupHeaderView()
  55. setupFooterView()
  56. }
  57. func setupFooterView() {
  58. guard let footerView = (Bundle.main.loadNibNamed("NCShareAdvancePermissionFooter", owner: self, options: nil)?.first as? NCShareAdvancePermissionFooter) else { return }
  59. footerView.setupUI(delegate: self)
  60. // tableFooterView can't use auto layout directly
  61. let container = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 120))
  62. container.addSubview(footerView)
  63. tableView.tableFooterView = container
  64. footerView.translatesAutoresizingMaskIntoConstraints = false
  65. footerView.bottomAnchor.constraint(equalTo: container.bottomAnchor).isActive = true
  66. footerView.heightAnchor.constraint(equalTo: container.heightAnchor).isActive = true
  67. footerView.widthAnchor.constraint(equalTo: container.widthAnchor).isActive = true
  68. }
  69. func setupHeaderView() {
  70. guard let headerView = (Bundle.main.loadNibNamed("NCShareAdvancePermissionHeader", owner: self, options: nil)?.first as? NCShareAdvancePermissionHeader) else { return }
  71. headerView.setupUI(with: metadata)
  72. headerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 200)
  73. tableView.tableHeaderView = headerView
  74. headerView.translatesAutoresizingMaskIntoConstraints = false
  75. headerView.heightAnchor.constraint(equalToConstant: 200).isActive = true
  76. headerView.widthAnchor.constraint(equalTo: view.safeAreaLayoutGuide.widthAnchor).isActive = true
  77. }
  78. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  79. if section == 0 {
  80. return NSLocalizedString("_advanced_", comment: "")
  81. } else if section == 1 {
  82. return NSLocalizedString("_misc_", comment: "")
  83. } else { return nil }
  84. }
  85. override func numberOfSections(in tableView: UITableView) -> Int {
  86. return 2
  87. }
  88. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  89. if section == 0 {
  90. // check reshare permission, if restricted add note
  91. let maxPermission = metadata.directory ? NCGlobal.shared.permissionMaxFolderShare : NCGlobal.shared.permissionMaxFileShare
  92. return shareConfig.resharePermission != maxPermission ? shareConfig.permissions.count + 1 : shareConfig.permissions.count
  93. } else if section == 1 {
  94. return shareConfig.advanced.count
  95. } else { return 0 }
  96. }
  97. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  98. guard let cell = shareConfig.cellFor(indexPath: indexPath) else {
  99. let noteCell = UITableViewCell(style: .subtitle, reuseIdentifier: "noteCell")
  100. noteCell.detailTextLabel?.text = NSLocalizedString("_share_reshare_restricted_", comment: "")
  101. noteCell.detailTextLabel?.isEnabled = false
  102. noteCell.isUserInteractionEnabled = false
  103. return noteCell
  104. }
  105. if let cell = cell as? NCShareDateCell { cell.onReload = tableView.reloadData }
  106. return cell
  107. }
  108. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  109. tableView.deselectRow(at: indexPath, animated: true)
  110. guard let cellConfig = shareConfig.config(for: indexPath) else { return }
  111. guard let cellConfig = cellConfig as? NCShareDetails else {
  112. cellConfig.didSelect(for: share)
  113. tableView.reloadData()
  114. return
  115. }
  116. switch cellConfig {
  117. case .hideDownload:
  118. share.hideDownload.toggle()
  119. tableView.reloadData()
  120. case .expirationDate:
  121. let cell = tableView.cellForRow(at: indexPath) as? NCShareDateCell
  122. cell?.textField.becomeFirstResponder()
  123. case .password:
  124. guard share.password.isEmpty else {
  125. share.password = ""
  126. tableView.reloadData()
  127. return
  128. }
  129. let alertController = UIAlertController.password(titleKey: "_share_password_") { password in
  130. self.share.password = password ?? ""
  131. tableView.reloadData()
  132. }
  133. self.present(alertController, animated: true)
  134. case .note:
  135. let storyboard = UIStoryboard(name: "NCShare", bundle: nil)
  136. guard let viewNewUserComment = storyboard.instantiateViewController(withIdentifier: "NCShareNewUserAddComment") as? NCShareNewUserAddComment else { return }
  137. viewNewUserComment.metadata = self.metadata
  138. viewNewUserComment.share = self.share
  139. viewNewUserComment.onDismiss = tableView.reloadData
  140. self.navigationController?.pushViewController(viewNewUserComment, animated: true)
  141. case .label:
  142. let alertController = UIAlertController.withTextField(titleKey: "_share_link_name_") { textField in
  143. textField.placeholder = cellConfig.title
  144. textField.text = self.share.label
  145. } completion: { newValue in
  146. self.share.label = newValue ?? ""
  147. self.setNavigationTitle()
  148. tableView.reloadData()
  149. }
  150. self.present(alertController, animated: true)
  151. }
  152. }
  153. }