summaryrefslogtreecommitdiffstats
path: root/MusicConverter/View/Napster/NapsterPlaylistView.swift
blob: ef907fe19e8d90bcb84c9b5a6f03db0823bc9099 (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
//
//  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")
                }
            }
            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 {
        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")
        }
    }
    
    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
                    }
                })
            }
        })
    }
}

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