Files
ios/Share/NCShareExtension+DataSource.swift

193 lines
9.0 KiB
Swift

// SPDX-FileCopyrightText: Nextcloud GmbH
// SPDX-FileCopyrightText: 2021 Marino Faggiana
// SPDX-FileCopyrightText: 2021 Henrik Storch
// SPDX-License-Identifier: GPL-3.0-or-later
import UIKit
import NextcloudKit
// MARK: - Collection View (target folder)
extension NCShareExtension: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
Task {
guard let tblAccount = NCShareExtensionData.shared.getTblAccoun(),
let metadata = self.dataSource.getMetadata(indexPath: indexPath) else {
return self.showAlert(description: "_invalid_url_")
}
let serverUrl = self.utilityFileSystem.createServerUrl(serverUrl: metadata.serverUrl, fileName: metadata.fileName)
if metadata.e2eEncrypted && !NCPreferences().isEndToEndEnabled(account: tblAccount.account) {
self.showAlert(title: "_info_", description: "_e2e_goto_settings_for_enable_")
return
}
let capabilities = await NKCapabilities.shared.getCapabilities(for: tblAccount.account)
if let fileNameError = FileNameValidator.checkFileName(metadata.fileNameView, account: tblAccount.account, capabilities: capabilities) {
let message = "\(fileNameError.errorDescription) \(NSLocalizedString("_please_rename_file_", comment: ""))"
await UIAlertController.warningAsync(message: message, presenter: self)
return
}
self.serverUrl = serverUrl
self.setNavigationBar(navigationTitle: metadata.fileNameView)
await self.reloadData()
await self.loadFolder()
}
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if kind == UICollectionView.elementKindSectionHeader {
let session = NCShareExtensionData.shared.getSession()
guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFirstHeaderEmptyData", for: indexPath) as? NCSectionFirstHeaderEmptyData else { return NCSectionFirstHeaderEmptyData() }
if self.dataSourceTask?.state == .running {
header.emptyImage.image = utility.loadImage(named: "wifi", colors: [NCBrandColor.shared.getElement(account: session.account)])
header.emptyTitle.text = NSLocalizedString("_request_in_progress_", comment: "")
header.emptyDescription.text = ""
} else {
header.emptyImage.image = NCImageCache.shared.getFolder(account: session.account)
header.emptyTitle.text = NSLocalizedString("_files_no_folders_", comment: "")
header.emptyDescription.text = ""
}
return header
} else {
return UICollectionReusableView()
}
}
}
extension NCShareExtension: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
var height: CGFloat = 0
if self.dataSource.isEmpty() {
height = NCUtility().getHeightHeaderEmptyData(view: view, portraitOffset: 0, landscapeOffset: -50)
}
return CGSize(width: collectionView.frame.width, height: height)
}
}
extension NCShareExtension: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return dataSource.numberOfSections()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.dataSource.numberOfItemsInSection(section)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = (collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as? NCListCell)!
guard let metadata = self.dataSource.getMetadata(indexPath: indexPath) else {
return cell
}
cell.metadata = metadata
cell.setBidiSafeFilename(metadata.fileNameView, isDirectory: metadata.directory, titleLabel: cell.labelTitle, extensionLabel: cell.labelExtension)
cell.labelTitle.textColor = NCBrandColor.shared.textColor
cell.labelExtension?.textColor = NCBrandColor.shared.textColor
cell.setButtonsHidden(true)
if metadata.directory {
setupDirectoryCell(cell, indexPath: indexPath, with: metadata)
}
if metadata.favorite {
cell.imageFavorite.image = NCImageCache.shared.getImageFavorite()
}
cell.writeInfoDateSize(date: metadata.date, size: metadata.size)
if metadata.isLivePhoto {
cell.imageStatus.image = utility.loadImage(named: "livephoto", colors: [NCBrandColor.shared.iconImageColor2])
}
cell.setTags(tags: Array(metadata.tags))
let accessibilityLabel = [metadata.fileNameView, cell.labelInfo.text, cell.labelSubinfo.text]
.compactMap { $0?.trimmingCharacters(in: .whitespacesAndNewlines) }
.filter { !$0.isEmpty }
.joined(separator: ", ")
cell.setAccessibility(label: accessibilityLabel, value: "")
cell.accessibilityTraits = .button
cell.separator.isHidden = collectionView.numberOfItems(inSection: indexPath.section) == indexPath.row + 1
return cell
}
func setupDirectoryCell(_ cell: NCListCell, indexPath: IndexPath, with metadata: tableMetadata) {
var isShare = false
var isMounted = false
let session = NCShareExtensionData.shared.getSession()
if let metadataFolder = metadataFolder {
isShare = metadata.permissions.contains(NCMetadataPermissions.permissionShared) && !metadataFolder.permissions.contains(NCMetadataPermissions.permissionShared)
isMounted = metadata.permissions.contains(NCMetadataPermissions.permissionMounted) && !metadataFolder.permissions.contains(NCMetadataPermissions.permissionMounted)
}
if metadata.e2eEncrypted {
cell.imageItem.image = NCImageCache.shared.getFolderEncrypted(account: metadata.account)
} else if isShare {
cell.imageItem.image = NCImageCache.shared.getFolderSharedWithMe(account: metadata.account)
} else if !metadata.shareType.isEmpty {
metadata.shareType.contains(3) ?
(cell.imageItem.image = NCImageCache.shared.getFolderPublic(account: metadata.account)) :
(cell.imageItem.image = NCImageCache.shared.getFolderSharedWithMe(account: metadata.account))
} else if metadata.mountType == "group" {
cell.imageItem.image = NCImageCache.shared.getFolderGroup(account: metadata.account)
} else if isMounted {
cell.imageItem.image = NCImageCache.shared.getFolderExternal(account: metadata.account)
} else if metadata.fileName == autoUploadFileName && metadata.serverUrl == autoUploadDirectory {
cell.imageItem.image = NCImageCache.shared.getFolderAutomaticUpload(account: metadata.account)
} else {
cell.imageItem.image = NCImageCache.shared.getFolder(account: metadata.account)
}
cell.labelInfo.text = utility.getRelativeDateTitle(metadata.date as Date)
let lockServerUrl = utilityFileSystem.createServerUrl(serverUrl: metadata.serverUrl, fileName: metadata.fileName)
let tableDirectory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", session.account, lockServerUrl))
// Local image: offline
if tableDirectory != nil && tableDirectory!.offline {
cell.imageLocal?.image = NCImageCache.shared.getImageOfflineFlag()
}
}
}
// MARK: - Table View (uploading files)
extension NCShareExtension: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return heightRowTableView
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let fileName = filesName[indexPath.row]
let session = NCShareExtensionData.shared.getSession()
showRenameFileDialog(named: fileName, account: session.account)
}
}
extension NCShareExtension: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
filesName.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? NCShareCell else {
return UITableViewCell()
}
let fileName = filesName[indexPath.row]
let session = NCShareExtensionData.shared.getSession()
cell.setup(fileName: fileName, iconName: "", account: session.account)
cell.delegate = self
return cell
}
}