NCDataSource.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. @objc class NCDataSource: NSObject {
  25. @objc var metadatas: [tableMetadata] = []
  26. @objc var sections: Int = 1
  27. private var sort: String = ""
  28. private var ascending: Bool = true
  29. private var directoryOnTop: Bool = true
  30. private var filterLivePhoto: Bool = true
  31. @objc init(metadatasSource: [tableMetadata], sort: String, ascending: Bool, groupBy: String? = nil, directoryOnTop: Bool, filterLivePhoto: Bool) {
  32. super.init()
  33. self.sort = sort
  34. self.ascending = ascending
  35. self.directoryOnTop = directoryOnTop
  36. self.filterLivePhoto = filterLivePhoto
  37. createMetadatas(metadatasSource: metadatasSource)
  38. }
  39. // MARK: -
  40. private func createMetadatas(metadatasSource: [tableMetadata]) {
  41. var metadatasFavorite: [tableMetadata] = []
  42. var numDirectory: Int = 0
  43. var numDirectoryFavorite:Int = 0
  44. /*
  45. Metadata order
  46. */
  47. let metadatasSourceSorted = metadatasSource.sorted { (obj1:tableMetadata, obj2:tableMetadata) -> Bool in
  48. if sort == "date" {
  49. if ascending {
  50. return obj1.date.compare(obj2.date as Date) == ComparisonResult.orderedAscending
  51. } else {
  52. return obj1.date.compare(obj2.date as Date) == ComparisonResult.orderedDescending
  53. }
  54. } else if sort == "sessionTaskIdentifier" {
  55. if ascending {
  56. return obj1.sessionTaskIdentifier > obj2.sessionTaskIdentifier
  57. } else {
  58. return obj1.sessionTaskIdentifier < obj2.sessionTaskIdentifier
  59. }
  60. } else if sort == "size" {
  61. if ascending {
  62. return obj1.size > obj2.size
  63. } else {
  64. return obj1.size < obj2.size
  65. }
  66. } else {
  67. let range = Range(NSMakeRange(0, obj1.fileNameView.count), in: obj1.fileNameView)
  68. if ascending {
  69. return obj1.fileNameView.compare(obj2.fileNameView, options: .caseInsensitive, range: range, locale: .current) == ComparisonResult.orderedAscending
  70. } else {
  71. return obj1.fileNameView.compare(obj2.fileNameView, options: .caseInsensitive, range: range, locale: .current) == ComparisonResult.orderedDescending
  72. }
  73. }
  74. }
  75. /*
  76. Initialize datasource
  77. */
  78. for metadata in metadatasSourceSorted {
  79. // skipped livePhoto
  80. if metadata.ext == "mov" && metadata.livePhoto {
  81. continue
  82. }
  83. if metadata.directory && directoryOnTop {
  84. if metadata.favorite {
  85. metadatas.insert(metadata, at: numDirectoryFavorite)
  86. numDirectoryFavorite += 1
  87. numDirectory += 1
  88. } else {
  89. metadatas.insert(metadata, at: numDirectory)
  90. numDirectory += 1
  91. }
  92. } else {
  93. if metadata.favorite && directoryOnTop {
  94. metadatasFavorite.append(metadata)
  95. } else {
  96. metadatas.append(metadata)
  97. }
  98. }
  99. }
  100. if directoryOnTop && metadatasFavorite.count > 0 {
  101. metadatas.insert(contentsOf: metadatasFavorite, at: numDirectory)
  102. }
  103. }
  104. func getFilesInformation() -> (directories: Int, files: Int, size: Double) {
  105. var directories: Int = 0
  106. var files: Int = 0
  107. var size: Double = 0
  108. for metadata in metadatas {
  109. if metadata.directory {
  110. directories += 1
  111. } else {
  112. files += 1
  113. }
  114. size = size + metadata.size
  115. }
  116. return (directories, files, size)
  117. }
  118. @objc func deleteMetadata(ocId: String) {
  119. if let index = self.getIndexMetadata(ocId: ocId) {
  120. metadatas.remove(at: index)
  121. }
  122. }
  123. @objc func reloadMetadata(ocId: String) {
  124. if let index = self.getIndexMetadata(ocId: ocId) {
  125. if let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", ocId)) {
  126. metadatas[index] = metadata
  127. }
  128. }
  129. }
  130. @objc func addMetadata(_ metadata: tableMetadata) {
  131. self.metadatas.append(metadata)
  132. createMetadatas(metadatasSource: metadatas)
  133. }
  134. func getIndexMetadata(ocId: String) -> Int? {
  135. var index: Int = 0
  136. for metadataCount in metadatas {
  137. if metadataCount.ocId == ocId {
  138. return index
  139. }
  140. index += 1
  141. }
  142. return nil
  143. }
  144. // MARK: -
  145. @objc func cellForItemAt(indexPath: IndexPath) -> tableMetadata? {
  146. let row = indexPath.row
  147. if row > self.metadatas.count - 1 {
  148. return nil
  149. } else {
  150. return metadatas[row]
  151. }
  152. }
  153. @objc func numberOfItemsInSection(section: Int) -> Int {
  154. return metadatas.count
  155. }
  156. }