NCDataSource.swift 14 KB

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