Compare commits

..
6 changed files with 137 additions and 56 deletions

No files matched your search

+10
View File
@@ -201,6 +201,16 @@ final class PlexClient: @unchecked Sendable {
}
/// Made on the server of the first song; songs from other servers are left out.
/// Servers a new playlist can live on, as (id, name).
var playlistServers: [(String, String)] { sources.map { ($0.server.id, $0.server.name) } }
/// An empty playlist on one server (the only one when [serverId] is nil).
func createEmptyPlaylist(_ title: String, serverId: String?) async throws -> Playlist? {
let list = sources
guard let s = list.first(where: { $0.server.id == serverId }) ?? list.first else { throw NotSignedIn() }
return try await s.createPlaylist(title, trackKeys: [])?.tagged(s.server.id)
}
func createPlaylist(_ title: String, trackKeys: [String]) async throws -> Playlist? {
guard let first = trackKeys.first else { return nil }
let (s, _) = try routed(first)
+3 -1
View File
@@ -266,7 +266,9 @@ final class PlexServer: MediaSource, @unchecked Sendable {
}
func createPlaylist(_ title: String, trackKeys: [String]) async throws -> Playlist? {
let uri = try await libraryUri(trackKeys)
// No songs: a bare library uri makes an empty playlist (newer Plex servers allow it).
if trackKeys.isEmpty, machine.isEmpty { machine = try await machineId() }
let uri = trackKeys.isEmpty ? "server://\(machine)/com.plexapp.plugins.library" : try await libraryUri(trackKeys)
return try await send("POST", "/playlists", [("type", "audio"), ("title", title), ("smart", 0), ("uri", uri)])
.metadata.first?.toPlaylist()
}
+3
View File
@@ -315,11 +315,14 @@ struct HingeEdge: ViewModifier {
struct Page<Content: View>: View {
let title: String
var large = true
/// Moves the content up into an empty nav bar.
var pullUp: CGFloat = 0
@ViewBuilder let content: () -> Content
@Environment(\.palette) private var p
var body: some View {
ScrollView {
LazyVStack(alignment: .leading, spacing: 0) { content() }
.padding(.top, -pullUp)
.padding(.bottom, 24)
}
.modifier(HingeEdge())
+66 -18
View File
@@ -54,13 +54,13 @@ struct MediaMenu {
playlistKey: nil, extra: [], confirmDownload: true, stemmed: stemmed(cov))
}
/// [changed] hears the playlist after a rename or a new cover, so an open page can follow.
func playlist(_ pl: Playlist, extra: [SheetAction] = [], changed: ((Playlist) -> Void)? = nil) -> [SheetAction] {
/// [changed] hears the playlist after a rename or a new cover, so an open page can follow; [deleted] after a delete.
func playlist(_ pl: Playlist, extra: [SheetAction] = [], changed: ((Playlist) -> Void)? = nil, deleted: (() -> Void)? = nil) -> [SheetAction] {
let plex = g.plex
let copy = SheetAction("Copy to Stem Playlist…", icon: "doc.on.doc") { copyToStems(pl) }
let gr = DownloadGroup(kind: .playlist, key: pl.ratingKey, title: pl.title, subtitle: "Playlist", thumb: pl.thumb)
return collection(pl.title, gr, { try await plex.playlistTracks(pl.ratingKey) }, playlistKey: pl.ratingKey,
extra: [copy] + (pl.smart ? [] : playlistEdits(pl, changed)) + extra,
extra: [copy] + (pl.smart ? [] : playlistEdits(pl, changed)) + extra + [delete(pl, deleted)],
confirmDownload: pl.trackCount > 50)
}
@@ -98,6 +98,24 @@ struct MediaMenu {
return [rename, cover]
}
private func delete(_ pl: Playlist, _ deleted: (() -> Void)?) -> SheetAction {
let plex = g.plex
let overlays = overlays
let ui = ui
return SheetAction("Delete Playlist…", icon: "trash", destructive: true) {
overlays.confirm("Delete “\(pl.title)”?", "This removes it from the server for every app.", "Delete") {
Task {
do {
try await plex.deletePlaylist(pl.ratingKey)
ui.playlistEdits += 1
overlays.hud("Deleted “\(pl.title)”", "trash")
deleted?()
} catch { overlays.hud("Couldn't delete: \(error.localizedDescription)", "exclamationmark.triangle") }
}
}
}
}
func stemPlaylist(_ pl: StemPlaylist, extra: [SheetAction] = [], onPage: Bool = false) -> [SheetAction] {
var a = [
SheetAction("Play Next", icon: "text.line.first.and.arrowtriangle.forward") {
@@ -111,23 +129,30 @@ struct MediaMenu {
{ pl.tracks }, confirm: false)
if pl.items.isEmpty { a = [] }
let lists = g.stemPlaylists
a.append(SheetAction("Rename…", icon: "pencil") {
overlays.prompt("Rename Stem Playlist", nil, initial: pl.title, "Save") { lists.rename(pl.id, $0) }
})
a.append(SheetAction("Change Cover…", icon: "photo") {
var seen = Set<String>()
var acts: [SheetAction] = [
SheetAction("Choose from Photos…", icon: "photo.on.rectangle") {
overlays.pickPhoto { data in
if !lists.setPhotoArt(pl.id, data) { overlays.hud("Couldn't use that photo", "exclamationmark.triangle") }
}
},
SheetAction("Automatic", icon: "wand.and.stars") { lists.setArt(pl.id, nil) },
]
for t in pl.tracks {
guard let th = t.thumb, seen.insert(th).inserted, seen.count <= 20 else { continue }
acts.append(SheetAction(t.album, icon: th == pl.artThumb ? "checkmark" : "photo") { lists.setArt(pl.id, th) })
}
overlays.actions("Choose a Cover", acts)
})
if !pl.items.isEmpty {
a.append(SheetAction("Change Cover…", icon: "photo") {
var seen = Set<String>()
var acts: [SheetAction] = [
SheetAction("Choose from Photos…", icon: "photo.on.rectangle") {
overlays.pickPhoto { data in
if !lists.setPhotoArt(pl.id, data) { overlays.hud("Couldn't use that photo", "exclamationmark.triangle") }
}
},
SheetAction("Automatic", icon: "wand.and.stars") { lists.setArt(pl.id, nil) },
]
for t in pl.tracks {
guard let th = t.thumb, seen.insert(th).inserted, seen.count <= 20 else { continue }
acts.append(SheetAction(t.album, icon: th == pl.artThumb ? "checkmark" : "photo") { lists.setArt(pl.id, th) })
}
overlays.actions("Choose a Cover", acts)
a.append(SheetAction("Set Mix for All Songs…", icon: "slider.horizontal.3") {
chooseMix(pl.tracks) { m in lists.setItems(pl.id, pl.items.map { var it = $0; it.mix = MixPresets.normalized(m); return it }) }
})
a.append(SheetAction("Copy to Plex Playlist…", icon: "doc.on.doc") { newPlaylist(pl.tracks) { ui.playlistEdits += 1 } })
}
a.append(SheetAction("Delete Stem Playlist", icon: "trash", destructive: true) {
overlays.confirm("Delete “\(pl.title)”?", "Stem playlists are only on this device.", "Delete") { lists.delete(pl.id); if onPage { ui.pop() } }
@@ -296,6 +321,29 @@ struct MediaMenu {
}
}
/// Empty playlist; asks which server first when more than one is signed in.
func newEmptyPlaylist(onCreated: @escaping () -> Void = {}) {
let plex = g.plex
let servers = plex.playlistServers
let make: (String?) -> Void = { id in
overlays.prompt("New Playlist", "Add songs later with “Add to Playlist”.", "Create", placeholder: "Playlist name") { name in
Task {
do {
_ = try await plex.createEmptyPlaylist(name, serverId: id)
overlays.hud("Created “\(name)”")
ui.playlistEdits += 1
onCreated()
} catch {
overlays.hud("Couldn't create: \(error.localizedDescription)", "exclamationmark.triangle")
}
}
}
}
if servers.count > 1 {
overlays.actions("Which Server?", servers.map { s in SheetAction(s.1.isEmpty ? "Server" : s.1) { make(s.0) } })
} else { make(servers.first?.0) }
}
func newPlaylist(_ tracks: [Track], onCreated: @escaping () -> Void = {}) {
let plex = g.plex
overlays.prompt("New Playlist", "Name it; you can add more songs later.", "Create", placeholder: "Playlist name") { name in
+54 -36
View File
@@ -10,6 +10,8 @@ struct PlaylistsScreen: View {
@State private var reload = 0
@AppStorage("playlistKind") private var kind = PlaylistKind.Plex
private let sort = SortState("playlists", PLAYLIST_SORTS)
private let stemSort = SortState("stemPlaylists", STEM_PLAYLIST_SORTS)
@State private var shuffleSeed = 0
private var g: AppGraph { .shared }
var body: some View {
@@ -31,7 +33,7 @@ struct PlaylistsScreen: View {
}
}
} else {
let stems = g.stemPlaylists.lists
let stems = g.stemPlaylists.lists.sorted(stemSort, seed: shuffleSeed)
if stems.isEmpty {
EmptyState(icon: "slider.horizontal.3", title: "No Stem Playlists",
message: "Each song plays with its own mix, like an instrumental or a cappella. Hold any song and choose “Add to Stem Playlist”, or copy a Plex playlist with +.")
@@ -42,11 +44,12 @@ struct PlaylistsScreen: View {
}
}
.toolbar {
if kind == .Plex { ToolbarItem(placement: .topBarTrailing) { SortMenu(sort: sort) } }
ToolbarItem(placement: .topBarTrailing) { SortMenu(sort: kind == .Plex ? sort : stemSort) }
ToolbarItem(placement: .topBarTrailing) {
Button { kind == .Plex ? newPlaylist() : newStemPlaylist() } label: { Image(systemName: "plus") }
}
}
.onChange(of: stemSort.token) { shuffleSeed += 1 }
.task(id: "\(g.settings.libraryKey)|\(sort.token)|\(reload)|\(ui.playlistEdits)") {
// /playlists may not take sort=random, so Random shuffles here.
let random = sort.option.random
@@ -54,18 +57,16 @@ struct PlaylistsScreen: View {
}
}
/// Plex can't hold an empty playlist, so new ones start from songs you have.
private func newPlaylist() {
let queue = g.playback.queue
var acts: [SheetAction] = []
var acts: [SheetAction] = [SheetAction("Empty Playlist…", icon: "plus") { ui.menu.newEmptyPlaylist { reload += 1 } }]
if !queue.isEmpty {
acts.append(SheetAction("New Playlist from Queue (\(queue.count))") { ui.menu.newPlaylist(queue.map(\.track)) { reload += 1 } })
}
if let now = g.playback.current?.track {
acts.append(SheetAction("New Playlist with “\(now.title)”") { ui.menu.newPlaylist([now]) { reload += 1 } })
}
if acts.isEmpty { ui.overlays.hud("Play something first, or use “Add to Playlist” on any song.", "info.circle") }
else { ui.overlays.actions("New Playlist", acts) }
ui.overlays.actions("New Playlist", acts)
}
private func newStemPlaylist() {
@@ -142,7 +143,7 @@ struct StemPlaylistScreen: View {
.navigationTitle(pl.title)
.navigationBarTitleDisplayMode(.inline)
} else {
Page(title: pl.title, large: false) {
Page(title: pl.title, large: false, pullUp: BAR_PULL) {
header(pl)
if pl.items.isEmpty {
EmptyState(icon: "slider.horizontal.3", title: "No Songs",
@@ -159,12 +160,11 @@ struct StemPlaylistScreen: View {
}
.toolbar {
ToolbarItemGroup(placement: .topBarTrailing) {
if !editing { MoreMenu(items: moreItems(pl)) }
if editing { Button("Done") { g.stemPlaylists.setItems(pl.id, items); editing = false }.bold() }
else if !pl.items.isEmpty { Button("Edit") { items = pl.items; editing = true } }
else { MoreMenu(items: moreItems(pl)) }
}
}
.modifier(SplitHidesTitle())
.modifier(SplitHidesTitle(bare: !editing))
}
private func header(_ pl: StemPlaylist) -> some View {
@@ -179,18 +179,40 @@ struct StemPlaylistScreen: View {
}
}
.frame(maxWidth: .infinity)
.padding(.top, 8).padding(.bottom, 16)
.padding(.bottom, 16)
}
private func moreItems(_ pl: StemPlaylist) -> [SheetAction] {
let lists = g.stemPlaylists
let rename = SheetAction("Rename…", icon: "pencil") {
ui.overlays.prompt("Rename Stem Playlist", nil, initial: pl.title, "Save") { lists.rename(pl.id, $0) }
let edit = pl.items.isEmpty ? [] : [SheetAction("Edit Songs", icon: "list.bullet") { items = pl.items; editing = true }]
return ui.menu.stemPlaylist(pl, extra: edit, onPage: true)
}
}
/// Stem playlists live on the device, so they sort here.
let STEM_PLAYLIST_SORTS = [
SortOption(title: "Title", key: "title", up: "A to Z", down: "Z to A"),
SortOption(title: "Date Added", key: "created", desc: true, up: "Oldest First", down: "Newest First"),
SortOption(title: "Last Updated", key: "updated", desc: true, up: "Oldest First", down: "Newest First"),
SortOption(title: "Songs", key: "count", desc: true, up: "Fewest First", down: "Most First"),
SortOption(title: "Duration", key: "duration", desc: true, up: "Shortest First", down: "Longest First"),
SortOption(title: "Random", key: "random", up: "Shuffled", down: "Shuffled"),
]
extension [StemPlaylist] {
func sorted(_ sort: SortState, seed: Int) -> [StemPlaylist] {
let o = sort.option
if o.random {
return map { ($0, ($0.id + "\(seed)").hashValue) }.sorted { $0.1 < $1.1 }.map(\.0)
}
let mixAll = SheetAction("Set Mix for All Songs…", icon: "slider.horizontal.3") {
ui.menu.chooseMix(pl.tracks) { m in lists.setItems(pl.id, pl.items.map { var it = $0; it.mix = MixPresets.normalized(m); return it }) }
let asc: [StemPlaylist]
switch o.key {
case "created": asc = sorted { $0.created < $1.created }
case "updated": asc = sorted { $0.updated < $1.updated }
case "count": asc = sorted { $0.items.count < $1.items.count }
case "duration": asc = sorted { $0.durationMs < $1.durationMs }
default: asc = sorted { $0.title.localizedStandardCompare($1.title) == .orderedAscending }
}
return ui.menu.stemPlaylist(pl, extra: pl.items.isEmpty ? [rename] : [rename, mixAll], onPage: true)
return sort.descending ? asc.reversed() : asc
}
}
@@ -257,7 +279,7 @@ struct PlaylistScreen: View {
.navigationTitle(title)
.navigationBarTitleDisplayMode(.inline)
} else {
Page(title: title, large: false) {
Page(title: title, large: false, pullUp: BAR_PULL) {
header(list)
Loaded(state: tracks, retry: { reload += 1 }) { list in
ForEach(Array(list.enumerated()), id: \.element.id) { i, t in
@@ -270,15 +292,12 @@ struct PlaylistScreen: View {
}
.toolbar {
ToolbarItemGroup(placement: .topBarTrailing) {
if !editing { MoreMenu(items: moreItems(list)) }
if !playlist.smart && !list.isEmpty {
if saving { ProgressView() }
else if editing { Button("Done") { commit(list) }.bold() }
else { Button("Edit") { items = list; removed = []; editing = true } }
}
if saving { ProgressView() }
else if editing { Button("Done") { commit(list) }.bold() }
else { MoreMenu(items: moreItems(list)) }
}
}
.modifier(SplitHidesTitle())
.modifier(SplitHidesTitle(bare: !editing))
.task(id: reload) {
await LoadState.load($tracks) { try await g.plex.playlistTracks(playlist.ratingKey) }
}
@@ -297,7 +316,7 @@ struct PlaylistScreen: View {
}
}
.frame(maxWidth: .infinity)
.padding(.top, 8).padding(.bottom, 16)
.padding(.bottom, 16)
}
private func removeAction(_ t: Track) -> [SheetAction] {
@@ -311,19 +330,13 @@ struct PlaylistScreen: View {
}
private func moreItems(_ list: [Track]) -> [SheetAction] {
var extra: [SheetAction] = []
let plex = g.plex
let key = playlist.ratingKey
extra.append(SheetAction("Delete Playlist", icon: "trash", destructive: true) {
ui.overlays.confirm("Delete “\(title)”?", "This removes it from Plex for every app.", "Delete") {
Task { if (try? await plex.deletePlaylist(key)) != nil { ui.pop() } }
}
})
let extra = playlist.smart || list.isEmpty ? [] :
[SheetAction("Edit Songs", icon: "list.bullet") { items = list; removed = []; editing = true }]
var pl = playlist
pl.title = title
pl.thumb = thumb
pl.trackCount = list.count
return ui.menu.playlist(pl, extra: extra) { title = $0.title; thumb = $0.thumb }
return ui.menu.playlist(pl, extra: extra, changed: { title = $0.title; thumb = $0.thumb }, deleted: { ui.pop() })
}
/// Sends removals, then replays the new order as "move after" calls against a simulated server list.
@@ -413,11 +426,16 @@ struct SearchScreen: View {
}
/// Split at the Duo's hinge the pane's own header already names the playlist, so the bar drops the title.
/// How far a playlist page's header rides up into the empty nav bar, between back and •••.
private let BAR_PULL: CGFloat = 40
/// Hides the bar title in split view, and while [bare] (the header already shows it).
private struct SplitHidesTitle: ViewModifier {
var bare = false
@Environment(Ui.self) private var ui
func body(content: Content) -> some View {
content.toolbar {
if ui.split { ToolbarItem(placement: .principal) { Color.clear.frame(width: 1, height: 1) } }
if ui.split || bare { ToolbarItem(placement: .principal) { Color.clear.frame(width: 1, height: 1) } }
}
}
}
+1 -1
View File
@@ -9,7 +9,7 @@ settings:
# iOS needs a plain number for the bundle version; the label is what the app and releases show.
MARKETING_VERSION: "0.10.1"
VERSION_LABEL: "0.10.1"
CURRENT_PROJECT_VERSION: "61"
CURRENT_PROJECT_VERSION: "64"
# Dev builds override these (build-ipa.sh dev) so they install next to the release app.
APP_ID: org.cdnutter.stemify
APP_NAME: Stemify