NCDataSource.swift 15 KB

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