NCCollectionViewCommon+SwipeCollectionViewCellDelegate.swift 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // NCCollectionViewCommon+SwipeCollectionViewCellDelegate.swift
  3. // Nextcloud
  4. //
  5. // Created by Milen on 01.03.24.
  6. // Copyright © 2024 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 SwipeCellKit
  25. extension NCCollectionViewCommon: SwipeCollectionViewCellDelegate {
  26. func collectionView(_ collectionView: UICollectionView, editActionsForItemAt indexPath: IndexPath, for orientation: SwipeCellKit.SwipeActionsOrientation) -> [SwipeCellKit.SwipeAction]? {
  27. guard orientation == .right, let metadata = self.dataSource.cellForItemAt(indexPath: indexPath) else { return nil }
  28. let scaleTransition = ScaleTransition(duration: 0.3, initialScale: 0.8, threshold: 0.8)
  29. // wait a fix for truncate the text .. ? ..
  30. let favoriteAction = SwipeAction(style: .default, title: NSLocalizedString(metadata.favorite ? "_favorite_short_" : "_favorite_short_", comment: "") ) { _, _ in
  31. NCNetworking.shared.favoriteMetadata(metadata) { error in
  32. if error != .success {
  33. NCContentPresenter().showError(error: error)
  34. }
  35. }
  36. }
  37. favoriteAction.backgroundColor = NCBrandColor.shared.yellowFavorite
  38. favoriteAction.image = .init(systemName: metadata.favorite ? "star.slash.fill" : "star.fill")
  39. favoriteAction.transitionDelegate = scaleTransition
  40. favoriteAction.hidesWhenSelected = true
  41. var actions = [favoriteAction]
  42. let shareAction = SwipeAction(style: .default, title: NSLocalizedString("_share_", comment: "")) { _, _ in
  43. NCActionCenter.shared.openActivityViewController(selectedMetadata: [metadata])
  44. }
  45. shareAction.backgroundColor = .blue
  46. shareAction.image = .init(systemName: "square.and.arrow.up")
  47. shareAction.transitionDelegate = scaleTransition
  48. shareAction.hidesWhenSelected = true
  49. let deleteAction = SwipeAction(style: .destructive, title: NSLocalizedString("_delete_", comment: "")) { _, _ in
  50. let titleDelete: String
  51. if metadata.directory {
  52. titleDelete = NSLocalizedString("_delete_folder_", comment: "")
  53. } else {
  54. titleDelete = NSLocalizedString("_delete_file_", comment: "")
  55. }
  56. let message = NSLocalizedString("_want_delete_", comment: "") + "\n - " + metadata.fileNameView
  57. let alertController = UIAlertController.deleteFileOrFolder(titleString: titleDelete + "?", message: message, canDeleteServer: !metadata.lock, selectedMetadatas: [metadata], indexPaths: self.selectIndexPaths) { _ in }
  58. self.viewController.present(alertController, animated: true, completion: nil)
  59. }
  60. deleteAction.image = .init(systemName: "trash")
  61. deleteAction.style = .destructive
  62. deleteAction.transitionDelegate = scaleTransition
  63. deleteAction.hidesWhenSelected = true
  64. if !NCManageDatabase.shared.isMetadataShareOrMounted(metadata: metadata, metadataFolder: metadataFolder) {
  65. actions.insert(deleteAction, at: 0)
  66. }
  67. if metadata.canShare {
  68. actions.append(shareAction)
  69. }
  70. return actions
  71. }
  72. func collectionView(_ collectionView: UICollectionView, editActionsOptionsForItemAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> SwipeOptions {
  73. var options = SwipeOptions()
  74. options.expansionStyle = .selection
  75. options.transitionStyle = .border
  76. options.backgroundColor = .clear
  77. return options
  78. }
  79. }