Skip to content

Commit

Permalink
Layout (#3083)
Browse files Browse the repository at this point in the history
* added invalidate()



---------

Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
  • Loading branch information
marinofaggiana authored Sep 30, 2024
1 parent 5798532 commit dce3a39
Show file tree
Hide file tree
Showing 25 changed files with 196 additions and 178 deletions.
7 changes: 6 additions & 1 deletion iOSClient/Data/NCManageDatabase+Metadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ extension NCManageDatabase {

do {
let realm = try Realm()
let groupfolders = realm.objects(TableGroupfolders.self).filter("account == %@", session.account)
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
Expand Down Expand Up @@ -1073,4 +1073,9 @@ extension NCManageDatabase {
}
return nil
}

func getCalculateCumulativeHash(for metadatas: [tableMetadata], account: String, serverUrl: String) -> String {
let concatenatedEtags = metadatas.map { $0.etag }.joined(separator: "-")
return sha256Hash(concatenatedEtags)
}
}
10 changes: 10 additions & 0 deletions iOSClient/Data/NCManageDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import RealmSwift
import NextcloudKit
import CoreMedia
import Photos
import CommonCrypto

protocol DateCompareable {
var dateKey: Date { get }
Expand Down Expand Up @@ -230,6 +231,15 @@ class NCManageDatabase: NSObject {
}
}

func sha256Hash(_ input: String) -> String {
let data = Data(input.utf8)
var digest = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
data.withUnsafeBytes {
_ = CC_SHA256($0.baseAddress, CC_LONG(data.count), &digest)
}
return digest.map { String(format: "%02hhx", $0) }.joined()
}

// MARK: -
// MARK: Func T

Expand Down
26 changes: 14 additions & 12 deletions iOSClient/Favorites/NCFavorite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,18 @@ class NCFavorite: NCCollectionViewCommon {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

if self.dataSource.isEmpty() {
reloadDataSource()
}
reloadDataSourceNetwork()
reloadDataSource()
}

// MARK: - DataSource + NC Endpoint
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

getServerData()
}

