NCDataSource.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. self.sectionsValue = self.sectionsValue.sorted {
  68. if self.ascending {
  69. return $0 < $1
  70. } else {
  71. return $0 > $1
  72. }
  73. }
  74. /* TEST
  75. if let providers = self.providers {
  76. var sectionsDictionary: [String:Int] = [:]
  77. for section in self.sectionsValue {
  78. if let provider = providers.filter({ $0.name.lowercased() == section.lowercased()}).first {
  79. sectionsDictionary[section] = provider.order
  80. }
  81. }
  82. self.sectionsValue.removeAll()
  83. let sectionsDictionarySorted = sectionsDictionary.sorted(by: { $0.value > $1.value } )
  84. for section in sectionsDictionarySorted { self.sectionsValue.append(section.key) }
  85. } else {
  86. self.sectionsValue = self.sectionsValue.sorted {
  87. if self.ascending {
  88. return $0 < $1
  89. } else {
  90. return $0 > $1
  91. }
  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. default:
  251. return metadata.name
  252. }
  253. }
  254. }
  255. class NCMetadatasForSection: NSObject {
  256. var sectionValue: String
  257. var metadatas: [tableMetadata]
  258. var shares: [tableShare]
  259. var localFiles: [tableLocalFile]
  260. private var sort : String
  261. private var ascending: Bool
  262. private var directoryOnTop: Bool
  263. private var favoriteOnTop: Bool
  264. private var filterLivePhoto: Bool
  265. private var metadatasSourceSorted: [tableMetadata] = []
  266. private var metadatasFavoriteDirectory: [tableMetadata] = []
  267. private var metadatasFavoriteFile: [tableMetadata] = []
  268. private var metadatasDirectory: [tableMetadata] = []
  269. private var metadatasFile: [tableMetadata] = []
  270. public var numDirectory: Int = 0
  271. public var numFile: Int = 0
  272. public var totalSize: Int64 = 0
  273. public var metadataShare: [String: tableShare] = [:]
  274. public var metadataOffLine: [String] = []
  275. init(sectionValue: String, metadatas: [tableMetadata], shares: [tableShare], localFiles: [tableLocalFile], sort: String, ascending: Bool, directoryOnTop: Bool, favoriteOnTop: Bool, filterLivePhoto: Bool) {
  276. self.sectionValue = sectionValue
  277. self.metadatas = metadatas
  278. self.shares = shares
  279. self.localFiles = localFiles
  280. self.sort = sort
  281. self.ascending = ascending
  282. self.directoryOnTop = directoryOnTop
  283. self.favoriteOnTop = favoriteOnTop
  284. self.filterLivePhoto = filterLivePhoto
  285. super.init()
  286. createMetadatasForSection()
  287. }
  288. func createMetadatasForSection() {
  289. // Clear
  290. //
  291. metadatasSourceSorted.removeAll()
  292. metadatasFavoriteDirectory.removeAll()
  293. metadatasFavoriteFile.removeAll()
  294. metadatasDirectory.removeAll()
  295. metadatasFile.removeAll()
  296. metadataShare.removeAll()
  297. metadataOffLine.removeAll()
  298. numDirectory = 0
  299. numFile = 0
  300. totalSize = 0
  301. // Metadata order
  302. //
  303. if sort != "none" && sort != "" {
  304. metadatasSourceSorted = metadatas.sorted {
  305. switch sort {
  306. case "date":
  307. if ascending {
  308. return ($0.date as Date) < ($1.date as Date)
  309. } else {
  310. return ($0.date as Date) > ($1.date as Date)
  311. }
  312. case "size":
  313. if ascending {
  314. return $0.size < $1.size
  315. } else {
  316. return $0.size > $1.size
  317. }
  318. default:
  319. if ascending {
  320. return $0.fileNameView.lowercased() < $1.fileNameView.lowercased()
  321. } else {
  322. return $0.fileNameView.lowercased() > $1.fileNameView.lowercased()
  323. }
  324. }
  325. }
  326. } else {
  327. metadatasSourceSorted = metadatas
  328. }
  329. // Initialize datasource
  330. //
  331. for metadata in metadatasSourceSorted {
  332. // skipped the root file
  333. if metadata.fileName == "." || metadata.serverUrl == ".." {
  334. continue
  335. }
  336. // skipped livePhoto
  337. if metadata.ext == "mov" && metadata.livePhoto && filterLivePhoto {
  338. continue
  339. }
  340. // share
  341. if let share = self.shares.filter({ $0.serverUrl == metadata.serverUrl && $0.fileName == metadata.fileName }).first {
  342. metadataShare[metadata.ocId] = share
  343. }
  344. // is Local / offline
  345. if !metadata.directory, CCUtility.fileProviderStorageExists(metadata) {
  346. let localFile = self.localFiles.filter({ $0.ocId == metadata.ocId }).first
  347. if localFile == nil {
  348. NCManageDatabase.shared.addLocalFile(metadata: metadata)
  349. }
  350. if localFile?.offline ?? false {
  351. metadataOffLine.append(metadata.ocId)
  352. }
  353. }
  354. // Organized the metadata
  355. if metadata.favorite && favoriteOnTop {
  356. if metadata.directory {
  357. metadatasFavoriteDirectory.append(metadata)
  358. } else {
  359. metadatasFavoriteFile.append(metadata)
  360. }
  361. } else if metadata.directory && directoryOnTop {
  362. metadatasDirectory.append(metadata)
  363. } else {
  364. metadatasFile.append(metadata)
  365. }
  366. //Info
  367. if metadata.directory {
  368. numDirectory += 1
  369. } else {
  370. numFile += 1
  371. totalSize += metadata.size
  372. }
  373. }
  374. metadatas.removeAll()
  375. // Struct view : favorite dir -> favorite file -> directory -> files
  376. metadatas += metadatasFavoriteDirectory
  377. metadatas += metadatasFavoriteFile
  378. metadatas += metadatasDirectory
  379. metadatas += metadatasFile
  380. }
  381. }