193 lines
6.3 KiB
Swift
193 lines
6.3 KiB
Swift
import SwiftUI
|
|
import UIKit
|
|
|
|
struct MainTabView: View {
|
|
@EnvironmentObject private var player: PlayerManager
|
|
@AppStorage("music.selected-tab") private var selectedTabRawValue = MainTab.home.rawValue
|
|
|
|
var body: some View {
|
|
tabs
|
|
.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)
|
|
}
|
|
|
|
private var tabs: some View {
|
|
TabView(selection: selectedTab) {
|
|
Tab("Home", systemImage: "house", value: MainTab.home) {
|
|
HomeView()
|
|
}
|
|
|
|
Tab("Library", systemImage: "books.vertical", value: MainTab.library) {
|
|
LibraryTabView()
|
|
}
|
|
|
|
Tab("Search", systemImage: "magnifyingglass", value: MainTab.search, role: .search) {
|
|
SearchTabView()
|
|
}
|
|
}
|
|
.tint(.red)
|
|
.tabBarMinimizeBehavior(player.currentTrack == nil ? .never : .onScrollDown)
|
|
}
|
|
|
|
private var selectedTab: Binding<MainTab> {
|
|
Binding(
|
|
get: { MainTab(rawValue: selectedTabRawValue) ?? .home },
|
|
set: { selectedTabRawValue = $0.rawValue }
|
|
)
|
|
}
|
|
|
|
private enum MainTab: String, Hashable {
|
|
case home
|
|
case library
|
|
case search
|
|
}
|
|
}
|
|
|
|
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 {
|
|
ContentUnavailableView(
|
|
"Your Library",
|
|
systemImage: "books.vertical",
|
|
description: Text("Your saved songs will appear here.")
|
|
)
|
|
.navigationTitle("Library")
|
|
.navigationBarTitleDisplayMode(.large)
|
|
}
|
|
}
|
|
}
|
|
|
|
private struct SearchTabView: View {
|
|
var body: some View {
|
|
NavigationStack {
|
|
ContentUnavailableView(
|
|
"Search Music",
|
|
systemImage: "magnifyingglass",
|
|
description: Text("Search for songs, albums, and artists.")
|
|
)
|
|
.navigationTitle("Search")
|
|
.navigationBarTitleDisplayMode(.large)
|
|
}
|
|
}
|
|
}
|
|
|
|
struct MainTabView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
MainTabView()
|
|
.environmentObject(PlayerManager())
|
|
}
|
|
}
|
|
|
|
private struct LibraryTabView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
LibraryTabView()
|
|
}
|
|
}
|
|
|
|
private struct SearchTabView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
SearchTabView()
|
|
}
|
|
}
|