NCDataSource.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 Foundation
  24. class NCDataSource: NSObject {
  25. public var metadatas: [tableMetadata] = []
  26. public var metadataShare: [String:tableShare] = [:]
  27. public var metadataLocalImage: [String:String] = [:]
  28. private var directoryOnTop: Bool = true
  29. private var favoriteOnTop: Bool = true
  30. private var filterLivePhoto: Bool = true
  31. override init() {
  32. super.init()
  33. }
  34. init(metadatasSource: [tableMetadata], directoryOnTop: Bool, favoriteOnTop: Bool, filterLivePhoto: Bool) {
  35. super.init()
  36. self.directoryOnTop = directoryOnTop
  37. self.favoriteOnTop = favoriteOnTop
  38. self.filterLivePhoto = filterLivePhoto
  39. createMetadatas(metadatasSource: metadatasSource)
  40. }
  41. // MARK: -
  42. private func createMetadatas(metadatasSource: [tableMetadata]) {
  43. var metadataFavoriteDirectory: [tableMetadata] = []
  44. var metadataFavoriteFile: [tableMetadata] = []
  45. var metadataDirectory: [tableMetadata] = []
  46. var metadataFile: [tableMetadata] = []
  47. for metadata in metadatasSource {
  48. // skipped the root file
  49. if metadata.fileName == "." || metadata.serverUrl == ".." {
  50. continue
  51. }
  52. // skipped livePhoto
  53. if metadata.ext == "mov" && metadata.livePhoto && filterLivePhoto {
  54. continue
  55. }
  56. // share
  57. let shares = NCManageDatabase.sharedInstance.getTableShares(account: metadata.account, serverUrl: metadata.serverUrl, fileName: metadata.fileName)
  58. if shares.count > 0 {
  59. metadataShare[metadata.ocId] = shares.first
  60. }
  61. // is Local / offline
  62. if !metadata.directory {
  63. let size = CCUtility.fileProviderStorageSize(metadata.ocId, fileNameView: metadata.fileNameView)
  64. if size > 0 {
  65. let tableLocalFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  66. if tableLocalFile == nil && size == metadata.size {
  67. NCManageDatabase.sharedInstance.addLocalFile(metadata: metadata)
  68. }
  69. if tableLocalFile?.offline ?? false {
  70. metadataLocalImage[metadata.ocId] = "offlineFlag"
  71. } else {
  72. metadataLocalImage[metadata.ocId] = "local"
  73. }
  74. }
  75. }
  76. // Organized the metadata
  77. if metadata.favorite && favoriteOnTop {
  78. if metadata.directory {
  79. metadataFavoriteDirectory.append(metadata)
  80. } else {
  81. metadataFavoriteFile.append(metadata)
  82. }
  83. } else if metadata.directory && directoryOnTop {
  84. metadataDirectory.append(metadata)
  85. } else {
  86. metadataFile.append(metadata)
  87. }
  88. }
  89. metadatas.removeAll()
  90. metadatas += metadataFavoriteDirectory
  91. metadatas += metadataFavoriteFile
  92. metadatas += metadataDirectory
  93. metadatas += metadataFile
  94. }
  95. // MARK: -
  96. func getFilesInformation() -> (directories: Int, files: Int, size: Double) {
  97. var directories: Int = 0
  98. var files: Int = 0
  99. var size: Double = 0
  100. for metadata in metadatas {
  101. if metadata.directory {
  102. directories += 1
  103. } else {
  104. files += 1
  105. }
  106. size = size + metadata.size
  107. }
  108. return (directories, files, size)
  109. }
  110. func deleteMetadata(ocId: String) -> Int? {
  111. if let index = self.getIndexMetadata(ocId: ocId) {
  112. metadatas.remove(at: index)
  113. return index
  114. }
  115. return nil
  116. }
  117. @discardableResult
  118. func reloadMetadata(ocId: String, ocIdTemp: String? = nil) -> Int? {
  119. var index: Int?
  120. if ocIdTemp != nil {
  121. index = self.getIndexMetadata(ocId: ocIdTemp!)
  122. } else {
  123. index = self.getIndexMetadata(ocId: ocId)
  124. }
  125. if index != nil {
  126. if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(ocId) {
  127. metadatas[index!] = metadata
  128. }
  129. }
  130. return index
  131. }
  132. func addMetadata(_ metadata: tableMetadata) -> Int? {
  133. var index: Int = 0
  134. // Already exists
  135. for metadataCount in metadatas {
  136. if metadataCount.fileNameView == metadata.fileNameView || metadataCount.ocId == metadata.ocId {
  137. metadatas[index] = metadata
  138. return index
  139. }
  140. index += 1
  141. }
  142. // Append & rebuild
  143. metadatas.append(metadata)
  144. createMetadatas(metadatasSource: metadatas)
  145. return getIndexMetadata(ocId: metadata.ocId)
  146. }
  147. func getIndexMetadata(ocId: String) -> Int? {
  148. var index: Int = 0
  149. for metadataCount in metadatas {
  150. if metadataCount.ocId == ocId {
  151. return index
  152. }
  153. index += 1
  154. }
  155. return nil
  156. }
  157. func numberOfItems() -> Int {
  158. return metadatas.count
  159. }
  160. func cellForItemAt(indexPath: IndexPath) -> tableMetadata? {
  161. let row = indexPath.row
  162. if row > metadatas.count - 1 {
  163. return nil
  164. } else {
  165. return metadatas[row]
  166. }
  167. }
  168. }