NCShareCells.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. //
  2. // NCShareCells.swift
  3. // Nextcloud
  4. //
  5. // Created by Henrik Storch on 18.03.22.
  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. protocol NCShareCellConfig {
  25. var title: String { get }
  26. func getCell(for share: NCTableShareable) -> UITableViewCell
  27. func didSelect(for share: NCTableShareable)
  28. }
  29. protocol NCToggleCellConfig: NCShareCellConfig {
  30. func isOn(for share: NCTableShareable) -> Bool
  31. func didChange(_ share: NCTableShareable, to newValue: Bool)
  32. }
  33. extension NCToggleCellConfig {
  34. func getCell(for share: NCTableShareable) -> UITableViewCell {
  35. return NCShareToggleCell(isOn: isOn(for: share))
  36. }
  37. func didSelect(for share: NCTableShareable) {
  38. didChange(share, to: !isOn(for: share))
  39. }
  40. }
  41. protocol NCPermission: NCToggleCellConfig {
  42. static var forDirectory: [Self] { get }
  43. static var forFile: [Self] { get }
  44. }
  45. enum NCUserPermission: CaseIterable, NCPermission {
  46. var permissionBitFlag: Int {
  47. switch self {
  48. case .reshare: return NCGlobal.shared.permissionShareShare
  49. case .edit: return NCGlobal.shared.permissionUpdateShare
  50. case .create: return NCGlobal.shared.permissionCreateShare
  51. case .delete: return NCGlobal.shared.permissionDeleteShare
  52. }
  53. }
  54. func didChange(_ share: NCTableShareable, to newValue: Bool) {
  55. share.permissions ^= permissionBitFlag
  56. }
  57. func isOn(for share: NCTableShareable) -> Bool {
  58. return (share.permissions & permissionBitFlag) != 0
  59. }
  60. case reshare, edit, create, delete
  61. static let forDirectory: [NCUserPermission] = NCUserPermission.allCases
  62. static let forFile: [NCUserPermission] = [.reshare, .edit]
  63. var title: String {
  64. switch self {
  65. case .reshare: return NSLocalizedString("_share_can_reshare_", comment: "")
  66. case .edit: return NSLocalizedString("_share_can_change_", comment: "")
  67. case .create: return NSLocalizedString("_share_can_create_", comment: "")
  68. case .delete: return NSLocalizedString("_share_can_delete_", comment: "")
  69. }
  70. }
  71. }
  72. enum NCLinkPermission: NCPermission {
  73. func didChange(_ share: NCTableShareable, to newValue: Bool) {
  74. guard self != .allowEdit else {
  75. // file
  76. share.permissions = CCUtility.getPermissionsValue(
  77. byCanEdit: newValue,
  78. andCanCreate: newValue,
  79. andCanChange: newValue,
  80. andCanDelete: newValue,
  81. andCanShare: false,
  82. andIsFolder: false)
  83. return
  84. }
  85. // can't deselect, only change
  86. guard newValue == true else { return }
  87. switch self {
  88. case .allowEdit: return
  89. case .viewOnly:
  90. share.permissions = CCUtility.getPermissionsValue(
  91. byCanEdit: false,
  92. andCanCreate: false,
  93. andCanChange: false,
  94. andCanDelete: false,
  95. andCanShare: false,
  96. andIsFolder: true)
  97. case .uploadEdit:
  98. share.permissions = CCUtility.getPermissionsValue(
  99. byCanEdit: true,
  100. andCanCreate: true,
  101. andCanChange: true,
  102. andCanDelete: true,
  103. andCanShare: false,
  104. andIsFolder: true)
  105. case .fileDrop:
  106. share.permissions = NCGlobal.shared.permissionCreateShare
  107. }
  108. }
  109. func isOn(for share: NCTableShareable) -> Bool {
  110. switch self {
  111. case .allowEdit: return CCUtility.isAnyPermission(toEdit: share.permissions)
  112. case .viewOnly: return !CCUtility.isAnyPermission(toEdit: share.permissions) && share.permissions != NCGlobal.shared.permissionCreateShare
  113. case .uploadEdit: return CCUtility.isAnyPermission(toEdit: share.permissions) && share.permissions != NCGlobal.shared.permissionCreateShare
  114. case .fileDrop: return share.permissions == NCGlobal.shared.permissionCreateShare
  115. }
  116. }
  117. var title: String {
  118. switch self {
  119. case .allowEdit: return NSLocalizedString("_share_can_change_", comment: "")
  120. case .viewOnly: return NSLocalizedString("_share_read_only_", comment: "")
  121. case .uploadEdit: return NSLocalizedString("_share_allow_upload_", comment: "")
  122. case .fileDrop: return NSLocalizedString("_share_file_drop_", comment: "")
  123. }
  124. }
  125. case allowEdit, viewOnly, uploadEdit, fileDrop
  126. static let forDirectory: [NCLinkPermission] = [.viewOnly, .uploadEdit, .fileDrop]
  127. static let forFile: [NCLinkPermission] = [.allowEdit]
  128. }
  129. enum NCShareDetails: CaseIterable, NCShareCellConfig {
  130. func didSelect(for share: NCTableShareable) {
  131. switch self {
  132. case .hideDownload: share.hideDownload.toggle()
  133. case .expirationDate: return
  134. case .password: return
  135. case .note: return
  136. case .label: return
  137. }
  138. }
  139. func getCell(for share: NCTableShareable) -> UITableViewCell {
  140. switch self {
  141. case .hideDownload:
  142. return NCShareToggleCell(isOn: share.hideDownload)
  143. case .expirationDate:
  144. return NCShareDateCell(share: share)
  145. case .password: return NCShareToggleCell(isOn: !share.password.isEmpty, customIcons: ("lock", "lock.open"))
  146. case .note:
  147. let cell = UITableViewCell(style: .value1, reuseIdentifier: "shareNote")
  148. cell.detailTextLabel?.text = share.note
  149. cell.accessoryType = .disclosureIndicator
  150. return cell
  151. case .label:
  152. let cell = UITableViewCell(style: .value1, reuseIdentifier: "shareLabel")
  153. cell.detailTextLabel?.text = share.label
  154. return cell
  155. }
  156. }
  157. var title: String {
  158. switch self {
  159. case .hideDownload: return NSLocalizedString("_share_hide_download_", comment: "")
  160. case .expirationDate: return NSLocalizedString("_share_expiration_date_", comment: "")
  161. case .password: return NSLocalizedString("_share_password_protect_", comment: "")
  162. case .note: return NSLocalizedString("_share_note_recipient_", comment: "")
  163. case .label: return NSLocalizedString("_share_link_name_", comment: "")
  164. }
  165. }
  166. case label, hideDownload, expirationDate, password, note
  167. static let forLink: [NCShareDetails] = NCShareDetails.allCases
  168. static let forUser: [NCShareDetails] = [.expirationDate, .note]
  169. }
  170. struct NCShareConfig {
  171. let permissions: [NCPermission]
  172. let advanced: [NCShareDetails]
  173. let share: NCTableShareable
  174. init(isDirectory: Bool, share: NCTableShareable) {
  175. self.share = share
  176. let type: NCPermission.Type = share.shareType == NCShareCommon.shared.SHARE_TYPE_LINK ? NCLinkPermission.self : NCUserPermission.self
  177. self.permissions = isDirectory ? type.forDirectory : type.forFile
  178. self.advanced = share.shareType == NCShareCommon.shared.SHARE_TYPE_LINK ? NCShareDetails.forLink : NCShareDetails.forUser
  179. }
  180. func cellFor(indexPath: IndexPath) -> UITableViewCell? {
  181. let cellConfig = config(for: indexPath)
  182. let cell = cellConfig?.getCell(for: share)
  183. cell?.textLabel?.text = cellConfig?.title
  184. return cell
  185. }
  186. func didSelectRow(at indexPath: IndexPath) {
  187. let cellConfig = config(for: indexPath)
  188. cellConfig?.didSelect(for: share)
  189. }
  190. func config(for indexPath: IndexPath) -> NCShareCellConfig? {
  191. if indexPath.section == 0, indexPath.row < permissions.count {
  192. return permissions[indexPath.row]
  193. } else if indexPath.section == 1, indexPath.row < advanced.count {
  194. return advanced[indexPath.row]
  195. } else { return nil }
  196. }
  197. }
  198. class NCShareToggleCell: UITableViewCell {
  199. typealias CustomToggleIcon = (onIconName: String?, offIconName: String?)
  200. init(isOn: Bool, customIcons: CustomToggleIcon? = nil) {
  201. super.init(style: .default, reuseIdentifier: "toggleCell")
  202. guard let customIcons = customIcons,
  203. let iconName = isOn ? customIcons.onIconName : customIcons.offIconName else {
  204. self.accessoryType = isOn ? .checkmark : .none
  205. return
  206. }
  207. let image = NCUtility.shared.loadImage(named: iconName, color: NCBrandColor.shared.brandElement)
  208. self.accessoryView = UIImageView(image: image)
  209. }
  210. required init?(coder: NSCoder) {
  211. fatalError("init(coder:) has not been implemented")
  212. }
  213. }
  214. open class NCShareDateCell: UITableViewCell {
  215. let picker = UIDatePicker()
  216. let textField = UITextField()
  217. var onReload: (() -> Void)?
  218. init(share: NCTableShareable) {
  219. super.init(style: .value1, reuseIdentifier: "shareExpDate")
  220. picker.datePickerMode = .date
  221. picker.minimumDate = Date()
  222. if #available(iOS 13.4, *) {
  223. picker.preferredDatePickerStyle = .wheels
  224. }
  225. picker.action(for: .valueChanged) { datePicker in
  226. guard let datePicker = datePicker as? UIDatePicker else { return }
  227. self.detailTextLabel?.text = DateFormatter.shareExpDate.string(from: datePicker.date)
  228. }
  229. accessoryView = textField
  230. let toolbar = UIToolbar.toolbar {
  231. self.resignFirstResponder()
  232. share.expirationDate = nil
  233. self.onReload?()
  234. } completion: {
  235. self.resignFirstResponder()
  236. share.expirationDate = self.picker.date as NSDate
  237. self.onReload?()
  238. }
  239. textField.inputAccessoryView = toolbar
  240. textField.inputView = picker
  241. if let expDate = share.expirationDate {
  242. detailTextLabel?.text = DateFormatter.shareExpDate.string(from: expDate as Date)
  243. }
  244. }
  245. required public init?(coder: NSCoder) {
  246. fatalError("init(coder:) has not been implemented")
  247. }
  248. }