NCTrashSelectTabBar.swift 4.8 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 UIKit
  23. import SwiftUI
  24. protocol NCTrashSelectTabBarDelegate: AnyObject {
  25. func selectAll()
  26. func recover()
  27. func delete()
  28. }
  29. class NCTrashSelectTabBar: ObservableObject {
  30. var tabBarController: UITabBarController?
  31. var hostingController: UIViewController?
  32. open weak var delegate: NCTrashSelectTabBarDelegate?
  33. @Published var isSelectedEmpty = true
  34. init(tabBarController: UITabBarController? = nil, delegate: NCTrashSelectTabBarDelegate? = nil) {
  35. let rootView = NCTrashSelectTabBarView(tabBarSelect: self)
  36. hostingController = UIHostingController(rootView: rootView)
  37. self.tabBarController = tabBarController
  38. self.delegate = delegate
  39. guard let tabBarController, let hostingController else { return }
  40. tabBarController.view.addSubview(hostingController.view)
  41. hostingController.view.frame = tabBarController.tabBar.frame
  42. hostingController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  43. hostingController.view.backgroundColor = .clear
  44. hostingController.view.isHidden = true
  45. }
  46. func show() {
  47. guard let tabBarController, let hostingController else { return }
  48. tabBarController.tabBar.isHidden = true
  49. if hostingController.view.isHidden {
  50. hostingController.view.isHidden = false
  51. hostingController.view.transform = .init(translationX: 0, y: hostingController.view.frame.height)
  52. UIView.animate(withDuration: 0.2) {
  53. hostingController.view.transform = .init(translationX: 0, y: 0)
  54. }
  55. }
  56. }
  57. func hide() {
  58. guard let tabBarController, let hostingController else { return }
  59. hostingController.view.isHidden = true
  60. tabBarController.tabBar.isHidden = false
  61. }
  62. func update(selectOcId: [String]) {
  63. isSelectedEmpty = selectOcId.isEmpty
  64. }
  65. func isHidden() -> Bool {
  66. guard let hostingController else { return false }
  67. return hostingController.view.isHidden
  68. }
  69. }
  70. struct NCTrashSelectTabBarView: View {
  71. @ObservedObject var tabBarSelect: NCTrashSelectTabBar
  72. @Environment(\.verticalSizeClass) var sizeClass
  73. var body: some View {
  74. VStack {
  75. Spacer().frame(height: sizeClass == .compact ? 5 : 10)
  76. HStack {
  77. Button {
  78. tabBarSelect.delegate?.recover()
  79. } label: {
  80. Image(systemName: "arrow.circlepath")
  81. .font(Font.system(.body).weight(.light))
  82. .imageScale(sizeClass == .compact ? .medium : .large)
  83. }
  84. .tint(Color(NCBrandColor.shared.iconImageColor))
  85. .frame(maxWidth: .infinity)
  86. .disabled(tabBarSelect.isSelectedEmpty)
  87. Button {
  88. tabBarSelect.delegate?.delete()
  89. } label: {
  90. Image(systemName: "trash")
  91. .font(Font.system(.body).weight(.light))
  92. .imageScale(sizeClass == .compact ? .medium : .large)
  93. }
  94. .tint(.red)
  95. .frame(maxWidth: .infinity)
  96. .disabled(tabBarSelect.isSelectedEmpty)
  97. Button {
  98. tabBarSelect.delegate?.selectAll()
  99. } label: {
  100. Image(systemName: "checkmark")
  101. .font(Font.system(.body).weight(.light))
  102. .imageScale(sizeClass == .compact ? .medium : .large)
  103. }
  104. .tint(Color(NCBrandColor.shared.iconImageColor))
  105. .frame(maxWidth: .infinity)
  106. }
  107. }
  108. .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
  109. .background(.thinMaterial)
  110. .overlay(Rectangle().frame(width: nil, height: 0.5, alignment: .top).foregroundColor(Color(UIColor.separator)), alignment: .top)
  111. }
  112. }
  113. #Preview {
  114. NCTrashSelectTabBarView(tabBarSelect: NCTrashSelectTabBar())
  115. }