From 4e6ff3b4b575b26801e066cc1051b6b8047e4a05 Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Tue, 1 Oct 2024 09:43:54 +0200 Subject: [PATCH 01/15] cod Signed-off-by: Marino Faggiana --- ...nViewCommon+CollectionViewDataSource.swift | 27 +++++++++---------- ...+CollectionViewDataSourcePrefetching.swift | 3 +++ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift index 438a459e20..9dfee73e1d 100644 --- a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift +++ b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift @@ -54,10 +54,12 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { 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() } + */ } } @@ -108,7 +110,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 +177,7 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { // color folder cell.filePreviewImageView?.image = cell.filePreviewImageView?.image?.colorizeFolder(metadata: metadata, tableDirectory: tableDirectory) + } else { if metadata.hasPreviewBorder { @@ -187,6 +189,7 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { if let image = NCImageCache.shared.getImageCache(ocId: metadata.ocId, etag: metadata.etag, ext: ext) { cell.filePreviewImageView?.image = image } else if let image = utility.getImage(ocId: metadata.ocId, etag: metadata.etag, ext: ext) { + NCImageCache.shared.addImageCache(ocId: metadata.ocId, etag: metadata.etag, image: image, ext: ext, cost: indexPath.row) cell.filePreviewImageView?.image = image } @@ -236,13 +239,15 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { } } - let tableLocalFile = database.getResultsTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))?.first - // image local - if let tableLocalFile, tableLocalFile.offline { - a11yValues.append(NSLocalizedString("_offline_", comment: "")) - cell.fileLocalImage?.image = imageCache.getImageOfflineFlag() - } else if utilityFileSystem.fileProviderStorageExists(metadata) { - cell.fileLocalImage?.image = imageCache.getImageLocal() + if !isLayoutPhoto { + let tableLocalFile = database.getResultsTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))?.first + // image local + if let tableLocalFile, tableLocalFile.offline { + a11yValues.append(NSLocalizedString("_offline_", comment: "")) + cell.fileLocalImage?.image = imageCache.getImageOfflineFlag() + } else if utilityFileSystem.fileProviderStorageExists(metadata) { + cell.fileLocalImage?.image = imageCache.getImageLocal() + } } } @@ -263,12 +268,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()) diff --git a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSourcePrefetching.swift b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSourcePrefetching.swift index f2d31a3747..082b13145e 100644 --- a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSourcePrefetching.swift +++ b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSourcePrefetching.swift @@ -26,6 +26,8 @@ import UIKit extension NCCollectionViewCommon: UICollectionViewDataSourcePrefetching { func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath]) { + + /* guard !isSearchingMode, !imageCache.isLoadingCache else { return } let ext = global.getSizeExtension(column: self.numberOfColumns) @@ -40,5 +42,6 @@ extension NCCollectionViewCommon: UICollectionViewDataSourcePrefetching { } } } + */ } } From 8ff7aa301c3c4924512882961c2c84630713843e Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Tue, 1 Oct 2024 09:52:56 +0200 Subject: [PATCH 02/15] revert Signed-off-by: Marino Faggiana --- ...onViewCommon+CollectionViewDataSource.swift | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift index 9dfee73e1d..326dfc745a 100644 --- a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift +++ b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift @@ -54,12 +54,10 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { 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() } - */ } } @@ -239,15 +237,13 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { } } - if !isLayoutPhoto { - let tableLocalFile = database.getResultsTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))?.first - // image local - if let tableLocalFile, tableLocalFile.offline { - a11yValues.append(NSLocalizedString("_offline_", comment: "")) - cell.fileLocalImage?.image = imageCache.getImageOfflineFlag() - } else if utilityFileSystem.fileProviderStorageExists(metadata) { - cell.fileLocalImage?.image = imageCache.getImageLocal() - } + let tableLocalFile = database.getResultsTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))?.first + // image local + if let tableLocalFile, tableLocalFile.offline { + a11yValues.append(NSLocalizedString("_offline_", comment: "")) + cell.fileLocalImage?.image = imageCache.getImageOfflineFlag() + } else if utilityFileSystem.fileProviderStorageExists(metadata) { + cell.fileLocalImage?.image = imageCache.getImageLocal() } } From 7d4b24b61997eb245c9c7b3aa143fc2c16a8fe31 Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Tue, 1 Oct 2024 11:24:31 +0200 Subject: [PATCH 03/15] change to results Signed-off-by: Marino Faggiana --- Share/NCShareExtension+Files.swift | 4 +- .../Data/NCManageDatabase+Metadata.swift | 21 ++++++---- iOSClient/Favorites/NCFavorite.swift | 4 +- iOSClient/Files/NCFiles.swift | 4 +- iOSClient/Groupfolders/NCGroupfolders.swift | 8 ++-- ...nViewCommon+CollectionViewDataSource.swift | 38 ++++++++++++++++--- .../NCCollectionViewCommon.swift | 7 +++- .../NCCollectionViewDataSource.swift | 12 +++++- iOSClient/Offline/NCOffline.swift | 13 +++---- iOSClient/Recent/NCRecent.swift | 4 +- iOSClient/Select/NCSelect.swift | 4 +- iOSClient/Shares/NCShares.swift | 21 +++++----- iOSClient/Transfers/NCTransfers.swift | 4 +- 13 files changed, 94 insertions(+), 50 deletions(-) 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 326dfc745a..632b42f5f8 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,6 +41,17 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { return self.dataSource.numberOfItemsInSection(section) } + 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 metadata = dataSource.getMetadata(indexPath: indexPath) else { return } let existsImagePreview = utilityFileSystem.fileProviderStorageImageExists(metadata.ocId, etag: metadata.etag) @@ -52,13 +64,28 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { } } - 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() + private func photoCell(cell: NCPhotoCell, indexPath: IndexPath, metadata: tableMetadata) -> NCPhotoCell { + if metadata.directory { + cell.filePreviewImageView?.image = imageCache.getFolder(account: metadata.account) + } else { + let ext = global.getSizeExtension(column: self.numberOfColumns) + + if let image = NCImageCache.shared.getImageCache(ocId: metadata.ocId, etag: metadata.etag, ext: ext) { + cell.filePreviewImageView?.image = image + } else if let image = utility.getImage(ocId: metadata.ocId, etag: metadata.etag, ext: ext) { + NCImageCache.shared.addImageCache(ocId: metadata.ocId, etag: metadata.etag, image: image, ext: ext, cost: indexPath.row) + cell.filePreviewImageView?.image = image + } + + if cell.filePreviewImageView?.image == nil { + if metadata.iconName.isEmpty { + cell.filePreviewImageView?.image = NCImageCache.shared.getImageFile() + } else { + cell.filePreviewImageView?.image = utility.loadImage(named: metadata.iconName, useTypeIconFile: true, account: metadata.account) + } } } + return cell } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { @@ -83,6 +110,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) } else { let gridCell = (collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as? NCGridCell)! gridCell.gridCellDelegate = self 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..d257a75e67 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 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() { From f8f012c06875e3273042b3e9f028c1784000be84 Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Tue, 1 Oct 2024 11:49:24 +0200 Subject: [PATCH 04/15] cod Signed-off-by: Marino Faggiana --- ...nViewCommon+CollectionViewDataSource.swift | 13 ++++++------ .../NCCollectionViewDataSource.swift | 21 +++++++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift index 632b42f5f8..bda9191325 100644 --- a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift +++ b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift @@ -42,12 +42,13 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { } 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() + if !collectionView.indexPathsForVisibleItems.contains(indexPath), let results = self.dataSource.getResults() { + let threadSafeResults = ThreadSafeReference(to: results) + DispatchQueue.global(qos: .background).async { + guard let metadata = self.dataSource.getMetadata(threadSafeResults: threadSafeResults, indexPath: indexPath) else { return } + for case let operation as NCCollectionViewDownloadThumbnail in NCNetworking.shared.downloadThumbnailQueue.operations where operation.metadata.ocId == metadata.ocId { + operation.cancel() + } } } } diff --git a/iOSClient/Main/Collection Common/NCCollectionViewDataSource.swift b/iOSClient/Main/Collection Common/NCCollectionViewDataSource.swift index d257a75e67..130e917977 100644 --- a/iOSClient/Main/Collection Common/NCCollectionViewDataSource.swift +++ b/iOSClient/Main/Collection Common/NCCollectionViewDataSource.swift @@ -173,6 +173,27 @@ class NCCollectionViewDataSource: NSObject { // MARK: - + func getResults() -> Results? { + return results + } + + func getMetadata(threadSafeResults: ThreadSafeReference>, indexPath: IndexPath) -> tableMetadata? { + autoreleasepool { + do { + let realm = try Realm() + if let resolvedResults = realm.resolve(threadSafeResults) { + let validMetadatas = resolvedResults.filter { !$0.isInvalidated } + if indexPath.row < validMetadatas.count { + return tableMetadata(value: validMetadatas[indexPath.row]) + } + } + } catch let error as NSError { + NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)") + } + return nil + } + } + func isMetadatasValid() -> Bool { let validMetadatas = metadatas.filter { !$0.isInvalidated } From a22fee4e3bd31893ef49c787345f102af49138f3 Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Tue, 1 Oct 2024 11:55:50 +0200 Subject: [PATCH 05/15] improvements Signed-off-by: Marino Faggiana --- ...nViewCommon+CollectionViewDataSource.swift | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift index bda9191325..7f572b0d1e 100644 --- a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift +++ b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift @@ -44,8 +44,10 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { 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(qos: .background).async { guard let metadata = self.dataSource.getMetadata(threadSafeResults: threadSafeResults, indexPath: indexPath) else { return } + for case let operation as NCCollectionViewDownloadThumbnail in NCNetworking.shared.downloadThumbnailQueue.operations where operation.metadata.ocId == metadata.ocId { operation.cancel() } @@ -54,14 +56,19 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { } 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) - - 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)) + guard let results = self.dataSource.getResults() else { return } + let threadSafeResults = ThreadSafeReference(to: results) + + DispatchQueue.global(qos: .background).async { + guard let metadata = self.dataSource.getMetadata(threadSafeResults: threadSafeResults, indexPath: indexPath) 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)) + } } } From c2062429de7effe081652ff570b756ce266d4bbb Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Tue, 1 Oct 2024 12:06:35 +0200 Subject: [PATCH 06/15] cod Signed-off-by: Marino Faggiana --- ...CollectionViewCommon+CollectionViewDataSource.swift | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift index 7f572b0d1e..d8ba8440d6 100644 --- a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift +++ b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift @@ -73,6 +73,10 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { } private func photoCell(cell: NCPhotoCell, indexPath: IndexPath, metadata: tableMetadata) -> NCPhotoCell { + + cell.ocId = metadata.ocId + cell.ocIdTransfer = metadata.ocIdTransfer + if metadata.directory { cell.filePreviewImageView?.image = imageCache.getFolder(account: metadata.account) } else { @@ -85,7 +89,11 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { cell.filePreviewImageView?.image = image } - if cell.filePreviewImageView?.image == nil { + if cell.filePreviewImageView?.image != nil { + cell.filePreviewImageView?.contentMode = .scaleAspectFill + } else { + cell.filePreviewImageView?.contentMode = .scaleAspectFit + /// default image if metadata.iconName.isEmpty { cell.filePreviewImageView?.image = NCImageCache.shared.getImageFile() } else { From ae3d2016076edfaf17ad135afa4bc778547a027a Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Tue, 1 Oct 2024 12:27:31 +0200 Subject: [PATCH 07/15] cod Signed-off-by: Marino Faggiana --- .../NCCollectionViewCommon+CollectionViewDataSource.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift index d8ba8440d6..61e34ba313 100644 --- a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift +++ b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift @@ -45,7 +45,7 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { if !collectionView.indexPathsForVisibleItems.contains(indexPath), let results = self.dataSource.getResults() { let threadSafeResults = ThreadSafeReference(to: results) - DispatchQueue.global(qos: .background).async { + DispatchQueue.global().async { guard let metadata = self.dataSource.getMetadata(threadSafeResults: threadSafeResults, indexPath: indexPath) else { return } for case let operation as NCCollectionViewDownloadThumbnail in NCNetworking.shared.downloadThumbnailQueue.operations where operation.metadata.ocId == metadata.ocId { @@ -59,7 +59,7 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { guard let results = self.dataSource.getResults() else { return } let threadSafeResults = ThreadSafeReference(to: results) - DispatchQueue.global(qos: .background).async { + DispatchQueue.global().async { guard let metadata = self.dataSource.getMetadata(threadSafeResults: threadSafeResults, indexPath: indexPath) else { return } let existsImagePreview = self.utilityFileSystem.fileProviderStorageImageExists(metadata.ocId, etag: metadata.etag) let ext = self.global.getSizeExtension(column: self.numberOfColumns) From d929a1a4e412e296eb2c962bb8c2a1d1e3ad961e Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Tue, 1 Oct 2024 13:56:24 +0200 Subject: [PATCH 08/15] cod Signed-off-by: Marino Faggiana --- ...nViewCommon+CollectionViewDataSource.swift | 68 ++++++++++++------- 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift index 61e34ba313..0f6c3f66bb 100644 --- a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift +++ b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift @@ -72,35 +72,41 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { } } - private func photoCell(cell: NCPhotoCell, indexPath: IndexPath, metadata: tableMetadata) -> NCPhotoCell { - - cell.ocId = metadata.ocId - cell.ocIdTransfer = metadata.ocIdTransfer - - if metadata.directory { - cell.filePreviewImageView?.image = imageCache.getFolder(account: metadata.account) - } else { - let ext = global.getSizeExtension(column: self.numberOfColumns) - - if let image = NCImageCache.shared.getImageCache(ocId: metadata.ocId, etag: metadata.etag, ext: ext) { - cell.filePreviewImageView?.image = image - } else if let image = utility.getImage(ocId: metadata.ocId, etag: metadata.etag, ext: ext) { - NCImageCache.shared.addImageCache(ocId: metadata.ocId, etag: metadata.etag, image: image, ext: ext, cost: indexPath.row) - cell.filePreviewImageView?.image = image - } + private func photoCell(cell: NCCellProtocol & UICollectionViewCell, indexPath: IndexPath, metadata: tableMetadata, ext: String) -> NCCellProtocol & UICollectionViewCell { + let width = UIScreen.main.bounds.width / CGFloat(self.numberOfColumns) + + /// Image + /// + if let image = NCImageCache.shared.getImageCache(ocId: metadata.ocId, etag: metadata.etag, ext: ext) { + cell.filePreviewImageView?.image = image + } else if let image = utility.getImage(ocId: metadata.ocId, etag: metadata.etag, ext: ext) { + NCImageCache.shared.addImageCache(ocId: metadata.ocId, etag: metadata.etag, image: image, ext: ext, cost: indexPath.row) + cell.filePreviewImageView?.image = image + } - if cell.filePreviewImageView?.image != nil { - cell.filePreviewImageView?.contentMode = .scaleAspectFill + /// Default image + /// + if cell.filePreviewImageView?.image == nil { + if metadata.iconName.isEmpty { + cell.filePreviewImageView?.image = NCImageCache.shared.getImageFile() } else { - cell.filePreviewImageView?.contentMode = .scaleAspectFit - /// default image - if metadata.iconName.isEmpty { - cell.filePreviewImageView?.image = NCImageCache.shared.getImageFile() - } else { - cell.filePreviewImageView?.image = utility.loadImage(named: metadata.iconName, useTypeIconFile: true, account: metadata.account) - } + cell.filePreviewImageView?.image = 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) + } + return cell } @@ -126,7 +132,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) + 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 @@ -408,6 +414,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) From 6e497ce408686cb4b2d79a4b284ed63fd810a6ba Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Tue, 1 Oct 2024 14:31:50 +0200 Subject: [PATCH 09/15] cod Signed-off-by: Marino Faggiana --- ...nViewCommon+CollectionViewDataSource.swift | 21 +++++++++++++++---- ...+CollectionViewDataSourcePrefetching.swift | 12 +++++------ .../NCCollectionViewDataSource.swift | 10 +++++---- ...+CollectionViewDataSourcePrefetching.swift | 3 +++ iOSClient/NCImageCache.swift | 4 ++++ 5 files changed, 35 insertions(+), 15 deletions(-) diff --git a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift index 0f6c3f66bb..682390b447 100644 --- a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift +++ b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift @@ -46,7 +46,7 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { let threadSafeResults = ThreadSafeReference(to: results) DispatchQueue.global().async { - guard let metadata = self.dataSource.getMetadata(threadSafeResults: threadSafeResults, indexPath: indexPath) else { return } + guard let metadata = self.dataSource.getMetadata(threadSafeResults: threadSafeResults, indexPaths: [indexPath]).first else { return } for case let operation as NCCollectionViewDownloadThumbnail in NCNetworking.shared.downloadThumbnailQueue.operations where operation.metadata.ocId == metadata.ocId { operation.cancel() @@ -60,7 +60,7 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { let threadSafeResults = ThreadSafeReference(to: results) DispatchQueue.global().async { - guard let metadata = self.dataSource.getMetadata(threadSafeResults: threadSafeResults, indexPath: indexPath) else { return } + 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) @@ -72,18 +72,28 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { } } - private func photoCell(cell: NCCellProtocol & UICollectionViewCell, indexPath: IndexPath, metadata: tableMetadata, ext: String) -> NCCellProtocol & UICollectionViewCell { + 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 } else if let image = utility.getImage(ocId: metadata.ocId, etag: metadata.etag, ext: ext) { - NCImageCache.shared.addImageCache(ocId: metadata.ocId, etag: metadata.etag, image: image, ext: ext, cost: indexPath.row) cell.filePreviewImageView?.image = image } + /// Content mode + /// + if cell.filePreviewImageView?.image != nil { + cell.filePreviewImageView?.contentMode = .scaleAspectFill + } else { + cell.filePreviewImageView?.contentMode = .scaleAspectFit + } + /// Default image /// if cell.filePreviewImageView?.image == nil { @@ -105,6 +115,9 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { if width < 100 { cell.hideButtonMore(true) cell.hideImageStatus(true) + } else { + cell.hideButtonMore(false) + cell.hideImageStatus(false) } return cell diff --git a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSourcePrefetching.swift b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSourcePrefetching.swift index 082b13145e..acbb578dd9 100644 --- a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSourcePrefetching.swift +++ b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSourcePrefetching.swift @@ -23,18 +23,17 @@ 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) { @@ -42,6 +41,5 @@ extension NCCollectionViewCommon: UICollectionViewDataSourcePrefetching { } } } - */ } } diff --git a/iOSClient/Main/Collection Common/NCCollectionViewDataSource.swift b/iOSClient/Main/Collection Common/NCCollectionViewDataSource.swift index 130e917977..ebfa5f6f65 100644 --- a/iOSClient/Main/Collection Common/NCCollectionViewDataSource.swift +++ b/iOSClient/Main/Collection Common/NCCollectionViewDataSource.swift @@ -177,20 +177,22 @@ class NCCollectionViewDataSource: NSObject { return results } - func getMetadata(threadSafeResults: ThreadSafeReference>, indexPath: IndexPath) -> tableMetadata? { + 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 } - if indexPath.row < validMetadatas.count { - return tableMetadata(value: validMetadatas[indexPath.row]) + 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 nil + return metadatas } } diff --git a/iOSClient/Media/NCMedia+CollectionViewDataSourcePrefetching.swift b/iOSClient/Media/NCMedia+CollectionViewDataSourcePrefetching.swift index 4cb1295c32..58455a2c1e 100644 --- a/iOSClient/Media/NCMedia+CollectionViewDataSourcePrefetching.swift +++ b/iOSClient/Media/NCMedia+CollectionViewDataSourcePrefetching.swift @@ -27,6 +27,8 @@ import UIKit extension NCMedia: UICollectionViewDataSourcePrefetching { func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath]) { guard !imageCache.isLoadingCache else { return } + /* + guard !imageCache.isLoadingCache else { return } let cost = indexPaths.first?.row ?? 0 let metadatas = self.dataSource.getMetadatas(indexPaths: indexPaths) @@ -48,5 +50,6 @@ extension NCMedia: UICollectionViewDataSourcePrefetching { } } } + */ } } 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 } From e390a8d487dd20d32d562feb0b16ef535e9271ea Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Tue, 1 Oct 2024 14:49:32 +0200 Subject: [PATCH 10/15] cod Signed-off-by: Marino Faggiana --- ...ctionViewCommon+CollectionViewDataSourcePrefetching.swift | 5 ++++- iOSClient/Media/NCMediaDataSource.swift | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSourcePrefetching.swift b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSourcePrefetching.swift index acbb578dd9..2347b6c056 100644 --- a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSourcePrefetching.swift +++ b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSourcePrefetching.swift @@ -28,7 +28,10 @@ import RealmSwift extension NCCollectionViewCommon: UICollectionViewDataSourcePrefetching { func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath]) { let ext = global.getSizeExtension(column: self.numberOfColumns) - guard !isSearchingMode, imageCache.allowExtensions(ext: ext), let results = self.dataSource.getResults() else { return } + guard !isSearchingMode, + imageCache.allowExtensions(ext: ext), + let results = self.dataSource.getResults() + else { return } let threadSafeResults = ThreadSafeReference(to: results) let cost = indexPaths.first?.row ?? 0 diff --git a/iOSClient/Media/NCMediaDataSource.swift b/iOSClient/Media/NCMediaDataSource.swift index efc8a87e37..f03a0de168 100644 --- a/iOSClient/Media/NCMediaDataSource.swift +++ b/iOSClient/Media/NCMediaDataSource.swift @@ -127,7 +127,7 @@ 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) From 3bf612a242b8e4c67ea94c5613c544ec0099a47e Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Tue, 1 Oct 2024 14:51:03 +0200 Subject: [PATCH 11/15] cod Signed-off-by: Marino Faggiana --- iOSClient/Networking/NCAutoUpload.swift | 4 ++-- iOSClient/Networking/NCNetworking+Download.swift | 4 ++-- iOSClient/Networking/NCNetworking+Upload.swift | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) 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, From e037ead9975e0cc1fe6382a43dd2aabc8f7de382 Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Tue, 1 Oct 2024 15:13:00 +0200 Subject: [PATCH 12/15] cod Signed-off-by: Marino Faggiana --- ...nViewCommon+CollectionViewDataSource.swift | 1 - ...+CollectionViewDataSourcePrefetching.swift | 28 ++++++------------- iOSClient/Media/NCMediaDataSource.swift | 7 +++-- 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift index 682390b447..49b7fa1122 100644 --- a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift +++ b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift @@ -250,7 +250,6 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { if let image = NCImageCache.shared.getImageCache(ocId: metadata.ocId, etag: metadata.etag, ext: ext) { cell.filePreviewImageView?.image = image } else if let image = utility.getImage(ocId: metadata.ocId, etag: metadata.etag, ext: ext) { - NCImageCache.shared.addImageCache(ocId: metadata.ocId, etag: metadata.etag, image: image, ext: ext, cost: indexPath.row) cell.filePreviewImageView?.image = image } diff --git a/iOSClient/Media/NCMedia+CollectionViewDataSourcePrefetching.swift b/iOSClient/Media/NCMedia+CollectionViewDataSourcePrefetching.swift index 58455a2c1e..49afb23989 100644 --- a/iOSClient/Media/NCMedia+CollectionViewDataSourcePrefetching.swift +++ b/iOSClient/Media/NCMedia+CollectionViewDataSourcePrefetching.swift @@ -26,30 +26,20 @@ import UIKit extension NCMedia: UICollectionViewDataSourcePrefetching { func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath]) { - guard !imageCache.isLoadingCache else { return } - /* - 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 f03a0de168..2ab1382f44 100644 --- a/iOSClient/Media/NCMediaDataSource.swift +++ b/iOSClient/Media/NCMediaDataSource.swift @@ -130,8 +130,11 @@ extension NCMedia { 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) From 94e201fd2f722699d3c536cbc387cdb438a394aa Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Tue, 1 Oct 2024 15:43:34 +0200 Subject: [PATCH 13/15] cleaning Signed-off-by: Marino Faggiana --- .../Collection Common/NCCollectionViewDownloadThumbnail.swift | 1 + 1 file changed, 1 insertion(+) 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, From 7bb6889299a364e9201c73ad30c79dd84a4114d9 Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Tue, 1 Oct 2024 15:51:29 +0200 Subject: [PATCH 14/15] cod Signed-off-by: Marino Faggiana --- .../NCCollectionViewCommon+CollectionViewDataSource.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift index 49b7fa1122..8591178146 100644 --- a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift +++ b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift @@ -84,6 +84,7 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { cell.filePreviewImageView?.image = image } else if let image = utility.getImage(ocId: metadata.ocId, etag: metadata.etag, ext: ext) { cell.filePreviewImageView?.image = image + self.imageCache.addImageCache(ocId: metadata.ocId, etag: metadata.etag, image: image, ext: ext, cost: indexPath.row) } /// Content mode From bf66fb220afc191dd930ab2dd65a1f062b4c5184 Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Tue, 1 Oct 2024 16:13:49 +0200 Subject: [PATCH 15/15] cod Signed-off-by: Marino Faggiana --- ...nViewCommon+CollectionViewDataSource.swift | 36 +++++++++---------- .../NCMedia+CollectionViewDataSource.swift | 2 +- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift index 8591178146..bce756d79a 100644 --- a/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift +++ b/iOSClient/Main/Collection Common/NCCollectionViewCommon+CollectionViewDataSource.swift @@ -82,26 +82,26 @@ extension NCCollectionViewCommon: UICollectionViewDataSource { /// if let image = NCImageCache.shared.getImageCache(ocId: metadata.ocId, etag: metadata.etag, ext: ext) { cell.filePreviewImageView?.image = image - } else if let image = utility.getImage(ocId: metadata.ocId, etag: metadata.etag, ext: ext) { - cell.filePreviewImageView?.image = image - self.imageCache.addImageCache(ocId: metadata.ocId, etag: metadata.etag, image: image, ext: ext, cost: indexPath.row) - } - - /// Content mode - /// - if cell.filePreviewImageView?.image != nil { cell.filePreviewImageView?.contentMode = .scaleAspectFill } else { - cell.filePreviewImageView?.contentMode = .scaleAspectFit - } - - /// Default image - /// - if cell.filePreviewImageView?.image == nil { - if metadata.iconName.isEmpty { - cell.filePreviewImageView?.image = NCImageCache.shared.getImageFile() - } else { - cell.filePreviewImageView?.image = utility.loadImage(named: metadata.iconName, useTypeIconFile: true, account: metadata.account) + 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) + } + } + } } } 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 } } }