added derivative playlist types and respective UI elements
This commit is contained in:
parent
fea9e3f84b
commit
6318ab4f16
@ -55,8 +55,10 @@ class Playlist: Identifiable, Equatable {
|
|||||||
self.shuffle = shuffle
|
self.shuffle = shuffle
|
||||||
}
|
}
|
||||||
|
|
||||||
static func fromDict(dictionary: JSON) -> Playlist {
|
static func fromDict(dictionary: JSON) -> Playlist? {
|
||||||
return Playlist(name: dictionary["name"].stringValue,
|
switch dictionary["type"].string {
|
||||||
|
case "default":
|
||||||
|
return Playlist(name: dictionary["name"].stringValue,
|
||||||
uri: dictionary["uri"].stringValue,
|
uri: dictionary["uri"].stringValue,
|
||||||
username: dictionary["username"].stringValue,
|
username: dictionary["username"].stringValue,
|
||||||
|
|
||||||
@ -68,6 +70,42 @@ class Playlist: Identifiable, Equatable {
|
|||||||
playlist_references: dictionary["playlist_references"].arrayObject as! Array<String>,
|
playlist_references: dictionary["playlist_references"].arrayObject as! Array<String>,
|
||||||
|
|
||||||
shuffle: dictionary["shuffle"].boolValue)
|
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<String>,
|
||||||
|
playlist_references: dictionary["playlist_references"].arrayObject as! Array<String>,
|
||||||
|
|
||||||
|
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<String>,
|
||||||
|
playlist_references: dictionary["playlist_references"].arrayObject as! Array<String>,
|
||||||
|
|
||||||
|
shuffle: dictionary["shuffle"].boolValue,
|
||||||
|
|
||||||
|
chart_range: LastFmRange(rawValue: dictionary["chart_range"].stringValue)!,
|
||||||
|
chart_limit: dictionary["chart_limit"].intValue)
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var link: String {
|
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<String>,
|
||||||
|
playlist_references: Array<String>,
|
||||||
|
|
||||||
|
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<String>,
|
||||||
|
playlist_references: Array<String>,
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -43,6 +43,13 @@ struct PlaylistView: View {
|
|||||||
|
|
||||||
@State private var rec_num: Int = 0
|
@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
|
@State private var isRefreshing = false
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
@ -77,6 +84,39 @@ struct PlaylistView: View {
|
|||||||
Toggle(isOn: $shuffle) {
|
Toggle(isOn: $shuffle) {
|
||||||
Text("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")){
|
Section(header: Text("Inputs")){
|
||||||
NavigationLink(destination: PlaylistInputList(names: self.playlist.playlist_references, nameType: "Managed Playlists")) {
|
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.$shuffle.wrappedValue = self.playlist.shuffle
|
||||||
|
|
||||||
self.$rec_num.wrappedValue = self.playlist.recommendation_sample
|
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() {
|
func runPlaylist() {
|
||||||
let api = PlaylistApi.runPlaylist(name: playlist.name)
|
let api = PlaylistApi.runPlaylist(name: playlist.name)
|
||||||
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
||||||
|
@ -158,7 +158,7 @@ struct RootView: View {
|
|||||||
let playlists = json["playlists"].arrayValue
|
let playlists = json["playlists"].arrayValue
|
||||||
// parse playlists
|
// parse playlists
|
||||||
.map({ dict in
|
.map({ dict in
|
||||||
Playlist.fromDict(dictionary: dict)
|
Playlist.fromDict(dictionary: dict)!
|
||||||
})
|
})
|
||||||
// sort
|
// sort
|
||||||
.sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
|
.sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
|
||||||
|
Loading…
Reference in New Issue
Block a user