override func queryDB() {
super.queryDB()
// MARK: - DataSource

override func reloadDataSource() {
var predicate = self.defaultPredicate

if self.serverUrl.isEmpty {
Expand All @@ -62,10 +64,12 @@ class NCFavorite: NCCollectionViewCommon {

let metadatas = self.database.getResultsMetadatasPredicate(predicate, layoutForView: layoutForView)
self.dataSource = NCCollectionViewDataSource(metadatas: metadatas, layoutForView: layoutForView)

super.reloadDataSource()
}

override func reloadDataSourceNetwork(withQueryDB: Bool = false) {
super.reloadDataSourceNetwork()
override func getServerData() {
super.getServerData()

NextcloudKit.shared.listingFavorites(showHiddenFiles: NCKeychain().showHiddenFiles, account: session.account) { task in
self.dataSourceTask = task
Expand All @@ -74,10 +78,8 @@ class NCFavorite: NCCollectionViewCommon {
if error == .success, let files {
self.database.convertFilesToMetadatas(files, useFirstAsMetadataFolder: false) { _, metadatas in
self.database.updateMetadatasFavorite(account: account, metadatas: metadatas)
self.reloadDataSource()
}
} else {
self.reloadDataSource(withQueryDB: withQueryDB)
self.reloadDataSource()
}
}
}
Expand Down
38 changes: 18 additions & 20 deletions iOSClient/Files/NCFiles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class NCFiles: NCCollectionViewCommon {
internal var isRoot: Bool = true
internal var fileNameBlink: String?
internal var fileNameOpen: String?
internal var matadatasHash: String = ""

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
Expand Down Expand Up @@ -77,7 +78,7 @@ class NCFiles: NCCollectionViewCommon {
self.setNavigationLeftItems()

self.reloadDataSource()
self.reloadDataSourceNetwork()
self.getServerData()
}
}
}
Expand All @@ -89,10 +90,7 @@ class NCFiles: NCCollectionViewCommon {
}
super.viewWillAppear(animated)

if self.dataSource.isEmpty() {
reloadDataSource(withQueryDB: true)
}
reloadDataSourceNetwork(withQueryDB: true)
reloadDataSource()
}

override func viewDidAppear(_ animated: Bool) {
Expand All @@ -104,6 +102,10 @@ class NCFiles: NCCollectionViewCommon {
self.fileNameBlink = nil
self.fileNameOpen = nil
}

if !isSearchingMode {
getServerData()
}
}

override func viewDidDisappear(_ animated: Bool) {
Expand All @@ -113,10 +115,9 @@ class NCFiles: NCCollectionViewCommon {
fileNameOpen = nil
}

// MARK: - DataSource + NC Endpoint
// MARK: - DataSource

override func queryDB() {
super.queryDB()
override func reloadDataSource() {
self.dataSource.removeAll()

var predicate = self.defaultPredicate
Expand All @@ -131,9 +132,13 @@ class NCFiles: NCCollectionViewCommon {

let metadatas = self.database.getResultsMetadatasPredicate(predicate, layoutForView: layoutForView)
self.dataSource = NCCollectionViewDataSource(metadatas: metadatas, layoutForView: layoutForView)

super.reloadDataSource()
}

override func reloadDataSourceNetwork(withQueryDB: Bool = false) {
override func getServerData() {
super.getServerData()

if UIApplication.shared.applicationState == .background {
NextcloudKit.shared.nkCommonInstance.writeLog("[DEBUG] Files not reload datasource network with the application in background")
return
Expand All @@ -151,12 +156,9 @@ class NCFiles: NCCollectionViewCommon {
return true
}
}

return false
}

super.reloadDataSourceNetwork()

DispatchQueue.global(qos: .background).async {
self.networkReadFolder { tableDirectory, metadatas, reloadDataSource, error in
DispatchQueue.main.async {
Expand All @@ -170,19 +172,18 @@ class NCFiles: NCCollectionViewCommon {
self.richWorkspaceText = tableDirectory?.richWorkspace

if reloadDataSource {
(self.collectionView.collectionViewLayout as? NCMediaLayout)?.invalidate()
self.reloadDataSource()
} else {
} else if self.dataSource.isEmpty() {
self.collectionView.reloadData()
}
} else {
self.reloadDataSource(withQueryDB: withQueryDB)
}
}
}
}
}

private func networkReadFolder(completion: @escaping (_ tableDirectory: tableDirectory?, _ metadatas: [tableMetadata]?, _ reloadDataSource: Bool, _ error: NKError) -> Void) {
private func networkReadFolder(completion: @escaping (_ tableDirectory: tableDirectory?, _ metadatas: [tableMetadata]?, _ isEtagChanged: Bool, _ error: NKError) -> Void) {
var tableDirectory: tableDirectory?

NCNetworking.shared.readFile(serverUrlFileName: serverUrl, account: session.account) { task in
Expand All @@ -208,14 +209,14 @@ class NCFiles: NCCollectionViewCommon {
}
self.metadataFolder = metadataFolder

/// E2EE
guard let metadataFolder = metadataFolder,
metadataFolder.e2eEncrypted,
NCKeychain().isEndToEndEnabled(account: account),
!NCNetworkingE2EE().isInUpload(account: account, serverUrl: self.serverUrl) else {
return completion(tableDirectory, metadatas, true, error)
}

/// E2EE
let lock = self.database.getE2ETokenLock(account: account, serverUrl: self.serverUrl)
NCNetworkingE2EE().getMetadata(fileId: metadataFolder.ocId, e2eToken: lock?.e2eToken, account: account) { account, version, e2eMetadata, signature, _, error in

Expand All @@ -233,10 +234,7 @@ class NCFiles: NCCollectionViewCommon {
NCContentPresenter().showError(error: error)
}
NCActivityIndicator.shared.stop()
self.reloadDataSource()
}
} else {
self.reloadDataSource()
}
} else {
// Client Diagnostic
Expand Down
40 changes: 21 additions & 19 deletions iOSClient/Groupfolders/NCGroupfolders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,18 @@ class NCGroupfolders: NCCollectionViewCommon {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

if self.dataSource.isEmpty() {
reloadDataSource()
}
reloadDataSourceNetwork()
reloadDataSource()
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

getServerData()
}

// MARK: - DataSource + NC Endpoint
// MARK: - DataSource

override func queryDB() {
super.queryDB()
override func reloadDataSource() {
var metadatas: [tableMetadata] = []

if self.serverUrl.isEmpty {
Expand All @@ -63,10 +65,13 @@ class NCGroupfolders: NCCollectionViewCommon {
}

self.dataSource = NCCollectionViewDataSource(metadatas: metadatas, layoutForView: layoutForView)

super.reloadDataSource()
}

override func reloadDataSourceNetwork(withQueryDB: Bool = false) {
super.reloadDataSourceNetwork()
override func getServerData() {
super.getServerData()

let homeServerUrl = utilityFileSystem.getHomeServer(session: session)

NextcloudKit.shared.getGroupfolders(account: session.account) { task in
Expand All @@ -79,20 +84,17 @@ class NCGroupfolders: NCCollectionViewCommon {
for groupfolder in groupfolders {
let mountPoint = groupfolder.mountPoint.hasPrefix("/") ? groupfolder.mountPoint : "/" + groupfolder.mountPoint
let serverUrlFileName = homeServerUrl + mountPoint
if self.database.getMetadataFromDirectory(account: account, serverUrl: serverUrlFileName) {
let results = await NCNetworking.shared.readFileOrFolder(serverUrlFileName: serverUrlFileName, depth: "0", showHiddenFiles: NCKeychain().showHiddenFiles, account: account)
if results.error == .success, let file = results.files?.first {
let isDirectoryE2EE = self.utilityFileSystem.isDirectoryE2EE(file: file)
let metadata = self.database.convertFileToMetadata(file, isDirectoryE2EE: isDirectoryE2EE)
self.database.addMetadata(metadata)
self.database.addDirectory(e2eEncrypted: isDirectoryE2EE, favorite: metadata.favorite, ocId: metadata.ocId, fileId: metadata.fileId, permissions: metadata.permissions, serverUrl: serverUrlFileName, account: metadata.account)
}
let results = await NCNetworking.shared.readFileOrFolder(serverUrlFileName: serverUrlFileName, depth: "0", showHiddenFiles: NCKeychain().showHiddenFiles, account: account)

if results.error == .success, let file = results.files?.first {
let isDirectoryE2EE = self.utilityFileSystem.isDirectoryE2EE(file: file)
let metadata = self.database.convertFileToMetadata(file, isDirectoryE2EE: isDirectoryE2EE)
self.database.addMetadata(metadata)
self.database.addDirectory(e2eEncrypted: isDirectoryE2EE, favorite: metadata.favorite, ocId: metadata.ocId, fileId: metadata.fileId, permissions: metadata.permissions, serverUrl: serverUrlFileName, account: metadata.account)
}
}
self.reloadDataSource()
}
} else {
self.reloadDataSource(withQueryDB: withQueryDB)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ extension NCCollectionViewCommon: UICollectionViewDataSource {
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// get auto upload folder
self.autoUploadFileName = self.database.getAccountAutoUploadFileName()
self.autoUploadDirectory = self.database.getAccountAutoUploadDirectory(session: self.session)
// get layout for view
self.layoutForView = self.database.getLayoutForView(account: self.session.account, key: self.layoutKey, serverUrl: self.serverUrl)

return self.dataSource.numberOfItemsInSection(section)
}

Expand Down
Loading

0 comments on commit dce3a39

Please sign in to comment.