NCDataSource.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. 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. 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. let metadataForSection = self.metadatasForSection[indexPath.section]
  196. return metadataForSection.sectionValue
  197. }
  198. func getFooterInformation() -> (directories: Int, files: Int, size: Int64) {
  199. var directories: Int = 0
  200. var files: Int = 0
  201. var size: Int64 = 0
  202. for metadataForSection in metadatasForSection {
  203. directories += metadataForSection.numDirectory
  204. files += metadataForSection.numFile
  205. size += metadataForSection.totalSize
  206. }
  207. return (directories, files, size)
  208. }
  209. internal func getSectionValue(metadata: tableMetadata) -> String {
  210. switch self.groupByField {
  211. case "name":
  212. return metadata.name
  213. default:
  214. return metadata.name
  215. }
  216. }
  217. }
  218. class NCMetadatasForSection: NSObject {
  219. var sectionValue: String
  220. var metadatas: [tableMetadata]
  221. private var sort : String
  222. private var ascending: Bool
  223. private var directoryOnTop: Bool
  224. private var favoriteOnTop: Bool
  225. private var filterLivePhoto: Bool
  226. private var metadatasSourceSorted: [tableMetadata] = []
  227. private var metadatasFavoriteDirectory: [tableMetadata] = []
  228. private var metadatasFavoriteFile: [tableMetadata] = []
  229. private var metadatasDirectory: [tableMetadata] = []
  230. private var metadatasFile: [tableMetadata] = []
  231. public var numDirectory: Int = 0
  232. public var numFile: Int = 0
  233. public var totalSize: Int64 = 0
  234. public var metadataShare: [String: tableShare] = [:]
  235. public var metadataOffLine: [String] = []
  236. init(sectionValue: String, metadatas: [tableMetadata], sort: String, ascending: Bool, directoryOnTop: Bool, favoriteOnTop: Bool, filterLivePhoto: Bool) {
  237. self.sectionValue = sectionValue
  238. self.metadatas = metadatas
  239. self.sort = sort
  240. self.ascending = ascending
  241. self.directoryOnTop = directoryOnTop
  242. self.favoriteOnTop = favoriteOnTop
  243. self.filterLivePhoto = filterLivePhoto
  244. super.init()
  245. createMetadatasForSection()
  246. }
  247. func createMetadatasForSection() {
  248. // Clear
  249. //
  250. metadatasSourceSorted.removeAll()
  251. metadatasFavoriteDirectory.removeAll()
  252. metadatasFavoriteFile.removeAll()
  253. metadatasDirectory.removeAll()
  254. metadatasFile.removeAll()
  255. metadataShare.removeAll()
  256. metadataOffLine.removeAll()
  257. numDirectory = 0
  258. numFile = 0
  259. totalSize = 0
  260. // Metadata order
  261. //
  262. if sort != "none" && sort != "" {
  263. metadatasSourceSorted = metadatas.sorted {
  264. switch sort {
  265. case "date":
  266. if ascending {
  267. return ($0.date as Date) < ($1.date as Date)
  268. } else {
  269. return ($0.date as Date) > ($1.date as Date)
  270. }
  271. case "size":
  272. if ascending {
  273. return $0.size < $1.size
  274. } else {
  275. return $0.size > $1.size
  276. }
  277. default:
  278. if ascending {
  279. return $0.fileNameView.lowercased() < $1.fileNameView.lowercased()
  280. } else {
  281. return $0.fileNameView.lowercased() > $1.fileNameView.lowercased()
  282. }
  283. }
  284. }
  285. } else {
  286. metadatasSourceSorted = metadatas
  287. }
  288. // Initialize datasource
  289. //
  290. for metadata in metadatasSourceSorted {
  291. // skipped the root file
  292. if metadata.fileName == "." || metadata.serverUrl == ".." {
  293. continue
  294. }
  295. // skipped livePhoto
  296. if metadata.ext == "mov" && metadata.livePhoto && filterLivePhoto {
  297. continue
  298. }
  299. // share
  300. let shares = NCManageDatabase.shared.getTableShares(account: metadata.account, serverUrl: metadata.serverUrl, fileName: metadata.fileName)
  301. if shares.count > 0 {
  302. metadataShare[metadata.ocId] = shares.first
  303. }
  304. // is Local / offline
  305. if !metadata.directory, CCUtility.fileProviderStorageExists(metadata) {
  306. let tableLocalFile = NCManageDatabase.shared.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  307. if tableLocalFile == nil {
  308. NCManageDatabase.shared.addLocalFile(metadata: metadata)
  309. }
  310. if tableLocalFile?.offline ?? false {
  311. metadataOffLine.append(metadata.ocId)
  312. }
  313. }
  314. // Organized the metadata
  315. if metadata.favorite && favoriteOnTop {
  316. if metadata.directory {
  317. metadatasFavoriteDirectory.append(metadata)
  318. } else {
  319. metadatasFavoriteFile.append(metadata)
  320. }
  321. } else if metadata.directory && directoryOnTop {
  322. metadatasDirectory.append(metadata)
  323. } else {
  324. metadatasFile.append(metadata)
  325. }
  326. //Info
  327. if metadata.directory {
  328. numDirectory += 1
  329. } else {
  330. numFile += 1
  331. totalSize += metadata.size
  332. }
  333. }
  334. metadatas.removeAll()
  335. // Struct view : favorite dir -> favorite file -> directory -> files
  336. metadatas += metadatasFavoriteDirectory
  337. metadatas += metadatasFavoriteFile
  338. metadatas += metadatasDirectory
  339. metadatas += metadatasFile
  340. }
  341. }