NCDataSource.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. //
  2. // NCDataSource.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 06/09/2020.
  6. // Copyright © 2020 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. import NCCommunication
  25. class NCDataSource: NSObject {
  26. public var metadatasSource: [tableMetadata] = []
  27. public var metadatasForSection: [NCMetadatasForSection] = []
  28. private var sectionsValue: [String] = []
  29. private var providers: [NCCSearchProvider]?
  30. private var shares: [tableShare] = []
  31. private var localFiles: [tableLocalFile] = []
  32. private var ascending: Bool = true
  33. private var sort: String = ""
  34. private var directoryOnTop: Bool = true
  35. private var favoriteOnTop: Bool = true
  36. private var filterLivePhoto: Bool = true
  37. private var groupByField: String = ""
  38. override init() {
  39. super.init()
  40. }
  41. init(metadatasSource: [tableMetadata], account: String, sort: String? = "none", ascending: Bool? = false, directoryOnTop: Bool? = true, favoriteOnTop: Bool? = true, filterLivePhoto: Bool? = true, groupByField: String = "name", providers: [NCCSearchProvider]? = nil) {
  42. super.init()
  43. self.metadatasSource = metadatasSource
  44. self.shares = NCManageDatabase.shared.getTableShares(account: account)
  45. self.localFiles = NCManageDatabase.shared.getTableLocalFile(account: account)
  46. self.sort = sort ?? "none"
  47. self.ascending = ascending ?? false
  48. self.directoryOnTop = directoryOnTop ?? true
  49. self.favoriteOnTop = favoriteOnTop ?? true
  50. self.filterLivePhoto = filterLivePhoto ?? true
  51. self.groupByField = groupByField
  52. self.providers = providers
  53. createSections()
  54. for sectionValue in self.sectionsValue {
  55. createMetadataForSection(sectionValue: sectionValue)
  56. }
  57. }
  58. // MARK: -
  59. func clearDataSource() {
  60. self.metadatasSource.removeAll()
  61. self.metadatasForSection.removeAll()
  62. self.sectionsValue.removeAll()
  63. }
  64. internal func createSections() {
  65. self.sectionsValue = metadatasSource.map { getSectionValue(metadata: $0) }
  66. self.sectionsValue = Array(Set(self.sectionsValue))
  67. if let providers = self.providers , !providers.isEmpty {
  68. var sectionsDictionary: [String:Int] = [:]
  69. for section in self.sectionsValue {
  70. if let provider = providers.filter({ $0.name.lowercased() == section.lowercased()}).first {
  71. sectionsDictionary[section] = provider.order
  72. }
  73. }
  74. self.sectionsValue.removeAll()
  75. let sectionsDictionarySorted = sectionsDictionary.sorted(by: { $0.value < $1.value } )
  76. for section in sectionsDictionarySorted {
  77. if section.key == NCGlobal.shared.appName {
  78. self.sectionsValue.insert(section.key, at: 0)
  79. } else {
  80. self.sectionsValue.append(section.key)
  81. }
  82. }
  83. } else {
  84. self.sectionsValue = self.sectionsValue.sorted {
  85. if directoryOnTop && $0.lowercased() == "directory" {
  86. return true
  87. }
  88. if self.ascending {
  89. return $0 < $1
  90. } else {
  91. return $0 > $1
  92. }
  93. }
  94. }
  95. }
  96. internal func createMetadataForSection(sectionValue: String) {
  97. let metadatas = metadatasSource.filter({ getSectionValue(metadata: $0) == sectionValue})
  98. let metadataForSection = NCMetadatasForSection.init(sectionValue: sectionValue,
  99. metadatas: metadatas,
  100. shares: self.shares,
  101. localFiles: self.localFiles,
  102. sort: self.sort,
  103. ascending: self.ascending,
  104. directoryOnTop: self.directoryOnTop,
  105. favoriteOnTop: self.favoriteOnTop,
  106. filterLivePhoto: self.filterLivePhoto)
  107. metadatasForSection.append(metadataForSection)
  108. }
  109. // MARK: -
  110. @discardableResult
  111. func addMetadata(_ metadata: tableMetadata) -> IndexPath? {
  112. // ADD metadatasSource
  113. if let rowIndex = self.metadatasSource.firstIndex(where: {$0.fileNameView == metadata.fileNameView || $0.ocId == metadata.ocId}) {
  114. self.metadatasSource[rowIndex] = metadata
  115. } else {
  116. self.metadatasSource.append(metadata)
  117. }
  118. // ADD metadataForSection
  119. if let sectionIndex = self.sectionsValue.firstIndex(where: {$0 == self.getSectionValue(metadata: metadata) }) {
  120. let metadataForSection = metadatasForSection[sectionIndex]
  121. if let rowIndex = metadataForSection.metadatas.firstIndex(where: {$0.fileNameView == metadata.fileNameView || $0.ocId == metadata.ocId}) {
  122. metadataForSection.metadatas[rowIndex] = metadata
  123. return IndexPath(row: rowIndex, section: sectionIndex)
  124. } else {
  125. metadataForSection.metadatas.append(metadata)
  126. metadataForSection.createMetadatasForSection()
  127. if let rowIndex = metadataForSection.metadatas.firstIndex(where: {$0.ocId == metadata.ocId}) {
  128. return IndexPath(row: rowIndex, section: sectionIndex)
  129. }
  130. return nil
  131. }
  132. } else {
  133. // NEW section
  134. createSections()
  135. let sectionValue = getSectionValue(metadata: metadata)
  136. createMetadataForSection(sectionValue: sectionValue)
  137. // get IndexPath of new section
  138. if let sectionIndex = self.sectionsValue.firstIndex(where: {$0 == sectionValue }) {
  139. let metadataForSection = metadatasForSection[sectionIndex]
  140. if let rowIndex = metadataForSection.metadatas.firstIndex(where: {$0.fileNameView == metadata.fileNameView || $0.ocId == metadata.ocId}) {
  141. return IndexPath(row: rowIndex, section: sectionIndex)
  142. }
  143. }
  144. }
  145. return nil
  146. }
  147. func deleteMetadata(ocId: String) -> IndexPath? {
  148. var indexPathReturn: IndexPath?
  149. var removeMetadataForSection = false
  150. var sectionValue = ""
  151. // DELETE metadataForSection (IMPORTANT FIRST)
  152. let (indexPath, metadataForSection) = self.getIndexPathMetadata(ocId: ocId)
  153. if let indexPath = indexPath, let metadataForSection = metadataForSection {
  154. metadataForSection.metadatas.remove(at: indexPath.row)
  155. if metadataForSection.metadatas.count == 0 {
  156. sectionValue = metadataForSection.sectionValue
  157. removeMetadataForSection = true
  158. } else {
  159. metadataForSection.createMetadatasForSection()
  160. }
  161. indexPathReturn = indexPath
  162. }
  163. // DELETE metadatasSource (IMPORTANT LAST)
  164. if let rowIndex = self.metadatasSource.firstIndex(where: {$0.ocId == ocId}) {
  165. self.metadatasSource.remove(at: rowIndex)
  166. }
  167. // REMOVE sectionsValue / metadatasForSection
  168. if removeMetadataForSection {
  169. if let index = self.sectionsValue.firstIndex(where: {$0 == sectionValue }) {
  170. self.sectionsValue.remove(at: index)
  171. }
  172. if let index = self.metadatasForSection.firstIndex(where: {$0.sectionValue == sectionValue }) {
  173. self.metadatasForSection.remove(at: index)
  174. }
  175. }
  176. return indexPathReturn
  177. }
  178. @discardableResult
  179. func reloadMetadata(ocId: String, ocIdTemp: String? = nil) -> IndexPath? {
  180. var ocIdSearch = ocId
  181. var indexPath: IndexPath?
  182. var metadataForSection: NCMetadatasForSection?
  183. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else { return nil }
  184. if let ocIdTemp = ocIdTemp {
  185. ocIdSearch = ocIdTemp
  186. }
  187. // UPDATE metadataForSection (IMPORTANT FIRST)
  188. (indexPath, metadataForSection) = self.getIndexPathMetadata(ocId: ocIdSearch)
  189. if let indexPath = indexPath, let metadataForSection = metadataForSection {
  190. metadataForSection.metadatas[indexPath.row] = metadata
  191. metadataForSection.createMetadatasForSection()
  192. }
  193. // UPDATE metadatasSource (IMPORTANT LAST)
  194. if let rowIndex = self.metadatasSource.firstIndex(where: {$0.ocId == ocIdSearch}) {
  195. self.metadatasSource[rowIndex] = metadata
  196. }
  197. return indexPath
  198. }
  199. // MARK: -
  200. func getIndexPathMetadata(ocId: String) -> (indexPath: IndexPath?, metadataForSection: NCMetadatasForSection?) {
  201. if let metadata = metadatasSource.filter({ $0.ocId == ocId}).first {
  202. let sectionValue = getSectionValue(metadata: metadata)
  203. if let sectionIndex = self.sectionsValue.firstIndex(where: {$0 == sectionValue}) {
  204. for metadataForSection in self.metadatasForSection {
  205. if metadataForSection.sectionValue == sectionValue {
  206. if let rowIndex = metadataForSection.metadatas.firstIndex(where: {$0.ocId == ocId}) {
  207. return (IndexPath(row: rowIndex, section: sectionIndex), metadataForSection)
  208. }
  209. }
  210. }
  211. }
  212. }
  213. return (nil, nil)
  214. }
  215. func numberOfSections() -> Int {
  216. if self.metadatasForSection.count == 0 {
  217. return 1
  218. } else {
  219. return self.metadatasForSection.count
  220. }
  221. }
  222. func numberOfItemsInSection(_ section: Int) -> Int {
  223. if self.metadatasForSection.count == 0 || self.metadatasSource.count == 0 { return 0 }
  224. return self.metadatasForSection[section].metadatas.count
  225. }
  226. func cellForItemAt(indexPath: IndexPath) -> tableMetadata? {
  227. let metadatasForSection = self.metadatasForSection[indexPath.section]
  228. return metadatasForSection.metadatas[indexPath.row]
  229. }
  230. func getSectionValue(indexPath: IndexPath) -> String {
  231. if metadatasForSection.count == 0 { return "" }
  232. let metadataForSection = self.metadatasForSection[indexPath.section]
  233. return metadataForSection.sectionValue
  234. }
  235. func getFooterInformation() -> (directories: Int, files: Int, size: Int64) {
  236. var directories: Int = 0
  237. var files: Int = 0
  238. var size: Int64 = 0
  239. for metadataForSection in metadatasForSection {
  240. directories += metadataForSection.numDirectory
  241. files += metadataForSection.numFile
  242. size += metadataForSection.totalSize
  243. }
  244. return (directories, files, size)
  245. }
  246. internal func getSectionValue(metadata: tableMetadata) -> String {
  247. switch self.groupByField {
  248. case "name":
  249. return metadata.name
  250. case "classFile":
  251. return metadata.classFile
  252. default:
  253. return metadata.name
  254. }
  255. }
  256. }
  257. class NCMetadatasForSection: NSObject {
  258. var sectionValue: String
  259. var metadatas: [tableMetadata]
  260. var shares: [tableShare]
  261. var localFiles: [tableLocalFile]
  262. private var sort : String
  263. private var ascending: Bool
  264. private var directoryOnTop: Bool
  265. private var favoriteOnTop: Bool
  266. private var filterLivePhoto: Bool
  267. private var metadatasSourceSorted: [tableMetadata] = []
  268. private var metadatasFavoriteDirectory: [tableMetadata] = []
  269. private var metadatasFavoriteFile: [tableMetadata] = []
  270. private var metadatasDirectory: [tableMetadata] = []
  271. private var metadatasFile: [tableMetadata] = []
  272. public var numDirectory: Int = 0
  273. public var numFile: Int = 0
  274. public var totalSize: Int64 = 0
  275. public var metadataShare: [String: tableShare] = [:]
  276. public var metadataOffLine: [String] = []
  277. init(sectionValue: String, metadatas: [tableMetadata], shares: [tableShare], localFiles: [tableLocalFile], sort: String, ascending: Bool, directoryOnTop: Bool, favoriteOnTop: Bool, filterLivePhoto: Bool) {
  278. self.sectionValue = sectionValue
  279. self.metadatas = metadatas
  280. self.shares = shares
  281. self.localFiles = localFiles
  282. self.sort = sort
  283. self.ascending = ascending
  284. self.directoryOnTop = directoryOnTop
  285. self.favoriteOnTop = favoriteOnTop
  286. self.filterLivePhoto = filterLivePhoto
  287. super.init()
  288. createMetadatasForSection()
  289. }
  290. func createMetadatasForSection() {
  291. // Clear
  292. //
  293. metadatasSourceSorted.removeAll()
  294. metadatasFavoriteDirectory.removeAll()
  295. metadatasFavoriteFile.removeAll()
  296. metadatasDirectory.removeAll()
  297. metadatasFile.removeAll()
  298. metadataShare.removeAll()
  299. metadataOffLine.removeAll()
  300. numDirectory = 0
  301. numFile = 0
  302. totalSize = 0
  303. // Metadata order
  304. //
  305. if sort != "none" && sort != "" {
  306. metadatasSourceSorted = metadatas.sorted {
  307. switch sort {
  308. case "date":
  309. if ascending {
  310. return ($0.date as Date) < ($1.date as Date)
  311. } else {
  312. return ($0.date as Date) > ($1.date as Date)
  313. }
  314. case "size":
  315. if ascending {
  316. return $0.size < $1.size
  317. } else {
  318. return $0.size > $1.size
  319. }
  320. default:
  321. if ascending {
  322. return $0.fileNameView.lowercased() < $1.fileNameView.lowercased()
  323. } else {
  324. return $0.fileNameView.lowercased() > $1.fileNameView.lowercased()
  325. }
  326. }
  327. }
  328. } else {
  329. metadatasSourceSorted = metadatas
  330. }
  331. // Initialize datasource
  332. //
  333. for metadata in metadatasSourceSorted {
  334. // skipped the root file
  335. if metadata.fileName == "." || metadata.serverUrl == ".." {
  336. continue
  337. }
  338. // skipped livePhoto
  339. if metadata.ext == "mov" && metadata.livePhoto && filterLivePhoto {
  340. continue
  341. }
  342. // share
  343. if let share = self.shares.filter({ $0.serverUrl == metadata.serverUrl && $0.fileName == metadata.fileName }).first {
  344. metadataShare[metadata.ocId] = share
  345. }
  346. // is Local / offline
  347. if !metadata.directory, CCUtility.fileProviderStorageExists(metadata) {
  348. let localFile = self.localFiles.filter({ $0.ocId == metadata.ocId }).first
  349. if localFile == nil {
  350. NCManageDatabase.shared.addLocalFile(metadata: metadata)
  351. }
  352. if localFile?.offline ?? false {
  353. metadataOffLine.append(metadata.ocId)
  354. }
  355. }
  356. // Organized the metadata
  357. if metadata.favorite && favoriteOnTop {
  358. if metadata.directory {
  359. metadatasFavoriteDirectory.append(metadata)
  360. } else {
  361. metadatasFavoriteFile.append(metadata)
  362. }
  363. } else if metadata.directory && directoryOnTop {
  364. metadatasDirectory.append(metadata)
  365. } else {
  366. metadatasFile.append(metadata)
  367. }
  368. //Info
  369. if metadata.directory {
  370. numDirectory += 1
  371. } else {
  372. numFile += 1
  373. totalSize += metadata.size
  374. }
  375. }
  376. metadatas.removeAll()
  377. // Struct view : favorite dir -> favorite file -> directory -> files
  378. metadatas += metadatasFavoriteDirectory
  379. metadatas += metadatasFavoriteFile
  380. metadatas += metadatasDirectory
  381. metadatas += metadatasFile
  382. }
  383. }