NCRecent.swift 7.0 KB

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