summaryrefslogtreecommitdiffstats
path: root/MusicConverter/View/Napster/NapsterPlaylistView.swift
blob: 0180dbbdd7a24b6bf0b16b6606c97c78b7c4c838 (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
//
//  NapsterPlaylist.swift
//  MusicConverter
//
//  Created by Finn Behrens on 02.04.21.
//

import SwiftUI

struct NapsterPlaylistView: View {
    @ObservedObject var napsterPlaylist: Napster
    @State private var showingSongs = false
    @State private var updated = false
    
    
    init(id: String, api: AppleMusicApi) {
        self.napsterPlaylist = Napster(id: id, api: api)
        
        self.napsterPlaylist.update(completion: {e in
            if e != nil {
                print("error updating: \(e!)")
                return
            }
        })
    }
    
    init(demoData: String) {
        self.napsterPlaylist = Napster(demo: demoData)
    }
    
    var body: some View {
        ScrollView {
            VStack(alignment: .leading, spacing: 10) {
                Text(self.napsterPlaylist.name)
                    .bold()
                    .font(.title)
                
                Divider()
                Button(action: { showingSongs.toggle() }) {
                    Text("Songs (\(self.napsterPlaylist.appleChoices.count))")
                }
            }
            Text("trackCount: \(napsterPlaylist.tracks?.count ?? 0)")
            Divider()
            NapsterPlaylistView_CreatButton(napsterPlaylist: napsterPlaylist)
        }
        .sheet(isPresented: $showingSongs) {
            NapsterSongListView(napsterPlaylist: napsterPlaylist)
        }
        .toolbar {
            Button(action: {
                updated = false
                napsterPlaylist.update(completion: {error in
                    if error != nil {
                        print("failed to update")
                        // TODO: error handling onscreen
                        return
                    }
                    self.updated = true
                    
                })
            }) {
                Text("Update")
            }
        }
    }
    
    
}

struct NapsterPlaylistView_CreatButton: View {
    @ObservedObject var napsterPlaylist: Napster

    @State private var state: CreationState = .none
    private enum CreationState {
        case none
        case creating
        case created
    }
    
    var body: some View {
        if self.napsterPlaylist.synced {
            switch self.state {
            case .none:
                Button(action: {
                    DispatchQueue.global(qos: .userInitiated).async {
                        self.createPlaylist()
                    }
                },
                label: {
                    Text("Create playlist")
                })
            case .creating:
                ProgressView()
            case .created:
                Text("done")
            }
        } else {
            ProgressView()
        }
    }
    
    func createPlaylist() {
        self.state = .creating
        /*self.napsterPlaylist.createApplePlaylist(completion: {result in
            switch result {
            case .failure(let e):
                print("error creating: \(e)")
            case .success(_):
                self.napsterPlaylist.convertToApplePlaylist(completion: {result in
                    switch result {
                    case .failure(let e):
                        print("error converting: \(e)")
                    case .success(_):
                        print("created playlist!")
                        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "test"), object: nil)
                        self.state = .created
                    }
                })
            }
        })*/
        self.napsterPlaylist.api.createPlaylistAPI(name: self.napsterPlaylist.name, tracks: self.napsterPlaylist.appleChoices.filter { song in
            if song.song.attributes.playParams != nil {
                print("including")
                return true
            }
            return false
        }.map {song in
            AppleMusicApi.LibraryPlaylistRequestTrack(id: song.song.attributes.playParams!.id, type: .songs)
        } , description: "MusicConverter", completion: {result in
            switch result {
            case .failure(let e):
                print("error creating playist: \(e)")
                self.state = .none
            case .success(_):
                print("created playlist")
                DispatchQueue.main.async {
                    //self.state = .created
                    self.state = .none
                }
            }
        })
    }
}

struct NapsterPlaylist_Previews: PreviewProvider {
    static var previews: some View {
        NapsterPlaylistView(demoData: "mp.demo")
    }
}