NCDataSource.swift 5.9 KB

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