NCShareCells.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. func hasResharePermission(for parentPermission: Int) -> Bool
  45. }
  46. enum NCUserPermission: CaseIterable, NCPermission {
  47. func hasResharePermission(for parentPermission: Int) -> Bool {
  48. return ((permissionBitFlag & parentPermission) != 0)
  49. }
  50. var permissionBitFlag: Int {
  51. switch self {
  52. case .reshare: return NCGlobal.shared.permissionShareShare
  53. case .edit: return NCGlobal.shared.permissionUpdateShare
  54. case .create: return NCGlobal.shared.permissionCreateShare
  55. case .delete: return NCGlobal.shared.permissionDeleteShare
  56. }
  57. }
  58. func didChange(_ share: NCTableShareable, to newValue: Bool) {
  59. share.permissions ^= permissionBitFlag
  60. }
  61. func isOn(for share: NCTableShareable) -> Bool {
  62. return (share.permissions & permissionBitFlag) != 0
  63. }
  64. case reshare, edit, create, delete
  65. static let forDirectory: [NCUserPermission] = NCUserPermission.allCases
  66. static let forFile: [NCUserPermission] = [.reshare, .edit]
  67. var title: String {
  68. switch self {
  69. case .reshare: return NSLocalizedString("_share_can_reshare_", comment: "")
  70. case .edit: return NSLocalizedString("_share_can_change_", comment: "")
  71. case .create: return NSLocalizedString("_share_can_create_", comment: "")
  72. case .delete: return NSLocalizedString("_share_can_delete_", comment: "")
  73. }
  74. }
  75. }
  76. enum NCLinkPermission: NCPermission {
  77. func didChange(_ share: NCTableShareable, to newValue: Bool) {
  78. guard self != .allowEdit || newValue else {
  79. share.permissions = NCGlobal.shared.permissionReadShare
  80. return
  81. }
  82. share.permissions = permissionValue
  83. }
  84. func hasResharePermission(for parentPermission: Int) -> Bool {
  85. permissionValue & parentPermission == permissionValue
  86. }
  87. var permissionValue: Int {
  88. switch self {
  89. case .allowEdit:
  90. return CCUtility.getPermissionsValue(
  91. byCanEdit: true,
  92. andCanCreate: true,
  93. andCanChange: true,
  94. andCanDelete: true,
  95. andCanShare: false,
  96. andIsFolder: false)
  97. case .viewOnly:
  98. return CCUtility.getPermissionsValue(
  99. byCanEdit: false,
  100. andCanCreate: false,
  101. andCanChange: false,
  102. andCanDelete: false,
  103. // not possible to create "read-only" shares without reshare option
  104. // https://github.com/nextcloud/server/blame/f99876997a9119518fe5f7ad3a3a51d33459d4cc/apps/files_sharing/lib/Controller/ShareAPIController.php#L1104-L1107
  105. andCanShare: true,
  106. andIsFolder: true)
  107. case .uploadEdit:
  108. return CCUtility.getPermissionsValue(
  109. byCanEdit: true,
  110. andCanCreate: true,
  111. andCanChange: true,
  112. andCanDelete: true,
  113. andCanShare: false,
  114. andIsFolder: true)
  115. case .fileDrop:
  116. return NCGlobal.shared.permissionCreateShare
  117. }
  118. }
  119. func isOn(for share: NCTableShareable) -> Bool {
  120. switch self {
  121. case .allowEdit: return CCUtility.isAnyPermission(toEdit: share.permissions)
  122. case .viewOnly: return !CCUtility.isAnyPermission(toEdit: share.permissions) && share.permissions != NCGlobal.shared.permissionCreateShare
  123. case .uploadEdit: return CCUtility.isAnyPermission(toEdit: share.permissions) && share.permissions != NCGlobal.shared.permissionCreateShare
  124. case .fileDrop: return share.permissions == NCGlobal.shared.permissionCreateShare
  125. }
  126. }
  127. var title: String {
  128. switch self {
  129. case .allowEdit: return NSLocalizedString("_share_can_change_", comment: "")
  130. case .viewOnly: return NSLocalizedString("_share_read_only_", comment: "")
  131. case .uploadEdit: return NSLocalizedString("_share_allow_upload_", comment: "")
  132. case .fileDrop: return NSLocalizedString("_share_file_drop_", comment: "")
  133. }
  134. }
  135. case allowEdit, viewOnly, uploadEdit, fileDrop
  136. static let forDirectory: [NCLinkPermission] = [.viewOnly, .uploadEdit, .fileDrop]
  137. static let forFile: [NCLinkPermission] = [.allowEdit]
  138. }
  139. enum NCShareDetails: CaseIterable, NCShareCellConfig {
  140. func didSelect(for share: NCTableShareable) {
  141. switch self {
  142. case .hideDownload: share.hideDownload.toggle()
  143. case .expirationDate: return
  144. case .password: return
  145. case .note: return
  146. case .label: return
  147. }
  148. }
  149. func getCell(for share: NCTableShareable) -> UITableViewCell {
  150. switch self {
  151. case .hideDownload:
  152. return NCShareToggleCell(isOn: share.hideDownload)
  153. case .expirationDate:
  154. return NCShareDateCell(share: share)
  155. case .password: return NCShareToggleCell(isOn: !share.password.isEmpty, customIcons: ("lock", "lock_open"))
  156. case .note:
  157. let cell = UITableViewCell(style: .value1, reuseIdentifier: "shareNote")
  158. cell.detailTextLabel?.text = share.note
  159. cell.accessoryType = .disclosureIndicator
  160. return cell
  161. case .label:
  162. let cell = UITableViewCell(style: .value1, reuseIdentifier: "shareLabel")
  163. cell.detailTextLabel?.text = share.label
  164. return cell
  165. }
  166. }
  167. var title: String {
  168. switch self {
  169. case .hideDownload: return NSLocalizedString("_share_hide_download_", comment: "")
  170. case .expirationDate: return NSLocalizedString("_share_expiration_date_", comment: "")
  171. case .password: return NSLocalizedString("_share_password_protect_", comment: "")
  172. case .note: return NSLocalizedString("_share_note_recipient_", comment: "")
  173. case .label: return NSLocalizedString("_share_link_name_", comment: "")
  174. }
  175. }
  176. case label, hideDownload, expirationDate, password, note
  177. static let forLink: [NCShareDetails] = NCShareDetails.allCases
  178. static let forUser: [NCShareDetails] = [.expirationDate, .note]
  179. }
  180. struct NCShareConfig {
  181. let permissions: [NCPermission]
  182. let advanced: [NCShareDetails]
  183. let share: NCTableShareable
  184. let resharePermission: Int
  185. init(parentMetadata: tableMetadata, share: NCTableShareable) {
  186. self.share = share
  187. self.resharePermission = parentMetadata.sharePermissionsCollaborationServices
  188. let type: NCPermission.Type = share.shareType == NCShareCommon.shared.SHARE_TYPE_LINK ? NCLinkPermission.self : NCUserPermission.self
  189. self.permissions = parentMetadata.directory ? type.forDirectory : type.forFile
  190. self.advanced = share.shareType == NCShareCommon.shared.SHARE_TYPE_LINK ? NCShareDetails.forLink : NCShareDetails.forUser
  191. }
  192. func cellFor(indexPath: IndexPath) -> UITableViewCell? {
  193. let cellConfig = config(for: indexPath)
  194. let cell = cellConfig?.getCell(for: share)
  195. cell?.textLabel?.text = cellConfig?.title
  196. if let cellConfig = cellConfig as? NCPermission, !cellConfig.hasResharePermission(for: resharePermission) {
  197. cell?.isUserInteractionEnabled = false
  198. cell?.textLabel?.isEnabled = false
  199. }
  200. return cell
  201. }
  202. func didSelectRow(at indexPath: IndexPath) {
  203. let cellConfig = config(for: indexPath)
  204. cellConfig?.didSelect(for: share)
  205. }
  206. func config(for indexPath: IndexPath) -> NCShareCellConfig? {
  207. if indexPath.section == 0, indexPath.row < permissions.count {
  208. return permissions[indexPath.row]
  209. } else if indexPath.section == 1, indexPath.row < advanced.count {
  210. return advanced[indexPath.row]
  211. } else { return nil }
  212. }
  213. }
  214. class NCShareToggleCell: UITableViewCell {
  215. typealias CustomToggleIcon = (onIconName: String?, offIconName: String?)
  216. init(isOn: Bool, customIcons: CustomToggleIcon? = nil) {
  217. super.init(style: .default, reuseIdentifier: "toggleCell")
  218. self.accessibilityValue = isOn ? NSLocalizedString("_on_", comment: "") : NSLocalizedString("_off_", comment: "")
  219. guard let customIcons = customIcons,
  220. let iconName = isOn ? customIcons.onIconName : customIcons.offIconName else {
  221. self.accessoryType = isOn ? .checkmark : .none
  222. return
  223. }
  224. let image = NCUtility.shared.loadImage(named: iconName, color: NCBrandColor.shared.brandElement, size: self.frame.height - 26)
  225. self.accessoryView = UIImageView(image: image)
  226. }
  227. required init?(coder: NSCoder) {
  228. fatalError("init(coder:) has not been implemented")
  229. }
  230. }
  231. class NCShareDateCell: UITableViewCell {
  232. let picker = UIDatePicker()
  233. let textField = UITextField()
  234. var onReload: (() -> Void)?
  235. init(share: NCTableShareable) {
  236. super.init(style: .value1, reuseIdentifier: "shareExpDate")
  237. picker.datePickerMode = .date
  238. picker.minimumDate = Date()
  239. if #available(iOS 13.4, *) {
  240. picker.preferredDatePickerStyle = .wheels
  241. }
  242. picker.action(for: .valueChanged) { datePicker in
  243. guard let datePicker = datePicker as? UIDatePicker else { return }
  244. self.detailTextLabel?.text = DateFormatter.shareExpDate.string(from: datePicker.date)
  245. }
  246. accessoryView = textField
  247. let toolbar = UIToolbar.toolbar {
  248. self.resignFirstResponder()
  249. share.expirationDate = nil
  250. self.onReload?()
  251. } completion: {
  252. self.resignFirstResponder()
  253. share.expirationDate = self.picker.date as NSDate
  254. self.onReload?()
  255. }
  256. textField.isAccessibilityElement = false
  257. textField.accessibilityElementsHidden = true
  258. textField.inputAccessoryView = toolbar.wrappedSafeAreaContainer
  259. textField.inputView = picker
  260. if let expDate = share.expirationDate {
  261. detailTextLabel?.text = DateFormatter.shareExpDate.string(from: expDate as Date)
  262. }
  263. }
  264. required public init?(coder: NSCoder) {
  265. fatalError("init(coder:) has not been implemented")
  266. }
  267. }