diff --git a/Share/NCShareExtension+Files.swift b/Share/NCShareExtension+Files.swift index 20ec709713..1677113fa2 100644 --- a/Share/NCShareExtension+Files.swift +++ b/Share/NCShareExtension+Files.swift @@ -29,9 +29,9 @@ import NextcloudKit extension NCShareExtension { @objc func reloadDatasource(withLoadFolder: Bool) { let predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND directory == true", session.account, serverUrl) - let metadatas = self.database.getResultsMetadatasPredicate(predicate, layoutForView: NCDBLayoutForView()) + let results = self.database.getResultsMetadatasPredicate(predicate, layoutForView: NCDBLayoutForView()) - self.dataSource = NCCollectionViewDataSource(metadatas: metadatas) + self.dataSource = NCCollectionViewDataSource(results: results) if withLoadFolder { loadFolder() diff --git a/iOSClient/Data/NCManageDatabase+Metadata.swift b/iOSClient/Data/NCManageDatabase+Metadata.swift index 0e474a03d1..cc84c82589 100644 --- a/iOSClient/Data/NCManageDatabase+Metadata.swift +++ b/iOSClient/Data/NCManageDatabase+Metadata.swift @@ -890,7 +890,7 @@ extension NCManageDatabase { // MARK: - GetResult(s)Metadata - func getResultsMetadatasPredicate(_ predicate: NSPredicate, layoutForView: NCDBLayoutForView?) -> [tableMetadata] { + func getResultsMetadatasPredicate(_ predicate: NSPredicate, layoutForView: NCDBLayoutForView?) -> Results? { do { let realm = try Realm() var results = realm.objects(tableMetadata.self).filter(predicate) @@ -901,11 +901,11 @@ extension NCManageDatabase { results = results.sorted(byKeyPath: layoutForView.sort, ascending: layoutForView.ascending).sorted(byKeyPath: "favorite", ascending: false) } } - return Array(results) + return results } catch let error as NSError { NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)") } - return [] + return nil } func getResultsMetadatas(predicate: NSPredicate, sortedByKeyPath: String, ascending: Bool, arraySlice: Int) -> [tableMetadata] { @@ -939,25 +939,30 @@ extension NCManageDatabase { return nil } - func getResultsMetadatasFromGroupfolders(session: NCSession.Session) -> [tableMetadata] { - var metadatas: [tableMetadata] = [] + func getResultsMetadatasFromGroupfolders(session: NCSession.Session) -> Results? { + var ocId: [String] = [] let homeServerUrl = utilityFileSystem.getHomeServer(session: session) do { let realm = try Realm() let groupfolders = realm.objects(TableGroupfolders.self).filter("account == %@", session.account).sorted(byKeyPath: "mountPoint", ascending: true) + for groupfolder in groupfolders { let mountPoint = groupfolder.mountPoint.hasPrefix("/") ? groupfolder.mountPoint : "/" + groupfolder.mountPoint let serverUrlFileName = homeServerUrl + mountPoint + if let directory = realm.objects(tableDirectory.self).filter("account == %@ AND serverUrl == %@", session.account, serverUrlFileName).first, - let metadata = realm.objects(tableMetadata.self).filter("ocId == %@", directory.ocId).first { - metadatas.append(metadata) + let result = realm.objects(tableMetadata.self).filter("ocId == %@", directory.ocId).first { + ocId.append(result.ocId) } } + + return realm.objects(tableMetadata.self).filter("ocId IN %@", ocId) } catch let error as NSError { NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)") } - return metadatas + + return nil } func getResultsImageCacheMetadatas(predicate: NSPredicate) -> Results? { diff --git a/iOSClient/Favorites/NCFavorite.swift b/iOSClient/Favorites/NCFavorite.swift index c12db1bbd3..ca0a5f0332 100644 --- a/iOSClient/Favorites/NCFavorite.swift +++ b/iOSClient/Favorites/NCFavorite.swift @@ -62,8 +62,8 @@ class NCFavorite: NCCollectionViewCommon { predicate = NSPredicate(format: "account == %@ AND favorite == true", session.account) } - let metadatas = self.database.getResultsMetadatasPredicate(predicate, layoutForView: layoutForView) - self.dataSource = NCCollectionViewDataSource(metadatas: metadatas, layoutForView: layoutForView) + let results = self.database.getResultsMetadatasPredicate(predicate, layoutForView: layoutForView) + self.dataSource = NCCollectionViewDataSource(results: results, layoutForView: layoutForView) super.reloadDataSource() } diff --git a/iOSClient/Files/NCFiles.swift b/iOSClient/Files/NCFiles.swift index 0a66e9e99e..7cb15804c2 100644 --- a/iOSClient/Files/NCFiles.swift +++ b/iOSClient/Files/NCFiles.swift @@ -130,8 +130,8 @@ class NCFiles: NCCollectionViewCommon { self.metadataFolder = database.getMetadataFolder(session: session, serverUrl: self.serverUrl) self.richWorkspaceText = database.getTableDirectory(predicate: predicateDirectory)?.richWorkspace - let metadatas = self.database.getResultsMetadatasPredicate(predicate, layoutForView: layoutForView) - self.dataSource = NCCollectionViewDataSource(metadatas: metadatas, layoutForView: layoutForView) + let results = self.database.getResultsMetadatasPredicate(predicate, layoutForView: layoutForView) + self.dataSource = NCCollectionViewDataSource(results: results, layoutForView: layoutForView) super.reloadDataSource() } diff --git a/iOSClient/Groupfolders/NCGroupfolders.swift b/iOSClient/Groupfolders/NCGroupfolders.swift index a7cb69edc7..b6b21e71bb 100644 --- a/iOSClient/Groupfolders/NCGroupfolders.swift +++ b/iOSClient/Groupfolders/NCGroupfolders.swift @@ -56,15 +56,15 @@ class NCGroupfolders: NCCollectionViewCommon { // MARK: - DataSource override func reloadDataSource() { - var metadatas: [tableMetadata] = [] + var results: Results? if self.serverUrl.isEmpty { - metadatas = database.getResultsMetadatasFromGroupfolders(session: session) + results = database.getResultsMetadatasFromGroupfolders(session: session) } else { - metadatas = self.database.getResultsMetadatasPredicate(self.defaultPredicate, layoutForView: layoutForView) + results = self.database.getResultsMetadatasPredicate(self.defaultPredicate, layoutForView: layoutForView) } - self.dataSource = NCCollectionViewDataSource(metadatas: metadatas, layoutForView: layoutForView) + self.dataSource = NCCollectionViewDataSource(results: results, layoutForView: layoutForView) super.reloadDataSource() } diff --git a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift index 438a459e20..bce756d79a 100644 --- a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift +++ b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift @@ -24,6 +24,7 @@ import Foundation import UIKit import NextcloudKit +import RealmSwift extension NCCollectionViewCommon: UICollectionViewDataSource { func numberOfSections(in collectionView: UICollectionView) -> Int { @@ -40,27 +41,89 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { return self.dataSource.numberOfItemsInSection(section) } - func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { - guard let metadata = dataSource.getMetadata(indexPath: indexPath) else { return } - let existsImagePreview = utilityFileSystem.fileProviderStorageImageExists(metadata.ocId, etag: metadata.etag) - let ext = global.getSizeExtension(column: self.numberOfColumns) + func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { + if !collectionView.indexPathsForVisibleItems.contains(indexPath), let results = self.dataSource.getResults() { + let threadSafeResults = ThreadSafeReference(to: results) + + DispatchQueue.global().async { + guard let metadata = self.dataSource.getMetadata(threadSafeResults: threadSafeResults, indexPaths: [indexPath]).first else { return } - if metadata.hasPreview, - !existsImagePreview, - NCNetworking.shared.downloadThumbnailQueue.operations.filter({ ($0 as? NCMediaDownloadThumbnail)?.metadata.ocId == metadata.ocId }).isEmpty { - NCNetworking.shared.downloadThumbnailQueue.addOperation(NCCollectionViewDownloadThumbnail(metadata: metadata, collectionView: collectionView, ext: ext)) + for case let operation as NCCollectionViewDownloadThumbnail in NCNetworking.shared.downloadThumbnailQueue.operations where operation.metadata.ocId == metadata.ocId { + operation.cancel() + } + } } } - func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { - if !collectionView.indexPathsForVisibleItems.contains(indexPath) { - guard let metadata = self.dataSource.getMetadata(indexPath: indexPath) else { return } - for case let operation as NCCollectionViewDownloadThumbnail in NCNetworking.shared.downloadThumbnailQueue.operations where operation.metadata.ocId == metadata.ocId { - operation.cancel() + func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { + guard let results = self.dataSource.getResults() else { return } + let threadSafeResults = ThreadSafeReference(to: results) + + DispatchQueue.global().async { + guard let metadata = self.dataSource.getMetadata(threadSafeResults: threadSafeResults, indexPaths: [indexPath]).first else { return } + let existsImagePreview = self.utilityFileSystem.fileProviderStorageImageExists(metadata.ocId, etag: metadata.etag) + let ext = self.global.getSizeExtension(column: self.numberOfColumns) + + if metadata.hasPreview, + !existsImagePreview, + NCNetworking.shared.downloadThumbnailQueue.operations.filter({ ($0 as? NCMediaDownloadThumbnail)?.metadata.ocId == metadata.ocId }).isEmpty { + NCNetworking.shared.downloadThumbnailQueue.addOperation(NCCollectionViewDownloadThumbnail(metadata: metadata, collectionView: collectionView, ext: ext)) } } } + private func photoCell(cell: NCPhotoCell, indexPath: IndexPath, metadata: tableMetadata, ext: String) -> NCPhotoCell { + let width = UIScreen.main.bounds.width / CGFloat(self.numberOfColumns) + + cell.ocId = metadata.ocId + cell.ocIdTransfer = metadata.ocIdTransfer + + /// Image + /// + if let image = NCImageCache.shared.getImageCache(ocId: metadata.ocId, etag: metadata.etag, ext: ext) { + cell.filePreviewImageView?.image = image + cell.filePreviewImageView?.contentMode = .scaleAspectFill + } else { + DispatchQueue.global(qos: .userInteractive).async { + let image = self.utility.getImage(ocId: metadata.ocId, etag: metadata.etag, ext: ext) + if let image { + self.imageCache.addImageCache(ocId: metadata.ocId, etag: metadata.etag, image: image, ext: ext, cost: indexPath.row) + DispatchQueue.main.async { + cell.filePreviewImageView?.image = image + cell.filePreviewImageView?.contentMode = .scaleAspectFill + } + } else { + DispatchQueue.main.async { + cell.filePreviewImageView?.contentMode = .scaleAspectFit + if metadata.iconName.isEmpty { + cell.filePreviewImageView?.image = NCImageCache.shared.getImageFile() + } else { + cell.filePreviewImageView?.image = self.utility.loadImage(named: metadata.iconName, useTypeIconFile: true, account: metadata.account) + } + } + } + } + } + + /// Status + /// + if metadata.isLivePhoto { + cell.fileStatusImage?.image = utility.loadImage(named: "livephoto", colors: isLayoutPhoto ? [.white] : [NCBrandColor.shared.iconImageColor2]) + } else if metadata.isVideo { + cell.fileStatusImage?.image = utility.loadImage(named: "play.circle", colors: NCBrandColor.shared.iconImageMultiColors) + } + + if width < 100 { + cell.hideButtonMore(true) + cell.hideImageStatus(true) + } else { + cell.hideButtonMore(false) + cell.hideImageStatus(false) + } + + return cell + } + func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { var cell: NCCellProtocol & UICollectionViewCell let permissions = NCPermissions() @@ -83,6 +146,7 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { let photoCell = (collectionView.dequeueReusableCell(withReuseIdentifier: "photoCell", for: indexPath) as? NCPhotoCell)! photoCell.photoCellDelegate = self cell = photoCell + return self.photoCell(cell: photoCell, indexPath: indexPath, metadata: metadata, ext: ext) } else { let gridCell = (collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as? NCGridCell)! gridCell.gridCellDelegate = self @@ -108,7 +172,6 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { cell.filePreviewImageView?.contentMode = .scaleAspectFill } else { cell.filePreviewImageView?.contentMode = .scaleAspectFit - } guard let metadata = self.dataSource.getMetadata(indexPath: indexPath) else { return cell } @@ -176,6 +239,7 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { // color folder cell.filePreviewImageView?.image = cell.filePreviewImageView?.image?.colorizeFolder(metadata: metadata, tableDirectory: tableDirectory) + } else { if metadata.hasPreviewBorder { @@ -263,12 +327,6 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { cell.fileSharedImage?.image = imageCache.getImageCanShare() } - /* - if appDelegate.account != metadata.account { - cell.fileSharedImage?.image = NCImageCache.images.shared - } - */ - // Button More if metadata.lock == true { cell.setButtonMore(image: imageCache.getImageButtonMoreLock()) @@ -369,6 +427,16 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { // Layout photo if isLayoutPhoto { let width = UIScreen.main.bounds.width / CGFloat(self.numberOfColumns) + + cell.hideImageFavorite(false) + cell.hideImageLocal(false) + cell.hideImageItem(false) + cell.hideButtonMore(false) + cell.hideLabelInfo(false) + cell.hideLabelSubinfo(false) + cell.hideImageStatus(false) + cell.fileTitleLabel?.font = UIFont.systemFont(ofSize: 15) + if width < 120 { cell.hideImageFavorite(true) cell.hideImageLocal(true) diff --git a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSourcePrefetching.swift b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSourcePrefetching.swift index f2d31a3747..2347b6c056 100644 --- a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSourcePrefetching.swift +++ b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSourcePrefetching.swift @@ -23,16 +23,20 @@ import Foundation import UIKit +import RealmSwift extension NCCollectionViewCommon: UICollectionViewDataSourcePrefetching { func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath]) { - guard !isSearchingMode, !imageCache.isLoadingCache else { return } - let ext = global.getSizeExtension(column: self.numberOfColumns) - let metadatas = self.dataSource.getMetadatas(indexPaths: indexPaths) + guard !isSearchingMode, + imageCache.allowExtensions(ext: ext), + let results = self.dataSource.getResults() + else { return } + let threadSafeResults = ThreadSafeReference(to: results) let cost = indexPaths.first?.row ?? 0 - DispatchQueue.global(qos: .userInteractive).async { + DispatchQueue.global().async { + let metadatas = self.dataSource.getMetadata(threadSafeResults: threadSafeResults, indexPaths: indexPaths) for metadata in metadatas where metadata.isImageOrVideo { if self.imageCache.getImageCache(ocId: metadata.ocId, etag: metadata.etag, ext: ext) == nil, let image = self.utility.getImage(ocId: metadata.ocId, etag: metadata.etag, ext: ext) { diff --git a/iOSClient/Main/Collection Common/NCCollectionViewCommon.swift b/iOSClient/Main/Collection Common/NCCollectionViewCommon.swift index 229da10143..1b600a0a45 100644 --- a/iOSClient/Main/Collection Common/NCCollectionViewCommon.swift +++ b/iOSClient/Main/Collection Common/NCCollectionViewCommon.swift @@ -1003,7 +1003,7 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS } providers: { _, searchProviders in self.providers = searchProviders self.searchResults = [] - self.dataSource = NCCollectionViewDataSource(metadatas: [], layoutForView: self.layoutForView, providers: self.providers, searchResults: self.searchResults) + self.dataSource = NCCollectionViewDataSource(results: nil, layoutForView: self.layoutForView, providers: self.providers, searchResults: self.searchResults) } update: { _, _, searchResult, metadatas in guard let metadatas, !metadatas.isEmpty, self.isSearchingMode, let searchResult else { return } NCNetworking.shared.unifiedSearchQueue.addOperation(NCCollectionViewUnifiedSearch(collectionViewCommon: self, metadatas: metadatas, searchResult: searchResult)) @@ -1021,7 +1021,10 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS self.collectionView.reloadData() } guard let metadatas, error == .success, self.isSearchingMode else { return } - self.dataSource = NCCollectionViewDataSource(metadatas: metadatas, layoutForView: self.layoutForView, providers: self.providers, searchResults: self.searchResults) + let ocId = metadatas.map { $0.ocId } + let results = self.database.getResultsMetadatasPredicate(NSPredicate(format: "ocId IN %@", ocId), layoutForView: self.layoutForView) + + self.dataSource = NCCollectionViewDataSource(results: results, layoutForView: self.layoutForView, providers: self.providers, searchResults: self.searchResults) } } } diff --git a/iOSClient/Main/Collection Common/NCCollectionViewDataSource.swift b/iOSClient/Main/Collection Common/NCCollectionViewDataSource.swift index fccede614c..ebfa5f6f65 100644 --- a/iOSClient/Main/Collection Common/NCCollectionViewDataSource.swift +++ b/iOSClient/Main/Collection Common/NCCollectionViewDataSource.swift @@ -23,6 +23,7 @@ import UIKit import NextcloudKit +import RealmSwift class NCCollectionViewDataSource: NSObject { private let utilityFileSystem = NCUtilityFileSystem() @@ -30,19 +31,26 @@ class NCCollectionViewDataSource: NSObject { private var sectionsValue: [String] = [] private var providers: [NKSearchProvider]? private var searchResults: [NKSearchResult]? + private var results: Results? private var metadatas: [tableMetadata] = [] private var metadatasForSection: [NCMetadataForSection] = [] private var layoutForView: NCDBLayoutForView? override init() { super.init() } - init(metadatas: [tableMetadata], + init(results: Results?, layoutForView: NCDBLayoutForView? = nil, providers: [NKSearchProvider]? = nil, searchResults: [NKSearchResult]? = nil) { super.init() - self.metadatas = metadatas + self.results = results + if let results { + self.metadatas = Array(results) + } else { + self.metadatas = [] + } + self.layoutForView = layoutForView /// unified search self.providers = providers @@ -165,6 +173,29 @@ class NCCollectionViewDataSource: NSObject { // MARK: - + func getResults() -> Results? { + return results + } + + func getMetadata(threadSafeResults: ThreadSafeReference>, indexPaths: [IndexPath]) -> [tableMetadata] { + autoreleasepool { + var metadatas: [tableMetadata] = [] + do { + let realm = try Realm() + if let resolvedResults = realm.resolve(threadSafeResults) { + let validMetadatas = resolvedResults.filter { !$0.isInvalidated } + for indexPath in indexPaths where indexPath.row < validMetadatas.count { + metadatas.append(tableMetadata(value: validMetadatas[indexPath.row])) + } + return metadatas + } + } catch let error as NSError { + NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)") + } + return metadatas + } + } + func isMetadatasValid() -> Bool { let validMetadatas = metadatas.filter { !$0.isInvalidated } diff --git a/iOSClient/Main/Collection Common/NCCollectionViewDownloadThumbnail.swift b/iOSClient/Main/Collection Common/NCCollectionViewDownloadThumbnail.swift index 1ebe9e1c89..b1c8747edf 100644 --- a/iOSClient/Main/Collection Common/NCCollectionViewDownloadThumbnail.swift +++ b/iOSClient/Main/Collection Common/NCCollectionViewDownloadThumbnail.swift @@ -68,6 +68,7 @@ class NCCollectionViewDownloadThumbnail: ConcurrentOperation, @unchecked Sendabl filePreviewImageView.layer.borderWidth = 0.2 filePreviewImageView.layer.borderColor = UIColor.systemGray3.cgColor } + UIView.transition(with: filePreviewImageView, duration: 0.75, options: .transitionCrossDissolve, diff --git a/iOSClient/Media/NCMedia+CollectionViewDataSource.swift b/iOSClient/Media/NCMedia+CollectionViewDataSource.swift index b5853d647a..19bb6dd336 100644 --- a/iOSClient/Media/NCMedia+CollectionViewDataSource.swift +++ b/iOSClient/Media/NCMedia+CollectionViewDataSource.swift @@ -133,8 +133,8 @@ extension NCMedia: UICollectionViewDataSource { DispatchQueue.main.async { if let currentCell = collectionView.cellForItem(at: indexPath) as? NCMediaCell, currentCell.ocId == metadata.ocId, let image { + self.imageCache.addImageCache(ocId: metadata.ocId, etag: metadata.etag, image: image, ext: ext, cost: indexPath.row) currentCell.imageItem.image = image - currentCell.backgroundColor = .black } } } diff --git a/iOSClient/Media/NCMedia+CollectionViewDataSourcePrefetching.swift b/iOSClient/Media/NCMedia+CollectionViewDataSourcePrefetching.swift index 4cb1295c32..49afb23989 100644 --- a/iOSClient/Media/NCMedia+CollectionViewDataSourcePrefetching.swift +++ b/iOSClient/Media/NCMedia+CollectionViewDataSourcePrefetching.swift @@ -26,24 +26,17 @@ import UIKit extension NCMedia: UICollectionViewDataSourcePrefetching { func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath]) { - guard !imageCache.isLoadingCache else { return } - - let cost = indexPaths.first?.row ?? 0 - let metadatas = self.dataSource.getMetadatas(indexPaths: indexPaths) let ext = global.getSizeExtension(column: self.numberOfColumns) - let percentageCache = (Double(self.imageCache.cache.count) / Double(self.imageCache.countLimit - 1)) * 100 - - if cost > self.imageCache.countLimit, percentageCache > 75 { - self.hiddenCellMetadats.forEach { ocIdPlusEtag in - self.imageCache.removeImageCache(ocIdPlusEtag: ocIdPlusEtag) - } - } - self.hiddenCellMetadats.removeAll() + guard !imageCache.isLoadingCache, + imageCache.allowExtensions(ext: ext) + else { return } + let cost = indexPaths.first?.row ?? 0 - metadatas.forEach { metadata in - if self.imageCache.getImageCache(ocId: metadata.ocId, etag: metadata.etag, ext: ext) == nil, - let image = self.utility.getImage(ocId: metadata.ocId, etag: metadata.etag, ext: ext) { - if self.imageCache.cache.count < self.imageCache.countLimit { + DispatchQueue.global().async { + let metadatas = self.dataSource.getMetadatas(indexPaths: indexPaths) + metadatas.forEach { metadata in + if self.imageCache.getImageCache(ocId: metadata.ocId, etag: metadata.etag, ext: ext) == nil, + let image = self.utility.getImage(ocId: metadata.ocId, etag: metadata.etag, ext: ext) { self.imageCache.addImageCache(ocId: metadata.ocId, etag: metadata.etag, image: image, ext: ext, cost: cost) } } diff --git a/iOSClient/Media/NCMediaDataSource.swift b/iOSClient/Media/NCMediaDataSource.swift index efc8a87e37..2ab1382f44 100644 --- a/iOSClient/Media/NCMediaDataSource.swift +++ b/iOSClient/Media/NCMediaDataSource.swift @@ -127,11 +127,14 @@ extension NCMedia { self.collectionViewReloadData() } - DispatchQueue.global(qos: .userInteractive).async { + DispatchQueue.global().async { self.database.convertFilesToMetadatas(files, useFirstAsMetadataFolder: false) { _, metadatas in let metadatas = metadatas.filter { metadata in - let tableMetadata = self.database.getMetadataFromOcId(metadata.ocId) - return tableMetadata?.status == self.global.metadataStatusNormal + if let tableMetadata = self.database.getMetadataFromOcId(metadata.ocId) { + return tableMetadata.status == self.global.metadataStatusNormal + } else { + return true + } } self.database.addMetadatas(metadatas) diff --git a/iOSClient/NCImageCache.swift b/iOSClient/NCImageCache.swift index 9f814af0ef..387f445c90 100644 --- a/iOSClient/NCImageCache.swift +++ b/iOSClient/NCImageCache.swift @@ -75,6 +75,10 @@ class NCImageCache: NSObject { #endif } + func allowExtensions(ext: String) -> Bool { + return allowExtensions.contains(ext) + } + func addImageCache(ocId: String, etag: String, data: Data, ext: String, cost: Int) { guard allowExtensions.contains(ext), let image = UIImage(data: data) else { return } diff --git a/iOSClient/Networking/NCAutoUpload.swift b/iOSClient/Networking/NCAutoUpload.swift index 263708a8a4..79ac9bf640 100644 --- a/iOSClient/Networking/NCAutoUpload.swift +++ b/iOSClient/Networking/NCAutoUpload.swift @@ -39,7 +39,7 @@ class NCAutoUpload: NSObject { func initAutoUpload(controller: NCMainTabBarController?, account: String, completion: @escaping (_ num: Int) -> Void) { applicationState = UIApplication.shared.applicationState - DispatchQueue.global(qos: .userInteractive).async { + DispatchQueue.global().async { guard NCNetworking.shared.isOnline, let tableAccount = self.database.getTableAccount(predicate: NSPredicate(format: "account == %@", account)), tableAccount.autoUpload else { @@ -73,7 +73,7 @@ class NCAutoUpload: NSObject { NCAskAuthorization().askAuthorizationPhotoLibrary(controller: controller) { hasPermission in guard hasPermission else { return } - DispatchQueue.global(qos: .userInteractive).async { + DispatchQueue.global().async { self.uploadAssetsNewAndFull(controller: controller, selector: NCGlobal.shared.selectorUploadAutoUploadAll, log: log, account: account) { _ in self.hud.dismiss() } diff --git a/iOSClient/Networking/NCNetworking+Download.swift b/iOSClient/Networking/NCNetworking+Download.swift index 865c497443..ad718d09c7 100644 --- a/iOSClient/Networking/NCNetworking+Download.swift +++ b/iOSClient/Networking/NCNetworking+Download.swift @@ -182,7 +182,7 @@ extension NCNetworking { return delegate.downloadComplete(fileName: fileName, serverUrl: serverUrl, etag: etag, date: date, dateLastModified: dateLastModified, length: length, task: task, error: error) } - DispatchQueue.global(qos: .userInteractive).async { + DispatchQueue.global().async { guard let url = task.currentRequest?.url, let metadata = self.database.getMetadata(from: url, sessionTaskIdentifier: task.taskIdentifier) else { return } @@ -259,7 +259,7 @@ extension NCNetworking { return delegate.downloadProgress(progress, totalBytes: totalBytes, totalBytesExpected: totalBytesExpected, fileName: fileName, serverUrl: serverUrl, session: session, task: task) } - DispatchQueue.global(qos: .userInteractive).async { + DispatchQueue.global().async { if let metadata = self.database.getResultMetadataFromFileName(fileName, serverUrl: serverUrl, sessionTaskIdentifier: task.taskIdentifier) { NotificationCenter.default.postOnMainThread(name: self.global.notificationCenterProgressTask, object: nil, diff --git a/iOSClient/Networking/NCNetworking+Upload.swift b/iOSClient/Networking/NCNetworking+Upload.swift index 913f3223e3..06a0718d73 100644 --- a/iOSClient/Networking/NCNetworking+Upload.swift +++ b/iOSClient/Networking/NCNetworking+Upload.swift @@ -303,7 +303,7 @@ extension NCNetworking { #if !EXTENSION isApplicationStateActive = UIApplication.shared.applicationState == .active #endif - DispatchQueue.global(qos: .userInteractive).async { + DispatchQueue.global().async { let selector = metadata.sessionSelector if error == .success, let ocId = ocId, size == metadata.size { @@ -475,7 +475,7 @@ extension NCNetworking { return delegate.uploadProgress(progress, totalBytes: totalBytes, totalBytesExpected: totalBytesExpected, fileName: fileName, serverUrl: serverUrl, session: session, task: task) } - DispatchQueue.global(qos: .userInteractive).async { + DispatchQueue.global().async { if let metadata = self.database.getResultMetadataFromFileName(fileName, serverUrl: serverUrl, sessionTaskIdentifier: task.taskIdentifier) { NotificationCenter.default.postOnMainThread(name: self.global.notificationCenterProgressTask, object: nil, diff --git a/iOSClient/Offline/NCOffline.swift b/iOSClient/Offline/NCOffline.swift index df8194441f..64285bb732 100644 --- a/iOSClient/Offline/NCOffline.swift +++ b/iOSClient/Offline/NCOffline.swift @@ -23,6 +23,7 @@ import UIKit import NextcloudKit +import RealmSwift class NCOffline: NCCollectionViewCommon { @@ -48,11 +49,11 @@ class NCOffline: NCCollectionViewCommon { reloadDataSource() } - // MARK: - DataSource + NC Endpoint + // MARK: - DataSource override func reloadDataSource() { var ocIds: [String] = [] - var metadatas: [tableMetadata] = [] + var results: Results? self.dataSource.removeAll() if self.serverUrl.isEmpty { @@ -65,14 +66,12 @@ class NCOffline: NCCollectionViewCommon { for file in files { ocIds.append(file.ocId) } - if let results = self.database.getResultsMetadatas(predicate: NSPredicate(format: "account == %@ AND ocId IN %@", session.account, ocIds)) { - metadatas = Array(results) - } + results = self.database.getResultsMetadatas(predicate: NSPredicate(format: "account == %@ AND ocId IN %@", session.account, ocIds)) } else { - metadatas = self.database.getResultsMetadatasPredicate(self.defaultPredicate, layoutForView: layoutForView) + results = self.database.getResultsMetadatasPredicate(self.defaultPredicate, layoutForView: layoutForView) } - self.dataSource = NCCollectionViewDataSource(metadatas: metadatas, layoutForView: layoutForView) + self.dataSource = NCCollectionViewDataSource(results: results, layoutForView: layoutForView) super.reloadDataSource() } diff --git a/iOSClient/Recent/NCRecent.swift b/iOSClient/Recent/NCRecent.swift index 842f2dbf42..2db946f7f6 100644 --- a/iOSClient/Recent/NCRecent.swift +++ b/iOSClient/Recent/NCRecent.swift @@ -55,13 +55,13 @@ class NCRecent: NCCollectionViewCommon { // MARK: - DataSource override func reloadDataSource() { - let metadatas = self.database.getResultsMetadatas(predicate: NSPredicate(format: "account == %@ AND fileName != '.'", session.account), sortedByKeyPath: "date", ascending: false, arraySlice: 200) + let results = self.database.getResultsMetadatas(predicate: NSPredicate(format: "account == %@ AND fileName != '.'", session.account), sortedByKeyPath: "date", ascending: false) layoutForView?.sort = "date" layoutForView?.ascending = false layoutForView?.directoryOnTop = false - self.dataSource = NCCollectionViewDataSource(metadatas: metadatas, layoutForView: layoutForView) + self.dataSource = NCCollectionViewDataSource(results: results, layoutForView: layoutForView) super.reloadDataSource() } diff --git a/iOSClient/Select/NCSelect.swift b/iOSClient/Select/NCSelect.swift index 220155017b..8dd5fa6703 100644 --- a/iOSClient/Select/NCSelect.swift +++ b/iOSClient/Select/NCSelect.swift @@ -484,9 +484,9 @@ extension NCSelect { self.dataSourceTask = task self.collectionView.reloadData() } completion: { _, _, _, _ in - let metadatas = self.database.getResultsMetadatasPredicate(predicate, layoutForView: NCDBLayoutForView()) + let results = self.database.getResultsMetadatasPredicate(predicate, layoutForView: NCDBLayoutForView()) - self.dataSource = NCCollectionViewDataSource(metadatas: metadatas) + self.dataSource = NCCollectionViewDataSource(results: results) self.collectionView.reloadData() NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource, userInfo: ["serverUrl": self.serverUrl]) diff --git a/iOSClient/Shares/NCShares.swift b/iOSClient/Shares/NCShares.swift index 69bc4cd962..aac86fe0cd 100644 --- a/iOSClient/Shares/NCShares.swift +++ b/iOSClient/Shares/NCShares.swift @@ -55,13 +55,13 @@ class NCShares: NCCollectionViewCommon { // MARK: - DataSource override func reloadDataSource() { - var metadatas: [tableMetadata] = [] - + var ocId: [String] = [] let sharess = self.database.getTableShares(account: session.account) + for share in sharess { - if let metadata = self.database.getMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileName == %@", session.account, share.serverUrl, share.fileName)) { - if !(metadatas.contains { $0.ocId == metadata.ocId }) { - metadatas.append(metadata) + if let result = self.database.getResultMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileName == %@", session.account, share.serverUrl, share.fileName)) { + if !(ocId.contains { $0 == result.ocId }) { + ocId.append(result.ocId) } } else { let serverUrlFileName = share.serverUrl + "/" + share.fileName @@ -71,17 +71,18 @@ class NCShares: NCCollectionViewCommon { } completion: { _, metadata, _ in if let metadata { self.database.addMetadata(metadata) - if !(metadatas.contains { $0.ocId == metadata.ocId }) { - metadatas.append(metadata) - self.dataSource = NCCollectionViewDataSource(metadatas: metadatas, layoutForView: self.layoutForView) - super.reloadDataSource() + if !(ocId.contains { $0 == metadata.ocId }) { + ocId.append(metadata.ocId) } } } } } - self.dataSource = NCCollectionViewDataSource(metadatas: metadatas, layoutForView: layoutForView) + let results = self.database.getResultsMetadatasPredicate(NSPredicate(format: "ocId IN %@", ocId), layoutForView: layoutForView) + + self.dataSource = NCCollectionViewDataSource(results: results, layoutForView: layoutForView) + super.reloadDataSource() } diff --git a/iOSClient/Transfers/NCTransfers.swift b/iOSClient/Transfers/NCTransfers.swift index c4e21bf5c3..6b1db2e17f 100644 --- a/iOSClient/Transfers/NCTransfers.swift +++ b/iOSClient/Transfers/NCTransfers.swift @@ -283,8 +283,8 @@ class NCTransfers: NCCollectionViewCommon, NCTransferCellDelegate { override func reloadDataSource() { self.dataSource.removeAll() - if let metadatas = self.database.getResultsMetadatas(predicate: NSPredicate(format: "status != %i", NCGlobal.shared.metadataStatusNormal), sortedByKeyPath: "sessionDate", ascending: true) { - self.dataSource = NCCollectionViewDataSource(metadatas: Array(metadatas), layoutForView: layoutForView) + if let results = self.database.getResultsMetadatas(predicate: NSPredicate(format: "status != %i", NCGlobal.shared.metadataStatusNormal), sortedByKeyPath: "sessionDate", ascending: true) { + self.dataSource = NCCollectionViewDataSource(results: results, layoutForView: layoutForView) } if self.dataSource.isEmpty() {