NCDataSource.swift 14 KB

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