Repository Public
1// SP Music iOS2// File: Features/Root/RootView.swift3// Description: Launch gate, app lock, iPhone tab layout and iPad split-view layout.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai78import SPMusicCore9import SwiftUI1011/// The gate: launch screen, sign-in, app lock — then the iPhone tabs or the iPad split view.12struct RootView: View {13 @Environment(AppEnvironment.self) private var env14 @Environment(\.horizontalSizeClass) private var hSize15 @Environment(\.scenePhase) private var phase1617 var body: some View {18 @Bindable var env = env19 ZStack {20 Color.spBackground.ignoresSafeArea()21 if !env.started || env.auth.state == .launching {22 VStack(spacing: 18) { LogoMark().frame(width: 96, height: 96); ProgressView() }23 } else if env.auth.canUseLibrary {24 Group {25 if hSize == .regular {26 // iPad: the player takes the whole window — cover and controls on one side, queue or lyrics on the other.27 PadShell().fullScreenCover(isPresented: $env.showNowPlaying) { NowPlayingView().environment(env) }28 } else {29 PhoneShell().sheet(isPresented: $env.showNowPlaying) { NowPlayingView().presentationDetents([.large]).presentationBackground(.clear) }30 }31 }32 .toast($env.toast)33 } else {34 WelcomeView()35 }36 if env.isLocked && env.started { LockScreen() }37 }38 .tint(Color.spEmber)39 .preferredColorScheme(.dark)40 .task { await env.start() }41 .onChange(of: phase) { _, new in42 if new == .active { Task { await env.sceneBecameActive() } }43 else if new == .background { env.sceneWentToBackground() }44 }45 }46}4748struct LockScreen: View {49 @Environment(AppEnvironment.self) private var env50 var body: some View {51 ZStack {52 Rectangle().fill(.ultraThickMaterial).ignoresSafeArea()53 VStack(spacing: 20) {54 LogoMark().frame(width: 88, height: 88)55 Text("SP Music est verrouillé").font(.title3.weight(.semibold))56 Button { Task { await env.unlock() } } label: { Label("Déverrouiller avec \(BiometricLock.kindName)", systemImage: "faceid") }.buttonStyle(.borderedProminent)57 }58 }59 .accessibilityIdentifier("lock-screen")60 }61}6263extension View {64 /// Every destination of the app, declared once per navigation stack.65 func appDestinations() -> some View {66 navigationDestination(for: Route.self) { route in67 switch route {68 case .album(let id): AlbumDetailView(albumId: id)69 case .artist(let id): ArtistDetailView(artistId: id)70 case .playlist(let id): PlaylistDetailView(playlistId: id)71 case .albums: AlbumsView()72 case .downloaded: TrackListView(kind: .downloaded)73 case .artists: ArtistsView()74 case .tracks: TrackListView(kind: .all)75 case .playlists: PlaylistsView()76 case .favorites: TrackListView(kind: .favorites)77 case .recentlyPlayed: TrackListView(kind: .recent)78 case .storage: StorageView()79 case .account: AccountView()80 case .networkSettings: NetworkSettingsView()81 case .audioSettings: AudioSettingsView()82 }83 }84 }85}8687// MARK: iPhone8889struct PhoneShell: View {90 @Environment(AppEnvironment.self) private var env91 enum TabID: Hashable { case home, library, search, downloads, settings }92 @State private var tab = TabID.home9394 var body: some View {95 TabView(selection: $tab) {96 Tab("Accueil", systemImage: "house.fill", value: TabID.home) { NavigationStack { HomeView().appDestinations() } }97 Tab("Bibliothèque", systemImage: "square.stack.fill", value: TabID.library) { NavigationStack { LibraryView().appDestinations() } }98 Tab("Téléchargements", systemImage: "arrow.down.circle.fill", value: TabID.downloads) { NavigationStack { DownloadsView().appDestinations() } }99 .badge(env.downloadsModel.activeCount)100 Tab("Réglages", systemImage: "gearshape.fill", value: TabID.settings) { NavigationStack { SettingsView().appDestinations() } }101 Tab("Recherche", systemImage: "magnifyingglass", value: TabID.search, role: .search) { NavigationStack { SearchView().appDestinations() } }102 }103 .tabBarMinimizeBehavior(.onScrollDown)104 .tabViewBottomAccessory(isEnabled: env.player.current != nil) { MiniPlayer() }105 }106}107108// MARK: iPad109110/// A real tablet layout: a sidebar with the library sections and the playlists (which accept dropped tracks),111/// a content column, and a full-width player bar. Falls back to the iPhone tabs when the window gets narrow.112struct PadShell: View {113 @Environment(AppEnvironment.self) private var env114 enum Item: Hashable { case home, search, albums, artists, tracks, favorites, recent, downloads, settings, playlist(String), playlists }115 @State private var selection: Item? = .home116 @State private var path = NavigationPath()117 @State private var playlists: [Playlist] = []118 @State private var columns = NavigationSplitViewVisibility.all119120 var body: some View {121 NavigationSplitView(columnVisibility: $columns) {122 List(selection: $selection) {123 Section {124 Label("Accueil", systemImage: "house").tag(Item.home)125 Label("Recherche", systemImage: "magnifyingglass").tag(Item.search)126 }127 Section("Bibliothèque") {128 Label("Albums", systemImage: "square.stack").tag(Item.albums)129 Label("Artistes", systemImage: "music.microphone").tag(Item.artists)130 Label("Titres", systemImage: "music.note").tag(Item.tracks)131 Label("Favoris", systemImage: "heart").tag(Item.favorites)132 Label("Récemment écoutés", systemImage: "clock.arrow.circlepath").tag(Item.recent)133 }134 Section("Listes de lecture") {135 Label("Toutes les listes", systemImage: "music.note.list").tag(Item.playlists)136 ForEach(playlists) { p in137 Label(p.name, systemImage: "music.note.list").tag(Item.playlist(p.id))138 .dropDestination(for: String.self) { items, _ in139 let ids = items.compactMap { $0.hasPrefix("spmusic-track:") ? String($0.dropFirst(14)) : nil }140 guard !ids.isEmpty else { return false }141 Task { if await env.playlistEdit({ try await $0.addToPlaylist(id: p.id, trackIds: ids) }) { env.toast = "Ajouté à « \(p.name) »" } }142 return true143 }144 }145 }146 Section {147 Label("Téléchargements", systemImage: "arrow.down.circle").badge(env.downloadsModel.activeCount).tag(Item.downloads)148 Label("Réglages", systemImage: "gearshape").tag(Item.settings)149 }150 }151 .navigationTitle("SP Music")152 .scrollContentBackground(.hidden).background(Color.spSurface)153 } detail: {154 NavigationStack(path: $path) {155 Group {156 switch selection ?? .home {157 case .home: HomeView()158 case .search: SearchView()159 case .albums: AlbumsView()160 case .artists: ArtistsView()161 case .tracks: TrackListView(kind: .all)162 case .favorites: TrackListView(kind: .favorites)163 case .recent: TrackListView(kind: .recent)164 case .downloads: DownloadsView()165 case .settings: SettingsView()166 case .playlists: PlaylistsView()167 case .playlist(let id): PlaylistDetailView(playlistId: id)168 }169 }170 .appDestinations()171 }172 }173 .navigationSplitViewStyle(.balanced)174 // Across the whole window, under the sidebar and the content, like a transport bar.175 .safeAreaInset(edge: .bottom, spacing: 0) { PlayerBar() }176 .onChange(of: selection) { _, _ in path = NavigationPath() }177 .task(id: env.libraryVersion) { playlists = await env.db.playlists() }178 // Hardware keyboard: ⌘F search, ⌘1…⌘5 sections (playback shortcuts are in the "Lecture" menu).179 .background {180 Group {181 Button("") { selection = .search }.keyboardShortcut("f", modifiers: .command)182 Button("") { selection = .home }.keyboardShortcut("1", modifiers: .command)183 Button("") { selection = .albums }.keyboardShortcut("2", modifiers: .command)184 Button("") { selection = .artists }.keyboardShortcut("3", modifiers: .command)185 Button("") { selection = .tracks }.keyboardShortcut("4", modifiers: .command)186 Button("") { selection = .downloads }.keyboardShortcut("5", modifiers: .command)187 }188 .opacity(0).accessibilityHidden(true)189 }190 }191}192