NCRecent.swift 6.7 KB

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