NCRecent.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. appDelegate.activeRecent = self
  29. titleCurrentFolder = NSLocalizedString("_recent_", comment: "")
  30. layoutKey = k_layout_view_recent
  31. enableSearchBar = false
  32. DZNimage = CCGraphics.changeThemingColorImage(UIImage.init(named: "recent"), width: 300, height: 300, color: NCBrandColor.sharedInstance.brandElement)
  33. DZNtitle = "_files_no_files_"
  34. DZNdescription = ""
  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. (layout, _, _, groupBy, _, titleButton, itemForLine) = NCUtility.shared.getLayoutForView(key: layoutKey, serverUrl: "")
  44. metadatasSource = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@", appDelegate.account), page: 1, limit: 100, sorted: "date", ascending: true)
  45. self.dataSource = NCDataSource.init(metadatasSource: metadatasSource, sort: "date", ascending: false, directoryOnTop: false, filterLivePhoto: true)
  46. refreshControl.endRefreshing()
  47. collectionView.reloadData()
  48. }
  49. override func reloadDataSourceNetwork(forced: Bool = false) {
  50. super.reloadDataSourceNetwork(forced: forced)
  51. let requestBodyRecent =
  52. """
  53. <?xml version=\"1.0\"?>
  54. <d:searchrequest xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">
  55. <d:basicsearch>
  56. <d:select>
  57. <d:prop>
  58. <d:displayname/>
  59. <d:getcontenttype/>
  60. <d:resourcetype/>
  61. <d:getcontentlength/>
  62. <d:getlastmodified/>
  63. <d:getetag/>
  64. <d:quota-used-bytes/>
  65. <d:quota-available-bytes/>
  66. <permissions xmlns=\"http://owncloud.org/ns\"/>
  67. <id xmlns=\"http://owncloud.org/ns\"/>
  68. <fileid xmlns=\"http://owncloud.org/ns\"/>
  69. <size xmlns=\"http://owncloud.org/ns\"/>
  70. <favorite xmlns=\"http://owncloud.org/ns\"/>
  71. <creation_time xmlns=\"http://nextcloud.org/ns\"/>
  72. <upload_time xmlns=\"http://nextcloud.org/ns\"/>
  73. <is-encrypted xmlns=\"http://nextcloud.org/ns\"/>
  74. <mount-type xmlns=\"http://nextcloud.org/ns\"/>
  75. <owner-id xmlns=\"http://owncloud.org/ns\"/>
  76. <owner-display-name xmlns=\"http://owncloud.org/ns\"/>
  77. <comments-unread xmlns=\"http://owncloud.org/ns\"/>
  78. <has-preview xmlns=\"http://nextcloud.org/ns\"/>
  79. <trashbin-filename xmlns=\"http://nextcloud.org/ns\"/>
  80. <trashbin-original-location xmlns=\"http://nextcloud.org/ns\"/>
  81. <trashbin-deletion-time xmlns=\"http://nextcloud.org/ns\"/>
  82. </d:prop>
  83. </d:select>
  84. <d:from>
  85. <d:scope>
  86. <d:href>%@</d:href>
  87. <d:depth>infinity</d:depth>
  88. </d:scope>
  89. </d:from>
  90. <d:where>
  91. <d:lt>
  92. <d:prop>
  93. <d:getlastmodified/>
  94. </d:prop>
  95. <d:literal>%@</d:literal>
  96. </d:lt>
  97. </d:where>
  98. <d:orderby>
  99. <d:order>
  100. <d:prop>
  101. <d:getlastmodified/>
  102. </d:prop>
  103. <d:descending/>
  104. </d:order>
  105. </d:orderby>
  106. <d:limit>
  107. <d:nresults>100</d:nresults>
  108. </d:limit>
  109. </d:basicsearch>
  110. </d:searchrequest>
  111. """
  112. let dateFormatter = DateFormatter()
  113. dateFormatter.locale = Locale.init(identifier: "en_US_POSIX")
  114. dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
  115. let lessDateString = dateFormatter.string(from: Date())
  116. let requestBody = String(format: requestBodyRecent, "/files/"+appDelegate.userID, lessDateString)
  117. isReloadDataSourceNetworkInProgress = true
  118. collectionView?.reloadData()
  119. NCCommunication.shared.searchBodyRequest(serverUrl: appDelegate.urlBase, requestBody: requestBody, showHiddenFiles: CCUtility.getShowHiddenFiles()) { (account, files, errorCode, errorDescription) in
  120. self.refreshControl.endRefreshing()
  121. self.isReloadDataSourceNetworkInProgress = false
  122. if errorCode == 0 {
  123. NCManageDatabase.sharedInstance.convertNCCommunicationFilesToMetadatas(files, useMetadataFolder: false, account: account) { (metadataFolder, metadatasFolder, metadatas) in
  124. // Update sub directories
  125. for metadata in metadatasFolder {
  126. let serverUrl = metadata.serverUrl + "/" + metadata.fileName
  127. NCManageDatabase.sharedInstance.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)
  128. }
  129. // Add metadatas
  130. NCManageDatabase.sharedInstance.addMetadatas(metadatas)
  131. self.reloadDataSource()
  132. }
  133. } else {
  134. self.collectionView?.reloadData()
  135. }
  136. }
  137. }
  138. }