From f4721a5a21abc1eed3362520e7f0a20db036f1e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20T=C3=A4kker?= Date: Wed, 7 Oct 2026 22:43:50 +0200 Subject: [PATCH] Hide empty tab player accessory --- Music/View/MainTabView.swift | 99 +++++++++++++++++++ .../Player/PlayerBottomAccessoryView.swift | 15 ++- 2 files changed, 105 insertions(+), 9 deletions(-) diff --git a/Music/View/MainTabView.swift b/Music/View/MainTabView.swift index 41cc5f7..493a5b9 100644 --- a/Music/View/MainTabView.swift +++ b/Music/View/MainTabView.swift @@ -1,4 +1,5 @@ import SwiftUI +import UIKit struct MainTabView: View { @EnvironmentObject private var player: PlayerManager @@ -9,6 +10,10 @@ struct MainTabView: View { .tabViewBottomAccessory { 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.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 { var body: some View { NavigationStack { diff --git a/Music/View/Player/PlayerBottomAccessoryView.swift b/Music/View/Player/PlayerBottomAccessoryView.swift index ff82340..301413b 100644 --- a/Music/View/Player/PlayerBottomAccessoryView.swift +++ b/Music/View/Player/PlayerBottomAccessoryView.swift @@ -20,18 +20,15 @@ struct PlayerBottomAccessoryView: View { ) .padding(.horizontal, placement == .inline ? 4 : 0) .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) .offset(y: 1 / displayScale) .opacity(player.currentTrack == nil ? 0 : 1) - .overlay { - Capsule() - .fill(.black) - .padding(-5) - .opacity(player.currentTrack == nil ? 1 : 0) - .allowsHitTesting(false) - .accessibilityHidden(true) - } + .allowsHitTesting(player.currentTrack != nil) + .accessibilityHidden(player.currentTrack == nil) .animation(.snappy(duration: 0.25), value: player.currentTrack?.id) } }