NCShareAdvancePermission.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. //
  2. // NCShareAdvancePermission.swift
  3. // Nextcloud
  4. //
  5. // Created by T-systems on 09/08/21.
  6. // Copyright © 2021 Marino Faggiana. All rights reserved.
  7. //
  8. import UIKit
  9. import NCCommunication
  10. import SVGKit
  11. import CloudKit
  12. class NCShareAdvancePermission: UITableViewController {
  13. var share: tableShare!
  14. var metadata: tableMetadata!
  15. var shareConfig: ShareConfig!
  16. override func viewDidLoad() {
  17. super.viewDidLoad()
  18. self.shareConfig = ShareConfig(isDirectory: metadata.directory, share: share)
  19. }
  20. override func viewWillLayoutSubviews() {
  21. super.viewWillLayoutSubviews()
  22. guard tableView.tableHeaderView == nil, tableView.tableFooterView == nil else { return }
  23. setupHeaderView()
  24. setupFooterView()
  25. }
  26. @objc func cancelClicked() {
  27. navigationController?.popViewController(animated: true)
  28. }
  29. @objc func nextClicked() {
  30. }
  31. func setupFooterView() {
  32. guard let footerView = (Bundle.main.loadNibNamed("NCShareAdvancePermissionFooter", owner: self, options: nil)?.first as? NCShareAdvancePermissionFooter) else { return }
  33. footerView.backgroundColor = .clear
  34. footerView.addShadow(location: .top)
  35. footerView.buttonCancel.addTarget(self, action: #selector(cancelClicked), for: .touchUpInside)
  36. footerView.buttonCancel.setTitle(NSLocalizedString("_cancel_", comment: ""), for: .normal)
  37. footerView.buttonCancel.layer.cornerRadius = 10
  38. footerView.buttonCancel.layer.masksToBounds = true
  39. footerView.buttonCancel.layer.borderWidth = 1
  40. if NCManageDatabase.shared.getTableShare(account: share.account, idShare: share.idShare) == nil {
  41. footerView.buttonNext.setTitle(NSLocalizedString("_next_", comment: ""), for: .normal)
  42. } else {
  43. footerView.buttonNext.setTitle(NSLocalizedString("_apply_changes_", comment: ""), for: .normal)
  44. }
  45. footerView.buttonNext.layer.cornerRadius = 10
  46. footerView.buttonNext.layer.masksToBounds = true
  47. footerView.buttonNext.backgroundColor = NCBrandColor.shared.brand
  48. footerView.buttonNext.addTarget(self, action: #selector(nextClicked), for: .touchUpInside)
  49. footerView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 100)
  50. tableView.tableFooterView = footerView
  51. tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 100, right: 0)
  52. footerView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
  53. footerView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor).isActive = true
  54. footerView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor).isActive = true
  55. footerView.heightAnchor.constraint(equalToConstant: 100).isActive = true
  56. }
  57. func setupHeaderView() {
  58. guard let headerView = (Bundle.main.loadNibNamed("NCShareAdvancePermissionHeader", owner: self, options: nil)?.first as? NCShareAdvancePermissionHeader) else { return }
  59. // headerView.backgroundColor = NCBrandColor.shared.secondarySystemBackground
  60. if FileManager.default.fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  61. headerView.fullWidthImageView.image = NCUtility.shared.getImageMetadata(metadata, for: headerView.frame.height)
  62. headerView.fullWidthImageView.contentMode = .scaleAspectFill
  63. headerView.imageView.isHidden = true
  64. } else {
  65. if metadata!.directory {
  66. headerView.imageView.image = UIImage(named: "folder")
  67. } else if !metadata.iconName.isEmpty {
  68. headerView.imageView.image = UIImage(named: metadata.iconName)
  69. } else {
  70. headerView.imageView.image = UIImage(named: "file")
  71. }
  72. }
  73. headerView.favorite.setNeedsUpdateConstraints()
  74. headerView.favorite.layoutIfNeeded()
  75. headerView.fileName.text = self.metadata?.fileNameView
  76. headerView.fileName.textColor = NCBrandColor.shared.label
  77. // headerView.favorite.addTarget(self, action: #selector(favoriteClicked), for: .touchUpInside)
  78. if metadata.favorite {
  79. headerView.favorite.setImage(NCUtility.shared.loadImage(named: "star.fill", color: NCBrandColor.shared.yellowFavorite, size: 24), for: .normal)
  80. } else {
  81. headerView.favorite.setImage(NCUtility.shared.loadImage(named: "star.fill", color: NCBrandColor.shared.systemGray, size: 24), for: .normal)
  82. }
  83. headerView.info.textColor = NCBrandColor.shared.secondaryLabel
  84. headerView.info.text = CCUtility.transformedSize(metadata.size) + ", " + CCUtility.dateDiff(metadata.date as Date)
  85. headerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 200)
  86. tableView.tableHeaderView = headerView
  87. headerView.translatesAutoresizingMaskIntoConstraints = false
  88. headerView.heightAnchor.constraint(equalToConstant: 200).isActive = true
  89. headerView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
  90. }
  91. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  92. if section == 0 { return NSLocalizedString("_advanced_", comment: "") }
  93. else if section == 1 { return NSLocalizedString("_misc_", comment: "") }
  94. else { return nil }
  95. }
  96. override func numberOfSections(in tableView: UITableView) -> Int {
  97. return 2
  98. }
  99. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  100. if section == 0 { return shareConfig.permissions.count }
  101. else if section == 1 { return shareConfig.advanced.count }
  102. else { return 0 }
  103. }
  104. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  105. guard let cell = shareConfig.cellFor(indexPath: indexPath) else { return UITableViewCell() }
  106. return cell
  107. }
  108. }
  109. protocol ShareCellConfig {
  110. var title: String { get }
  111. func getCell(for share: tableShare) -> UITableViewCell
  112. }
  113. protocol ToggleCellConfig: ShareCellConfig {
  114. func isOn(for share: tableShare) -> Bool
  115. func didChange(_ share: tableShare, to newValue: Bool)
  116. }
  117. extension ToggleCellConfig {
  118. func getCell(for share: tableShare) -> UITableViewCell {
  119. return ToggleCell(isOn: isOn(for: share))
  120. }
  121. }
  122. protocol Permission: ToggleCellConfig {
  123. static var forDirectory: [Self] { get }
  124. static var forFile: [Self] { get }
  125. }
  126. enum UserPermission: CaseIterable, Permission {
  127. func didChange(_ share: tableShare, to newValue: Bool) {
  128. }
  129. func isOn(for share: tableShare) -> Bool {
  130. switch self {
  131. case .reshare: return CCUtility.isPermission(toCanShare: share.permissions)
  132. case .edit: return CCUtility.isPermission(toCanChange: share.permissions)
  133. case .create: return CCUtility.isPermission(toCanCreate: share.permissions)
  134. case .delete: return CCUtility.isPermission(toCanDelete: share.permissions)
  135. }
  136. }
  137. func handleAction(for tableShare: tableShare) {
  138. switch self {
  139. case .reshare: break
  140. case .edit: break
  141. case .create: break
  142. case .delete: break
  143. }
  144. }
  145. case reshare, edit, create, delete
  146. static let forDirectory: [UserPermission] = UserPermission.allCases
  147. static let forFile: [UserPermission] = [.reshare, .edit]
  148. var title: String {
  149. switch self {
  150. case .reshare: return NSLocalizedString("_share_can_reshare_", comment: "")
  151. case .edit: return NSLocalizedString("_share_can_change_", comment: "")
  152. case .create: return NSLocalizedString("_share_can_create_", comment: "")
  153. case .delete: return NSLocalizedString("_share_can_delete_", comment: "")
  154. }
  155. }
  156. }
  157. enum LinkPermission: Permission {
  158. func didChange(_ share: tableShare, to newValue: Bool) {
  159. }
  160. func isOn(for share: tableShare) -> Bool {
  161. switch self {
  162. case .allowEdit: return CCUtility.isAnyPermission(toEdit: share.permissions)
  163. case .viewOnly: return !CCUtility.isAnyPermission(toEdit: share.permissions) && share.permissions != NCGlobal.shared.permissionCreateShare
  164. case .uploadEdit: return CCUtility.isAnyPermission(toEdit: share.permissions) && share.permissions != NCGlobal.shared.permissionCreateShare
  165. case .fileDrop: return share.permissions == NCGlobal.shared.permissionCreateShare
  166. }
  167. }
  168. var title: String {
  169. switch self {
  170. case .allowEdit: return NSLocalizedString("_share_can_change_", comment: "")
  171. case .viewOnly: return NSLocalizedString("_share_read_only_", comment: "")
  172. case .uploadEdit: return NSLocalizedString("_share_allow_upload_", comment: "")
  173. case .fileDrop: return NSLocalizedString("_share_file_drop_", comment: "")
  174. }
  175. }
  176. case allowEdit, viewOnly, uploadEdit, fileDrop
  177. static let forDirectory: [LinkPermission] = [.viewOnly, .uploadEdit, .fileDrop]
  178. static let forFile: [LinkPermission] = [.allowEdit]
  179. }
  180. enum Advanced: CaseIterable, ShareCellConfig {
  181. func getCell(for share: tableShare) -> UITableViewCell {
  182. switch self {
  183. case .hideDownload:
  184. return ToggleCell(isOn: share.hideDownload)
  185. case .expirationDate:
  186. let cell = UITableViewCell(style: .value1, reuseIdentifier: "shareExpDate")
  187. if let expDate = share.expirationDate {
  188. cell.detailTextLabel?.text = DateFormatter.shareExpDate.string(from: expDate as Date)
  189. }
  190. return cell
  191. case .password: return ToggleCell(isOn: !share.shareWith.isEmpty)
  192. case .note:
  193. let cell = UITableViewCell(style: .value1, reuseIdentifier: "shareNote")
  194. cell.detailTextLabel?.text = share.note
  195. cell.accessoryType = .disclosureIndicator
  196. return cell
  197. }
  198. }
  199. func didChange(_ share: tableShare, to newValue: Bool) {
  200. }
  201. var title: String {
  202. switch self {
  203. case .hideDownload: return NSLocalizedString("_share_hide_download_", comment: "")
  204. case .expirationDate: return NSLocalizedString("_share_expiration_date_", comment: "")
  205. case .password: return NSLocalizedString("_share_password_protect_", comment: "")
  206. case .note: return NSLocalizedString("_share_note_recipient_", comment: "")
  207. }
  208. }
  209. case hideDownload, expirationDate, password, note
  210. static let forLink: [Advanced] = Advanced.allCases
  211. static let forUser: [Advanced] = [.expirationDate, .note]
  212. }
  213. struct ShareConfig {
  214. let permissions: [Permission]
  215. let advanced: [Advanced]
  216. let share: tableShare
  217. init(isDirectory: Bool, share: tableShare) {
  218. self.share = share
  219. let type: Permission.Type = share.shareType == 3 ? LinkPermission.self : UserPermission.self
  220. self.permissions = isDirectory ? type.forDirectory : type.forFile
  221. self.advanced = share.shareType == 3 ? Advanced.forLink : Advanced.forUser
  222. }
  223. func cellFor(indexPath: IndexPath) -> UITableViewCell? {
  224. let cellConfig: ShareCellConfig
  225. if indexPath.section == 0, indexPath.row < permissions.count {
  226. cellConfig = permissions[indexPath.row]
  227. } else if indexPath.section == 1, indexPath.row < advanced.count {
  228. cellConfig = advanced[indexPath.row]
  229. } else { return nil }
  230. let cell = cellConfig.getCell(for: share)
  231. cell.textLabel?.text = cellConfig.title
  232. return cell
  233. }
  234. }
  235. class ToggleCell: UITableViewCell {
  236. init(isOn: Bool) {
  237. super.init(style: .default, reuseIdentifier: "toggleCell")
  238. if isOn {
  239. self.accessoryType = .checkmark
  240. } else {
  241. self.accessoryType = .none
  242. }
  243. }
  244. required init?(coder: NSCoder) {
  245. fatalError("init(coder:) has not been implemented")
  246. }
  247. }
  248. extension DateFormatter {
  249. static let shareExpDate: DateFormatter = {
  250. let dateFormatter = DateFormatter()
  251. dateFormatter.formatterBehavior = .behavior10_4
  252. dateFormatter.dateStyle = .medium
  253. return dateFormatter
  254. }()
  255. }