2020-02-17 02:21:49 +00:00
|
|
|
//
|
|
|
|
// PlaylistView.swift
|
|
|
|
// Music Tools
|
|
|
|
//
|
2020-02-18 01:44:30 +00:00
|
|
|
// Created by Andy Pack on 16/02/2020.
|
2020-02-17 02:21:49 +00:00
|
|
|
// Copyright © 2020 Sarsoo. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
2020-02-22 13:28:15 +00:00
|
|
|
//import SwiftUIRefresh
|
2020-02-19 01:20:19 +00:00
|
|
|
import SwiftyJSON
|
2020-02-17 02:21:49 +00:00
|
|
|
|
2020-02-22 13:28:15 +00:00
|
|
|
final class ChangeableBool: ObservableObject {
|
|
|
|
|
|
|
|
var onClick: () -> ()
|
|
|
|
|
|
|
|
init(onClick: @escaping () -> ()) {
|
|
|
|
self.onClick = onClick
|
|
|
|
}
|
|
|
|
|
|
|
|
@Published var state: Bool = false {
|
|
|
|
didSet {
|
|
|
|
self.onClick()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 02:21:49 +00:00
|
|
|
struct PlaylistView: View {
|
2020-02-19 23:00:23 +00:00
|
|
|
|
2020-02-22 13:28:15 +00:00
|
|
|
@EnvironmentObject var liveUser: LiveUser
|
|
|
|
|
2020-02-19 23:00:23 +00:00
|
|
|
init(playlist: Playlist) {
|
|
|
|
self.playlist = playlist
|
2020-02-20 18:25:28 +00:00
|
|
|
|
|
|
|
// hide empty items below list
|
2020-02-19 23:00:23 +00:00
|
|
|
UITableView.appearance().tableFooterView = UIView()
|
|
|
|
}
|
|
|
|
|
2020-02-17 02:21:49 +00:00
|
|
|
var playlist: Playlist
|
2020-02-19 23:00:23 +00:00
|
|
|
@State private var recommendations: Bool = true
|
2020-02-18 21:43:30 +00:00
|
|
|
@State private var library_Tracks: Bool = false
|
2020-02-19 01:20:19 +00:00
|
|
|
@State private var shuffle: Bool = false
|
|
|
|
|
|
|
|
@State private var rec_num: Int = 0
|
2020-02-17 02:21:49 +00:00
|
|
|
|
2020-02-23 00:24:02 +00:00
|
|
|
@State private var this_month: Bool = false
|
|
|
|
@State private var last_month: Bool = false
|
|
|
|
@State private var chart_range: LastFmRange = .overall
|
|
|
|
@State private var chart_limit: Int = 0
|
|
|
|
|
|
|
|
@State private var showingSheet = false
|
|
|
|
|
2020-02-22 13:28:15 +00:00
|
|
|
@State private var isRefreshing = false
|
|
|
|
|
2020-02-17 02:21:49 +00:00
|
|
|
var body: some View {
|
2020-02-19 23:00:23 +00:00
|
|
|
List {
|
|
|
|
Section(header: Text("Options")){
|
2020-02-19 01:20:19 +00:00
|
|
|
Toggle(isOn: $recommendations) {
|
|
|
|
Text("Spotify Recommendations")
|
|
|
|
}
|
|
|
|
|
2020-02-19 23:00:23 +00:00
|
|
|
// if recommendations {
|
2020-02-19 01:20:19 +00:00
|
|
|
Stepper(onIncrement: {
|
|
|
|
self.$rec_num.wrappedValue += 1
|
|
|
|
self.updatePlaylist(updates: JSON(["recommendation_sample": self.$rec_num.wrappedValue]))
|
|
|
|
},
|
|
|
|
onDecrement: {
|
|
|
|
self.$rec_num.wrappedValue -= 1
|
2020-02-19 23:00:23 +00:00
|
|
|
self.updatePlaylist(updates: JSON(["recommendation_sample": self.$rec_num.wrappedValue]))
|
2020-02-19 01:20:19 +00:00
|
|
|
}){
|
|
|
|
Text("#:")
|
|
|
|
.foregroundColor(Color.gray)
|
|
|
|
.multilineTextAlignment(.trailing)
|
|
|
|
Text("\(rec_num)")
|
|
|
|
.multilineTextAlignment(.trailing)
|
|
|
|
|
2020-02-19 23:00:23 +00:00
|
|
|
}
|
|
|
|
// }
|
2020-02-19 01:20:19 +00:00
|
|
|
|
|
|
|
Toggle(isOn: $library_Tracks) {
|
|
|
|
Text("Library Tracks")
|
2020-02-19 23:00:23 +00:00
|
|
|
}
|
2020-02-19 01:20:19 +00:00
|
|
|
|
|
|
|
Toggle(isOn: $shuffle) {
|
|
|
|
Text("Shuffle")
|
2020-02-19 23:00:23 +00:00
|
|
|
}
|
2020-02-23 00:24:02 +00:00
|
|
|
|
|
|
|
if playlist is RecentsPlaylist {
|
|
|
|
Toggle(isOn: $this_month) {
|
|
|
|
Text("This Month")
|
|
|
|
}
|
|
|
|
|
|
|
|
Toggle(isOn: $last_month) {
|
|
|
|
Text("Last Month")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if playlist is LastFMChartPlaylist {
|
|
|
|
HStack {
|
|
|
|
Text("Chart Range")
|
|
|
|
Spacer()
|
|
|
|
Button(action: {
|
|
|
|
self.showingSheet = true
|
|
|
|
}) {
|
|
|
|
Text("\(self.chart_range.rawValue)")
|
|
|
|
.foregroundColor(Color.gray)
|
|
|
|
}.actionSheet(isPresented: $showingSheet) {
|
|
|
|
ActionSheet(title: Text("Chart range"),
|
|
|
|
message: Text("Select time range for Last.fm chart"),
|
|
|
|
buttons: [.default(Text("7 Days")),
|
|
|
|
.default(Text("1 Month")),
|
|
|
|
.default(Text("3 Months")),
|
|
|
|
.default(Text("6 Months")),
|
|
|
|
.default(Text("Year")),
|
|
|
|
.default(Text("Overall")),
|
|
|
|
.default(Text("Dismiss"))])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-19 23:00:23 +00:00
|
|
|
}
|
|
|
|
Section(header: Text("Inputs")){
|
|
|
|
NavigationLink(destination: PlaylistInputList(names: self.playlist.playlist_references, nameType: "Managed Playlists")) {
|
|
|
|
HStack {
|
|
|
|
Text("Managed Playlists")
|
|
|
|
Spacer()
|
|
|
|
Text("\(self.playlist.playlist_references.count)")
|
|
|
|
.foregroundColor(Color.gray)
|
|
|
|
}
|
2020-02-19 01:20:19 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 23:00:23 +00:00
|
|
|
NavigationLink(destination: PlaylistInputList(names: self.playlist.parts, nameType: "Spotify Playlists")) {
|
|
|
|
HStack {
|
|
|
|
Text("Spotify Playlists")
|
|
|
|
Spacer()
|
|
|
|
Text("\(self.playlist.parts.count)")
|
|
|
|
.foregroundColor(Color.gray)
|
|
|
|
}
|
|
|
|
}
|
2020-02-19 01:20:19 +00:00
|
|
|
}
|
2020-02-19 23:00:23 +00:00
|
|
|
Section(header: Text("Actions")){
|
|
|
|
Button(action: { self.runPlaylist() }) {
|
|
|
|
Text("Update")
|
|
|
|
}
|
2020-02-19 01:20:19 +00:00
|
|
|
|
2020-02-19 23:00:23 +00:00
|
|
|
Button(action: { self.openPlaylist() }) {
|
|
|
|
Text("Open")
|
|
|
|
}
|
2020-02-17 02:21:49 +00:00
|
|
|
}
|
2020-02-19 01:20:19 +00:00
|
|
|
}
|
2020-02-22 13:28:15 +00:00
|
|
|
// .pullToRefresh(isShowing: $isRefreshing) {
|
|
|
|
// self.refreshPlaylist()
|
|
|
|
// }
|
2020-02-19 23:00:23 +00:00
|
|
|
.navigationBarTitle(Text(playlist.name))
|
|
|
|
.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
|
2020-02-23 00:24:02 +00:00
|
|
|
|
|
|
|
if let playlist = self.playlist as? RecentsPlaylist {
|
|
|
|
self.$this_month.wrappedValue = playlist.add_this_month
|
|
|
|
self.$last_month.wrappedValue = playlist.add_last_month
|
|
|
|
}
|
|
|
|
|
|
|
|
if let playlist = self.playlist as? LastFMChartPlaylist {
|
|
|
|
self.$chart_range.wrappedValue = playlist.chart_range
|
|
|
|
self.$chart_limit.wrappedValue = playlist.chart_limit
|
|
|
|
}
|
2020-02-19 23:00:23 +00:00
|
|
|
}
|
2020-02-19 01:20:19 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 00:24:02 +00:00
|
|
|
func changeChartRange(newRange: LastFmRange) {
|
|
|
|
self.chart_range = newRange
|
|
|
|
// self.updatePlaylist(["chart_range": newRange.rawValue])
|
|
|
|
//TODO: are enums wrong by the time they're here? not sure api will accept it now
|
|
|
|
//TODO: fix downcasting local playlist object to change state
|
|
|
|
}
|
|
|
|
|
2020-02-19 01:20:19 +00:00
|
|
|
func runPlaylist() {
|
|
|
|
let api = PlaylistApi.runPlaylist(name: playlist.name)
|
|
|
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
2020-02-17 02:21:49 +00:00
|
|
|
|
|
|
|
}
|
2020-02-19 01:20:19 +00:00
|
|
|
//TODO: do better error checking
|
|
|
|
}
|
|
|
|
|
2020-02-19 23:00:23 +00:00
|
|
|
func openPlaylist() {
|
|
|
|
if let url = URL(string: self.playlist.link) {
|
|
|
|
UIApplication.shared.open(url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 01:20:19 +00:00
|
|
|
func updatePlaylist(updates: JSON) {
|
|
|
|
let api = PlaylistApi.updatePlaylist(name: playlist.name, updates: updates)
|
|
|
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
2020-02-22 13:28:15 +00:00
|
|
|
switch response.result {
|
|
|
|
case .success:
|
|
|
|
debugPrint("success")
|
|
|
|
case .failure:
|
|
|
|
debugPrint("error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//TODO: do better error checking
|
|
|
|
}
|
|
|
|
|
|
|
|
func refreshPlaylist(updates: JSON) {
|
|
|
|
let api = PlaylistApi.getPlaylist(name: self.playlist.name)
|
|
|
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
|
|
|
guard let data = response.data else {
|
|
|
|
fatalError("error getting playlist")
|
|
|
|
}
|
2020-02-19 01:20:19 +00:00
|
|
|
|
2020-02-22 13:28:15 +00:00
|
|
|
guard let json = try? JSON(data: data) else {
|
|
|
|
fatalError("error parsing reponse")
|
|
|
|
}
|
|
|
|
|
|
|
|
// let playlist = Playlist.fromDict(json["playlist"])
|
|
|
|
//
|
|
|
|
// self.playlist = playlist
|
|
|
|
// self.isRefreshing = false
|
2020-02-18 21:43:30 +00:00
|
|
|
}
|
2020-02-19 01:20:19 +00:00
|
|
|
//TODO: do better error checking
|
2020-02-17 02:21:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PlaylistView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
PlaylistView(playlist:
|
|
|
|
Playlist(name: "playlist name",
|
|
|
|
uri: "uri",
|
|
|
|
username: "username",
|
|
|
|
|
|
|
|
include_recommendations: true,
|
|
|
|
recommendation_sample: 5,
|
|
|
|
include_library_tracks: true,
|
|
|
|
|
|
|
|
parts: ["name"],
|
|
|
|
playlist_references: ["ref name"],
|
|
|
|
|
|
|
|
shuffle: true)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|