318 lines
15 KiB
Swift
318 lines
15 KiB
Swift
import SwiftUI
|
||
import UniformTypeIdentifiers
|
||
|
||
struct PlaylistsView: View {
|
||
@EnvironmentObject var theme: Theme
|
||
@EnvironmentObject var ui: UIState
|
||
@EnvironmentObject var lib: Library
|
||
@EnvironmentObject var lists: Playlists
|
||
@EnvironmentObject var stats: PlayStats
|
||
@EnvironmentObject var player: Player
|
||
@State private var importing = false
|
||
@State private var askNew = false
|
||
@State private var newName = ""
|
||
@State private var exportFormat = false
|
||
|
||
static let smart: [(key: String, title: String, icon: String)] = [
|
||
("fav", "Favourites", "heart.fill"), ("most", "Most played", "chart.line.uptrend.xyaxis"),
|
||
("recent", "Recently played", "clock.arrow.circlepath"), ("added", "Recently added", "plus.square.on.square"),
|
||
("never", "Never played", "eye"), ("rated", "Top rated", "star.fill"),
|
||
]
|
||
|
||
static func smartSongs(_ key: String) -> [Song] {
|
||
let all = Library.shared.songs, s = PlayStats.shared
|
||
switch key {
|
||
case "fav": return s.favourites(all)
|
||
case "most": return s.mostPlayed(all)
|
||
case "recent": return s.recentlyPlayed(all)
|
||
case "added": return s.recentlyAdded(all)
|
||
case "never": return s.neverPlayed(all)
|
||
case "rated": return s.topRated(all)
|
||
default: return []
|
||
}
|
||
}
|
||
|
||
var body: some View {
|
||
let p = theme.palette
|
||
NavigationStack {
|
||
ScrollView {
|
||
VStack(alignment: .leading, spacing: 0) {
|
||
Text("Playlists").font(.system(size: 34, weight: .bold)).foregroundColor(p.cOnSurface)
|
||
.padding(.horizontal, 20).padding(.top, 8)
|
||
ScrollView(.horizontal, showsIndicators: false) {
|
||
HStack(spacing: 8) {
|
||
Pill(icon: "plus", title: "New", filled: true) { newName = ""; askNew = true }
|
||
Pill(icon: "square.and.arrow.down", title: "Import") { importing = true }
|
||
Pill(icon: "square.and.arrow.up", title: "Export all") { exportFormat = true }
|
||
Pill(icon: "list.bullet.rectangle", title: "Save queue") {
|
||
guard !player.queue.isEmpty else { ui.show("The queue is empty"); return }
|
||
lists.create("Queue \(Date().formatted(date: .abbreviated, time: .shortened))", player.queue)
|
||
ui.show("Saved")
|
||
}
|
||
}
|
||
.padding(.horizontal, 16).padding(.vertical, 12)
|
||
}
|
||
SectionLabel(text: "Smart playlists")
|
||
ForEach(Self.smart, id: \.key) { sm in
|
||
NavigationLink(value: LibraryRoute.smart(sm.key)) {
|
||
listRow(icon: sm.icon, title: sm.title, sub: "\(Self.smartSongs(sm.key).count) songs", tile: p.cTertiary)
|
||
}
|
||
.buttonStyle(.plain)
|
||
}
|
||
SectionLabel(text: lists.all.isEmpty ? "Your playlists — none yet" : "Your playlists")
|
||
ForEach(lists.all) { pl in
|
||
NavigationLink(value: LibraryRoute.playlist(pl.id)) {
|
||
listRow(icon: "music.note.list", title: pl.name, sub: "\(pl.paths.count) songs", tile: p.cSecondaryC)
|
||
}
|
||
.buttonStyle(.plain)
|
||
.contextMenu { playlistMenu(pl) }
|
||
}
|
||
if lists.all.isEmpty {
|
||
Text("Tap New, import an .m3u/.pls/.xspf file, or drop playlists into Files › On My iPhone › Mulberry › Playlists.")
|
||
.font(.system(size: 14)).foregroundColor(p.cOnSurfaceVar)
|
||
.padding(.horizontal, 20).padding(.top, 4)
|
||
}
|
||
}
|
||
.padding(.bottom, 20)
|
||
}
|
||
.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: Self.playlistTypes, allowsMultipleSelection: true) { r in
|
||
guard case .success(let urls) = r else { return }
|
||
var msgs: [String] = []
|
||
for u in urls {
|
||
let ok = u.startAccessingSecurityScopedResource()
|
||
defer { if ok { u.stopAccessingSecurityScopedResource() } }
|
||
do {
|
||
let (pl, found, total) = try lists.importFile(u)
|
||
msgs.append("\(pl.name): \(found)/\(total)")
|
||
} catch { msgs.append("Import failed: \(error.localizedDescription)") }
|
||
}
|
||
ui.show(msgs.joined(separator: " · "))
|
||
}
|
||
.alert("New playlist", isPresented: $askNew) {
|
||
TextField("Name", text: $newName)
|
||
Button("Create") { lists.create(newName, []) }
|
||
Button("Cancel", role: .cancel) {}
|
||
}
|
||
.confirmationDialog("Export all playlists", isPresented: $exportFormat) {
|
||
ForEach(["m3u8", "m3u", "pls", "xspf", "wpl"], id: \.self) { f in
|
||
Button(f.uppercased()) {
|
||
let n = lists.exportAll(format: f)
|
||
ui.show(n == 0 ? "No playlists to export" : "Exported \(n) to Files › Mulberry › Playlists")
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
static var playlistTypes: [UTType] {
|
||
["m3u", "m3u8", "pls", "xspf", "wpl", "asx"].compactMap { UTType(filenameExtension: $0) } + [.m3uPlaylist, .plainText, .xml, .item]
|
||
}
|
||
|
||
private func listRow(icon: String, title: String, sub: String, tile: Color) -> some View {
|
||
let p = theme.palette
|
||
return HStack(spacing: 14) {
|
||
RoundedRectangle(cornerRadius: 12, style: .continuous).fill(tile.opacity(0.35))
|
||
.frame(width: 48, height: 48)
|
||
.overlay(Image(systemName: icon).font(.system(size: 19, weight: .semibold)).foregroundColor(p.cOnSurface))
|
||
VStack(alignment: .leading, spacing: 2) {
|
||
Text(title).font(.system(size: 16)).foregroundColor(p.cOnSurface)
|
||
Text(sub).font(.system(size: 14)).foregroundColor(p.cOnSurfaceVar)
|
||
}
|
||
Spacer()
|
||
}
|
||
.padding(.horizontal, 16).padding(.vertical, 6)
|
||
.contentShape(Rectangle())
|
||
}
|
||
|
||
@ViewBuilder private func playlistMenu(_ pl: Playlists.PL) -> some View {
|
||
Button { player.playList(lists.songs(pl)) } label: { Label("Play", systemImage: "play") }
|
||
Button { player.playShuffled(lists.songs(pl)) } label: { Label("Shuffle", systemImage: "shuffle") }
|
||
Button { if let u = try? lists.export(pl, format: "m3u8") { share([u]) } } label: { Label("Share", systemImage: "square.and.arrow.up") }
|
||
Button(role: .destructive) { lists.delete(pl) } label: { Label("Delete playlist", systemImage: "trash") }
|
||
}
|
||
}
|
||
|
||
struct SmartListView: View {
|
||
let key: String
|
||
@EnvironmentObject var stats: PlayStats
|
||
@EnvironmentObject var lib: Library
|
||
|
||
var body: some View {
|
||
let title = PlaylistsView.smart.first { $0.key == key }?.title ?? ""
|
||
SongListPage(title: title, songs: PlaylistsView.smartSongs(key))
|
||
}
|
||
}
|
||
|
||
struct PlaylistDetail: View {
|
||
let id: UUID
|
||
@EnvironmentObject var lists: Playlists
|
||
@EnvironmentObject var theme: Theme
|
||
@EnvironmentObject var player: Player
|
||
@EnvironmentObject var ui: UIState
|
||
@Environment(\.dismiss) var dismiss
|
||
@State private var renaming = false
|
||
@State private var name = ""
|
||
@State private var exporting = false
|
||
@State private var editing = false
|
||
|
||
var body: some View {
|
||
if let pl = lists.all.first(where: { $0.id == id }) {
|
||
let p = theme.palette
|
||
let songs = lists.songs(pl)
|
||
VStack(spacing: 0) {
|
||
HStack {
|
||
IconButton(icon: "chevron.left", tint: p.cOnSurface) { dismiss() }
|
||
Spacer()
|
||
Button(editing ? "Done" : "Edit") { withAnimation { editing.toggle() } }.foregroundColor(p.cPrimary)
|
||
Menu {
|
||
Button { name = pl.name; renaming = true } label: { Label("Rename", systemImage: "pencil") }
|
||
Button { exporting = true } label: { Label("Export…", systemImage: "square.and.arrow.up") }
|
||
Button { lists.removeDuplicates(pl) } label: { Label("Remove duplicates", systemImage: "square.on.square") }
|
||
Button { lists.removeMissing(pl) } label: { Label("Remove missing files", systemImage: "questionmark.square.dashed") }
|
||
Button(role: .destructive) { lists.delete(pl); dismiss() } label: { Label("Delete playlist", systemImage: "trash") }
|
||
} label: {
|
||
Image(systemName: "ellipsis").rotationEffect(.degrees(90)).foregroundColor(p.cOnSurface).frame(width: 44, height: 44)
|
||
}
|
||
}
|
||
.padding(.horizontal, 8)
|
||
Text(pl.name).font(.system(size: 28, weight: .bold)).foregroundColor(p.cOnSurface)
|
||
.frame(maxWidth: .infinity, alignment: .leading).padding(.horizontal, 20)
|
||
Text("\(songs.count) songs" + (pl.missing.isEmpty ? "" : " · \(pl.missing.count) not found"))
|
||
.font(.system(size: 14)).foregroundColor(p.cOnSurfaceVar)
|
||
.frame(maxWidth: .infinity, alignment: .leading).padding(.horizontal, 20)
|
||
HStack(spacing: 10) {
|
||
Pill(icon: "play.fill", title: "Play", filled: true) { player.playList(songs) }
|
||
Pill(icon: "shuffle", title: "Shuffle") { player.playShuffled(songs) }
|
||
Spacer()
|
||
}
|
||
.padding(.horizontal, 16).padding(.vertical, 12)
|
||
List {
|
||
ForEach(Array(songs.enumerated()), id: \.offset) { i, s in
|
||
SongRow(song: s, current: player.current?.id == s.id) { ui.songMenu(s, in: songs, playlist: pl, index: i) }
|
||
.listRowInsets(EdgeInsets())
|
||
.listRowBackground(Color.clear)
|
||
.listRowSeparator(.hidden)
|
||
.onTapGesture { player.playList(songs, start: i) }
|
||
}
|
||
.onDelete { lists.removeAt(pl, $0) }
|
||
.onMove { lists.move(pl, $0, $1) }
|
||
if !pl.missing.isEmpty {
|
||
Section("Not found in your library") {
|
||
ForEach(pl.missing, id: \.self) { m in
|
||
Text(m).font(.system(size: 14)).foregroundColor(p.cOnSurfaceVar.opacity(0.6))
|
||
.listRowBackground(Color.clear)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
.listStyle(.plain)
|
||
.scrollContentBackground(.hidden)
|
||
.environment(\.editMode, .constant(editing ? .active : .inactive))
|
||
}
|
||
.surface(p)
|
||
.toolbar(.hidden, for: .navigationBar)
|
||
.alert("Rename playlist", isPresented: $renaming) {
|
||
TextField("Name", text: $name)
|
||
Button("Rename") { lists.rename(pl, name) }
|
||
Button("Cancel", role: .cancel) {}
|
||
}
|
||
.confirmationDialog("Export", isPresented: $exporting) {
|
||
ForEach(["m3u8", "m3u", "pls", "xspf", "wpl"], id: \.self) { f in
|
||
Button(f.uppercased()) { if let u = try? lists.export(pl, format: f) { share([u]) } }
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Plain titled song list page (smart lists).
|
||
struct SongListPage: View {
|
||
let title: String
|
||
let songs: [Song]
|
||
@EnvironmentObject var theme: Theme
|
||
@EnvironmentObject var player: Player
|
||
@EnvironmentObject var ui: UIState
|
||
@Environment(\.dismiss) var dismiss
|
||
|
||
var body: some View {
|
||
let p = theme.palette
|
||
VStack(spacing: 0) {
|
||
HStack {
|
||
IconButton(icon: "chevron.left", tint: p.cOnSurface) { dismiss() }
|
||
Spacer()
|
||
}
|
||
.padding(.horizontal, 8)
|
||
Text(title).font(.system(size: 28, weight: .bold)).foregroundColor(p.cOnSurface)
|
||
.frame(maxWidth: .infinity, alignment: .leading).padding(.horizontal, 20)
|
||
HStack(spacing: 10) {
|
||
Pill(icon: "play.fill", title: "Play", filled: true) { player.playList(songs) }
|
||
Pill(icon: "shuffle", title: "Shuffle") { player.playShuffled(songs) }
|
||
Spacer()
|
||
}
|
||
.padding(.horizontal, 16).padding(.vertical, 12)
|
||
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) }
|
||
}
|
||
}
|
||
}
|
||
}
|
||
.surface(p)
|
||
.toolbar(.hidden, for: .navigationBar)
|
||
}
|
||
}
|
||
|
||
struct AddToPlaylistSheet: View {
|
||
let songs: [Song]
|
||
@EnvironmentObject var theme: Theme
|
||
@EnvironmentObject var lists: Playlists
|
||
@EnvironmentObject var ui: UIState
|
||
@Environment(\.dismiss) var dismiss
|
||
@State private var newName = ""
|
||
|
||
var body: some View {
|
||
let p = theme.palette
|
||
NavigationStack {
|
||
List {
|
||
Section {
|
||
HStack {
|
||
TextField("New playlist…", text: $newName)
|
||
Button("Create") {
|
||
lists.create(newName, songs)
|
||
ui.show("Added to \(newName.isEmpty ? "New playlist" : newName)")
|
||
dismiss()
|
||
}
|
||
.disabled(newName.trimmingCharacters(in: .whitespaces).isEmpty)
|
||
}
|
||
}
|
||
Section("Your playlists") {
|
||
ForEach(lists.all) { pl in
|
||
Button {
|
||
lists.add(pl, songs)
|
||
ui.show("Added to \(pl.name)")
|
||
dismiss()
|
||
} label: {
|
||
HStack { Text(pl.name).foregroundColor(p.cOnSurface); Spacer(); Text("\(pl.paths.count)").foregroundColor(p.cOnSurfaceVar) }
|
||
}
|
||
}
|
||
}
|
||
}
|
||
.scrollContentBackground(.hidden)
|
||
.navigationTitle("Add \(songs.count) song\(songs.count == 1 ? "" : "s") to playlist")
|
||
.navigationBarTitleDisplayMode(.inline)
|
||
}
|
||
.presentationDetents([.medium, .large])
|
||
.presentationBackground(p.cSurfaceLow)
|
||
}
|
||
}
|