NCTrashSelectTabBar.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. func isHidden() -> Bool {
  65. guard let hostingController else { return false }
  66. return hostingController.view.isHidden
  67. }
  68. }
  69. struct NCTrashSelectTabBarView: View {
  70. @ObservedObject var tabBarSelect: NCTrashSelectTabBar
  71. @Environment(\.verticalSizeClass) var sizeClass
  72. var body: some View {
  73. VStack {
  74. Spacer().frame(height: sizeClass == .compact ? 5 : 10)
  75. HStack {
  76. Button {
  77. tabBarSelect.delegate?.recover()
  78. } label: {
  79. Image(systemName: "arrow.circlepath")
  80. .font(Font.system(.body).weight(.light))
  81. .imageScale(sizeClass == .compact ? .medium : .large)
  82. }
  83. .tint(Color(NCBrandColor.shared.iconImageColor))
  84. .frame(maxWidth: .infinity)
  85. .disabled(tabBarSelect.isSelectedEmpty)
  86. Button {
  87. tabBarSelect.delegate?.delete()
  88. } label: {
  89. Image(systemName: "trash")
  90. .font(Font.system(.body).weight(.light))
  91. .imageScale(sizeClass == .compact ? .medium : .large)
  92. }
  93. .tint(.red)
  94. .frame(maxWidth: .infinity)
  95. .disabled(tabBarSelect.isSelectedEmpty)
  96. Button {
  97. tabBarSelect.delegate?.selectAll()
  98. } label: {
  99. Image(systemName: "checkmark")
  100. .font(Font.system(.body).weight(.light))
  101. .imageScale(sizeClass == .compact ? .medium : .large)
  102. }
  103. .tint(Color(NCBrandColor.shared.iconImageColor))
  104. .frame(maxWidth: .infinity)
  105. }
  106. }
  107. .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
  108. .background(.thinMaterial)
  109. .overlay(Rectangle().frame(width: nil, height: 0.5, alignment: .top).foregroundColor(Color(UIColor.separator)), alignment: .top)
  110. }
  111. }
  112. #Preview {
  113. NCTrashSelectTabBarView(tabBarSelect: NCTrashSelectTabBar())
  114. }