-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathNCShareCells.swift
More file actions
282 lines (244 loc) · 10.9 KB
/
NCShareCells.swift
File metadata and controls
282 lines (244 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//
// NCShareCells.swift
// Nextcloud
//
// Created by Henrik Storch on 18.03.22.
// Copyright © 2022 Henrik Storch. All rights reserved.
//
// Author Henrik Storch <henrik.storch@nextcloud.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
import UIKit
import OSLog
import NextcloudKit
protocol NCShareCellConfig {
var title: String { get }
func getCell(for share: Shareable) -> UITableViewCell
func didSelect(for share: Shareable)
}
protocol NCToggleCellConfig: NCShareCellConfig {
func isOn(for share: Shareable) -> Bool
func didChange(_ share: Shareable, to newValue: Bool)
}
extension NCToggleCellConfig {
func getCell(for share: Shareable) -> UITableViewCell {
return NCShareToggleCell(isOn: isOn(for: share))
}
func didSelect(for share: Shareable) {
didChange(share, to: !isOn(for: share))
}
}
protocol NCPermission: NCToggleCellConfig {
static var forDirectory: [Self] { get }
static var forFile: [Self] { get }
static func forDirectoryE2EE(account: String) -> [NCPermission]
func hasPermission(for parentPermission: Int) -> Bool
func hasReadPermission() -> Bool
}
enum NCUserPermission: CaseIterable, NCPermission {
func hasPermission(for parentPermission: Int) -> Bool {
return ((permissionBitFlag & parentPermission) != 0)
}
func hasReadPermission() -> Bool {
return self == .read
}
var permissionBitFlag: Int {
return switch self {
case .read: NKShare.Permission.read.rawValue
case .reshare: NKShare.Permission.share.rawValue
case .edit: NKShare.Permission.update.rawValue
case .create: NKShare.Permission.create.rawValue
case .delete: NKShare.Permission.delete.rawValue
}
}
func didChange(_ share: Shareable, to newValue: Bool) {
share.permissions ^= permissionBitFlag
}
func isOn(for share: Shareable) -> Bool {
return (share.permissions & permissionBitFlag) != 0
}
static func forDirectoryE2EE(account: String) -> [NCPermission] {
let capabilities = NCNetworking.shared.capabilities[account] ?? NKCapabilities.Capabilities()
if capabilities.e2EEApiVersion == NCGlobal.shared.e2eeVersionV20 {
return NCUserPermission.allCases
}
return []
}
case read, reshare, edit, create, delete
static let forDirectory: [NCUserPermission] = NCUserPermission.allCases
static let forFile: [NCUserPermission] = [.read, .reshare, .edit]
var title: String {
switch self {
case .read: return NSLocalizedString("_share_can_read_", comment: "")
case .reshare: return NSLocalizedString("_share_can_reshare_", comment: "")
case .edit: return NSLocalizedString("_share_can_change_", comment: "")
case .create: return NSLocalizedString("_share_can_create_", comment: "")
case .delete: return NSLocalizedString("_share_can_delete_", comment: "")
}
}
}
enum NCLinkEmailPermission: CaseIterable, NCPermission {
static func forDirectoryE2EE(account: String) -> [any NCPermission] {
let capabilities = NCNetworking.shared.capabilities[account] ?? NKCapabilities.Capabilities()
if capabilities.e2EEApiVersion == NCGlobal.shared.e2eeVersionV20 {
return NCUserPermission.allCases
}
return []
}
func hasReadPermission() -> Bool {
return self == .read
}
func hasPermission(for parentPermission: Int) -> Bool {
return ((permissionBitFlag & parentPermission) != 0)
}
var permissionBitFlag: Int {
return switch self {
case .read: NKShare.Permission.read.rawValue
case .edit: NKShare.Permission.update.rawValue
case .create: NKShare.Permission.create.rawValue
case .delete: NKShare.Permission.delete.rawValue
}
}
func didChange(_ share: Shareable, to newValue: Bool) {
share.permissions ^= permissionBitFlag
}
func isOn(for share: Shareable) -> Bool {
return (share.permissions & permissionBitFlag) != 0
}
var title: String {
switch self {
case .read: return NSLocalizedString("_share_can_read_", comment: "")
case .edit: return NSLocalizedString("_share_can_change_", comment: "")
case .create: return NSLocalizedString("_share_can_create_", comment: "")
case .delete: return NSLocalizedString("_share_can_delete_", comment: "")
}
}
case edit, read, create, delete
static let forDirectory: [NCLinkEmailPermission] = NCLinkEmailPermission.allCases
static let forFile: [NCLinkEmailPermission] = [.read, .edit]
}
///
/// Individual aspects of share.
///
enum NCAdvancedPermission: CaseIterable, NCShareCellConfig {
func didSelect(for share: Shareable) {
switch self {
case .hideDownload: share.hideDownload.toggle()
case .limitDownload: return
case .expirationDate: return
case .password: return
case .note: return
case .label: return
case .downloadAndSync: return
}
}
func getCell(for share: Shareable) -> UITableViewCell {
switch self {
case .hideDownload:
return NCShareToggleCell(isOn: share.hideDownload)
case .limitDownload:
let cell = UITableViewCell(style: .value1, reuseIdentifier: "downloadLimit")
cell.accessibilityIdentifier = "downloadLimit"
cell.accessoryType = .disclosureIndicator
return cell
case .expirationDate:
return NCShareDateCell(share: share)
case .password: return NCShareToggleCell(isOn: !share.password.isEmpty, customIcons: ("lock", "lock_open"))
case .note:
let cell = UITableViewCell(style: .value1, reuseIdentifier: "shareNote")
cell.detailTextLabel?.text = share.note
cell.accessoryType = .disclosureIndicator
return cell
case .label:
let cell = UITableViewCell(style: .value1, reuseIdentifier: "shareLabel")
cell.detailTextLabel?.text = share.label
return cell
case .downloadAndSync:
return NCShareToggleCell(isOn: share.downloadAndSync)
}
}
var title: String {
switch self {
case .hideDownload: return NSLocalizedString("_share_hide_download_", comment: "")
case .limitDownload: return NSLocalizedString("_share_limit_download_", comment: "")
case .expirationDate: return NSLocalizedString("_share_expiration_date_", comment: "")
case .password: return NSLocalizedString("_share_password_protect_", comment: "")
case .note: return NSLocalizedString("_share_note_recipient_", comment: "")
case .label: return NSLocalizedString("_share_link_name_", comment: "")
case .downloadAndSync: return NSLocalizedString("_share_can_download_", comment: "")
}
}
case label, hideDownload, limitDownload, expirationDate, password, note, downloadAndSync
static let forLink: [NCAdvancedPermission] = [.expirationDate, .hideDownload, .label, .limitDownload, .note, .password]
static let forUser: [NCAdvancedPermission] = [.expirationDate, .note, .downloadAndSync]
}
struct NCShareConfig {
let permissions: [NCPermission]
let advanced: [NCAdvancedPermission]
let shareable: Shareable
let sharePermission: Int
let isDirectory: Bool
/// There are many share types, but we only classify them as a link share (link type, email type) and a user share (every other share type).
init(parentMetadata: tableMetadata, share: Shareable) {
self.shareable = share
self.sharePermission = parentMetadata.sharePermissionsCollaborationServices
self.isDirectory = parentMetadata.directory
let type: NCPermission.Type = (share.shareType == NKShare.ShareType.publicLink.rawValue || share.shareType == NKShare.ShareType.email.rawValue) ? NCLinkEmailPermission.self : NCUserPermission.self
self.permissions = parentMetadata.directory ? (parentMetadata.e2eEncrypted ? type.forDirectoryE2EE(account: parentMetadata.account) : type.forDirectory) : type.forFile
if share.shareType == NKShare.ShareType.publicLink.rawValue {
let capabilities = NCNetworking.shared.capabilities[parentMetadata.account] ?? NKCapabilities.Capabilities()
let hasDownloadLimitCapability = capabilities.fileSharingDownloadLimit
if parentMetadata.isDirectory || hasDownloadLimitCapability == false {
self.advanced = NCAdvancedPermission.forLink.filter { $0 != .limitDownload }
} else {
self.advanced = NCAdvancedPermission.forLink
}
} else {
self.advanced = NCAdvancedPermission.forUser
}
}
func cellFor(indexPath: IndexPath) -> UITableViewCell? {
let cellConfig = config(for: indexPath)
let cell = cellConfig?.getCell(for: shareable)
cell?.textLabel?.text = cellConfig?.title
Logger().info("\(cellConfig?.title ?? "")")
if let cellConfig = cellConfig as? NCPermission, !cellConfig.hasPermission(for: sharePermission) {
cell?.isUserInteractionEnabled = false
cell?.textLabel?.isEnabled = false
}
// For user permissions: Read permission is always enabled and we show it as a non-interactable permission for brevity.
if let cellConfig = cellConfig as? NCUserPermission, cellConfig.hasReadPermission() {
cell?.isUserInteractionEnabled = false
cell?.textLabel?.isEnabled = false
}
// For link permissions: Read permission is always enabled and we show it as a non-interactable permission in files only for brevity.
if let cellConfig = cellConfig as? NCLinkEmailPermission, cellConfig.hasReadPermission(), !isDirectory {
cell?.isUserInteractionEnabled = false
cell?.textLabel?.isEnabled = false
}
return cell
}
func didSelectRow(at indexPath: IndexPath) {
let cellConfig = config(for: indexPath)
cellConfig?.didSelect(for: shareable)
}
func config(for indexPath: IndexPath) -> NCShareCellConfig? {
if indexPath.section == 0, indexPath.row < permissions.count {
return permissions[indexPath.row]
} else if indexPath.section == 1, indexPath.row < advanced.count {
return advanced[indexPath.row]
} else { return nil }
}
}