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 {
case getPlaylists
case runPlaylist(name: String)
case updatePlaylist(name: String, updates: JSON)
}
extension PlaylistApi: ApiRequest {
@ -23,6 +25,10 @@ extension PlaylistApi: ApiRequest {
switch self {
case .getPlaylists:
return "api/playlists"
case .runPlaylist:
return "api/playlist/run"
case .updatePlaylist:
return "api/playlist"
}
}
@ -30,15 +36,36 @@ extension PlaylistApi: ApiRequest {
switch self {
case .getPlaylists:
return .get
case .runPlaylist:
return .get
case .updatePlaylist:
return .post
}
}
var parameters: JSON? {
return nil
switch self {
case .getPlaylists:
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? {
return nil
switch self {
case .getPlaylists:
return nil
case .runPlaylist:
return URLEncodedFormParameterEncoder()
case .updatePlaylist:
return JSONParameterEncoder.default
}
}
var headers: HTTPHeaders? {

View File

@ -7,52 +7,113 @@
//
import SwiftUI
import SwiftyJSON
struct PlaylistView: View {
var playlist: Playlist
@State private var recommendations: 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 {
VStack(alignment: .leading) {
Text(playlist.name)
.font(.largeTitle)
.multilineTextAlignment(.leading)
Text(playlist.username)
.foregroundColor(Color.gray)
Image("PlaylistCoverImage")
.resizable()
.frame(width: 200.0, height: 200.0, alignment: .trailing)
.cornerRadius(18)
.padding(.bottom, 20)
Toggle(isOn: $recommendations) {
Text("Spotify Recommendations")
}
if $recommendations.wrappedValue {
Stepper(value: /*@START_MENU_TOKEN@*//*@PLACEHOLDER=Value@*/.constant(4)/*@END_MENU_TOKEN@*/, in: /*@START_MENU_TOKEN@*//*@PLACEHOLDER=Range@*/1...10/*@END_MENU_TOKEN@*/){
Text("#:")
.foregroundColor(Color.gray)
.multilineTextAlignment(.trailing)
.padding(.leading, 20)
Text("100")
.multilineTextAlignment(.trailing)
ScrollView {
VStack(alignment: .leading) {
Text(playlist.name)
.font(.largeTitle)
.multilineTextAlignment(.leading)
.padding(.leading)
Text(playlist.username)
.foregroundColor(Color.gray)
.padding(.leading)
Image("PlaylistCoverImage")
.resizable()
.frame(width: 200.0, height: 200.0, alignment: .trailing)
.cornerRadius(18)
.padding(.all, 20)
Toggle(isOn: $recommendations) {
Text("Spotify Recommendations")
}
.padding()
if recommendations {
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("#:")
.foregroundColor(Color.gray)
.multilineTextAlignment(.trailing)
.padding(.leading, 20)
Text("\(rec_num)")
.multilineTextAlignment(.trailing)
}.padding()
}
Toggle(isOn: $library_Tracks) {
Text("Library Tracks")
}.padding()
Toggle(isOn: $shuffle) {
Text("Shuffle")
}.padding()
HStack {
Button(action: { self.runPlaylist() }) {
Text("Update")
}.padding().multilineTextAlignment(.center)
}
Spacer()
}
.padding(.vertical)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
.onAppear {
self.$recommendations.wrappedValue = self.playlist.include_recommendations
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")
}
Toggle(isOn: $library_Tracks) {
Text("Library Tracks")
guard let json = try? JSON(data: data) else {
fatalError("error parsing reponse")
}
}
.padding()
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
.onAppear {
self.$recommendations.wrappedValue = self.playlist.include_recommendations
self.$library_Tracks.wrappedValue = self.playlist.include_library_tracks
//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
}
}

View File

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