NCRecent.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 NextcloudKit
  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. headerMenuButtonsView = false
  33. headerRichWorkspaceDisable = true
  34. emptyImage = NCUtility.shared.loadImage(named: "clock.arrow.circlepath", color: .gray, size: UIScreen.main.bounds.width)
  35. emptyTitle = "_files_no_files_"
  36. emptyDescription = ""
  37. }
  38. override func viewWillAppear(_ animated: Bool) {
  39. super.viewWillAppear(animated)
  40. navigationController?.setFileAppreance()
  41. }
  42. // MARK: - DataSource + NC Endpoint
  43. override func reloadDataSource(forced: Bool = true) {
  44. super.reloadDataSource()
  45. DispatchQueue.global().async {
  46. let metadatas = NCManageDatabase.shared.getAdvancedMetadatas(predicate: NSPredicate(format: "account == %@", self.appDelegate.account), page: 1, limit: 100, sorted: "date", ascending: false)
  47. self.dataSource = NCDataSource(metadatas: metadatas,
  48. account: self.appDelegate.account,
  49. directoryOnTop: false,
  50. favoriteOnTop: false,
  51. groupByField: self.groupByField,
  52. providers: self.providers,
  53. searchResults: self.searchResults)
  54. DispatchQueue.main.async {
  55. self.refreshControl.endRefreshing()
  56. self.collectionView.reloadData()
  57. }
  58. }
  59. }
  60. override func reloadDataSourceNetwork(forced: Bool = false) {
  61. super.reloadDataSourceNetwork(forced: forced)
  62. let requestBodyRecent =
  63. """
  64. <?xml version=\"1.0\"?>
  65. <d:searchrequest xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">
  66. <d:basicsearch>
  67. <d:select>
  68. <d:prop>
  69. <d:displayname/>
  70. <d:getcontenttype/>
  71. <d:resourcetype/>
  72. <d:getcontentlength/>
  73. <d:getlastmodified/>
  74. <d:getetag/>
  75. <d:quota-used-bytes/>
  76. <d:quota-available-bytes/>
  77. <permissions xmlns=\"http://owncloud.org/ns\"/>
  78. <id xmlns=\"http://owncloud.org/ns\"/>
  79. <fileid xmlns=\"http://owncloud.org/ns\"/>
  80. <size xmlns=\"http://owncloud.org/ns\"/>
  81. <favorite xmlns=\"http://owncloud.org/ns\"/>
  82. <creation_time xmlns=\"http://nextcloud.org/ns\"/>
  83. <upload_time xmlns=\"http://nextcloud.org/ns\"/>
  84. <is-encrypted xmlns=\"http://nextcloud.org/ns\"/>
  85. <mount-type xmlns=\"http://nextcloud.org/ns\"/>
  86. <owner-id xmlns=\"http://owncloud.org/ns\"/>
  87. <owner-display-name xmlns=\"http://owncloud.org/ns\"/>
  88. <comments-unread xmlns=\"http://owncloud.org/ns\"/>
  89. <has-preview xmlns=\"http://nextcloud.org/ns\"/>
  90. <trashbin-filename xmlns=\"http://nextcloud.org/ns\"/>
  91. <trashbin-original-location xmlns=\"http://nextcloud.org/ns\"/>
  92. <trashbin-deletion-time xmlns=\"http://nextcloud.org/ns\"/>
  93. </d:prop>
  94. </d:select>
  95. <d:from>
  96. <d:scope>
  97. <d:href>%@</d:href>
  98. <d:depth>infinity</d:depth>
  99. </d:scope>
  100. </d:from>
  101. <d:where>
  102. <d:lt>
  103. <d:prop>
  104. <d:getlastmodified/>
  105. </d:prop>
  106. <d:literal>%@</d:literal>
  107. </d:lt>
  108. </d:where>
  109. <d:orderby>
  110. <d:order>
  111. <d:prop>
  112. <d:getlastmodified/>
  113. </d:prop>
  114. <d:descending/>
  115. </d:order>
  116. </d:orderby>
  117. <d:limit>
  118. <d:nresults>100</d:nresults>
  119. </d:limit>
  120. </d:basicsearch>
  121. </d:searchrequest>
  122. """
  123. let dateFormatter = DateFormatter()
  124. dateFormatter.locale = Locale(identifier: "en_US_POSIX")
  125. dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
  126. let lessDateString = dateFormatter.string(from: Date())
  127. let requestBody = String(format: requestBodyRecent, "/files/"+appDelegate.userId, lessDateString)
  128. isReloadDataSourceNetworkInProgress = true
  129. collectionView?.reloadData()
  130. let options = NKRequestOptions(queue: NextcloudKit.shared.nkCommonInstance.backgroundQueue)
  131. NextcloudKit.shared.searchBodyRequest(serverUrl: appDelegate.urlBase, requestBody: requestBody, showHiddenFiles: CCUtility.getShowHiddenFiles(), options: options) { account, files, data, error in
  132. if error == .success {
  133. NCManageDatabase.shared.convertFilesToMetadatas(files, useMetadataFolder: false) { _, metadatasFolder, metadatas in
  134. // Update sub directories
  135. for metadata in metadatasFolder {
  136. let serverUrl = metadata.serverUrl + "/" + metadata.fileName
  137. NCManageDatabase.shared.addDirectory(encrypted: metadata.e2eEncrypted, favorite: metadata.favorite, ocId: metadata.ocId, fileId: metadata.fileId, etag: nil, permissions: metadata.permissions, serverUrl: serverUrl, account: account)
  138. }
  139. // Add metadatas
  140. NCManageDatabase.shared.addMetadatas(metadatas)
  141. self.reloadDataSource()
  142. }
  143. } else {
  144. DispatchQueue.main.async {
  145. self.collectionView?.reloadData()
  146. }
  147. }
  148. DispatchQueue.main.async {
  149. self.refreshControl.endRefreshing()
  150. self.isReloadDataSourceNetworkInProgress = false
  151. }
  152. }
  153. }
  154. }