NCRecent.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 = utility.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. reloadDataSourceNetwork()
  41. }
  42. // MARK: - DataSource + NC Endpoint
  43. override func queryDB() {
  44. super.queryDB()
  45. let metadatas = NCManageDatabase.shared.getAdvancedMetadatas(predicate: NSPredicate(format: "account == %@", self.appDelegate.account), page: 1, limit: 100, sorted: "date", ascending: false)
  46. self.dataSource = NCDataSource(metadatas: metadatas,
  47. account: self.appDelegate.account,
  48. directoryOnTop: false,
  49. favoriteOnTop: false,
  50. groupByField: self.groupByField,
  51. providers: self.providers,
  52. searchResults: self.searchResults)
  53. }
  54. override func reloadDataSourceNetwork() {
  55. super.reloadDataSourceNetwork()
  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. NextcloudKit.shared.searchBodyRequest(serverUrl: appDelegate.urlBase,
  123. requestBody: requestBody,
  124. showHiddenFiles: NCKeychain().showHiddenFiles,
  125. options: NKRequestOptions(queue: NextcloudKit.shared.nkCommonInstance.backgroundQueue)) { account, files, _, error in
  126. if error == .success {
  127. NCManageDatabase.shared.convertFilesToMetadatas(files, useMetadataFolder: false) { _, metadatasFolder, metadatas in
  128. // Update sub directories
  129. for metadata in metadatasFolder {
  130. let serverUrl = metadata.serverUrl + "/" + metadata.fileName
  131. NCManageDatabase.shared.addDirectory(encrypted: metadata.e2eEncrypted, favorite: metadata.favorite, ocId: metadata.ocId, fileId: metadata.fileId, etag: nil, permissions: metadata.permissions, serverUrl: serverUrl, account: account)
  132. }
  133. // Add metadatas
  134. NCManageDatabase.shared.addMetadatas(metadatas)
  135. self.reloadDataSource()
  136. }
  137. } else {
  138. self.reloadDataSource(withQueryDB: false)
  139. }
  140. }
  141. }
  142. }