Hide empty tab player accessory

This commit is contained in:
lucast committed 2026-10-07 22:43:50 +02:00
1 parent 875045a4a9
commit f4721a5a21
2 files changed
+105 -9

No files matched your search

+99
View File
@@ -1,4 +1,5 @@
import SwiftUI import SwiftUI
import UIKit
struct MainTabView: View { struct MainTabView: View {
@EnvironmentObject private var player: PlayerManager @EnvironmentObject private var player: PlayerManager
@@ -9,6 +10,10 @@ struct MainTabView: View {
.tabViewBottomAccessory { .tabViewBottomAccessory {
PlayerBottomAccessoryView() PlayerBottomAccessoryView()
} }
.overlay {
TabAccessoryVisibilityBridge(isVisible: player.currentTrack != nil)
.frame(width: 0, height: 0)
}
.animation(.snappy(duration: 0.25), value: player.currentTrack?.id) .animation(.snappy(duration: 0.25), value: player.currentTrack?.id)
.animation(.snappy(duration: 0.25), value: player.isMiniPlayerCompact) .animation(.snappy(duration: 0.25), value: player.isMiniPlayerCompact)
} }
@@ -45,6 +50,100 @@ struct MainTabView: View {
} }
} }
private struct TabAccessoryVisibilityBridge: UIViewControllerRepresentable {
let isVisible: Bool
func makeCoordinator() -> Coordinator {
Coordinator()
}
func makeUIViewController(context: Context) -> UIViewController {
let controller = UIViewController()
controller.view.isHidden = true
controller.view.isUserInteractionEnabled = false
return controller
}
func updateUIViewController(_ controller: UIViewController, context: Context) {
context.coordinator.scheduleUpdate(from: controller, isVisible: isVisible)
}
final class Coordinator {
private weak var tabBarController: UITabBarController?
private var savedAccessory: UITabAccessory?
private var updateGeneration = 0
func scheduleUpdate(from probe: UIViewController, isVisible: Bool) {
updateGeneration += 1
let generation = updateGeneration
retryUpdate(from: probe, isVisible: isVisible, generation: generation, attemptsRemaining: 8)
}
private func retryUpdate(
from probe: UIViewController,
isVisible: Bool,
generation: Int,
attemptsRemaining: Int
) {
DispatchQueue.main.async { [weak self, weak probe] in
guard let self, let probe, self.updateGeneration == generation else { return }
if let tabBarController = self.findTabBarController(from: probe) {
self.tabBarController = tabBarController
if let accessory = tabBarController.bottomAccessory {
self.savedAccessory = accessory
}
if isVisible {
if tabBarController.bottomAccessory == nil, let accessory = self.savedAccessory {
tabBarController.setBottomAccessory(accessory, animated: true)
}
} else if tabBarController.bottomAccessory != nil {
tabBarController.setBottomAccessory(nil, animated: true)
}
} else if attemptsRemaining > 0 {
self.retryUpdate(
from: probe,
isVisible: isVisible,
generation: generation,
attemptsRemaining: attemptsRemaining - 1
)
}
}
}
private func findTabBarController(from probe: UIViewController) -> UITabBarController? {
if let tabBarController = probe.tabBarController {
return tabBarController
}
guard let root = probe.viewIfLoaded?.window?.rootViewController else { return nil }
return findTabBarController(in: root)
}
private func findTabBarController(in controller: UIViewController) -> UITabBarController? {
if let tabBarController = controller as? UITabBarController {
return tabBarController
}
if let navigationController = controller as? UINavigationController,
let found = navigationController.viewControllers.lazy.compactMap(findTabBarController(in:)).first {
return found
}
for child in controller.children {
if let found = findTabBarController(in: child) { return found }
}
if let presented = controller.presentedViewController {
return findTabBarController(in: presented)
}
return nil
}
}
}
private struct LibraryTabView: View { private struct LibraryTabView: View {
var body: some View { var body: some View {
NavigationStack { NavigationStack {
@@ -20,18 +20,15 @@ struct PlayerBottomAccessoryView: View {
) )
.padding(.horizontal, placement == .inline ? 4 : 0) .padding(.horizontal, placement == .inline ? 4 : 0)
.frame(maxWidth: placement == .inline ? 320 : .infinity) .frame(maxWidth: placement == .inline ? 320 : .infinity)
.glassEffect(.regular.tint(.black.opacity(0.18)), in: Capsule()) .glassEffect(
player.currentTrack == nil ? .clear : .regular.tint(.black.opacity(0.18)),
in: Capsule()
)
.shadow(color: .black.opacity(placement == .inline ? 0 : 0.08), radius: 8, y: 3) .shadow(color: .black.opacity(placement == .inline ? 0 : 0.08), radius: 8, y: 3)
.offset(y: 1 / displayScale) .offset(y: 1 / displayScale)
.opacity(player.currentTrack == nil ? 0 : 1) .opacity(player.currentTrack == nil ? 0 : 1)
.overlay { .allowsHitTesting(player.currentTrack != nil)
Capsule() .accessibilityHidden(player.currentTrack == nil)
.fill(.black)
.padding(-5)
.opacity(player.currentTrack == nil ? 1 : 0)
.allowsHitTesting(false)
.accessibilityHidden(true)
}
.animation(.snappy(duration: 0.25), value: player.currentTrack?.id) .animation(.snappy(duration: 0.25), value: player.currentTrack?.id)
} }
} }