NCCollectionViewCommonSelectTabBar.swift 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // NCCollectionViewCommonSelectionTabBar.swift
  3. // Nextcloud
  4. //
  5. // Created by Milen 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 Foundation
  24. import SwiftUI
  25. protocol NCCollectionViewCommonSelectTabBarDelegate: AnyObject {
  26. func selectAll()
  27. func delete()
  28. func move()
  29. func share()
  30. func saveAsAvailableOffline(isAnyOffline: Bool)
  31. func lock(isAnyLocked: Bool)
  32. }
  33. class NCCollectionViewCommonSelectTabBar: ObservableObject {
  34. var tabBarController: NCMainTabBarController?
  35. var hostingController: UIViewController?
  36. open weak var delegate: NCCollectionViewCommonSelectTabBarDelegate?
  37. @Published var isAnyOffline = false
  38. @Published var canSetAsOffline = false
  39. @Published var isAnyDirectory = false
  40. @Published var isAllDirectory = false
  41. @Published var isAnyLocked = false
  42. @Published var canUnlock = true
  43. @Published var enableLock = false
  44. @Published var isSelectedEmpty = true
  45. init(tabBarController: NCMainTabBarController? = nil, delegate: NCCollectionViewCommonSelectTabBarDelegate? = nil) {
  46. let rootView = NCCollectionViewCommonSelectTabBarView(tabBarSelect: self)
  47. hostingController = UIHostingController(rootView: rootView)
  48. self.tabBarController = tabBarController
  49. self.delegate = delegate
  50. guard let tabBarController, let hostingController else { return }
  51. tabBarController.view.addSubview(hostingController.view)
  52. hostingController.view.frame = tabBarController.tabBar.frame
  53. hostingController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  54. hostingController.view.backgroundColor = .clear
  55. hostingController.view.isHidden = true
  56. }
  57. func show() {
  58. guard let tabBarController, let hostingController else { return }
  59. tabBarController.tabBar.isHidden = true
  60. if hostingController.view.isHidden {
  61. hostingController.view.isHidden = false
  62. hostingController.view.transform = .init(translationX: 0, y: hostingController.view.frame.height)
  63. UIView.animate(withDuration: 0.2) {
  64. hostingController.view.transform = .init(translationX: 0, y: 0)
  65. }
  66. }
  67. }
  68. func hide() {
  69. guard let tabBarController, let hostingController else { return }
  70. hostingController.view.isHidden = true
  71. tabBarController.tabBar.isHidden = false
  72. }
  73. func isHidden() -> Bool {
  74. guard let hostingController else { return false }
  75. return hostingController.view.isHidden
  76. }
  77. func update(selectOcId: [String], metadatas: [tableMetadata]? = nil, userId: String? = nil) {
  78. if let metadatas {
  79. isAnyOffline = false
  80. canSetAsOffline = true
  81. isAnyDirectory = false
  82. isAllDirectory = true
  83. isAnyLocked = false
  84. canUnlock = true
  85. for metadata in metadatas {
  86. if metadata.directory {
  87. isAnyDirectory = true
  88. } else {
  89. isAllDirectory = false
  90. }
  91. if !metadata.canSetAsAvailableOffline {
  92. canSetAsOffline = false
  93. }
  94. if metadata.lock {
  95. isAnyLocked = true
  96. if metadata.lockOwner != userId {
  97. canUnlock = false
  98. }
  99. }
  100. guard !isAnyOffline else { continue }
  101. if metadata.directory,
  102. let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", metadata.account, metadata.serverUrl + "/" + metadata.fileName)) {
  103. isAnyOffline = directory.offline
  104. } else if let localFile = NCManageDatabase.shared.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId)) {
  105. isAnyOffline = localFile.offline
  106. } // else: file is not offline, continue
  107. }
  108. enableLock = !isAnyDirectory && canUnlock && !NCGlobal.shared.capabilityFilesLockVersion.isEmpty
  109. }
  110. isSelectedEmpty = selectOcId.isEmpty
  111. }
  112. }
  113. struct NCCollectionViewCommonSelectTabBarView: View {
  114. @ObservedObject var tabBarSelect: NCCollectionViewCommonSelectTabBar
  115. @Environment(\.verticalSizeClass) var sizeClass
  116. var body: some View {
  117. VStack {
  118. Spacer().frame(height: sizeClass == .compact ? 5 : 10)
  119. HStack {
  120. Button {
  121. tabBarSelect.delegate?.share()
  122. } label: {
  123. Image(systemName: "square.and.arrow.up")
  124. .imageScale(sizeClass == .compact ? .medium : .large)
  125. }
  126. .frame(maxWidth: .infinity)
  127. .disabled(tabBarSelect.isSelectedEmpty || tabBarSelect.isAllDirectory)
  128. Button {
  129. tabBarSelect.delegate?.move()
  130. } label: {
  131. Image(systemName: "rectangle.portrait.and.arrow.right")
  132. .imageScale(sizeClass == .compact ? .medium : .large)
  133. }
  134. .frame(maxWidth: .infinity)
  135. .disabled(tabBarSelect.isSelectedEmpty)
  136. Button {
  137. tabBarSelect.delegate?.delete()
  138. } label: {
  139. Image(systemName: "trash")
  140. .imageScale(sizeClass == .compact ? .medium : .large)
  141. }
  142. .tint(.red)
  143. .frame(maxWidth: .infinity)
  144. .disabled(tabBarSelect.isSelectedEmpty)
  145. Menu {
  146. Button(action: {
  147. tabBarSelect.delegate?.saveAsAvailableOffline(isAnyOffline: tabBarSelect.isAnyOffline)
  148. }, label: {
  149. Label(NSLocalizedString(tabBarSelect.isAnyOffline ? "_remove_available_offline_" : "_set_available_offline_", comment: ""), systemImage: tabBarSelect.isAnyOffline ? "icloud.slash" : "icloud.and.arrow.down")
  150. if !tabBarSelect.canSetAsOffline && !tabBarSelect.isAnyOffline {
  151. Text(NSLocalizedString("_e2ee_set_as_offline_", comment: ""))
  152. }
  153. })
  154. .disabled(!tabBarSelect.isAnyOffline && (!tabBarSelect.canSetAsOffline || tabBarSelect.isSelectedEmpty))
  155. Button(action: {
  156. tabBarSelect.delegate?.lock(isAnyLocked: tabBarSelect.isAnyLocked)
  157. }, label: {
  158. Label(NSLocalizedString(tabBarSelect.isAnyLocked ? "_unlock_" : "_lock_", comment: ""), systemImage: tabBarSelect.isAnyLocked ? "lock.open" : "lock")
  159. if !tabBarSelect.enableLock {
  160. Text(NSLocalizedString("_lock_no_permissions_selected_", comment: ""))
  161. }
  162. })
  163. .disabled(!tabBarSelect.enableLock || tabBarSelect.isSelectedEmpty)
  164. Button(action: {
  165. tabBarSelect.delegate?.selectAll()
  166. }, label: {
  167. Label(NSLocalizedString("_select_all_", comment: ""), systemImage: "checkmark")
  168. })
  169. } label: {
  170. Image(systemName: "ellipsis.circle")
  171. .imageScale(sizeClass == .compact ? .medium : .large)
  172. }
  173. .frame(maxWidth: .infinity)
  174. }
  175. }
  176. .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
  177. .background(.thinMaterial)
  178. .overlay(Rectangle().frame(width: nil, height: 0.5, alignment: .top).foregroundColor(Color(UIColor.separator)), alignment: .top)
  179. }
  180. }
  181. #Preview {
  182. NCCollectionViewCommonSelectTabBarView(tabBarSelect: NCCollectionViewCommonSelectTabBar())
  183. }