NCDataSource.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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 ascending: Bool = true
  31. private var sort: String = ""
  32. private var directoryOnTop: Bool = true
  33. private var favoriteOnTop: Bool = true
  34. private var filterLivePhoto: Bool = true
  35. private var groupByField: String = ""
  36. override init() {
  37. super.init()
  38. }
  39. init(metadatasSource: [tableMetadata], sort: String? = "none", ascending: Bool? = false, directoryOnTop: Bool? = true, favoriteOnTop: Bool? = true, filterLivePhoto: Bool? = true, groupByField: String = "name", providers: [NCCSearchProvider]? = nil) {
  40. super.init()
  41. self.metadatasSource = metadatasSource
  42. self.sort = sort ?? "none"
  43. self.ascending = ascending ?? false
  44. self.directoryOnTop = directoryOnTop ?? true
  45. self.favoriteOnTop = favoriteOnTop ?? true
  46. self.filterLivePhoto = filterLivePhoto ?? true
  47. self.groupByField = groupByField
  48. self.providers = providers
  49. createSections()
  50. for sectionValue in self.sectionsValue {
  51. createMetadataForSection(sectionValue: sectionValue)
  52. }
  53. }
  54. // MARK: -
  55. func clearDataSource() {
  56. self.metadatasSource.removeAll()
  57. self.metadatasForSection.removeAll()
  58. self.sectionsValue.removeAll()
  59. }
  60. internal func createSections() {
  61. self.sectionsValue = metadatasSource.map { getSectionValue(metadata: $0) }
  62. self.sectionsValue = Array(Set(self.sectionsValue))
  63. self.sectionsValue = self.sectionsValue.sorted {
  64. if self.ascending {
  65. return $0 < $1
  66. } else {
  67. return $0 > $1
  68. }
  69. }
  70. /* TEST
  71. if let providers = self.providers {
  72. var sectionsDictionary: [String:Int] = [:]
  73. for section in self.sectionsValue {
  74. if let provider = providers.filter({ $0.name.lowercased() == section.lowercased()}).first {
  75. sectionsDictionary[section] = provider.order
  76. }
  77. }
  78. self.sectionsValue.removeAll()
  79. let sectionsDictionarySorted = sectionsDictionary.sorted(by: { $0.value > $1.value } )
  80. for section in sectionsDictionarySorted { self.sectionsValue.append(section.key) }
  81. } else {
  82. self.sectionsValue = self.sectionsValue.sorted {
  83. if self.ascending {
  84. return $0 < $1
  85. } else {
  86. return $0 > $1
  87. }
  88. }
  89. }
  90. */
  91. }
  92. internal func createMetadataForSection(sectionValue: String) {
  93. let metadatas = metadatasSource.filter({ getSectionValue(metadata: $0) == sectionValue})
  94. let metadataForSection = NCMetadatasForSection.init(sectionValue: sectionValue, metadatas: metadatas, sort: self.sort, ascending: self.ascending, directoryOnTop: self.directoryOnTop, favoriteOnTop: self.favoriteOnTop, filterLivePhoto: self.filterLivePhoto)
  95. metadatasForSection.append(metadataForSection)
  96. }
  97. // MARK: -
  98. @discardableResult
  99. func addMetadata(_ metadata: tableMetadata) -> IndexPath? {
  100. // ADD metadatasSource
  101. if let rowIndex = self.metadatasSource.firstIndex(where: {$0.fileNameView == metadata.fileNameView || $0.ocId == metadata.ocId}) {
  102. self.metadatasSource[rowIndex] = metadata
  103. } else {
  104. self.metadatasSource.append(metadata)
  105. }
  106. // ADD metadataForSection
  107. if let sectionIndex = self.sectionsValue.firstIndex(where: {$0 == self.getSectionValue(metadata: metadata) }) {
  108. let metadataForSection = metadatasForSection[sectionIndex]
  109. if let rowIndex = metadataForSection.metadatas.firstIndex(where: {$0.fileNameView == metadata.fileNameView || $0.ocId == metadata.ocId}) {
  110. metadataForSection.metadatas[rowIndex] = metadata
  111. return IndexPath(row: rowIndex, section: sectionIndex)
  112. } else {
  113. metadataForSection.metadatas.append(metadata)
  114. metadataForSection.createMetadatasForSection()
  115. if let rowIndex = metadataForSection.metadatas.firstIndex(where: {$0.ocId == metadata.ocId}) {
  116. return IndexPath(row: rowIndex, section: sectionIndex)
  117. }
  118. return nil
  119. }
  120. } else {
  121. // NEW section
  122. createSections()
  123. let sectionValue = getSectionValue(metadata: metadata)
  124. createMetadataForSection(sectionValue: sectionValue)
  125. // get IndexPath of new section
  126. if let sectionIndex = self.sectionsValue.firstIndex(where: {$0 == sectionValue }) {
  127. let metadataForSection = metadatasForSection[sectionIndex]
  128. if let rowIndex = metadataForSection.metadatas.firstIndex(where: {$0.fileNameView == metadata.fileNameView || $0.ocId == metadata.ocId}) {
  129. return IndexPath(row: rowIndex, section: sectionIndex)
  130. }
  131. }
  132. }
  133. return nil
  134. }
  135. func deleteMetadata(ocId: String) -> IndexPath? {
  136. var indexPathReturn: IndexPath?
  137. var removeMetadataForSection = false
  138. var sectionValue = ""
  139. // DELETE metadataForSection (IMPORTANT FIRST)
  140. let (indexPath, metadataForSection) = self.getIndexPathMetadata(ocId: ocId)
  141. if let indexPath = indexPath, let metadataForSection = metadataForSection {
  142. metadataForSection.metadatas.remove(at: indexPath.row)
  143. if metadataForSection.metadatas.count == 0 {
  144. sectionValue = metadataForSection.sectionValue
  145. removeMetadataForSection = true
  146. } else {
  147. metadataForSection.createMetadatasForSection()
  148. }
  149. indexPathReturn = indexPath
  150. }
  151. // DELETE metadatasSource (IMPORTANT LAST)
  152. if let rowIndex = self.metadatasSource.firstIndex(where: {$0.ocId == ocId}) {
  153. self.metadatasSource.remove(at: rowIndex)
  154. }
  155. // REMOVE sectionsValue / metadatasForSection
  156. if removeMetadataForSection {
  157. if let index = self.sectionsValue.firstIndex(where: {$0 == sectionValue }) {
  158. self.sectionsValue.remove(at: index)
  159. }
  160. if let index = self.metadatasForSection.firstIndex(where: {$0.sectionValue == sectionValue }) {
  161. self.metadatasForSection.remove(at: index)
  162. }
  163. }
  164. return indexPathReturn
  165. }
  166. @discardableResult
  167. func reloadMetadata(ocId: String, ocIdTemp: String? = nil) -> IndexPath? {
  168. var ocIdSearch = ocId
  169. var indexPath: IndexPath?
  170. var metadataForSection: NCMetadatasForSection?
  171. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else { return nil }
  172. if let ocIdTemp = ocIdTemp {
  173. ocIdSearch = ocIdTemp
  174. }
  175. // UPDATE metadataForSection (IMPORTANT FIRST)
  176. (indexPath, metadataForSection) = self.getIndexPathMetadata(ocId: ocIdSearch)
  177. if let indexPath = indexPath, let metadataForSection = metadataForSection {
  178. metadataForSection.metadatas[indexPath.row] = metadata
  179. metadataForSection.createMetadatasForSection()
  180. }
  181. // UPDATE metadatasSource (IMPORTANT LAST)
  182. if let rowIndex = self.metadatasSource.firstIndex(where: {$0.ocId == ocIdSearch}) {
  183. self.metadatasSource[rowIndex] = metadata
  184. }
  185. return indexPath
  186. }
  187. // MARK: -
  188. func getIndexPathMetadata(ocId: String) -> (indexPath: IndexPath?, metadataForSection: NCMetadatasForSection?) {
  189. if let metadata = metadatasSource.filter({ $0.ocId == ocId}).first {
  190. let sectionValue = getSectionValue(metadata: metadata)
  191. if let sectionIndex = self.sectionsValue.firstIndex(where: {$0 == sectionValue}) {
  192. for metadataForSection in self.metadatasForSection {
  193. if metadataForSection.sectionValue == sectionValue {
  194. if let rowIndex = metadataForSection.metadatas.firstIndex(where: {$0.ocId == ocId}) {
  195. return (IndexPath(row: rowIndex, section: sectionIndex), metadataForSection)
  196. }
  197. }
  198. }
  199. }
  200. }
  201. return (nil, nil)
  202. }
  203. func numberOfSections() -> Int {
  204. if self.metadatasForSection.count == 0 {
  205. return 1
  206. } else {
  207. return self.metadatasForSection.count
  208. }
  209. }
  210. func numberOfItemsInSection(_ section: Int) -> Int {
  211. if self.metadatasForSection.count == 0 || self.metadatasSource.count == 0 { return 0 }
  212. return self.metadatasForSection[section].metadatas.count
  213. }
  214. func cellForItemAt(indexPath: IndexPath) -> tableMetadata? {
  215. let metadatasForSection = self.metadatasForSection[indexPath.section]
  216. return metadatasForSection.metadatas[indexPath.row]
  217. }
  218. func getSectionValue(indexPath: IndexPath) -> String {
  219. if metadatasForSection.count == 0 { return "" }
  220. let metadataForSection = self.metadatasForSection[indexPath.section]
  221. return metadataForSection.sectionValue
  222. }
  223. func getFooterInformation() -> (directories: Int, files: Int, size: Int64) {
  224. var directories: Int = 0
  225. var files: Int = 0
  226. var size: Int64 = 0
  227. for metadataForSection in metadatasForSection {
  228. directories += metadataForSection.numDirectory
  229. files += metadataForSection.numFile
  230. size += metadataForSection.totalSize
  231. }
  232. return (directories, files, size)
  233. }
  234. internal func getSectionValue(metadata: tableMetadata) -> String {
  235. switch self.groupByField {
  236. case "name":
  237. return metadata.name
  238. default:
  239. return metadata.name
  240. }
  241. }
  242. }
  243. class NCMetadatasForSection: NSObject {
  244. var sectionValue: String
  245. var metadatas: [tableMetadata]
  246. private var sort : String
  247. private var ascending: Bool
  248. private var directoryOnTop: Bool
  249. private var favoriteOnTop: Bool
  250. private var filterLivePhoto: Bool
  251. private var metadatasSourceSorted: [tableMetadata] = []
  252. private var metadatasFavoriteDirectory: [tableMetadata] = []
  253. private var metadatasFavoriteFile: [tableMetadata] = []
  254. private var metadatasDirectory: [tableMetadata] = []
  255. private var metadatasFile: [tableMetadata] = []
  256. public var numDirectory: Int = 0
  257. public var numFile: Int = 0
  258. public var totalSize: Int64 = 0
  259. public var metadataShare: [String: tableShare] = [:]
  260. public var metadataOffLine: [String] = []
  261. init(sectionValue: String, metadatas: [tableMetadata], sort: String, ascending: Bool, directoryOnTop: Bool, favoriteOnTop: Bool, filterLivePhoto: Bool) {
  262. self.sectionValue = sectionValue
  263. self.metadatas = metadatas
  264. self.sort = sort
  265. self.ascending = ascending
  266. self.directoryOnTop = directoryOnTop
  267. self.favoriteOnTop = favoriteOnTop
  268. self.filterLivePhoto = filterLivePhoto
  269. super.init()
  270. createMetadatasForSection()
  271. }
  272. func createMetadatasForSection() {
  273. // Clear
  274. //
  275. metadatasSourceSorted.removeAll()
  276. metadatasFavoriteDirectory.removeAll()
  277. metadatasFavoriteFile.removeAll()
  278. metadatasDirectory.removeAll()
  279. metadatasFile.removeAll()
  280. metadataShare.removeAll()
  281. metadataOffLine.removeAll()
  282. numDirectory = 0
  283. numFile = 0
  284. totalSize = 0
  285. // Metadata order
  286. //
  287. if sort != "none" && sort != "" {
  288. metadatasSourceSorted = metadatas.sorted {
  289. switch sort {
  290. case "date":
  291. if ascending {
  292. return ($0.date as Date) < ($1.date as Date)
  293. } else {
  294. return ($0.date as Date) > ($1.date as Date)
  295. }
  296. case "size":
  297. if ascending {
  298. return $0.size < $1.size
  299. } else {
  300. return $0.size > $1.size
  301. }
  302. default:
  303. if ascending {
  304. return $0.fileNameView.lowercased() < $1.fileNameView.lowercased()
  305. } else {
  306. return $0.fileNameView.lowercased() > $1.fileNameView.lowercased()
  307. }
  308. }
  309. }
  310. } else {
  311. metadatasSourceSorted = metadatas
  312. }
  313. // Initialize datasource
  314. //
  315. for metadata in metadatasSourceSorted {
  316. // skipped the root file
  317. if metadata.fileName == "." || metadata.serverUrl == ".." {
  318. continue
  319. }
  320. // skipped livePhoto
  321. if metadata.ext == "mov" && metadata.livePhoto && filterLivePhoto {
  322. continue
  323. }
  324. // share
  325. let shares = NCManageDatabase.shared.getTableShares(account: metadata.account, serverUrl: metadata.serverUrl, fileName: metadata.fileName)
  326. if shares.count > 0 {
  327. metadataShare[metadata.ocId] = shares.first
  328. }
  329. // is Local / offline
  330. if !metadata.directory, CCUtility.fileProviderStorageExists(metadata) {
  331. let tableLocalFile = NCManageDatabase.shared.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  332. if tableLocalFile == nil {
  333. NCManageDatabase.shared.addLocalFile(metadata: metadata)
  334. }
  335. if tableLocalFile?.offline ?? false {
  336. metadataOffLine.append(metadata.ocId)
  337. }
  338. }
  339. // Organized the metadata
  340. if metadata.favorite && favoriteOnTop {
  341. if metadata.directory {
  342. metadatasFavoriteDirectory.append(metadata)
  343. } else {
  344. metadatasFavoriteFile.append(metadata)
  345. }
  346. } else if metadata.directory && directoryOnTop {
  347. metadatasDirectory.append(metadata)
  348. } else {
  349. metadatasFile.append(metadata)
  350. }
  351. //Info
  352. if metadata.directory {
  353. numDirectory += 1
  354. } else {
  355. numFile += 1
  356. totalSize += metadata.size
  357. }
  358. }
  359. metadatas.removeAll()
  360. // Struct view : favorite dir -> favorite file -> directory -> files
  361. metadatas += metadatasFavoriteDirectory
  362. metadatas += metadatasFavoriteFile
  363. metadatas += metadatasDirectory
  364. metadatas += metadatasFile
  365. }
  366. }