NCDataSource.swift 15 KB

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