2020-02-16 18:47:09 +00:00
|
|
|
//
|
2020-02-18 01:44:30 +00:00
|
|
|
// RootView.swift
|
2020-02-16 18:47:09 +00:00
|
|
|
// Music Tools
|
|
|
|
//
|
|
|
|
// Created by Andy Pack on 16/02/2020.
|
|
|
|
// Copyright © 2020 Sarsoo. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
2020-02-18 01:44:30 +00:00
|
|
|
import Alamofire
|
2020-02-18 21:43:30 +00:00
|
|
|
import SwiftyJSON
|
2020-02-16 18:47:09 +00:00
|
|
|
|
2020-02-17 02:21:49 +00:00
|
|
|
struct RootView: View {
|
2020-02-19 23:00:23 +00:00
|
|
|
|
|
|
|
@EnvironmentObject var liveUser: LiveUser
|
|
|
|
|
2020-02-16 18:47:09 +00:00
|
|
|
@State private var selection = 0
|
2020-02-18 21:43:30 +00:00
|
|
|
@State private var playlists: Array<Playlist> = []
|
2020-02-19 23:00:23 +00:00
|
|
|
|
|
|
|
@State private var isLoading = true
|
|
|
|
@State private var showAdd = false
|
|
|
|
|
|
|
|
@State private var onClose = onSheetClose
|
|
|
|
|
|
|
|
@State private var justDeleted: Array<Playlist> = []
|
|
|
|
|
|
|
|
func onSheetClose() {
|
|
|
|
self.fetch()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let timer = Timer.publish(every: 3, on: .main, in: .common).autoconnect()
|
2020-02-16 18:47:09 +00:00
|
|
|
|
|
|
|
var body: some View {
|
2020-02-19 23:00:23 +00:00
|
|
|
TabView {
|
2020-02-18 01:44:30 +00:00
|
|
|
NavigationView {
|
2020-02-19 23:00:23 +00:00
|
|
|
List{
|
|
|
|
ForEach(playlists) { playlist in
|
|
|
|
PlaylistRow(playlist: playlist)
|
|
|
|
}
|
|
|
|
.onDelete { indexSet in
|
|
|
|
|
|
|
|
indexSet.forEach { index in
|
|
|
|
self.justDeleted.append(self.playlists[index])
|
|
|
|
|
|
|
|
let api = PlaylistApi.deletePlaylist(name: self.playlists[index].name)
|
|
|
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.playlists.remove(atOffsets: indexSet)
|
|
|
|
}
|
2020-02-18 01:44:30 +00:00
|
|
|
}
|
|
|
|
.navigationBarTitle(Text("Playlists").font(.title))
|
2020-02-19 23:00:23 +00:00
|
|
|
.navigationBarItems(trailing:
|
|
|
|
Button(
|
|
|
|
action: { self.showAdd = true },
|
|
|
|
label: { Text("Add") }
|
|
|
|
).sheet(isPresented: $showAdd) {
|
|
|
|
AddPlaylistSheet(state: self.$showAdd, playlists: self.$playlists)
|
|
|
|
}
|
|
|
|
)
|
2020-02-16 18:47:09 +00:00
|
|
|
}
|
|
|
|
.tabItem {
|
|
|
|
VStack {
|
2020-02-18 01:44:30 +00:00
|
|
|
Image(systemName: "music.note.list")
|
2020-02-16 18:47:09 +00:00
|
|
|
Text("Playlists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.tag(0)
|
2020-02-18 01:44:30 +00:00
|
|
|
//
|
|
|
|
//
|
|
|
|
NavigationView {
|
|
|
|
List(/*@START_MENU_TOKEN@*/0 ..< 5/*@END_MENU_TOKEN@*/) { item in
|
|
|
|
Text("Tag")
|
|
|
|
.font(.title)
|
|
|
|
|
2020-02-16 18:47:09 +00:00
|
|
|
}
|
2020-02-18 01:44:30 +00:00
|
|
|
.navigationBarTitle(Text("Tags"))
|
|
|
|
}
|
|
|
|
.tabItem {
|
|
|
|
VStack {
|
|
|
|
Image(systemName: "sum")
|
|
|
|
Text("Tags")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.tag(1)
|
|
|
|
//
|
|
|
|
//
|
2020-02-16 18:47:09 +00:00
|
|
|
Text("Settings")
|
|
|
|
.font(.title)
|
|
|
|
.tabItem {
|
|
|
|
VStack {
|
2020-02-18 01:44:30 +00:00
|
|
|
Image(systemName: "slider.horizontal.3")
|
2020-02-16 18:47:09 +00:00
|
|
|
Text("Settings")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.tag(2)
|
2020-02-19 23:00:23 +00:00
|
|
|
.onReceive(timer) { _ in
|
|
|
|
self.fetch()
|
|
|
|
}
|
2020-02-18 01:44:30 +00:00
|
|
|
}.onAppear {
|
|
|
|
self.fetch()
|
2020-02-16 18:47:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-18 01:44:30 +00:00
|
|
|
|
|
|
|
private func fetch() {
|
2020-02-18 21:43:30 +00:00
|
|
|
let api = PlaylistApi.getPlaylists
|
|
|
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
|
|
|
|
|
|
|
guard let data = response.data else {
|
|
|
|
fatalError("error getting playlists")
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let json = try? JSON(data: data) else {
|
|
|
|
fatalError("error parsing reponse")
|
|
|
|
}
|
|
|
|
|
2020-02-19 23:00:23 +00:00
|
|
|
let playlists = json["playlists"].arrayValue.map({ dict in
|
2020-02-18 21:43:30 +00:00
|
|
|
Playlist.fromDict(dictionary: dict)
|
|
|
|
}).sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
|
2020-02-19 23:00:23 +00:00
|
|
|
.filter { (rxPlaylist) -> Bool in
|
|
|
|
|
|
|
|
var deleted = false
|
|
|
|
for playlist in self.justDeleted {
|
|
|
|
if playlist == rxPlaylist {
|
|
|
|
deleted = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return !deleted
|
|
|
|
}
|
|
|
|
self.justDeleted = []
|
|
|
|
|
|
|
|
self.liveUser.playlists = playlists
|
|
|
|
self.playlists = self.liveUser.playlists
|
2020-02-18 21:43:30 +00:00
|
|
|
}
|
2020-02-19 01:20:19 +00:00
|
|
|
//TODO: do better error checking
|
2020-02-18 01:44:30 +00:00
|
|
|
}
|
2020-02-16 18:47:09 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 02:21:49 +00:00
|
|
|
struct RootView_Previews: PreviewProvider {
|
2020-02-16 18:47:09 +00:00
|
|
|
static var previews: some View {
|
2020-02-17 02:21:49 +00:00
|
|
|
RootView()
|
2020-02-16 18:47:09 +00:00
|
|
|
}
|
|
|
|
}
|