diff --git a/Music Tools/Model/Playlist.swift b/Music Tools/Model/Playlist.swift index b273137..83575ac 100644 --- a/Music Tools/Model/Playlist.swift +++ b/Music Tools/Model/Playlist.swift @@ -55,8 +55,10 @@ class Playlist: Identifiable, Equatable { self.shuffle = shuffle } - static func fromDict(dictionary: JSON) -> Playlist { - return Playlist(name: dictionary["name"].stringValue, + static func fromDict(dictionary: JSON) -> Playlist? { + switch dictionary["type"].string { + case "default": + return Playlist(name: dictionary["name"].stringValue, uri: dictionary["uri"].stringValue, username: dictionary["username"].stringValue, @@ -68,6 +70,42 @@ class Playlist: Identifiable, Equatable { playlist_references: dictionary["playlist_references"].arrayObject as! Array, shuffle: dictionary["shuffle"].boolValue) + case "recents": + return RecentsPlaylist(name: dictionary["name"].stringValue, + uri: dictionary["uri"].stringValue, + username: dictionary["username"].stringValue, + + include_recommendations: dictionary["include_recommendations"].boolValue, + recommendation_sample: dictionary["recommendation_sample"].intValue, + include_library_tracks: dictionary["include_library_tracks"].boolValue, + + parts: dictionary["parts"].arrayObject as! Array, + playlist_references: dictionary["playlist_references"].arrayObject as! Array, + + shuffle: dictionary["shuffle"].boolValue, + + add_last_month: dictionary["add_last_month"].boolValue, + add_this_month: dictionary["add_this_month"].boolValue, + day_boundary: dictionary["day_boundary"].intValue) + case "fmchart": + return LastFMChartPlaylist(name: dictionary["name"].stringValue, + uri: dictionary["uri"].stringValue, + username: dictionary["username"].stringValue, + + include_recommendations: dictionary["include_recommendations"].boolValue, + recommendation_sample: dictionary["recommendation_sample"].intValue, + include_library_tracks: dictionary["include_library_tracks"].boolValue, + + parts: dictionary["parts"].arrayObject as! Array, + playlist_references: dictionary["playlist_references"].arrayObject as! Array, + + shuffle: dictionary["shuffle"].boolValue, + + chart_range: LastFmRange(rawValue: dictionary["chart_range"].stringValue)!, + chart_limit: dictionary["chart_limit"].intValue) + default: + return nil + } } var link: String { @@ -81,3 +119,79 @@ class Playlist: Identifiable, Equatable { } } + +class RecentsPlaylist: Playlist { + + //MARK: Properties + + var add_last_month: Bool + var add_this_month: Bool + var day_boundary: Int + + //MARK: Initialization + + init(name: String, + uri: String, + username: String, + + include_recommendations: Bool, + recommendation_sample: Int, + include_library_tracks: Bool, + + parts: Array, + playlist_references: Array, + + shuffle: Bool, + + add_last_month: Bool, + add_this_month: Bool, + day_boundary: Int){ + + self.add_last_month = add_last_month + self.add_this_month = add_this_month + self.day_boundary = day_boundary + + super.init(name: name, uri: uri, username: username, include_recommendations: include_recommendations, recommendation_sample: recommendation_sample, include_library_tracks: include_library_tracks, parts: parts, playlist_references: playlist_references, shuffle: shuffle) + } +} + +enum LastFmRange: String { + case overall = "OVERALL" + case week = "WEEK" + case month = "MONTH" + case quarter = "QUARTER" + case halfyear = "HALFYEAR" + case year = "YEAR" +} + +class LastFMChartPlaylist: Playlist { + + //MARK: Properties + + var chart_range: LastFmRange + var chart_limit: Int + + //MARK: Initialization + + init(name: String, + uri: String, + username: String, + + include_recommendations: Bool, + recommendation_sample: Int, + include_library_tracks: Bool, + + parts: Array, + playlist_references: Array, + + shuffle: Bool, + + chart_range: LastFmRange, + chart_limit: Int){ + + self.chart_range = chart_range + self.chart_limit = chart_limit + + super.init(name: name, uri: uri, username: username, include_recommendations: include_recommendations, recommendation_sample: recommendation_sample, include_library_tracks: include_library_tracks, parts: parts, playlist_references: playlist_references, shuffle: shuffle) + } +} diff --git a/Music Tools/Views/Playlist/PlaylistView.swift b/Music Tools/Views/Playlist/PlaylistView.swift index 962d39f..a405f95 100644 --- a/Music Tools/Views/Playlist/PlaylistView.swift +++ b/Music Tools/Views/Playlist/PlaylistView.swift @@ -43,6 +43,13 @@ struct PlaylistView: View { @State private var rec_num: Int = 0 + @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 + @State private var isRefreshing = false var body: some View { @@ -77,6 +84,39 @@ struct PlaylistView: View { Toggle(isOn: $shuffle) { Text("Shuffle") } + + 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"))]) + } + } + } } Section(header: Text("Inputs")){ NavigationLink(destination: PlaylistInputList(names: self.playlist.playlist_references, nameType: "Managed Playlists")) { @@ -117,9 +157,26 @@ struct PlaylistView: View { self.$shuffle.wrappedValue = self.playlist.shuffle self.$rec_num.wrappedValue = self.playlist.recommendation_sample + + 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 + } } } + 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 + } + func runPlaylist() { let api = PlaylistApi.runPlaylist(name: playlist.name) RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in diff --git a/Music Tools/Views/RootView.swift b/Music Tools/Views/RootView.swift index bfa3542..7e07a1b 100644 --- a/Music Tools/Views/RootView.swift +++ b/Music Tools/Views/RootView.swift @@ -158,7 +158,7 @@ struct RootView: View { let playlists = json["playlists"].arrayValue // parse playlists .map({ dict in - Playlist.fromDict(dictionary: dict) + Playlist.fromDict(dictionary: dict)! }) // sort .sorted(by: { $0.name.lowercased() < $1.name.lowercased() })