259 lines
11 KiB
Swift
259 lines
11 KiB
Swift
import SwiftUI
|
||
import UniformTypeIdentifiers
|
||
|
||
struct LibraryView: View {
|
||
@EnvironmentObject var theme: Theme
|
||
@EnvironmentObject var ui: UIState
|
||
@EnvironmentObject var lib: Library
|
||
@EnvironmentObject var player: Player
|
||
@EnvironmentObject var prefs: Prefs
|
||
@State private var query = ""
|
||
@State private var importing = false
|
||
|
||
private let tabs = ["Songs", "Albums", "Artists", "Folders", "Genres"]
|
||
|
||
var body: some View {
|
||
let p = theme.palette
|
||
NavigationStack(path: $ui.libraryPath) {
|
||
VStack(spacing: 0) {
|
||
header
|
||
searchField
|
||
ScrollView(.horizontal, showsIndicators: false) {
|
||
HStack(spacing: 8) {
|
||
ForEach(tabs.indices, id: \.self) { i in
|
||
Chip(title: tabs[i], selected: prefs.libTab == i) {
|
||
withAnimation(.easeOut(duration: 0.15)) { prefs.libTab = i }
|
||
}
|
||
}
|
||
}
|
||
.padding(.horizontal, 16)
|
||
.padding(.vertical, 10)
|
||
}
|
||
content
|
||
}
|
||
.surface(p)
|
||
.toolbar(.hidden, for: .navigationBar)
|
||
.navigationDestination(for: LibraryRoute.self) { r in
|
||
switch r {
|
||
case .group(let g): GroupDetail(group: g)
|
||
case .smart(let k): SmartListView(key: k)
|
||
case .playlist(let id): PlaylistDetail(id: id)
|
||
}
|
||
}
|
||
}
|
||
.fileImporter(isPresented: $importing, allowedContentTypes: [.audio, .folder, .item], allowsMultipleSelection: true) { r in
|
||
if case .success(let urls) = r { importFiles(urls) }
|
||
}
|
||
}
|
||
|
||
private var header: some View {
|
||
let p = theme.palette
|
||
return HStack(spacing: 6) {
|
||
Text("Library")
|
||
.font(.system(size: 34, weight: .bold))
|
||
.foregroundColor(p.cOnSurface)
|
||
Spacer()
|
||
Menu {
|
||
Picker("Sort songs by", selection: $prefs.sortKey) {
|
||
Text("Title").tag("title")
|
||
Text("Artist").tag("artist")
|
||
Text("Album").tag("album")
|
||
Text("Year").tag("year")
|
||
Text("Recently added").tag("added")
|
||
Text("Length").tag("duration")
|
||
}
|
||
Divider()
|
||
Button { importing = true } label: { Label("Import from Files…", systemImage: "square.and.arrow.down") }
|
||
Button { lib.scan(force: true); ui.show("Rescanning…") } label: { Label("Rescan library", systemImage: "arrow.clockwise") }
|
||
} label: {
|
||
Image(systemName: "line.3.horizontal.decrease")
|
||
.font(.system(size: 20, weight: .medium))
|
||
.foregroundColor(p.cOnSurfaceVar)
|
||
.frame(width: 44, height: 44)
|
||
}
|
||
Pill(icon: "shuffle", title: "Shuffle", filled: true) {
|
||
player.playShuffled(query.isEmpty ? lib.songs : lib.search(query))
|
||
}
|
||
}
|
||
.padding(.leading, 20)
|
||
.padding(.trailing, 14)
|
||
.padding(.top, 8)
|
||
}
|
||
|
||
private var searchField: some View {
|
||
let p = theme.palette
|
||
return HStack(spacing: 12) {
|
||
Image(systemName: "magnifyingglass").font(.system(size: 18)).foregroundColor(p.cOnSurfaceVar)
|
||
TextField("", text: $query, prompt: Text("Search songs, artists, albums").foregroundColor(p.cOnSurfaceVar.opacity(0.8)))
|
||
.foregroundColor(p.cOnSurface)
|
||
.autocorrectionDisabled()
|
||
.textInputAutocapitalization(.never)
|
||
.submitLabel(.search)
|
||
if !query.isEmpty {
|
||
Button { query = "" } label: { Image(systemName: "xmark.circle.fill").foregroundColor(p.cOnSurfaceVar) }
|
||
}
|
||
}
|
||
.padding(.horizontal, 18)
|
||
.frame(height: 54)
|
||
.background(Capsule().fill(p.cSurfaceHigh))
|
||
.padding(.horizontal, 16)
|
||
.padding(.top, 8)
|
||
}
|
||
|
||
@ViewBuilder private var content: some View {
|
||
if lib.songs.isEmpty {
|
||
EmptyLibrary(importing: $importing)
|
||
} else if !query.isEmpty {
|
||
songList(lib.search(query))
|
||
} else {
|
||
switch prefs.libTab {
|
||
case 1: groupList(lib.albums)
|
||
case 2: groupList(lib.artists, circle: true)
|
||
case 3: groupList(lib.folders, folder: true)
|
||
case 4: groupList(lib.genres)
|
||
default: songList(lib.sorted(lib.songs, by: prefs.sortKey))
|
||
}
|
||
}
|
||
}
|
||
|
||
private func songList(_ songs: [Song]) -> some View {
|
||
ScrollView {
|
||
LazyVStack(spacing: 0) {
|
||
ForEach(Array(songs.enumerated()), id: \.element.id) { i, s in
|
||
SongRow(song: s, current: player.current?.id == s.id) { ui.songMenu(s, in: songs) }
|
||
.onTapGesture { player.playList(songs, start: i) }
|
||
.onLongPressGesture { ui.songMenu(s, in: songs) }
|
||
}
|
||
}
|
||
.padding(.bottom, 8)
|
||
}
|
||
.scrollDismissesKeyboard(.immediately)
|
||
}
|
||
|
||
private func groupList(_ groups: [Library.Group], circle: Bool = false, folder: Bool = false) -> some View {
|
||
ScrollView {
|
||
LazyVStack(spacing: 0) {
|
||
ForEach(groups) { g in
|
||
NavigationLink(value: LibraryRoute.group(g)) {
|
||
GroupRow(group: g, circle: circle, folder: folder)
|
||
}
|
||
.buttonStyle(.plain)
|
||
.simultaneousGesture(LongPressGesture().onEnded { _ in ui.groupMenu(g) })
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private func importFiles(_ urls: [URL]) {
|
||
let dest = Library.root.appendingPathComponent("Music")
|
||
var n = 0
|
||
for u in urls {
|
||
let ok = u.startAccessingSecurityScopedResource()
|
||
defer { if ok { u.stopAccessingSecurityScopedResource() } }
|
||
let ext = u.pathExtension.lowercased()
|
||
let target = (Library.playlistExts.contains(ext) ? Playlists.folder : dest).appendingPathComponent(u.lastPathComponent)
|
||
try? FileManager.default.removeItem(at: target)
|
||
if (try? FileManager.default.copyItem(at: u, to: target)) != nil { n += 1 }
|
||
}
|
||
ui.show("Imported \(n) item\(n == 1 ? "" : "s")")
|
||
lib.scan()
|
||
}
|
||
}
|
||
|
||
struct EmptyLibrary: View {
|
||
@Binding var importing: Bool
|
||
@EnvironmentObject var theme: Theme
|
||
@EnvironmentObject var lib: Library
|
||
|
||
var body: some View {
|
||
let p = theme.palette
|
||
VStack(spacing: 16) {
|
||
Spacer()
|
||
if lib.scanning {
|
||
ProgressView().tint(p.cPrimary)
|
||
Text("Scanning your music… \(lib.scanned)").foregroundColor(p.cOnSurfaceVar)
|
||
} else {
|
||
Image(systemName: "music.note.list").font(.system(size: 44)).foregroundColor(p.cPrimary)
|
||
Text("No music yet").font(.system(size: 22, weight: .semibold)).foregroundColor(p.cOnSurface)
|
||
Text("Open the Files app › On My iPhone › Mulberry › Music and copy your songs there, or import them here. Playlists go in the Playlists folder.")
|
||
.multilineTextAlignment(.center)
|
||
.foregroundColor(p.cOnSurfaceVar)
|
||
.padding(.horizontal, 32)
|
||
Pill(icon: "square.and.arrow.down", title: "Import from Files", filled: true) { importing = true }
|
||
Pill(icon: "arrow.clockwise", title: "Rescan") { lib.scan(force: true) }
|
||
}
|
||
Spacer()
|
||
Spacer()
|
||
}
|
||
.frame(maxWidth: .infinity)
|
||
}
|
||
}
|
||
|
||
/// Album / artist / folder / genre page.
|
||
struct GroupDetail: View {
|
||
let group: Library.Group
|
||
@EnvironmentObject var theme: Theme
|
||
@EnvironmentObject var ui: UIState
|
||
@EnvironmentObject var player: Player
|
||
@Environment(\.dismiss) var dismiss
|
||
|
||
var body: some View {
|
||
let p = theme.palette
|
||
ScrollView {
|
||
VStack(spacing: 0) {
|
||
HStack {
|
||
IconButton(icon: "chevron.left", tint: p.cOnSurface) { dismiss() }
|
||
Spacer()
|
||
IconButton(icon: "ellipsis", tint: p.cOnSurface) { ui.groupMenu(group) }
|
||
}
|
||
.padding(.horizontal, 8)
|
||
CoverThumb(song: group.cover, size: 220, radius: 28, circle: group.id.hasPrefix("artist:"))
|
||
.shadow(color: .black.opacity(0.25), radius: 16, y: 8)
|
||
.padding(.top, 4)
|
||
Text(group.title)
|
||
.font(.system(size: 26, weight: .bold))
|
||
.foregroundColor(p.cOnSurface)
|
||
.multilineTextAlignment(.center)
|
||
.padding(.top, 18)
|
||
.padding(.horizontal, 20)
|
||
Text(subtitle)
|
||
.font(.system(size: 15))
|
||
.foregroundColor(p.cOnSurfaceVar)
|
||
.padding(.top, 4)
|
||
HStack(spacing: 10) {
|
||
Pill(icon: "play.fill", title: "Play", filled: true) { player.playList(group.songs) }
|
||
Pill(icon: "shuffle", title: "Shuffle") { player.playShuffled(group.songs) }
|
||
}
|
||
.padding(.vertical, 16)
|
||
LazyVStack(spacing: 0) {
|
||
ForEach(Array(group.songs.enumerated()), id: \.element.id) { i, s in
|
||
SongRow(song: s, current: player.current?.id == s.id, showCover: !group.id.hasPrefix("album:"),
|
||
trailing: nil) { ui.songMenu(s, in: group.songs) }
|
||
.overlay(alignment: .leading) {
|
||
if group.id.hasPrefix("album:") {
|
||
Text(s.track > 0 ? "\(s.track)" : "")
|
||
.font(.system(size: 14).monospacedDigit())
|
||
.foregroundColor(p.cOnSurfaceVar)
|
||
.frame(width: 28)
|
||
.padding(.leading, 4)
|
||
}
|
||
}
|
||
.padding(.leading, group.id.hasPrefix("album:") ? 22 : 0)
|
||
.onTapGesture { player.playList(group.songs, start: i) }
|
||
}
|
||
}
|
||
}
|
||
}
|
||
.surface(p)
|
||
.toolbar(.hidden, for: .navigationBar)
|
||
}
|
||
|
||
private var subtitle: String {
|
||
let total = group.songs.reduce(0) { $0 + $1.duration }
|
||
let year = group.songs.map(\.year).filter { $0 > 0 }.max()
|
||
var parts = [group.subtitle]
|
||
if group.id.hasPrefix("album:"), let year { parts.append(String(year)) }
|
||
parts.append("\(group.songs.count) songs · \(Int(total / 60)) min")
|
||
return parts.filter { !$0.isEmpty }.joined(separator: " · ")
|
||
}
|
||
}
|