2020-02-19 23:00:23 +00:00
|
|
|
//
|
|
|
|
// AddPlaylistSheet.swift
|
2022-08-07 13:31:15 +01:00
|
|
|
// Mixonomer
|
2020-02-19 23:00:23 +00:00
|
|
|
//
|
|
|
|
// Created by Andy Pack on 19/02/2020.
|
|
|
|
// Copyright © 2020 Sarsoo. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
import SwiftyJSON
|
|
|
|
|
|
|
|
struct AddPlaylistSheet: View {
|
|
|
|
|
2022-08-09 17:42:01 +01:00
|
|
|
@EnvironmentObject var liveUser: LiveUser
|
2020-02-19 23:00:23 +00:00
|
|
|
@State private var selectedType = 0
|
|
|
|
@State private var name = ""
|
|
|
|
@State private var errorMessage = ""
|
|
|
|
@State private var isLoading = false
|
|
|
|
|
2020-03-20 00:08:18 +00:00
|
|
|
@Environment(\.presentationMode) var presentationMode
|
|
|
|
|
2020-02-19 23:00:23 +00:00
|
|
|
@Binding var playlists: Array<Playlist>
|
2020-03-03 00:04:20 +00:00
|
|
|
@Binding var username: String
|
2020-02-19 23:00:23 +00:00
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
VStack {
|
|
|
|
HStack(alignment: .center) {
|
|
|
|
Text("New Playlist")
|
|
|
|
.font(.largeTitle)
|
|
|
|
.multilineTextAlignment(.center)
|
|
|
|
.padding([.top, .leading, .trailing], 20.0)
|
|
|
|
|
|
|
|
}
|
|
|
|
Picker(selection: $selectedType, label: Text("Picker")) {
|
|
|
|
Text("Default").tag(0)
|
|
|
|
Text("Recents").tag(1)
|
|
|
|
Text("Last.fm Chart").tag(2)
|
|
|
|
}
|
|
|
|
.pickerStyle(SegmentedPickerStyle())
|
|
|
|
.padding()
|
|
|
|
TextField("Name", text: $name)
|
|
|
|
.textFieldStyle(RoundedBorderTextFieldStyle())
|
|
|
|
.padding([.bottom, .leading, .trailing], 20.0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Button(action: create){
|
|
|
|
Text("Add")
|
|
|
|
.font(.title)
|
|
|
|
}
|
|
|
|
.disabled(isLoading)
|
|
|
|
.padding()
|
|
|
|
|
|
|
|
Text(errorMessage)
|
|
|
|
.foregroundColor(Color.red)
|
|
|
|
.padding()
|
|
|
|
}
|
|
|
|
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
|
|
|
|
}
|
|
|
|
|
|
|
|
func create(){
|
|
|
|
if name.count == 0 {
|
|
|
|
errorMessage = "Enter Playlist Name"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var namePresent = false
|
|
|
|
for playlist in playlists {
|
|
|
|
if playlist.name == name {
|
|
|
|
namePresent = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if namePresent == true {
|
|
|
|
errorMessage = "Playlist already created"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-30 17:05:23 +01:00
|
|
|
let playlist: Playlist = Playlist(name: name, username: username)
|
2020-03-03 00:04:20 +00:00
|
|
|
switch PlaylistType(rawValue: selectedType) ?? .defaultPlaylist {
|
|
|
|
case .defaultPlaylist:
|
2020-04-30 17:05:23 +01:00
|
|
|
playlist.type = "default"
|
2020-03-03 00:04:20 +00:00
|
|
|
break
|
|
|
|
case .recents:
|
2020-04-30 17:05:23 +01:00
|
|
|
playlist.type = "recents"
|
2020-03-03 00:04:20 +00:00
|
|
|
break
|
|
|
|
case .fmchart:
|
2020-04-30 17:05:23 +01:00
|
|
|
playlist.type = "fmchart"
|
2020-03-03 00:04:20 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-02-19 23:00:23 +00:00
|
|
|
isLoading = true
|
|
|
|
let api = PlaylistApi.newPlaylist(name: self.name,
|
|
|
|
type: PlaylistType(rawValue: selectedType) ?? .defaultPlaylist)
|
|
|
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
2020-03-03 00:04:20 +00:00
|
|
|
|
2022-08-13 22:07:59 +01:00
|
|
|
if self.liveUser.check_network_response(response: response) {
|
2022-08-11 20:15:21 +01:00
|
|
|
|
2022-08-09 17:42:01 +01:00
|
|
|
self.playlists.append(playlist)
|
|
|
|
self.playlists = self.playlists.sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
|
|
|
|
|
|
|
|
self.isLoading = false
|
|
|
|
self.presentationMode.wrappedValue.dismiss()
|
|
|
|
|
2022-08-11 20:15:21 +01:00
|
|
|
} else {
|
|
|
|
|
2022-08-09 17:42:01 +01:00
|
|
|
}
|
2020-02-19 23:00:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct AddPlaylistSheet_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2020-03-20 00:08:18 +00:00
|
|
|
AddPlaylistSheet(playlists: .constant([]), username: .constant("username"))
|
2022-08-11 20:15:21 +01:00
|
|
|
.environmentObject(LiveUser(playlists: [], tags: [], username: "user", loggedIn: false))
|
2020-02-19 23:00:23 +00:00
|
|
|
}
|
|
|
|
}
|