NCRecent.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // NCRecent.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 29/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. import NCCommunication
  25. class NCRecent: NCCollectionViewCommon {
  26. // MARK: - View Life Cycle
  27. required init?(coder aDecoder: NSCoder) {
  28. super.init(coder: aDecoder)
  29. titleCurrentFolder = NSLocalizedString("_recent_", comment: "")
  30. layoutKey = NCGlobal.shared.layoutViewRecent
  31. enableSearchBar = false
  32. headerMenuButtonsCommand = false
  33. headerMenuButtonsView = false
  34. emptyImage = UIImage(named: "recent")?.image(color: .gray, size: UIScreen.main.bounds.width)
  35. emptyTitle = "_files_no_files_"
  36. emptyDescription = ""
  37. }
  38. // MARK: - DataSource + NC Endpoint
  39. override func reloadDataSource() {
  40. super.reloadDataSource()
  41. DispatchQueue.global().async {
  42. self.metadatasSource = NCManageDatabase.shared.getAdvancedMetadatas(predicate: NSPredicate(format: "account == %@", self.appDelegate.account), page: 1, limit: 100, sorted: "date", ascending: false)
  43. self.dataSource = NCDataSource(metadatasSource: self.metadatasSource, directoryOnTop: false, favoriteOnTop: false)
  44. DispatchQueue.main.async {
  45. self.refreshControl.endRefreshing()
  46. self.collectionView.reloadData()
  47. }
  48. }
  49. }
  50. override func reloadDataSourceNetwork(forced: Bool = false) {
  51. super.reloadDataSourceNetwork(forced: forced)
  52. if isSearching {
  53. networkSearch()
  54. return
  55. }
  56. let requestBodyRecent =
  57. """
  58. <?xml version=\"1.0\"?>
  59. <d:searchrequest xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">
  60. <d:basicsearch>
  61. <d:select>
  62. <d:prop>
  63. <d:displayname/>
  64. <d:getcontenttype/>
  65. <d:resourcetype/>
  66. <d:getcontentlength/>
  67. <d:getlastmodified/>
  68. <d:getetag/>
  69. <d:quota-used-bytes/>
  70. <d:quota-available-bytes/>
  71. <permissions xmlns=\"http://owncloud.org/ns\"/>
  72. <id xmlns=\"http://owncloud.org/ns\"/>
  73. <fileid xmlns=\"http://owncloud.org/ns\"/>
  74. <size xmlns=\"http://owncloud.org/ns\"/>
  75. <favorite xmlns=\"http://owncloud.org/ns\"/>
  76. <creation_time xmlns=\"http://nextcloud.org/ns\"/>
  77. <upload_time xmlns=\"http://nextcloud.org/ns\"/>
  78. <is-encrypted xmlns=\"http://nextcloud.org/ns\"/>
  79. <mount-type xmlns=\"http://nextcloud.org/ns\"/>
  80. <owner-id xmlns=\"http://owncloud.org/ns\"/>
  81. <owner-display-name xmlns=\"http://owncloud.org/ns\"/>
  82. <comments-unread xmlns=\"http://owncloud.org/ns\"/>
  83. <has-preview xmlns=\"http://nextcloud.org/ns\"/>
  84. <trashbin-filename xmlns=\"http://nextcloud.org/ns\"/>
  85. <trashbin-original-location xmlns=\"http://nextcloud.org/ns\"/>
  86. <trashbin-deletion-time xmlns=\"http://nextcloud.org/ns\"/>
  87. </d:prop>
  88. </d:select>
  89. <d:from>
  90. <d:scope>
  91. <d:href>%@</d:href>
  92. <d:depth>infinity</d:depth>
  93. </d:scope>
  94. </d:from>
  95. <d:where>
  96. <d:lt>
  97. <d:prop>
  98. <d:getlastmodified/>
  99. </d:prop>
  100. <d:literal>%@</d:literal>
  101. </d:lt>
  102. </d:where>
  103. <d:orderby>
  104. <d:order>
  105. <d:prop>
  106. <d:getlastmodified/>
  107. </d:prop>
  108. <d:descending/>
  109. </d:order>
  110. </d:orderby>
  111. <d:limit>
  112. <d:nresults>100</d:nresults>
  113. </d:limit>
  114. </d:basicsearch>
  115. </d:searchrequest>
  116. """
  117. let dateFormatter = DateFormatter()
  118. dateFormatter.locale = Locale(identifier: "en_US_POSIX")
  119. dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
  120. let lessDateString = dateFormatter.string(from: Date())
  121. let requestBody = String(format: requestBodyRecent, "/files/"+appDelegate.userId, lessDateString)
  122. isReloadDataSourceNetworkInProgress = true
  123. collectionView?.reloadData()
  124. NCCommunication.shared.searchBodyRequest(serverUrl: appDelegate.urlBase, requestBody: requestBody, showHiddenFiles: CCUtility.getShowHiddenFiles(), queue: NCCommunicationCommon.shared.backgroundQueue) { account, files, errorCode, _ in
  125. if errorCode == 0 {
  126. NCManageDatabase.shared.convertNCCommunicationFilesToMetadatas(files, useMetadataFolder: false, account: account) { _, metadatasFolder, metadatas in
  127. // Update sub directories
  128. for metadata in metadatasFolder {
  129. let serverUrl = metadata.serverUrl + "/" + metadata.fileName
  130. NCManageDatabase.shared.addDirectory(encrypted: metadata.e2eEncrypted, favorite: metadata.favorite, ocId: metadata.ocId, fileId: metadata.fileId, etag: nil, permissions: metadata.permissions, serverUrl: serverUrl, account: account)
  131. }
  132. // Add metadatas
  133. NCManageDatabase.shared.addMetadatas(metadatas)
  134. self.reloadDataSource()
  135. }
  136. } else {
  137. DispatchQueue.main.async {
  138. self.collectionView?.reloadData()
  139. }
  140. }
  141. DispatchQueue.main.async {
  142. self.refreshControl.endRefreshing()
  143. self.isReloadDataSourceNetworkInProgress = false
  144. }
  145. }
  146. }
  147. }