NCDataSource.swift 6.2 KB

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