NCRecent.swift 6.7 KB

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