NCRecent.swift 5.8 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. 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. emptyImageName = "clock.arrow.circlepath"
  33. emptyTitle = "_files_no_files_"
  34. emptyDescription = ""
  35. }
  36. // MARK: - View Life Cycle
  37. override func viewWillAppear(_ animated: Bool) {
  38. super.viewWillAppear(animated)
  39. reloadDataSource()
  40. }
  41. override func viewDidAppear(_ animated: Bool) {
  42. super.viewDidAppear(animated)
  43. getServerData()
  44. }
  45. // MARK: - DataSource
  46. override func reloadDataSource() {
  47. let results = self.database.getResultsMetadatas(predicate: NSPredicate(format: "account == %@ AND fileName != '.'", session.account), sortedByKeyPath: "date", ascending: false)
  48. layoutForView?.sort = "date"
  49. layoutForView?.ascending = false
  50. layoutForView?.directoryOnTop = false
  51. self.dataSource = NCCollectionViewDataSource(results: results, layoutForView: layoutForView)
  52. super.reloadDataSource()
  53. }
  54. override func getServerData() {
  55. let requestBodyRecent =
  56. """
  57. <?xml version=\"1.0\"?>
  58. <d:searchrequest xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">
  59. <d:basicsearch>
  60. <d:select>
  61. <d:prop>
  62. <d:displayname/>
  63. <d:getcontenttype/>
  64. <d:resourcetype/>
  65. <d:getcontentlength/>
  66. <d:getlastmodified/>
  67. <d:getetag/>
  68. <d:quota-used-bytes/>
  69. <d:quota-available-bytes/>
  70. <permissions xmlns=\"http://owncloud.org/ns\"/>
  71. <id xmlns=\"http://owncloud.org/ns\"/>
  72. <fileid xmlns=\"http://owncloud.org/ns\"/>
  73. <size xmlns=\"http://owncloud.org/ns\"/>
  74. <favorite xmlns=\"http://owncloud.org/ns\"/>
  75. <creation_time xmlns=\"http://nextcloud.org/ns\"/>
  76. <upload_time xmlns=\"http://nextcloud.org/ns\"/>
  77. <is-encrypted xmlns=\"http://nextcloud.org/ns\"/>
  78. <mount-type xmlns=\"http://nextcloud.org/ns\"/>
  79. <owner-id xmlns=\"http://owncloud.org/ns\"/>
  80. <owner-display-name xmlns=\"http://owncloud.org/ns\"/>
  81. <comments-unread xmlns=\"http://owncloud.org/ns\"/>
  82. <has-preview xmlns=\"http://nextcloud.org/ns\"/>
  83. <trashbin-filename xmlns=\"http://nextcloud.org/ns\"/>
  84. <trashbin-original-location xmlns=\"http://nextcloud.org/ns\"/>
  85. <trashbin-deletion-time xmlns=\"http://nextcloud.org/ns\"/>
  86. </d:prop>
  87. </d:select>
  88. <d:from>
  89. <d:scope>
  90. <d:href>%@</d:href>
  91. <d:depth>infinity</d:depth>
  92. </d:scope>
  93. </d:from>
  94. <d:where>
  95. <d:lt>
  96. <d:prop>
  97. <d:getlastmodified/>
  98. </d:prop>
  99. <d:literal>%@</d:literal>
  100. </d:lt>
  101. </d:where>
  102. <d:orderby>
  103. <d:order>
  104. <d:prop>
  105. /Users/marinofaggiana/Developer/ios/iOSClient/Assistant <d:getlastmodified/>
  106. </d:prop>
  107. <d:descending/>
  108. </d:order>
  109. </d:orderby>
  110. <d:limit>
  111. <d:nresults>100</d:nresults>
  112. </d:limit>
  113. </d:basicsearch>
  114. </d:searchrequest>
  115. """
  116. let dateFormatter = DateFormatter()
  117. dateFormatter.locale = Locale(identifier: "en_US_POSIX")
  118. dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
  119. let lessDateString = dateFormatter.string(from: Date())
  120. let requestBody = String(format: requestBodyRecent, "/files/" + session.userId, lessDateString)
  121. NextcloudKit.shared.searchBodyRequest(serverUrl: session.urlBase,
  122. requestBody: requestBody,
  123. showHiddenFiles: NCKeychain().showHiddenFiles,
  124. account: session.account) { task in
  125. self.dataSourceTask = task
  126. if self.dataSource.isEmpty() {
  127. self.collectionView.reloadData()
  128. }
  129. } completion: { _, files, _, error in
  130. if error == .success, let files {
  131. self.database.convertFilesToMetadatas(files, useFirstAsMetadataFolder: false) { _, metadatas in
  132. // Add metadatas
  133. self.database.addMetadatas(metadatas)
  134. self.reloadDataSource()
  135. }
  136. }
  137. self.refreshControl.endRefreshing()
  138. }
  139. }
  140. }