NCRecent.swift 7.1 KB

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