Repository Public
1// SP Music iOS2// File: Features/Home/HomeView.swift3// Description: Home screen and the status banner (offline, signed out, synchronising).4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai78import SPMusicCore9import SwiftUI1011struct HomeView: View {12 @Environment(AppEnvironment.self) private var env13 @State private var recentAlbums: [Album] = []14 @State private var recentlyPlayed: [Track] = []15 @State private var playlists: [Playlist] = []16 @State private var downloaded: [Album] = []17 @State private var counts = LibraryCounts()18 @State private var loaded = false19 @State private var playlistFor: Track?2021 var body: some View {22 ScrollView {23 LazyVStack(alignment: .leading, spacing: 28) {24 StatusBanner()25 if loaded && counts.tracks == 0 {26 ContentUnavailableView {27 Label(env.downloadedOnly ? "Rien sur cet appareil" : "Bibliothèque vide", systemImage: "music.note.house")28 } description: {29 Text(env.downloadedOnly ? "Téléchargez des albums pour les écouter sans connexion." : "Votre musique apparaîtra ici après la première synchronisation avec MacLustr.")30 } actions: {31 if !env.downloadedOnly { Button("Synchroniser") { Task { await env.syncNow() } }.buttonStyle(.borderedProminent) }32 }33 .padding(.top, 60)34 }35 if !recentlyPlayed.isEmpty {36 VStack(alignment: .leading, spacing: 10) {37 SectionTitle(title: "Récemment écoutés", route: .recentlyPlayed)38 VStack(spacing: 10) {39 ForEach(Array(recentlyPlayed.prefix(4).enumerated()), id: \.element.id) { i, t in40 TrackRow(track: t, onAddToPlaylist: { playlistFor = $0 }) { env.play(recentlyPlayed, startAt: i) }41 }42 }43 .padding(.horizontal)44 }45 }46 if !recentAlbums.isEmpty { shelf(env.downloadedOnly ? "Sur cet appareil" : "Ajouts récents", .albums, recentAlbums) }47 if !playlists.isEmpty {48 VStack(alignment: .leading, spacing: 10) {49 SectionTitle(title: "Listes de lecture", route: .playlists)50 ScrollView(.horizontal) {51 LazyHStack(alignment: .top, spacing: 14) {52 ForEach(playlists) { p in53 NavigationLink(value: Route.playlist(p.id)) {54 VStack(alignment: .leading, spacing: 6) {55 MosaicArtwork(ids: p.artworkIds, side: 150, corner: 10)56 Text(p.name).font(.subheadline.weight(.semibold)).lineLimit(1)57 Text("\(p.trackCount) titre\(p.trackCount > 1 ? "s" : "")").font(.footnote).foregroundStyle(Color.spMuted)58 }59 .frame(width: 150, alignment: .leading)60 }61 .buttonStyle(.plain)62 }63 }64 .padding(.horizontal)65 }66 .scrollIndicators(.hidden)67 }68 }69 if !downloaded.isEmpty && !env.downloadedOnly { shelf("Téléchargés", .downloaded, downloaded) }70 if counts.tracks > 0 {71 Text("\(counts.tracks) titres · \(counts.albums) albums · \(counts.artists) artistes" + (counts.downloadedTracks > 0 ? " · \(counts.downloadedTracks) sur cet appareil" : ""))72 .font(.footnote).foregroundStyle(Color.spMuted).frame(maxWidth: .infinity).padding(.bottom, 8)73 .accessibilityIdentifier("library-counts")74 }75 }76 .padding(.vertical, 12)77 }78 .background(Color.spBackground)79 .navigationTitle("Accueil")80 .refreshable { await env.syncNow() }81 .sheet(item: $playlistFor) { AddToPlaylistSheet(tracks: [$0]) }82 .task(id: env.reloadKey()) {83 let only = env.downloadedOnly84 recentAlbums = await env.db.albums(sort: .recent, downloadedOnly: only, limit: 14)85 recentlyPlayed = await env.db.recentlyPlayed(limit: 12, downloadedOnly: only)86 playlists = await env.db.playlists()87 downloaded = await env.db.albums(sort: .recent, downloadedOnly: true, limit: 14)88 counts = await env.db.counts()89 if only { counts.tracks = counts.downloadedTracks; counts.albums = counts.downloadedAlbums }90 loaded = true91 }92 }9394 private func shelf(_ title: String, _ route: Route, _ albums: [Album]) -> some View {95 VStack(alignment: .leading, spacing: 10) {96 SectionTitle(title: title, route: route)97 ScrollView(.horizontal) {98 LazyHStack(alignment: .top, spacing: 14) {99 ForEach(albums) { a in NavigationLink(value: Route.album(a.id)) { AlbumCard(album: a, width: 150) }.buttonStyle(.plain) }100 }101 .padding(.horizontal)102 }103 .scrollIndicators(.hidden)104 }105 }106}107108/// One line that says why the library looks the way it does: offline, signed out, synchronising, or sync failed.109struct StatusBanner: View {110 @Environment(AppEnvironment.self) private var env111 var body: some View {112 Group {113 if !env.isOnline { banner("Hors ligne — musique téléchargée uniquement", "airplane", .spMuted) }114 else if env.auth.state != .signedIn { banner("Non connecté — musique téléchargée uniquement", "person.crop.circle.badge.exclamationmark", .yellow) }115 else if case .failed(let why) = env.syncState { banner(why, "exclamationmark.icloud", .yellow) }116 else if env.syncState == .running { banner("Synchronisation avec MacLustr…", "arrow.triangle.2.circlepath", .spMuted) }117 else if env.settings.downloadedOnly { banner("Affichage limité à la musique téléchargée", "arrow.down.circle", .spMuted) }118 }119 }120 private func banner(_ text: String, _ icon: String, _ color: Color) -> some View {121 Label(text, systemImage: icon).font(.footnote.weight(.medium)).foregroundStyle(color)122 .frame(maxWidth: .infinity, alignment: .leading).padding(.horizontal, 14).padding(.vertical, 10)123 .background(Color.spSurface, in: .rect(cornerRadius: 12)).padding(.horizontal)124 .accessibilityIdentifier("status-banner")125 }126}127