NCMediaSelectTabBar.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // NCMediaTabbarSelect.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 01/02/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 UIKit
  24. import SwiftUI
  25. protocol NCMediaSelectTabBarDelegate: AnyObject {
  26. func delete()
  27. }
  28. class NCMediaSelectTabBar: ObservableObject {
  29. var hostingController: UIViewController!
  30. var mediaTabBarController: UITabBarController?
  31. open weak var delegate: NCMediaSelectTabBarDelegate?
  32. @Published var selectCount: Int = 0
  33. init(tabBarController: UITabBarController? = nil, delegate: NCMediaSelectTabBarDelegate? = nil) {
  34. guard let tabBarController else { return }
  35. let mediaTabBarSelectView = MediaTabBarSelectView(tabBarSelect: self)
  36. hostingController = UIHostingController(rootView: mediaTabBarSelectView)
  37. self.mediaTabBarController = tabBarController
  38. self.delegate = delegate
  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. hostingController.view.isHidden = false
  47. hostingController.view.transform = .init(translationX: 0, y: hostingController.view.frame.height)
  48. UIView.animate(withDuration: 0.2) {
  49. self.hostingController.view.transform = .init(translationX: 0, y: 0)
  50. }
  51. mediaTabBarController?.tabBar.isHidden = true
  52. }
  53. func hide() {
  54. mediaTabBarController?.tabBar.isHidden = false
  55. hostingController.view.isHidden = true
  56. }
  57. }
  58. struct MediaTabBarSelectView: View {
  59. @ObservedObject var tabBarSelect: NCMediaSelectTabBar
  60. @Environment(\.verticalSizeClass) var sizeClass
  61. var body: some View {
  62. VStack {
  63. Spacer().frame(height: sizeClass == .compact ? 5 : 10)
  64. HStack {
  65. Spacer().frame(maxWidth: .infinity)
  66. Group {
  67. if tabBarSelect.selectCount == 0 {
  68. Text(NSLocalizedString("_select_photos_", comment: ""))
  69. } else if tabBarSelect.selectCount == 1 {
  70. Text(String(tabBarSelect.selectCount) + " " + NSLocalizedString("_selected_photo_", comment: ""))
  71. } else {
  72. Text(String(tabBarSelect.selectCount) + " " + NSLocalizedString("_selected_photos_", comment: ""))
  73. }
  74. }
  75. .frame(minWidth: 250, maxWidth: .infinity)
  76. Button {
  77. tabBarSelect.delegate?.delete()
  78. } label: {
  79. Image(systemName: "trash")
  80. .font(Font.system(.body).weight(.light))
  81. .imageScale(sizeClass == .compact ? .medium : .large)
  82. }
  83. .tint(.red)
  84. .disabled(tabBarSelect.selectCount == 0)
  85. .frame(maxWidth: .infinity)
  86. }
  87. .frame(maxWidth: .infinity)
  88. }
  89. .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
  90. .background(.thinMaterial)
  91. .overlay(Rectangle().frame(width: nil, height: 0.5, alignment: .top).foregroundColor(Color(UIColor.separator)), alignment: .top)
  92. }
  93. }
  94. #Preview {
  95. MediaTabBarSelectView(tabBarSelect: NCMediaSelectTabBar())
  96. }