|
@@ -25,7 +25,9 @@ import UIKit
|
|
|
|
|
|
class NCDataSource: NSObject {
|
|
|
|
|
|
- public var metadatas: [tableMetadata] = []
|
|
|
+ public var metadatasForSection: [NCMetadatasForSection] = []
|
|
|
+
|
|
|
+ public var metadatasSource: [tableMetadata] = []
|
|
|
public var metadataShare: [String: tableShare] = [:]
|
|
|
public var metadataOffLine: [String] = []
|
|
|
public var sections: [String] = []
|
|
@@ -41,10 +43,10 @@ class NCDataSource: NSObject {
|
|
|
super.init()
|
|
|
}
|
|
|
|
|
|
- init(metadatas: [tableMetadata], sort: String? = "none", ascending: Bool? = false, directoryOnTop: Bool? = true, favoriteOnTop: Bool? = true, filterLivePhoto: Bool? = true, groupByField: String = "name") {
|
|
|
+ init(metadatasSource: [tableMetadata], sort: String? = "none", ascending: Bool? = false, directoryOnTop: Bool? = true, favoriteOnTop: Bool? = true, filterLivePhoto: Bool? = true, groupByField: String = "name") {
|
|
|
super.init()
|
|
|
|
|
|
- self.metadatas = metadatas
|
|
|
+ self.metadatasSource = metadatasSource
|
|
|
self.sort = sort ?? "none"
|
|
|
self.ascending = ascending ?? false
|
|
|
self.directoryOnTop = directoryOnTop ?? true
|
|
@@ -52,117 +54,22 @@ class NCDataSource: NSObject {
|
|
|
self.filterLivePhoto = filterLivePhoto ?? true
|
|
|
self.groupByField = groupByField
|
|
|
|
|
|
- createMetadatas()
|
|
|
- }
|
|
|
-
|
|
|
- // MARK: -
|
|
|
-
|
|
|
- @discardableResult
|
|
|
- func createMetadatas() -> [tableMetadata] {
|
|
|
-
|
|
|
- var metadatasSourceSorted: [tableMetadata] = []
|
|
|
- var metadataFavoriteDirectory: [tableMetadata] = []
|
|
|
- var metadataFavoriteFile: [tableMetadata] = []
|
|
|
- var metadataDirectory: [tableMetadata] = []
|
|
|
- var metadataFile: [tableMetadata] = []
|
|
|
-
|
|
|
- /*
|
|
|
- Metadata order
|
|
|
- */
|
|
|
-
|
|
|
- if sort != "none" && sort != "" {
|
|
|
- metadatasSourceSorted = self.metadatas.sorted {
|
|
|
-
|
|
|
- switch sort {
|
|
|
- case "date":
|
|
|
- if ascending {
|
|
|
- return (getSectionField(metadata:$0), ($0.date as Date)) < (getSectionField(metadata:$1), ($1.date as Date))
|
|
|
- } else {
|
|
|
- return (getSectionField(metadata:$0), ($0.date as Date)) > (getSectionField(metadata:$1), ($1.date as Date))
|
|
|
- }
|
|
|
- case "size":
|
|
|
- if ascending {
|
|
|
- return (getSectionField(metadata:$0), $0.size) < (getSectionField(metadata:$1), $1.size)
|
|
|
- } else {
|
|
|
- return (getSectionField(metadata:$0), $0.size) > (getSectionField(metadata:$1), $1.size)
|
|
|
- }
|
|
|
- default:
|
|
|
- if ascending {
|
|
|
- return (getSectionField(metadata:$0), $0.fileNameView.lowercased()) < (getSectionField(metadata:$1), $1.fileNameView.lowercased())
|
|
|
- } else {
|
|
|
- return (getSectionField(metadata:$0), $0.fileNameView.lowercased()) > (getSectionField(metadata:$1), $1.fileNameView.lowercased())
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- metadatasSourceSorted = self.metadatas.sorted {
|
|
|
- (getSectionField(metadata:$0)) < (getSectionField(metadata:$1))
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /*
|
|
|
- Initialize datasource
|
|
|
- */
|
|
|
-
|
|
|
- for metadata in metadatasSourceSorted {
|
|
|
-
|
|
|
- // skipped the root file
|
|
|
- if metadata.fileName == "." || metadata.serverUrl == ".." {
|
|
|
- continue
|
|
|
- }
|
|
|
-
|
|
|
- // skipped livePhoto
|
|
|
- if metadata.ext == "mov" && metadata.livePhoto && filterLivePhoto {
|
|
|
- continue
|
|
|
- }
|
|
|
-
|
|
|
- // share
|
|
|
- let shares = NCManageDatabase.shared.getTableShares(account: metadata.account, serverUrl: metadata.serverUrl, fileName: metadata.fileName)
|
|
|
- if shares.count > 0 {
|
|
|
- metadataShare[metadata.ocId] = shares.first
|
|
|
- }
|
|
|
-
|
|
|
- // is Local / offline
|
|
|
- if !metadata.directory, CCUtility.fileProviderStorageExists(metadata) {
|
|
|
- let tableLocalFile = NCManageDatabase.shared.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
|
|
|
- if tableLocalFile == nil {
|
|
|
- NCManageDatabase.shared.addLocalFile(metadata: metadata)
|
|
|
- }
|
|
|
- if tableLocalFile?.offline ?? false {
|
|
|
- metadataOffLine.append(metadata.ocId)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // Organized the metadata
|
|
|
- if metadata.favorite && favoriteOnTop {
|
|
|
- if metadata.directory {
|
|
|
- metadataFavoriteDirectory.append(metadata)
|
|
|
- } else {
|
|
|
- metadataFavoriteFile.append(metadata)
|
|
|
- }
|
|
|
- } else if metadata.directory && directoryOnTop {
|
|
|
- metadataDirectory.append(metadata)
|
|
|
+ // Create sections && sorted
|
|
|
+ self.sections = self.metadatasSource.map { getSectionField(metadata: $0) }
|
|
|
+ self.sections = self.sections.sorted {
|
|
|
+ if self.ascending {
|
|
|
+ return $0 < $1
|
|
|
} else {
|
|
|
- metadataFile.append(metadata)
|
|
|
- }
|
|
|
-
|
|
|
- // sections
|
|
|
- let section = getSectionField(metadata:metadata)
|
|
|
- if !self.sections.contains(section) {
|
|
|
- self.sections.append(section)
|
|
|
+ return $0 > $1
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- self.metadatas.removeAll()
|
|
|
-
|
|
|
- // Struct view : favorite dir -> favorite file -> directory -> files
|
|
|
- // TODO: for sections, this is for 1 sections
|
|
|
- self.metadatas += metadataFavoriteDirectory
|
|
|
- self.metadatas += metadataFavoriteFile
|
|
|
- self.metadatas += metadataDirectory
|
|
|
- self.metadatas += metadataFile
|
|
|
-
|
|
|
- return self.metadatas
|
|
|
+ // Create metadataForSection
|
|
|
+ for section in self.sections {
|
|
|
+ let metadatas = self.metadatasSource.filter({ getSectionField(metadata: $0) == section})
|
|
|
+ let metadataForSection = NCMetadatasForSection.init(section: section, metadatas: metadatas, sort: self.sort, ascending: self.ascending, directoryOnTop: self.directoryOnTop, favoriteOnTop: self.favoriteOnTop, filterLivePhoto: self.filterLivePhoto)
|
|
|
+ metadatasForSection.append(metadataForSection)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// MARK: -
|
|
@@ -173,7 +80,7 @@ class NCDataSource: NSObject {
|
|
|
var files: Int = 0
|
|
|
var size: Int64 = 0
|
|
|
|
|
|
- for metadata in self.metadatas {
|
|
|
+ for metadata in self.metadatasSource {
|
|
|
if metadata.directory {
|
|
|
directories += 1
|
|
|
} else {
|
|
@@ -188,7 +95,7 @@ class NCDataSource: NSObject {
|
|
|
func deleteMetadata(ocId: String) -> IndexPath? {
|
|
|
|
|
|
if let indexPath = self.getIndexPathMetadata(ocId: ocId) {
|
|
|
- self.metadatas.remove(at: indexPath.row)
|
|
|
+ self.metadatasSource.remove(at: indexPath.row)
|
|
|
return indexPath
|
|
|
}
|
|
|
|
|
@@ -207,7 +114,7 @@ class NCDataSource: NSObject {
|
|
|
}
|
|
|
|
|
|
guard let indexPath = indexPath, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else { return nil }
|
|
|
- self.metadatas[indexPath.row] = metadata
|
|
|
+ self.metadatasSource[indexPath.row] = metadata
|
|
|
|
|
|
if CCUtility.fileProviderStorageExists(metadata) {
|
|
|
let tableLocalFile = NCManageDatabase.shared.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
|
|
@@ -225,9 +132,9 @@ class NCDataSource: NSObject {
|
|
|
var row: Int = 0
|
|
|
|
|
|
// Already exists
|
|
|
- for metadataCount in self.metadatas {
|
|
|
+ for metadataCount in self.metadatasSource {
|
|
|
if metadataCount.fileNameView == metadata.fileNameView || metadataCount.ocId == metadata.ocId {
|
|
|
- metadatas[row] = metadata
|
|
|
+ self.metadatasSource[row] = metadata
|
|
|
let section = getSection(metadata: metadata)
|
|
|
return IndexPath(row: row, section: section)
|
|
|
}
|
|
@@ -235,8 +142,8 @@ class NCDataSource: NSObject {
|
|
|
}
|
|
|
|
|
|
// Append & rebuild
|
|
|
- metadatas.append(metadata)
|
|
|
- createMetadatas()
|
|
|
+ self.metadatasSource.append(metadata)
|
|
|
+ //createMetadatas()
|
|
|
|
|
|
return getIndexPathMetadata(ocId: metadata.ocId)
|
|
|
}
|
|
@@ -245,7 +152,7 @@ class NCDataSource: NSObject {
|
|
|
|
|
|
var row: Int = 0
|
|
|
|
|
|
- for metadata in self.metadatas {
|
|
|
+ for metadata in self.metadatasSource {
|
|
|
if metadata.ocId == ocId {
|
|
|
let section = getSection(metadata: metadata)
|
|
|
return IndexPath(row: row, section: section)
|
|
@@ -267,9 +174,9 @@ class NCDataSource: NSObject {
|
|
|
|
|
|
func numberOfItemsInSection(_ section: Int) -> Int {
|
|
|
|
|
|
- if self.sections.count == 0 || self.metadatas.count == 0 { return 0 }
|
|
|
+ if self.sections.count == 0 || self.metadatasSource.count == 0 { return 0 }
|
|
|
let sectionName = self.sections[section]
|
|
|
- let metadatas = self.metadatas.filter({ getSectionField(metadata: $0) == sectionName})
|
|
|
+ let metadatas = self.metadatasSource.filter({ getSectionField(metadata: $0) == sectionName})
|
|
|
|
|
|
return metadatas.count
|
|
|
}
|
|
@@ -278,7 +185,7 @@ class NCDataSource: NSObject {
|
|
|
|
|
|
let row = indexPath.row
|
|
|
let sectionName = self.sections[indexPath.section]
|
|
|
- let metadatas = self.metadatas.filter({ getSectionField(metadata: $0) == sectionName})
|
|
|
+ let metadatas = self.metadatasSource.filter({ getSectionField(metadata: $0) == sectionName})
|
|
|
|
|
|
if row > metadatas.count - 1 {
|
|
|
return nil
|
|
@@ -303,3 +210,128 @@ class NCDataSource: NSObject {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+class NCMetadatasForSection: NSObject {
|
|
|
+
|
|
|
+ var section: String
|
|
|
+ var metadatas: [tableMetadata]
|
|
|
+
|
|
|
+ var sort : String
|
|
|
+ var ascending: Bool
|
|
|
+ var directoryOnTop: Bool
|
|
|
+ var favoriteOnTop: Bool
|
|
|
+ var filterLivePhoto: Bool
|
|
|
+
|
|
|
+ var metadatasSourceSorted: [tableMetadata] = []
|
|
|
+ var metadatasFavoriteDirectory: [tableMetadata] = []
|
|
|
+ var metadatasFavoriteFile: [tableMetadata] = []
|
|
|
+ var metadatasDirectory: [tableMetadata] = []
|
|
|
+ var metadatasFile: [tableMetadata] = []
|
|
|
+ var metadataShare: [String: tableShare] = [:]
|
|
|
+ var metadataOffLine: [String] = []
|
|
|
+
|
|
|
+ init(section: String, metadatas: [tableMetadata], sort: String, ascending: Bool, directoryOnTop: Bool, favoriteOnTop: Bool, filterLivePhoto: Bool) {
|
|
|
+
|
|
|
+ self.section = section
|
|
|
+ self.metadatas = metadatas
|
|
|
+ self.sort = sort
|
|
|
+ self.ascending = ascending
|
|
|
+ self.directoryOnTop = directoryOnTop
|
|
|
+ self.favoriteOnTop = favoriteOnTop
|
|
|
+ self.filterLivePhoto = filterLivePhoto
|
|
|
+
|
|
|
+ super.init()
|
|
|
+
|
|
|
+ createMetadatasForSection()
|
|
|
+ }
|
|
|
+
|
|
|
+ private func createMetadatasForSection() {
|
|
|
+
|
|
|
+ /*
|
|
|
+ Metadata order
|
|
|
+ */
|
|
|
+
|
|
|
+ if sort != "none" && sort != "" {
|
|
|
+ metadatasSourceSorted = metadatas.sorted {
|
|
|
+
|
|
|
+ switch sort {
|
|
|
+ case "date":
|
|
|
+ if ascending {
|
|
|
+ return ($0.date as Date) < ($1.date as Date)
|
|
|
+ } else {
|
|
|
+ return ($0.date as Date) > ($1.date as Date)
|
|
|
+ }
|
|
|
+ case "size":
|
|
|
+ if ascending {
|
|
|
+ return $0.size < $1.size
|
|
|
+ } else {
|
|
|
+ return $0.size > $1.size
|
|
|
+ }
|
|
|
+ default:
|
|
|
+ if ascending {
|
|
|
+ return $0.fileNameView.lowercased() < $1.fileNameView.lowercased()
|
|
|
+ } else {
|
|
|
+ return $0.fileNameView.lowercased() > $1.fileNameView.lowercased()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ metadatasSourceSorted = metadatas
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ Initialize datasource
|
|
|
+ */
|
|
|
+
|
|
|
+ for metadata in metadatasSourceSorted {
|
|
|
+
|
|
|
+ // skipped the root file
|
|
|
+ if metadata.fileName == "." || metadata.serverUrl == ".." {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ // skipped livePhoto
|
|
|
+ if metadata.ext == "mov" && metadata.livePhoto && filterLivePhoto {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ // share
|
|
|
+ let shares = NCManageDatabase.shared.getTableShares(account: metadata.account, serverUrl: metadata.serverUrl, fileName: metadata.fileName)
|
|
|
+ if shares.count > 0 {
|
|
|
+ metadataShare[metadata.ocId] = shares.first
|
|
|
+ }
|
|
|
+
|
|
|
+ // is Local / offline
|
|
|
+ if !metadata.directory, CCUtility.fileProviderStorageExists(metadata) {
|
|
|
+ let tableLocalFile = NCManageDatabase.shared.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
|
|
|
+ if tableLocalFile == nil {
|
|
|
+ NCManageDatabase.shared.addLocalFile(metadata: metadata)
|
|
|
+ }
|
|
|
+ if tableLocalFile?.offline ?? false {
|
|
|
+ metadataOffLine.append(metadata.ocId)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Organized the metadata
|
|
|
+ if metadata.favorite && favoriteOnTop {
|
|
|
+ if metadata.directory {
|
|
|
+ metadatasFavoriteDirectory.append(metadata)
|
|
|
+ } else {
|
|
|
+ metadatasFavoriteFile.append(metadata)
|
|
|
+ }
|
|
|
+ } else if metadata.directory && directoryOnTop {
|
|
|
+ metadatasDirectory.append(metadata)
|
|
|
+ } else {
|
|
|
+ metadatasFile.append(metadata)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ metadatas.removeAll()
|
|
|
+
|
|
|
+ // Struct view : favorite dir -> favorite file -> directory -> files
|
|
|
+ metadatas += metadatasFavoriteDirectory
|
|
|
+ metadatas += metadatasFavoriteFile
|
|
|
+ metadatas += metadatasDirectory
|
|
|
+ metadatas += metadatasFile
|
|
|
+ }
|
|
|
+}
|