NCTrashSelectTabBar.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // NCTrashSelectTabBar.swift
  3. // Nextcloud
  4. //
  5. // Created by Milen on 05.02.24.
  6. // Copyright © 2024 Marino Faggiana. All rights reserved.
  7. //
  8. import Foundation
  9. import SwiftUI
  10. protocol NCTrashSelectTabBarDelegate: AnyObject {
  11. func selectAll()
  12. func recover()
  13. func delete()
  14. }
  15. class NCTrashSelectTabBar: NCSelectableViewTabBar, ObservableObject {
  16. var tabBarController: UITabBarController?
  17. var hostingController: UIViewController?
  18. open weak var delegate: NCTrashSelectTabBarDelegate?
  19. var selectedMetadatas: [tableMetadata] = []
  20. @Published var isSelectedEmpty = true
  21. init(tabBarController: UITabBarController? = nil, delegate: NCTrashSelectTabBarDelegate? = nil) {
  22. let rootView = NCTrashSelectTabBarView(tabBarSelect: self)
  23. hostingController = UIHostingController(rootView: rootView)
  24. self.tabBarController = tabBarController
  25. self.delegate = delegate
  26. guard let tabBarController, let hostingController else { return }
  27. tabBarController.view.addSubview(hostingController.view)
  28. hostingController.view.frame = tabBarController.tabBar.frame
  29. hostingController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  30. hostingController.view.backgroundColor = .clear
  31. hostingController.view.isHidden = true
  32. }
  33. func show() {
  34. guard let tabBarController, let hostingController else { return }
  35. tabBarController.tabBar.isHidden = true
  36. if hostingController.view.isHidden {
  37. hostingController.view.isHidden = false
  38. hostingController.view.transform = .init(translationX: 0, y: hostingController.view.frame.height)
  39. UIView.animate(withDuration: 0.2) {
  40. hostingController.view.transform = .init(translationX: 0, y: 0)
  41. }
  42. }
  43. }
  44. func hide() {
  45. guard let tabBarController, let hostingController else { return }
  46. hostingController.view.isHidden = true
  47. tabBarController.tabBar.isHidden = false
  48. }
  49. }
  50. struct NCTrashSelectTabBarView: View {
  51. @ObservedObject var tabBarSelect: NCTrashSelectTabBar
  52. @Environment(\.verticalSizeClass) var sizeClass
  53. var body: some View {
  54. VStack {
  55. Spacer().frame(height: sizeClass == .compact ? 5 : 10)
  56. HStack {
  57. Button {
  58. tabBarSelect.delegate?.recover()
  59. } label: {
  60. Image(systemName: "arrow.circlepath")
  61. .imageScale(sizeClass == .compact ? .medium : .large)
  62. }
  63. .frame(maxWidth: .infinity)
  64. .disabled(tabBarSelect.isSelectedEmpty)
  65. Button {
  66. tabBarSelect.delegate?.delete()
  67. } label: {
  68. Image(systemName: "trash")
  69. .imageScale(sizeClass == .compact ? .medium : .large)
  70. .tint(.red)
  71. }
  72. .frame(maxWidth: .infinity)
  73. .disabled(tabBarSelect.isSelectedEmpty)
  74. Button {
  75. tabBarSelect.delegate?.selectAll()
  76. } label: {
  77. Image(systemName: "checkmark")
  78. .imageScale(sizeClass == .compact ? .medium : .large)
  79. }
  80. .frame(maxWidth: .infinity)
  81. }
  82. }
  83. .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
  84. .background(.thinMaterial)
  85. .overlay(Rectangle().frame(width: nil, height: 0.5, alignment: .top).foregroundColor(Color(UIColor.separator)), alignment: .top)
  86. }
  87. }
  88. #Preview {
  89. NCTrashSelectTabBarView(tabBarSelect: NCTrashSelectTabBar())
  90. }