NCTrashSelectTabBar.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // This program is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. //
  21. import Foundation
  22. import SwiftUI
  23. protocol NCTrashSelectTabBarDelegate: AnyObject {
  24. func selectAll()
  25. func recover()
  26. func delete()
  27. }
  28. class NCTrashSelectTabBar: ObservableObject {
  29. var tabBarController: UITabBarController?
  30. var hostingController: UIViewController?
  31. open weak var delegate: NCTrashSelectTabBarDelegate?
  32. @Published var isSelectedEmpty = true
  33. init(tabBarController: UITabBarController? = nil, delegate: NCTrashSelectTabBarDelegate? = nil) {
  34. let rootView = NCTrashSelectTabBarView(tabBarSelect: self)
  35. hostingController = UIHostingController(rootView: rootView)
  36. self.tabBarController = tabBarController
  37. self.delegate = delegate
  38. guard let tabBarController, let hostingController else { return }
  39. tabBarController.view.addSubview(hostingController.view)
  40. hostingController.view.frame = tabBarController.tabBar.frame
  41. hostingController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  42. hostingController.view.backgroundColor = .clear
  43. hostingController.view.isHidden = true
  44. }
  45. func show() {
  46. guard let tabBarController, let hostingController else { return }
  47. tabBarController.tabBar.isHidden = true
  48. if hostingController.view.isHidden {
  49. hostingController.view.isHidden = false
  50. hostingController.view.transform = .init(translationX: 0, y: hostingController.view.frame.height)
  51. UIView.animate(withDuration: 0.2) {
  52. hostingController.view.transform = .init(translationX: 0, y: 0)
  53. }
  54. }
  55. }
  56. func hide() {
  57. guard let tabBarController, let hostingController else { return }
  58. hostingController.view.isHidden = true
  59. tabBarController.tabBar.isHidden = false
  60. }
  61. func update(selectOcId: [String]) {
  62. isSelectedEmpty = selectOcId.isEmpty
  63. }
  64. }
  65. struct NCTrashSelectTabBarView: View {
  66. @ObservedObject var tabBarSelect: NCTrashSelectTabBar
  67. @Environment(\.verticalSizeClass) var sizeClass
  68. var body: some View {
  69. VStack {
  70. Spacer().frame(height: sizeClass == .compact ? 5 : 10)
  71. HStack {
  72. Button {
  73. tabBarSelect.delegate?.recover()
  74. } label: {
  75. Image(systemName: "arrow.circlepath")
  76. .imageScale(sizeClass == .compact ? .medium : .large)
  77. }
  78. .frame(maxWidth: .infinity)
  79. .disabled(tabBarSelect.isSelectedEmpty)
  80. Button {
  81. tabBarSelect.delegate?.delete()
  82. } label: {
  83. Image(systemName: "trash")
  84. .imageScale(sizeClass == .compact ? .medium : .large)
  85. .tint(.red)
  86. }
  87. .frame(maxWidth: .infinity)
  88. .disabled(tabBarSelect.isSelectedEmpty)
  89. Button {
  90. tabBarSelect.delegate?.selectAll()
  91. } label: {
  92. Image(systemName: "checkmark")
  93. .imageScale(sizeClass == .compact ? .medium : .large)
  94. }
  95. .frame(maxWidth: .infinity)
  96. }
  97. }
  98. .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
  99. .background(.thinMaterial)
  100. .overlay(Rectangle().frame(width: nil, height: 0.5, alignment: .top).foregroundColor(Color(UIColor.separator)), alignment: .top)
  101. }
  102. }
  103. #Preview {
  104. NCTrashSelectTabBarView(tabBarSelect: NCTrashSelectTabBar())
  105. }