NCShares.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // NCShares.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 20/10/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 NCShares: NCCollectionViewCommon {
  26. required init?(coder aDecoder: NSCoder) {
  27. super.init(coder: aDecoder)
  28. titleCurrentFolder = NSLocalizedString("_list_shares_", comment: "")
  29. layoutKey = NCGlobal.shared.layoutViewShares
  30. enableSearchBar = false
  31. headerRichWorkspaceDisable = true
  32. emptyImageName = "person.fill.badge.plus"
  33. emptyTitle = "_list_shares_no_files_"
  34. emptyDescription = "_tutorial_list_shares_view_"
  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. var ocId: [String] = []
  48. let sharess = self.database.getTableShares(account: session.account)
  49. for share in sharess {
  50. if let result = self.database.getResultMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileName == %@", session.account, share.serverUrl, share.fileName)) {
  51. if !(ocId.contains { $0 == result.ocId }) {
  52. ocId.append(result.ocId)
  53. }
  54. } else {
  55. let serverUrlFileName = share.serverUrl + "/" + share.fileName
  56. NCNetworking.shared.readFile(serverUrlFileName: serverUrlFileName, account: session.account) { task in
  57. self.dataSourceTask = task
  58. if self.dataSource.isEmpty() {
  59. self.collectionView.reloadData()
  60. }
  61. } completion: { _, metadata, _ in
  62. if let metadata {
  63. self.database.addMetadata(metadata)
  64. if !(ocId.contains { $0 == metadata.ocId }) {
  65. ocId.append(metadata.ocId)
  66. }
  67. }
  68. }
  69. }
  70. }
  71. let metadatas = self.database.getResultsMetadatasPredicate(NSPredicate(format: "ocId IN %@", ocId), layoutForView: layoutForView)
  72. self.dataSource = NCCollectionViewDataSource(metadatas: metadatas, layoutForView: layoutForView)
  73. super.reloadDataSource()
  74. }
  75. override func getServerData() {
  76. NextcloudKit.shared.readShares(parameters: NKShareParameter(), account: session.account) { task in
  77. self.dataSourceTask = task
  78. if self.dataSource.isEmpty() {
  79. self.collectionView.reloadData()
  80. }
  81. } completion: { account, shares, _, error in
  82. if error == .success {
  83. self.database.deleteTableShare(account: account)
  84. if let shares = shares, !shares.isEmpty {
  85. let home = self.utilityFileSystem.getHomeServer(session: self.session)
  86. self.database.addShare(account: account, home: home, shares: shares)
  87. }
  88. self.reloadDataSource()
  89. }
  90. self.refreshControl.endRefreshing()
  91. }
  92. }
  93. }