NCDataSource.swift 15 KB

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