added run playlist and update playlist functions

This commit is contained in:
aj 2020-02-19 01:20:19 +00:00
parent b80152ba13
commit 36320aecc7
3 changed files with 124 additions and 35 deletions

View File

@ -12,6 +12,8 @@ import SwiftyJSON
public enum PlaylistApi { public enum PlaylistApi {
case getPlaylists case getPlaylists
case runPlaylist(name: String)
case updatePlaylist(name: String, updates: JSON)
} }
extension PlaylistApi: ApiRequest { extension PlaylistApi: ApiRequest {
@ -23,6 +25,10 @@ extension PlaylistApi: ApiRequest {
switch self { switch self {
case .getPlaylists: case .getPlaylists:
return "api/playlists" return "api/playlists"
case .runPlaylist:
return "api/playlist/run"
case .updatePlaylist:
return "api/playlist"
} }
} }
@ -30,15 +36,36 @@ extension PlaylistApi: ApiRequest {
switch self { switch self {
case .getPlaylists: case .getPlaylists:
return .get return .get
case .runPlaylist:
return .get
case .updatePlaylist:
return .post
} }
} }
var parameters: JSON? { var parameters: JSON? {
switch self {
case .getPlaylists:
return nil return nil
case .runPlaylist(let name):
return JSON(["name": name])
case .updatePlaylist(let name, let updates):
var txUpdates = updates
txUpdates["name"].string = name
debugPrint(txUpdates)
return txUpdates
}
} }
var parameterType: ParameterEncoder? { var parameterType: ParameterEncoder? {
switch self {
case .getPlaylists:
return nil return nil
case .runPlaylist:
return URLEncodedFormParameterEncoder()
case .updatePlaylist:
return JSONParameterEncoder.default
}
} }
var headers: HTTPHeaders? { var headers: HTTPHeaders? {

View File

@ -7,55 +7,116 @@
// //
import SwiftUI import SwiftUI
import SwiftyJSON
struct PlaylistView: View { struct PlaylistView: View {
var playlist: Playlist var playlist: Playlist
@State private var recommendations: Bool = false @State private var recommendations: Bool = false
@State private var library_Tracks: Bool = false @State private var library_Tracks: Bool = false
@State private var shuffle: Bool = false
@State private var rec_num: Int = 0
var body: some View { var body: some View {
ScrollView {
VStack(alignment: .leading) { VStack(alignment: .leading) {
Text(playlist.name) Text(playlist.name)
.font(.largeTitle) .font(.largeTitle)
.multilineTextAlignment(.leading) .multilineTextAlignment(.leading)
.padding(.leading)
Text(playlist.username) Text(playlist.username)
.foregroundColor(Color.gray) .foregroundColor(Color.gray)
.padding(.leading)
Image("PlaylistCoverImage") Image("PlaylistCoverImage")
.resizable() .resizable()
.frame(width: 200.0, height: 200.0, alignment: .trailing) .frame(width: 200.0, height: 200.0, alignment: .trailing)
.cornerRadius(18) .cornerRadius(18)
.padding(.bottom, 20) .padding(.all, 20)
Toggle(isOn: $recommendations) { Toggle(isOn: $recommendations) {
Text("Spotify Recommendations") Text("Spotify Recommendations")
} }
.padding()
if $recommendations.wrappedValue { if recommendations {
Stepper(value: /*@START_MENU_TOKEN@*//*@PLACEHOLDER=Value@*/.constant(4)/*@END_MENU_TOKEN@*/, in: /*@START_MENU_TOKEN@*//*@PLACEHOLDER=Range@*/1...10/*@END_MENU_TOKEN@*/){ Stepper(onIncrement: {
self.$rec_num.wrappedValue += 1
self.updatePlaylist(updates: JSON(["recommendation_sample": self.$rec_num.wrappedValue]))
},
onDecrement: {
self.$rec_num.wrappedValue -= 1
self.updatePlaylist(updates: JSON(["recommendation_sample": self.$rec_num.wrappedValue]))
}){
Text("#:") Text("#:")
.foregroundColor(Color.gray) .foregroundColor(Color.gray)
.multilineTextAlignment(.trailing) .multilineTextAlignment(.trailing)
.padding(.leading, 20) .padding(.leading, 20)
Text("100") Text("\(rec_num)")
.multilineTextAlignment(.trailing) .multilineTextAlignment(.trailing)
} }.padding()
} }
Toggle(isOn: $library_Tracks) { Toggle(isOn: $library_Tracks) {
Text("Library Tracks") Text("Library Tracks")
}.padding()
Toggle(isOn: $shuffle) {
Text("Shuffle")
}.padding()
HStack {
Button(action: { self.runPlaylist() }) {
Text("Update")
}.padding().multilineTextAlignment(.center)
} }
Spacer()
} }
.padding() .padding(.vertical)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading) .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
.onAppear { .onAppear {
self.$recommendations.wrappedValue = self.playlist.include_recommendations self.$recommendations.wrappedValue = self.playlist.include_recommendations
self.$library_Tracks.wrappedValue = self.playlist.include_library_tracks self.$library_Tracks.wrappedValue = self.playlist.include_library_tracks
self.$shuffle.wrappedValue = self.playlist.shuffle
self.$rec_num.wrappedValue = self.playlist.recommendation_sample
} }
} }
} }
func runPlaylist() {
let api = PlaylistApi.runPlaylist(name: playlist.name)
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
guard let data = response.data else {
fatalError("error getting playlists")
}
guard let json = try? JSON(data: data) else {
fatalError("error parsing reponse")
}
}
//TODO: do better error checking
}
func updatePlaylist(updates: JSON) {
let api = PlaylistApi.updatePlaylist(name: playlist.name, updates: updates)
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
guard let data = response.data else {
fatalError("error getting playlists")
}
guard let json = try? JSON(data: data) else {
fatalError("error parsing reponse")
}
}
//TODO: do better error checking
}
}
struct PlaylistView_Previews: PreviewProvider { struct PlaylistView_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
PlaylistView(playlist: PlaylistView(playlist:

View File

@ -78,6 +78,7 @@ struct RootView: View {
Playlist.fromDict(dictionary: dict) Playlist.fromDict(dictionary: dict)
}).sorted(by: { $0.name.lowercased() < $1.name.lowercased() }) }).sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
} }
//TODO: do better error checking
} }
} }