Skip to content

Commit

Permalink
recommended files (#3264)
Browse files Browse the repository at this point in the history

Signed-off-by: Marino Faggiana <[email protected]>
  • Loading branch information
marinofaggiana authored Jan 10, 2025
1 parent eaf35c7 commit d1e4f0d
Show file tree
Hide file tree
Showing 18 changed files with 485 additions and 173 deletions.
22 changes: 17 additions & 5 deletions Nextcloud.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,8 @@
F75C0C4823D1FAE300163CC8 /* NCRichWorkspaceCommon.swift in Sources */ = {isa = PBXBuildFile; fileRef = F75C0C4723D1FAE300163CC8 /* NCRichWorkspaceCommon.swift */; };
F75CA1472962F13700B01130 /* NCHUDView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F75CA1462962F13700B01130 /* NCHUDView.swift */; };
F75D19E325EFE09000D74598 /* NCTrash+Menu.swift in Sources */ = {isa = PBXBuildFile; fileRef = F75D19E225EFE09000D74598 /* NCTrash+Menu.swift */; };
F75D901F2D2BE12E003E740B /* NCRecommendationsCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = F75D901E2D2BE12E003E740B /* NCRecommendationsCell.xib */; };
F75D90212D2BE26F003E740B /* NCRecommendationsCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = F75D90202D2BE26C003E740B /* NCRecommendationsCell.swift */; };
F75DD765290ABB25002EB562 /* Intent.intentdefinition in Sources */ = {isa = PBXBuildFile; fileRef = F75DD769290ABB25002EB562 /* Intent.intentdefinition */; };
F75DD766290ABB25002EB562 /* Intent.intentdefinition in Sources */ = {isa = PBXBuildFile; fileRef = F75DD769290ABB25002EB562 /* Intent.intentdefinition */; };
F75DD767290ABB25002EB562 /* Intent.intentdefinition in Sources */ = {isa = PBXBuildFile; fileRef = F75DD769290ABB25002EB562 /* Intent.intentdefinition */; };
Expand Down Expand Up @@ -757,6 +759,9 @@
F79B646326CA661600838ACA /* UIControl+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F79B645F26CA661600838ACA /* UIControl+Extension.swift */; };
F79B869B265E19D40085C0E0 /* NSMutableAttributedString+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F79B869A265E19D40085C0E0 /* NSMutableAttributedString+Extension.swift */; };
F79EC78926316AC4004E59D6 /* NCPopupViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F702F30725EE5D47008F8E80 /* NCPopupViewController.swift */; };
F79ED0F12D2FCA5B00A389D9 /* NCSectionFirstHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = F78ACD51219046DC0088454D /* NCSectionFirstHeader.swift */; };
F79ED0F22D2FCA6A00A389D9 /* NCRecommendationsCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = F75D901E2D2BE12E003E740B /* NCRecommendationsCell.xib */; };
F79ED0F32D2FCA7100A389D9 /* NCRecommendationsCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = F75D90202D2BE26C003E740B /* NCRecommendationsCell.swift */; };
F79EDAA326B004980007D134 /* NCPlayerToolBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = F79EDA9F26B004980007D134 /* NCPlayerToolBar.swift */; };
F79EDAA526B004980007D134 /* NCPlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = F79EDAA126B004980007D134 /* NCPlayer.swift */; };
F79FFB262A97C24A0055EEA4 /* NCNetworkingE2EEMarkFolder.swift in Sources */ = {isa = PBXBuildFile; fileRef = F79FFB252A97C24A0055EEA4 /* NCNetworkingE2EEMarkFolder.swift */; };
Expand Down Expand Up @@ -952,7 +957,6 @@
F7ED547C25EEA65400956C55 /* QRCodeReader in Frameworks */ = {isa = PBXBuildFile; productRef = F7ED547B25EEA65400956C55 /* QRCodeReader */; };
F7EDE4D6262D7B9600414FE6 /* NCListCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = F78ACD4121903CE00088454D /* NCListCell.swift */; };
F7EDE4DB262D7BA200414FE6 /* NCCellProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 370D26AE248A3D7A00121797 /* NCCellProtocol.swift */; };
F7EDE4E5262D7BBE00414FE6 /* NCSectionFirstHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = F78ACD51219046DC0088454D /* NCSectionFirstHeader.swift */; };
F7EDE509262DA9D600414FE6 /* NCSelectCommandViewSelect.xib in Resources */ = {isa = PBXBuildFile; fileRef = F7EDE508262DA9D600414FE6 /* NCSelectCommandViewSelect.xib */; };
F7EDE514262DC2CD00414FE6 /* NCSelectCommandViewSelect+CreateFolder.xib in Resources */ = {isa = PBXBuildFile; fileRef = F7EDE513262DC2CD00414FE6 /* NCSelectCommandViewSelect+CreateFolder.xib */; };
F7EDE51B262DD0C400414FE6 /* NCSelectCommandViewCopyMove.xib in Resources */ = {isa = PBXBuildFile; fileRef = F7EDE51A262DD0C400414FE6 /* NCSelectCommandViewCopyMove.xib */; };
Expand Down Expand Up @@ -1398,6 +1402,8 @@
F75C0C4723D1FAE300163CC8 /* NCRichWorkspaceCommon.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCRichWorkspaceCommon.swift; sourceTree = "<group>"; };
F75CA1462962F13700B01130 /* NCHUDView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCHUDView.swift; sourceTree = "<group>"; };
F75D19E225EFE09000D74598 /* NCTrash+Menu.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NCTrash+Menu.swift"; sourceTree = "<group>"; };
F75D901E2D2BE12E003E740B /* NCRecommendationsCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = NCRecommendationsCell.xib; sourceTree = "<group>"; };
F75D90202D2BE26C003E740B /* NCRecommendationsCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCRecommendationsCell.swift; sourceTree = "<group>"; };
F75DD768290ABB25002EB562 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.intentdefinition; name = Base; path = Base.lproj/Intent.intentdefinition; sourceTree = "<group>"; };
F760329D252F0F8E0015A421 /* NCTransferCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = NCTransferCell.swift; path = iOSClient/Transfers/NCTransferCell.swift; sourceTree = SOURCE_ROOT; };
F760329E252F0F8E0015A421 /* NCTransferCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; name = NCTransferCell.xib; path = iOSClient/Transfers/NCTransferCell.xib; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -2350,12 +2356,14 @@
isa = PBXGroup;
children = (
370D26AE248A3D7A00121797 /* NCCellProtocol.swift */,
F751247A2C42919C00E63DB8 /* NCPhotoCell.swift */,
F78ACD3F21903CC20088454D /* NCGridCell.swift */,
F78ACD4121903CE00088454D /* NCListCell.swift */,
F751247B2C42919C00E63DB8 /* NCPhotoCell.xib */,
F78ACD4521903D010088454D /* NCGridCell.xib */,
F78ACD4121903CE00088454D /* NCListCell.swift */,
F78ACD4321903CF20088454D /* NCListCell.xib */,
F751247A2C42919C00E63DB8 /* NCPhotoCell.swift */,
F751247B2C42919C00E63DB8 /* NCPhotoCell.xib */,
F75D901E2D2BE12E003E740B /* NCRecommendationsCell.xib */,
F75D90202D2BE26C003E740B /* NCRecommendationsCell.swift */,
);
path = Cell;
sourceTree = "<group>";
Expand Down Expand Up @@ -3736,6 +3744,7 @@
F76C26A62850D3A500E42BDF /* Images.xcassets in Resources */,
F714805E262ED52900693E51 /* NCSectionFooter.xib in Resources */,
F700222D1EC479840080073F /* Custom.xcassets in Resources */,
F79ED0F22D2FCA6A00A389D9 /* NCRecommendationsCell.xib in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -3790,6 +3799,7 @@
F7CEE6002BA9A5C9003EFD89 /* NCTrashGridCell.xib in Resources */,
3704EB2A23D5A58400455C5B /* NCMenu.storyboard in Resources */,
AF93471C27E2361E002537EE /* NCShareHeader.xib in Resources */,
F75D901F2D2BE12E003E740B /* NCRecommendationsCell.xib in Resources */,
F76032A0252F0F8E0015A421 /* NCTransferCell.xib in Resources */,
F7F4F10527ECDBDB008676F9 /* Inconsolata-SemiBold.ttf in Resources */,
F7A48415297028FC00BD1B49 /* Nextcloud Hub.png in Resources */,
Expand Down Expand Up @@ -4041,11 +4051,11 @@
F7864ACF2A78FE73004870E0 /* NCManageDatabase+LocalFile.swift in Sources */,
F71F6D0A2B6A6A5E00F1EB15 /* ThreadSafeArray.swift in Sources */,
F7245925289BB59100474787 /* ThreadSafeDictionary.swift in Sources */,
F7EDE4E5262D7BBE00414FE6 /* NCSectionFirstHeader.swift in Sources */,
F7BF9D852934CA21009EE9A6 /* NCManageDatabase+LayoutForView.swift in Sources */,
F79EC78926316AC4004E59D6 /* NCPopupViewController.swift in Sources */,
F7C30DFB291BCF790017149B /* NCNetworkingE2EECreateFolder.swift in Sources */,
F73EF7DA2B0226080087E6E9 /* NCManageDatabase+Tip.swift in Sources */,
F79ED0F32D2FCA7100A389D9 /* NCRecommendationsCell.swift in Sources */,
F7817CFB29801A3500FFBC65 /* Data+Extension.swift in Sources */,
F72429362AFE39860040AEF3 /* NCLivePhoto.swift in Sources */,
AA3494FF2CE65EB6005CC075 /* NCManageDatabase+DownloadLimit.swift in Sources */,
Expand All @@ -4066,6 +4076,7 @@
F359D86A2A7D03420023F405 /* NCUtility+Exif.swift in Sources */,
F7B769AB2B7A0B2000C1AAEB /* NCManageDatabase+Metadata+Session.swift in Sources */,
F7E98C1727E0D0FC001F9F19 /* NCManageDatabase+Video.swift in Sources */,
F79ED0F12D2FCA5B00A389D9 /* NCSectionFirstHeader.swift in Sources */,
F79B646126CA661600838ACA /* UIControl+Extension.swift in Sources */,
F77C973A2953143A00FDDD09 /* NCCameraRoll.swift in Sources */,
F740BEF02A35C2AD00E9B6D5 /* UILabel+Extension.swift in Sources */,
Expand Down Expand Up @@ -4503,6 +4514,7 @@
F310B1EF2BA862F1001C42F5 /* NCViewerMedia+VisionKit.swift in Sources */,
F702F2F725EE5CED008F8E80 /* NCLogin.swift in Sources */,
F7EB9B132BBC12F300EDF036 /* UIApplication+Extension.swift in Sources */,
F75D90212D2BE26F003E740B /* NCRecommendationsCell.swift in Sources */,
F7E98C1627E0D0FC001F9F19 /* NCManageDatabase+Video.swift in Sources */,
F7F4F11227ECDC52008676F9 /* UIFont+Extension.swift in Sources */,
AAF806B62CE34C7A009C2D43 /* NCShareDownloadLimitViewController.swift in Sources */,
Expand Down
3 changes: 2 additions & 1 deletion iOSClient/Data/NCManageDatabase+Capabilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,8 @@ extension NCManageDatabase {
capabilities.capabilityForbiddenFileNameCharacters = data.capabilities.files?.forbiddenFileNameCharacters ?? []
capabilities.capabilityForbiddenFileNameExtensions = data.capabilities.files?.forbiddenFileNameExtensions ?? []

capabilities.capabilityRecommendations = data.capabilities.recommendations?.enabled ?? false
//TODO: not yet available (IN TEST)

Check warning on line 401 in iOSClient/Data/NCManageDatabase+Capabilities.swift

View workflow job for this annotation

GitHub Actions / Lint

Comment Spacing Violation: Prefer at least one space after slashes for comments (comment_spacing)
//capabilities.capabilityRecommendations = data.capabilities.recommendations?.enabled ?? false

Check warning on line 402 in iOSClient/Data/NCManageDatabase+Capabilities.swift

View workflow job for this annotation

GitHub Actions / Lint

Comment Spacing Violation: Prefer at least one space after slashes for comments (comment_spacing)

NCCapabilities.shared.appendCapabilities(account: account, capabilities: capabilities)

Expand Down
26 changes: 5 additions & 21 deletions iOSClient/Data/NCManageDatabase+RecommendedFiles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,6 @@ import RealmSwift
import NextcloudKit

class tableRecommendedFiles: Object {
override func isEqual(_ object: Any?) -> Bool {
if let object = object as? tableRecommendedFiles,
self.timestamp == object.timestamp,
self.name == object.name,
self.directory == object.directory,
self.extensionType == object.extensionType,
self.mimeType == object.mimeType,
self.hasPreview == object.hasPreview,
self.reason == object.reason {
return true
} else {
return false
}
}

@Persisted var account = ""
@Persisted var id = ""
@Persisted(primaryKey: true) var primaryKey = ""
Expand Down Expand Up @@ -50,7 +35,7 @@ class tableRecommendedFiles: Object {
}

extension NCManageDatabase {
func addRecommendedFiles(account: String, recommendations: [NKRecommendation]) {
func createRecommendedFiles(account: String, recommendations: [NKRecommendation]) {
do {
let realm = try Realm()

Expand All @@ -71,17 +56,17 @@ extension NCManageDatabase {
}
}

func getResultsRecommendedFiles(account: String) -> Results<tableRecommendedFiles>? {
func getResultsRecommendedFiles(account: String) -> [tableRecommendedFiles] {
do {
let realm = try Realm()
let results = realm.objects(tableRecommendedFiles.self).filter("account == %@", account)

return results
return Array(results)
} catch let error {
NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
}

return nil
return []
}

func getNKRecommendation(account: String) -> [NKRecommendation] {
Expand All @@ -94,7 +79,6 @@ extension NCManageDatabase {
for result in results {
let recommendation = NKRecommendation(id: result.id, timestamp: result.timestamp, name: result.name, directory: result.directory, extensionType: result.extensionType, mimeType: result.mimeType, hasPreview: result.hasPreview, reason: result.reason)
recommendations.append(recommendation)

}
} catch let error {
NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
Expand Down Expand Up @@ -134,7 +118,7 @@ extension NCManageDatabase {
for (primaryKey, existingObject) in existingDictionary {
if let newObject = newDictionary[primaryKey] {
// If exists, verify if is changed
if !existingObject.isEqual(newObject) {
if existingObject.timestamp != newObject.timestamp {
changed.append(newObject)
}
} else {
Expand Down
86 changes: 71 additions & 15 deletions iOSClient/Files/NCFiles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import RealmSwift
import SwiftUI

class NCFiles: NCCollectionViewCommon {
internal var isRoot: Bool = true
internal var fileNameBlink: String?
internal var fileNameOpen: String?
internal var matadatasHash: String = ""
Expand All @@ -50,7 +49,14 @@ class NCFiles: NCCollectionViewCommon {
override func viewDidLoad() {
super.viewDidLoad()

if isRoot {
if self.serverUrl.isEmpty {

///
/// Set ServerURL when start (isEmpty)
///
self.serverUrl = utilityFileSystem.getHomeServer(session: session)
self.titleCurrentFolder = getNavigationTitle()

NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeUser), object: nil, queue: nil) { notification in

if let userInfo = notification.userInfo, let account = userInfo["account"] as? String {
Expand Down Expand Up @@ -88,10 +94,6 @@ class NCFiles: NCCollectionViewCommon {
}

override func viewWillAppear(_ animated: Bool) {
if isRoot {
serverUrl = utilityFileSystem.getHomeServer(session: session)
titleCurrentFolder = getNavigationTitle()
}
super.viewWillAppear(animated)

reloadDataSource()
Expand Down Expand Up @@ -127,7 +129,7 @@ class NCFiles: NCCollectionViewCommon {
return super.reloadDataSource()
}

// This is only a fail safe "dead lock", I don't think the timeout will ever be called but at least nothing gets stuck, if after 5 sec. (which is a long time in this routine), the semaphore is still locked
// Watchdog: this is only a fail safe "dead lock", I don't think the timeout will ever be called but at least nothing gets stuck, if after 5 sec. (which is a long time in this routine), the semaphore is still locked
//
if self.semaphoreReloadDataSource.wait(timeout: .now() + 5) == .timedOut {
self.semaphoreReloadDataSource.signal()
Expand Down Expand Up @@ -184,13 +186,25 @@ class NCFiles: NCCollectionViewCommon {
return false
}

DispatchQueue.global().async {
if self.isRoot,
NCCapabilities.shared.getCapabilities(account: self.session.account).capabilityRecommendations {
NextcloudKit.shared.getRecommendedFiles(account: self.session.account) { account, recommendations, responseData, error in

///
/// Recommended files
///
if self.serverUrl == self.utilityFileSystem.getHomeServer(session: self.session),
NCCapabilities.shared.getCapabilities(account: self.session.account).capabilityRecommendations {
let options = NKRequestOptions(queue: NextcloudKit.shared.nkCommonInstance.backgroundQueue)

NextcloudKit.shared.getRecommendedFiles(account: self.session.account, options: options) { _, recommendations, _, error in
if error == .success,
let recommendations,
!recommendations.isEmpty {
Task.detached {
await self.createRecommendations(recommendations)
}
}
}
}

DispatchQueue.global().async {
self.networkReadFolder { metadatas, isChanged, error in
DispatchQueue.main.async {
self.refreshControl.endRefreshing()
Expand Down Expand Up @@ -352,6 +366,48 @@ class NCFiles: NCCollectionViewCommon {
}
}

private func createRecommendations(_ recommendations: [NKRecommendation]) async {
let home = self.utilityFileSystem.getHomeServer(session: self.session)
var recommendationsToInsert: [NKRecommendation] = []

for recommendation in recommendations {
var metadata = database.getResultMetadataFromFileId(recommendation.id)
if metadata == nil || metadata?.fileName != recommendation.name {
let serverUrlFileName = home + recommendation.directory + recommendation.name
let results = await NCNetworking.shared.readFileOrFolder(serverUrlFileName: serverUrlFileName, depth: "0", showHiddenFiles: NCKeychain().showHiddenFiles, account: session.account)

if results.error == .success, let file = results.files?.first {
let isDirectoryE2EE = self.utilityFileSystem.isDirectoryE2EE(file: file)
let metadataConverted = self.database.convertFileToMetadata(file, isDirectoryE2EE: isDirectoryE2EE)
metadata = metadataConverted

self.database.addMetadata(metadataConverted)
recommendationsToInsert.append(recommendation)
}
} else {
recommendationsToInsert.append(recommendation)
}

if let metadata,
recommendation.hasPreview,
!self.utilityFileSystem.fileProviderStorageImageExists(metadata.ocId, etag: metadata.etag) {
let result = await NCNetworking.shared.downloadPreview(fileId: metadata.fileId, account: session.account)

if result.error == .success, let data = result.responseData?.data {
self.utility.createImageFileFrom(data: data, ocId: metadata.ocId, etag: metadata.etag)
}
}
}

let results = self.database.compareRecommendations(account: session.account, newObjects: recommendationsToInsert)
if results.added.count + results.changed.count + results.deleted.count > 0 {
self.database.createRecommendedFiles(account: session.account, recommendations: recommendationsToInsert)
Task { @MainActor in
self.collectionView.reloadData()
}
}
}

// MARK: - NCAccountSettingsModelDelegate

override func accountSettingsDidDismiss(tableAccount: tableAccount?, controller: NCMainTabBarController?) {
Expand All @@ -361,9 +417,9 @@ class NCFiles: NCCollectionViewCommon {
appDelegate.openLogin(selector: NCGlobal.shared.introLogin)
} else if let account = tableAccount?.account, account != currentAccount {
NCAccount().changeAccount(account, userProfile: nil, controller: controller) { }
} else if isRoot {
titleCurrentFolder = getNavigationTitle()
navigationItem.title = titleCurrentFolder
} else if self.serverUrl == self.utilityFileSystem.getHomeServer(session: self.session) {
self.titleCurrentFolder = getNavigationTitle()
navigationItem.title = self.titleCurrentFolder
}

setNavigationLeftItems()
Expand Down
Loading

0 comments on commit d1e4f0d

Please sign in to comment.