NCRecent.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. required init?(coder aDecoder: NSCoder) {
  27. super.init(coder: aDecoder)
  28. titleCurrentFolder = NSLocalizedString("_recent_", comment: "")
  29. layoutKey = NCGlobal.shared.layoutViewRecent
  30. enableSearchBar = false
  31. headerRichWorkspaceDisable = true
  32. emptyImage = utility.loadImage(named: "clock.arrow.circlepath", colors: [NCBrandColor.shared.brandElement])
  33. emptyTitle = "_files_no_files_"
  34. emptyDescription = ""
  35. }
  36. // MARK: - View Life Cycle
  37. override func viewWillAppear(_ animated: Bool) {
  38. super.viewWillAppear(animated)
  39. reloadDataSourceNetwork()
  40. }
  41. // MARK: - DataSource + NC Endpoint
  42. override func queryDB() {
  43. super.queryDB()
  44. let metadatas = NCManageDatabase.shared.getMetadatas(predicate: NSPredicate(format: "account == %@", self.appDelegate.account), numItems: 200, sorted: "date", ascending: false)
  45. layoutForView?.sort = "date"
  46. layoutForView?.ascending = false
  47. layoutForView?.directoryOnTop = false
  48. self.dataSource = NCDataSource(metadatas: metadatas, account: self.appDelegate.account, layoutForView: layoutForView, favoriteOnTop: false, providers: self.providers, searchResults: self.searchResults)
  49. }
  50. override func reloadDataSourceNetwork(withQueryDB: Bool = false) {
  51. super.reloadDataSourceNetwork()
  52. let requestBodyRecent =
  53. """
  54. <?xml version=\"1.0\"?>
  55. <d:searchrequest xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">
  56. <d:basicsearch>
  57. <d:select>
  58. <d:prop>
  59. <d:displayname/>
  60. <d:getcontenttype/>
  61. <d:resourcetype/>
  62. <d:getcontentlength/>
  63. <d:getlastmodified/>
  64. <d:getetag/>
  65. <d:quota-used-bytes/>
  66. <d:quota-available-bytes/>
  67. <permissions xmlns=\"http://owncloud.org/ns\"/>
  68. <id xmlns=\"http://owncloud.org/ns\"/>
  69. <fileid xmlns=\"http://owncloud.org/ns\"/>
  70. <size xmlns=\"http://owncloud.org/ns\"/>
  71. <favorite xmlns=\"http://owncloud.org/ns\"/>
  72. <creation_time xmlns=\"http://nextcloud.org/ns\"/>
  73. <upload_time xmlns=\"http://nextcloud.org/ns\"/>
  74. <is-encrypted xmlns=\"http://nextcloud.org/ns\"/>
  75. <mount-type xmlns=\"http://nextcloud.org/ns\"/>
  76. <owner-id xmlns=\"http://owncloud.org/ns\"/>
  77. <owner-display-name xmlns=\"http://owncloud.org/ns\"/>
  78. <comments-unread xmlns=\"http://owncloud.org/ns\"/>
  79. <has-preview xmlns=\"http://nextcloud.org/ns\"/>
  80. <trashbin-filename xmlns=\"http://nextcloud.org/ns\"/>
  81. <trashbin-original-location xmlns=\"http://nextcloud.org/ns\"/>
  82. <trashbin-deletion-time xmlns=\"http://nextcloud.org/ns\"/>
  83. </d:prop>
  84. </d:select>
  85. <d:from>
  86. <d:scope>
  87. <d:href>%@</d:href>
  88. <d:depth>infinity</d:depth>
  89. </d:scope>
  90. </d:from>
  91. <d:where>
  92. <d:lt>
  93. <d:prop>
  94. <d:getlastmodified/>
  95. </d:prop>
  96. <d:literal>%@</d:literal>
  97. </d:lt>
  98. </d:where>
  99. <d:orderby>
  100. <d:order>
  101. <d:prop>
  102. <d:getlastmodified/>
  103. </d:prop>
  104. <d:descending/>
  105. </d:order>
  106. </d:orderby>
  107. <d:limit>
  108. <d:nresults>100</d:nresults>
  109. </d:limit>
  110. </d:basicsearch>
  111. </d:searchrequest>
  112. """
  113. let dateFormatter = DateFormatter()
  114. dateFormatter.locale = Locale(identifier: "en_US_POSIX")
  115. dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
  116. let lessDateString = dateFormatter.string(from: Date())
  117. let requestBody = String(format: requestBodyRecent, "/files/" + appDelegate.userId, lessDateString)
  118. NextcloudKit.shared.searchBodyRequest(serverUrl: appDelegate.urlBase,
  119. requestBody: requestBody,
  120. showHiddenFiles: NCKeychain().showHiddenFiles,
  121. options: NKRequestOptions(queue: NextcloudKit.shared.nkCommonInstance.backgroundQueue)) { task in
  122. self.dataSourceTask = task
  123. self.collectionView.reloadData()
  124. } completion: { _, files, _, error in
  125. if error == .success {
  126. NCManageDatabase.shared.convertFilesToMetadatas(files, useFirstAsMetadataFolder: false) { _, metadatas in
  127. // Add metadatas
  128. NCManageDatabase.shared.addMetadatas(metadatas)
  129. self.reloadDataSource()
  130. }
  131. } else {
  132. self.reloadDataSource(withQueryDB: withQueryDB)
  133. }
  134. }
  135. }
  136. }