aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/modules/sonix/SonixPlaylists.qml
blob: 07e97baf1f1e35aead82fd2d13ee9f4745bb103c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.


import QtQuick
import "../.."
import "../appearance"
import "../sound"
import "Sonix.js" as S

// Playlists: list, play, open, remove tracks, and new names that become
// playlists when Folders adds their first track. No rename, delete or
// reorder: SonixLink has none (README.md).
Column {
    id: pl
    required property var mod
    property var lists: []
    property string open: ""
    property var tracks: []
    property string error: ""
    property var rev: undefined
    spacing: 8

    function glyph(cp) { return String.fromCodePoint(cp); }
    function report(ok, d, err) { pl.error = ok ? "" : err; }

    function load() {
        Sonix.get("/api/playlists", (ok, d, err) => {
            pl.report(ok, d, err);
            if (!ok) return;
            pl.lists = d.playlists || [];
            const names = pl.lists.map(l => l.name);
            pl.mod.draftPlaylists = pl.mod.draftPlaylists.filter(n => names.indexOf(n) < 0);
        });
        if (pl.open !== "") pl.loadTracks();
    }
    function loadTracks() {
        Sonix.get("/api/playlist?" + S.query({ name: pl.open }), (ok, d, err) => {
            pl.report(ok, d, err);
            if (ok && d.name === pl.open) pl.tracks = d.tracks || [];
        });
    }

    Component.onCompleted: { pl.rev = Sonix.player.playlists_revision; pl.load(); }
    // Edits made on the player show up: the state carries a revision.
    Connections {
        target: Sonix
        function onPlayerChanged() {
            const r = Sonix.player.playlists_revision;
            if (r !== undefined && r !== pl.rev) { pl.rev = r; pl.load(); }
        }
    }

    // --- the list ---------------------------------------------------------

    Row {
        visible: pl.open === ""
        spacing: 8
        Field { id: newName; width: 220; placeholder: "New playlist name" }
        Button {
            text: "New"
            onClicked: {
                const n = newName.value.trim();
                if (n === "" || pl.lists.some(l => l.name === n) || pl.mod.draftPlaylists.indexOf(n) >= 0) return;
                pl.mod.draftPlaylists = pl.mod.draftPlaylists.concat([n]);
                newName.value = "";
            }
        }
    }

    Repeater {
        model: pl.open === "" ? pl.lists : []
        delegate: SonixRow {
            required property var modelData
            width: pl.width
            text: modelData.name
            sub: modelData.count + " tracks"
            onClicked: { pl.open = modelData.name; pl.tracks = []; pl.loadTracks(); }
            TransportButton {
                glyph: pl.glyph(0xF040A)  // md-play
                onClicked: Sonix.command({ do: "play_all", list: "playlist", value: modelData.name }, null, pl.report)
            }
        }
    }

    Repeater {
        model: pl.open === "" ? pl.mod.draftPlaylists : []
        delegate: SonixRow {
            required property var modelData
            width: pl.width
            text: modelData
            sub: "empty: add its first track from Folders"
        }
    }

    // --- one playlist -----------------------------------------------------

    Row {
        visible: pl.open !== ""
        spacing: 8
        Button { text: "Playlists"; icon: "\uF053"; onClicked: { pl.open = ""; pl.tracks = []; pl.load(); } }
        Button {
            text: "Play all"
            onClicked: Sonix.command({ do: "play_all", list: "playlist", value: pl.open }, null, pl.report)
        }
    }

    Text {
        textFormat: Text.PlainText
        visible: pl.open !== ""
        text: pl.open
        font { family: Theme.fontFamily; pixelSize: Theme.fontSize; bold: true }
        color: Theme.text
    }

    Repeater {
        model: pl.open !== "" ? pl.tracks : []
        delegate: SonixRow {
            required property var modelData
            width: pl.width
            text: modelData.title || (modelData.path || "").split("/").pop()
            sub: modelData.artist || ""
            TransportButton {
                glyph: pl.glyph(0xF040A)  // md-play
                onClicked: Sonix.command({ do: "play_path", path: modelData.path, list: "playlist", value: pl.open },
                                         null, pl.report)
            }
            TransportButton {
                glyph: pl.glyph(0xF0156)  // md-close
                onClicked: Sonix.command({ do: "playlist_remove", value: pl.open }, modelData.path,
                                         (ok, d, err) => { pl.report(ok, d, err); if (ok) pl.loadTracks(); })
            }
        }
    }

    Text {
        visible: pl.error !== ""
        width: pl.width
        wrapMode: Text.Wrap
        text: pl.error
        font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 4 }
        color: Theme.red
    }
}