2020-02-17 02:21:49 +00:00
|
|
|
//
|
|
|
|
// PlaylistView.swift
|
2022-08-07 13:31:15 +01:00
|
|
|
// Mixonomer
|
2020-02-17 02:21:49 +00:00
|
|
|
//
|
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
|
2022-08-11 20:15:21 +01:00
|
|
|
import ToastUI
|
2020-02-19 01:20:19 +00:00
|
|
|
import SwiftyJSON
|
2020-03-08 00:47:35 +00:00
|
|
|
import SwiftUICharts
|
2020-02-17 02:21:49 +00:00
|
|
|
|
|
|
|
struct PlaylistView: View {
|
2020-02-19 23:00:23 +00:00
|
|
|
|
2022-08-09 17:42:01 +01:00
|
|
|
@EnvironmentObject var liveUser: LiveUser
|
2020-03-03 00:04:20 +00:00
|
|
|
@Binding var playlist: Playlist
|
2020-02-17 02:21:49 +00:00
|
|
|
|
2020-02-23 00:24:02 +00:00
|
|
|
@State private var showingSheet = false
|
2020-02-22 13:28:15 +00:00
|
|
|
@State private var isRefreshing = false
|
2022-08-11 20:15:21 +01:00
|
|
|
|
|
|
|
// TOAST
|
|
|
|
@State private var showingToast = false
|
|
|
|
@State private var toastText = ""
|
|
|
|
@State private var toastSuccess = true
|
2020-02-22 13:28:15 +00:00
|
|
|
|
2022-08-13 22:07:59 +01:00
|
|
|
var trackChartStyle: ChartStyle {
|
2020-03-08 00:47:35 +00:00
|
|
|
get {
|
2022-08-13 22:07:59 +01:00
|
|
|
let _style = ChartStyle(backgroundColor: .white, accentColor: Color(red: 0.4765, green: 0.5976, blue: 0.7578), gradientColor: GradientColors.bluPurpl, textColor: .black, legendTextColor: .gray)
|
|
|
|
return _style
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var albumChartStyle: ChartStyle {
|
|
|
|
get {
|
|
|
|
let _style = ChartStyle(backgroundColor: .white, accentColor: Color(red: 0.6367, green: 0.2968, blue: 0.4648), gradientColor: GradientColors.bluPurpl, textColor: .black, legendTextColor: .gray)
|
|
|
|
return _style
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var artistChartStyle: ChartStyle {
|
|
|
|
get {
|
|
|
|
let _style = ChartStyle(backgroundColor: .white, accentColor: Color(red: 0.3476, green: 0.5195, blue: 0.3359), gradientColor: GradientColors.bluPurpl, textColor: .black, legendTextColor: .gray)
|
2020-03-08 00:47:35 +00:00
|
|
|
return _style
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 13:31:15 +01:00
|
|
|
var chartSize = CGSize(width:210, height:250);
|
2022-03-23 17:47:56 +00:00
|
|
|
|
2020-02-17 02:21:49 +00:00
|
|
|
var body: some View {
|
2020-04-30 17:05:23 +01:00
|
|
|
Form {
|
2022-08-13 22:07:59 +01:00
|
|
|
|
|
|
|
if liveUser.lastfm_connected() {
|
|
|
|
|
|
|
|
Section(header: Text("Stats")){
|
|
|
|
HStack {
|
|
|
|
Text("Track Count")
|
|
|
|
Spacer()
|
|
|
|
Text("\(self.playlist.lastfm_stat_count)")
|
|
|
|
.font(.title)
|
|
|
|
.foregroundColor(Color.gray)
|
|
|
|
Text("\(self.playlist.lastfm_stat_percent_str)")
|
|
|
|
.font(.body)
|
|
|
|
.foregroundColor(Color.gray)
|
|
|
|
}
|
|
|
|
HStack {
|
|
|
|
Text("Album Count")
|
|
|
|
Spacer()
|
|
|
|
Text("\(self.playlist.lastfm_stat_album_count)")
|
|
|
|
.font(.title)
|
|
|
|
.foregroundColor(Color.gray)
|
|
|
|
Text("\(self.playlist.lastfm_stat_album_percent_str)")
|
|
|
|
.font(.body)
|
|
|
|
.foregroundColor(Color.gray)
|
|
|
|
}
|
|
|
|
HStack {
|
|
|
|
Text("Artist Count")
|
|
|
|
Spacer()
|
|
|
|
Text("\(self.playlist.lastfm_stat_artist_count)")
|
|
|
|
.font(.title)
|
|
|
|
.foregroundColor(Color.gray)
|
|
|
|
Text("\(self.playlist.lastfm_stat_artist_percent_str)")
|
|
|
|
.font(.body)
|
|
|
|
.foregroundColor(Color.gray)
|
|
|
|
}
|
|
|
|
Button(action: {
|
|
|
|
self.refreshStats()
|
|
|
|
}){
|
|
|
|
Text("Refresh")
|
|
|
|
}
|
2020-03-08 00:47:35 +00:00
|
|
|
}
|
2022-08-07 13:31:15 +01:00
|
|
|
|
2022-08-13 22:07:59 +01:00
|
|
|
ScrollView(.horizontal){
|
|
|
|
HStack {
|
|
|
|
Spacer()
|
|
|
|
PieChartView(
|
|
|
|
data: [Double(self.playlist.lastfm_stat_percent), Double(100 - self.playlist.lastfm_stat_percent)],
|
|
|
|
title: "Tracks",
|
|
|
|
legend:"Listening",
|
|
|
|
style: trackChartStyle,
|
|
|
|
form: chartSize)
|
|
|
|
Spacer(minLength: 20)
|
|
|
|
PieChartView(
|
|
|
|
data: [Double(self.playlist.lastfm_stat_album_percent), Double(100 - self.playlist.lastfm_stat_album_percent)],
|
|
|
|
title: "Albums",
|
|
|
|
legend:"Listening",
|
|
|
|
style: albumChartStyle,
|
|
|
|
form: chartSize)
|
|
|
|
Spacer(minLength: 20)
|
|
|
|
PieChartView(
|
|
|
|
data: [Double(self.playlist.lastfm_stat_artist_percent), Double(100 - self.playlist.lastfm_stat_artist_percent)],
|
|
|
|
title: "Artists",
|
|
|
|
legend:"Listening",
|
|
|
|
style: artistChartStyle,
|
|
|
|
form: chartSize)
|
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
.padding([.vertical], 20)
|
|
|
|
.padding([.horizontal], 10)
|
2020-03-08 00:47:35 +00:00
|
|
|
}
|
2022-08-13 22:07:59 +01:00
|
|
|
.listRowInsets(EdgeInsets())
|
2020-03-08 00:47:35 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 23:00:23 +00:00
|
|
|
Section(header: Text("Options")){
|
2020-03-03 00:04:20 +00:00
|
|
|
Toggle(isOn: self.$playlist.include_recommendations) {
|
2020-02-19 01:20:19 +00:00
|
|
|
Text("Spotify Recommendations")
|
|
|
|
}
|
|
|
|
|
2020-03-03 00:04:20 +00:00
|
|
|
if self.playlist.include_recommendations {
|
2020-02-19 01:20:19 +00:00
|
|
|
Stepper(onIncrement: {
|
2020-03-03 00:04:20 +00:00
|
|
|
self.$playlist.recommendation_sample.wrappedValue += 1
|
2020-02-19 01:20:19 +00:00
|
|
|
},
|
2020-04-30 17:05:23 +01:00
|
|
|
onDecrement: {
|
|
|
|
if self.playlist.recommendation_sample > 0 {
|
|
|
|
self.$playlist.recommendation_sample.wrappedValue -= 1
|
|
|
|
|
|
|
|
}
|
2020-02-19 01:20:19 +00:00
|
|
|
}){
|
|
|
|
Text("#:")
|
|
|
|
.foregroundColor(Color.gray)
|
|
|
|
.multilineTextAlignment(.trailing)
|
2020-03-03 00:04:20 +00:00
|
|
|
Text("\(self.playlist.recommendation_sample)")
|
2020-02-19 01:20:19 +00:00
|
|
|
.multilineTextAlignment(.trailing)
|
|
|
|
|
2020-02-19 23:00:23 +00:00
|
|
|
}
|
2020-03-03 00:04:20 +00:00
|
|
|
}
|
2020-02-19 01:20:19 +00:00
|
|
|
|
2020-03-03 00:04:20 +00:00
|
|
|
Toggle(isOn: self.$playlist.include_library_tracks) {
|
2020-02-19 01:20:19 +00:00
|
|
|
Text("Library Tracks")
|
2020-02-19 23:00:23 +00:00
|
|
|
}
|
2020-02-19 01:20:19 +00:00
|
|
|
|
2020-03-03 00:04:20 +00:00
|
|
|
Toggle(isOn: self.$playlist.shuffle) {
|
2020-02-19 01:20:19 +00:00
|
|
|
Text("Shuffle")
|
2020-02-19 23:00:23 +00:00
|
|
|
}
|
2020-02-23 00:24:02 +00:00
|
|
|
|
2020-04-30 17:05:23 +01:00
|
|
|
if playlist.type == "recents" {
|
|
|
|
Toggle(isOn: self.$playlist.add_this_month) {
|
2020-02-23 00:24:02 +00:00
|
|
|
Text("This Month")
|
|
|
|
}
|
|
|
|
|
2020-04-30 17:05:23 +01:00
|
|
|
Toggle(isOn: self.$playlist.add_last_month) {
|
2020-02-23 00:24:02 +00:00
|
|
|
Text("Last Month")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 17:05:23 +01:00
|
|
|
if playlist.type == "fmchart" {
|
2020-02-23 00:24:02 +00:00
|
|
|
HStack {
|
|
|
|
Text("Chart Range")
|
|
|
|
Spacer()
|
|
|
|
Button(action: {
|
|
|
|
self.showingSheet = true
|
|
|
|
}) {
|
2020-04-30 17:05:23 +01:00
|
|
|
Text("\(self.playlist.chart_range.rawValue)")
|
2020-02-23 00:24:02 +00:00
|
|
|
.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")){
|
2022-08-13 22:07:59 +01:00
|
|
|
NavigationLink(destination: PlaylistInputList(names: self.$playlist.playlist_references, nameType: "Managed Playlists", type: .MixonomerPlaylists)) {
|
2020-02-19 23:00:23 +00:00
|
|
|
HStack {
|
|
|
|
Text("Managed Playlists")
|
|
|
|
Spacer()
|
|
|
|
Text("\(self.playlist.playlist_references.count)")
|
|
|
|
.foregroundColor(Color.gray)
|
|
|
|
}
|
2020-02-19 01:20:19 +00:00
|
|
|
}
|
|
|
|
|
2022-08-13 22:07:59 +01:00
|
|
|
NavigationLink(destination: PlaylistInputList(names: self.$playlist.parts, nameType: "Spotify Playlists", type: .SpotifyPlaylists)) {
|
2020-02-19 23:00:23 +00:00
|
|
|
HStack {
|
|
|
|
Text("Spotify Playlists")
|
|
|
|
Spacer()
|
|
|
|
Text("\(self.playlist.parts.count)")
|
|
|
|
.foregroundColor(Color.gray)
|
|
|
|
}
|
|
|
|
}
|
2020-02-19 01:20:19 +00:00
|
|
|
}
|
2020-03-07 18:51:52 +00:00
|
|
|
Section(header: Text("Actions"),
|
|
|
|
footer: VStack(alignment: .leading) {
|
2020-03-20 00:08:18 +00:00
|
|
|
Text("Last Updated \(self.playlist.last_updated ?? "never")")
|
|
|
|
Text("Stats Updated \(self.playlist.lastfm_stat_last_refresh ?? "never")")
|
2020-03-07 18:51:52 +00:00
|
|
|
}){
|
2020-02-19 23:00:23 +00:00
|
|
|
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
|
|
|
}
|
2022-08-12 10:04:03 +01:00
|
|
|
.toast(isPresented: $showingToast, dismissAfter: 1.0){
|
2022-08-11 20:15:21 +01:00
|
|
|
|
|
|
|
if toastSuccess {
|
|
|
|
ToastView(toastText)
|
|
|
|
.toastViewStyle(.success)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ToastView(toastText)
|
|
|
|
.toastViewStyle(.failure)
|
|
|
|
}
|
2020-04-27 21:33:26 +01:00
|
|
|
}
|
2022-08-11 20:15:21 +01:00
|
|
|
.toastDimmedBackground(false)
|
2020-04-27 21:33:26 +01:00
|
|
|
|
2020-04-30 17:05:23 +01:00
|
|
|
}
|
2022-04-22 18:10:14 +01:00
|
|
|
.navigationBarTitle(Text(playlist.name))
|
2022-04-21 15:02:44 +01:00
|
|
|
.refreshable {
|
2020-03-03 00:04:20 +00:00
|
|
|
self.refreshPlaylist()
|
|
|
|
}
|
2020-02-23 00:24:02 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 01:20:19 +00:00
|
|
|
func runPlaylist() {
|
|
|
|
let api = PlaylistApi.runPlaylist(name: playlist.name)
|
2020-04-26 00:11:07 +01:00
|
|
|
RequestBuilder.buildRequest(apiRequest: api)
|
|
|
|
.validate()
|
|
|
|
.responseJSON{ response in
|
2022-08-09 17:42:01 +01:00
|
|
|
|
2022-08-13 22:07:59 +01:00
|
|
|
if self.liveUser.check_network_response(response: response) {
|
2022-08-09 17:42:01 +01:00
|
|
|
|
2022-08-11 20:15:21 +01:00
|
|
|
toastText = "Running!"
|
|
|
|
toastSuccess = true
|
|
|
|
showingToast = true
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
toastText = "Run Request Failed"
|
|
|
|
toastSuccess = false
|
|
|
|
showingToast = true
|
|
|
|
}
|
2020-02-17 02:21:49 +00:00
|
|
|
}
|
2020-02-19 01:20:19 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 00:47:35 +00:00
|
|
|
func refreshStats() {
|
|
|
|
let api = PlaylistApi.refreshStats(name: playlist.name)
|
|
|
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
|
|
|
|
2022-08-13 22:07:59 +01:00
|
|
|
if self.liveUser.check_network_response(response: response) {
|
2022-08-11 20:15:21 +01:00
|
|
|
|
|
|
|
toastText = "Refreshing Stats!"
|
|
|
|
toastSuccess = true
|
|
|
|
showingToast = true
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
toastText = "Stat Refresh Failed"
|
|
|
|
toastSuccess = false
|
|
|
|
showingToast = true
|
|
|
|
|
2022-08-09 17:42:01 +01:00
|
|
|
}
|
2020-03-08 00:47:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2022-08-09 17:42:01 +01:00
|
|
|
|
2022-08-13 22:07:59 +01:00
|
|
|
if self.liveUser.check_network_response(response: response) {
|
2020-02-22 13:28:15 +00:00
|
|
|
debugPrint("success")
|
2022-08-11 20:15:21 +01:00
|
|
|
} else {
|
2020-02-22 13:28:15 +00:00
|
|
|
debugPrint("error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//TODO: do better error checking
|
|
|
|
}
|
|
|
|
|
2020-03-03 00:04:20 +00:00
|
|
|
func refreshPlaylist() {
|
2020-02-22 13:28:15 +00:00
|
|
|
let api = PlaylistApi.getPlaylist(name: self.playlist.name)
|
|
|
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
2022-08-09 17:42:01 +01:00
|
|
|
|
2022-08-13 22:07:59 +01:00
|
|
|
if self.liveUser.check_network_response(response: response) {
|
2022-08-09 17:42:01 +01:00
|
|
|
|
|
|
|
guard let data = response.data else {
|
|
|
|
fatalError("error getting playlist")
|
|
|
|
}
|
|
|
|
|
|
|
|
self.playlist = PlaylistApi.fromJSON(playlist: data)!
|
|
|
|
|
2022-08-11 20:15:21 +01:00
|
|
|
toastText = "Refreshed!"
|
|
|
|
toastSuccess = true
|
|
|
|
showingToast = true
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
toastText = "Refresh Failed"
|
|
|
|
toastSuccess = false
|
|
|
|
showingToast = true
|
|
|
|
|
2020-02-22 13:28:15 +00:00
|
|
|
}
|
2020-02-19 01:20:19 +00:00
|
|
|
|
2020-03-03 00:04:20 +00:00
|
|
|
self.isRefreshing = false
|
2020-02-18 21:43:30 +00:00
|
|
|
}
|
2020-02-17 02:21:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PlaylistView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2022-08-14 13:14:21 +01:00
|
|
|
Group {
|
|
|
|
PlaylistView(playlist: .constant(
|
|
|
|
Playlist(name: "playlist name",
|
|
|
|
username: "username",
|
|
|
|
lastfm_stat_percent: 30,
|
|
|
|
lastfm_stat_album_percent: 40,
|
|
|
|
lastfm_stat_artist_percent: 80
|
|
|
|
)
|
|
|
|
))
|
|
|
|
.environmentObject(LiveUser(playlists: [], tags: [], username: "user", loggedIn: false))
|
|
|
|
PlaylistView(playlist: .constant(
|
|
|
|
Playlist(name: "playlist name",
|
|
|
|
username: "username",
|
|
|
|
lastfm_stat_percent: 30,
|
|
|
|
lastfm_stat_album_percent: 40,
|
|
|
|
lastfm_stat_artist_percent: 80
|
|
|
|
)
|
|
|
|
))
|
2022-08-15 19:24:56 +01:00
|
|
|
.environmentObject(LiveUser(playlists: [], tags: [], username: "user", loggedIn: false, user: User()))
|
2022-08-14 13:14:21 +01:00
|
|
|
}
|
2022-08-07 13:31:15 +01:00
|
|
|
|
2020-02-17 02:21:49 +00:00
|
|
|
}
|
|
|
|
}
|