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-22 13:28:15 +00:00
|
|
|
import SwiftUIRefresh
|
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-20 18:25:28 +00:00
|
|
|
@State private var selection = 0 // Tab view selection
|
2020-02-19 23:00:23 +00:00
|
|
|
|
2020-02-20 18:25:28 +00:00
|
|
|
@State private var showAdd = false // State for showing add modal view
|
2020-02-19 23:00:23 +00:00
|
|
|
|
2020-02-20 18:25:28 +00:00
|
|
|
@State private var justDeletedPlaylists: Array<Playlist> = [] // Cache of recently deleted playlists for removing from next net request
|
|
|
|
@State private var justDeletedTags: Array<Tag> = []
|
2020-02-19 23:00:23 +00:00
|
|
|
|
2020-02-22 13:28:15 +00:00
|
|
|
@State private var isRefreshingPlaylists = false
|
|
|
|
@State private var isRefreshingTags = false
|
|
|
|
|
2020-02-20 18:25:28 +00:00
|
|
|
// refresh playlist list on interval
|
2020-02-22 13:28:15 +00:00
|
|
|
// 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-20 18:25:28 +00:00
|
|
|
|
|
|
|
// PLAYLISTS
|
2020-02-18 01:44:30 +00:00
|
|
|
NavigationView {
|
2020-02-19 23:00:23 +00:00
|
|
|
List{
|
2020-02-22 13:28:15 +00:00
|
|
|
ForEach(liveUser.playlists) { playlist in
|
2020-02-19 23:00:23 +00:00
|
|
|
PlaylistRow(playlist: playlist)
|
|
|
|
}
|
|
|
|
.onDelete { indexSet in
|
|
|
|
|
|
|
|
indexSet.forEach { index in
|
2020-02-20 18:25:28 +00:00
|
|
|
// add to recently deleted playlist cache
|
2020-02-22 13:28:15 +00:00
|
|
|
self.justDeletedPlaylists.append(self.liveUser.playlists[index])
|
2020-02-19 23:00:23 +00:00
|
|
|
|
2020-02-22 13:28:15 +00:00
|
|
|
let api = PlaylistApi.deletePlaylist(name: self.liveUser.playlists[index].name)
|
2020-02-19 23:00:23 +00:00
|
|
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-22 13:28:15 +00:00
|
|
|
self.liveUser.playlists.remove(atOffsets: indexSet)
|
2020-02-19 23:00:23 +00:00
|
|
|
}
|
2020-02-18 01:44:30 +00:00
|
|
|
}
|
2020-02-22 13:28:15 +00:00
|
|
|
.pullToRefresh(isShowing: $isRefreshingPlaylists) {
|
|
|
|
self.refreshPlaylists()
|
|
|
|
}
|
2020-02-18 01:44:30 +00:00
|
|
|
.navigationBarTitle(Text("Playlists").font(.title))
|
2020-02-20 18:25:28 +00:00
|
|
|
|
|
|
|
// add playlist button
|
2020-02-19 23:00:23 +00:00
|
|
|
.navigationBarItems(trailing:
|
|
|
|
Button(
|
|
|
|
action: { self.showAdd = true },
|
|
|
|
label: { Text("Add") }
|
|
|
|
).sheet(isPresented: $showAdd) {
|
2020-02-22 13:28:15 +00:00
|
|
|
AddPlaylistSheet(state: self.$showAdd, playlists: self.$liveUser.playlists)
|
2020-02-19 23:00:23 +00:00
|
|
|
}
|
|
|
|
)
|
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-20 18:25:28 +00:00
|
|
|
|
|
|
|
// TAGS
|
2020-02-18 01:44:30 +00:00
|
|
|
NavigationView {
|
2020-02-20 18:25:28 +00:00
|
|
|
List{
|
2020-02-22 13:28:15 +00:00
|
|
|
ForEach(liveUser.tags) { tag in
|
2020-02-20 18:25:28 +00:00
|
|
|
TagRow(tag: tag)
|
|
|
|
}
|
|
|
|
.onDelete { indexSet in
|
2020-02-18 01:44:30 +00:00
|
|
|
|
2020-02-20 18:25:28 +00:00
|
|
|
indexSet.forEach { index in
|
|
|
|
// add to recently deleted playlist cache
|
2020-02-22 13:28:15 +00:00
|
|
|
self.justDeletedTags.append(self.liveUser.tags[index])
|
2020-02-20 18:25:28 +00:00
|
|
|
|
2020-02-22 13:28:15 +00:00
|
|
|
let api = TagApi.deleteTag(tag_id: self.liveUser.tags[index].tag_id)
|
2020-02-20 18:25:28 +00:00
|
|
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-22 13:28:15 +00:00
|
|
|
self.liveUser.tags.remove(atOffsets: indexSet)
|
2020-02-20 18:25:28 +00:00
|
|
|
}
|
2020-02-16 18:47:09 +00:00
|
|
|
}
|
2020-02-22 13:28:15 +00:00
|
|
|
.pullToRefresh(isShowing: $isRefreshingTags) {
|
|
|
|
self.refreshTags()
|
|
|
|
}
|
2020-02-20 18:25:28 +00:00
|
|
|
.navigationBarTitle(Text("Tags").font(.title))
|
|
|
|
|
|
|
|
// add playlist button
|
|
|
|
.navigationBarItems(trailing:
|
|
|
|
Button(
|
|
|
|
action: { self.showAdd = true },
|
|
|
|
label: { Text("Add") }
|
|
|
|
).sheet(isPresented: $showAdd) {
|
2020-02-22 13:28:15 +00:00
|
|
|
AddPlaylistSheet(state: self.$showAdd, playlists: self.$liveUser.playlists)
|
2020-02-20 18:25:28 +00:00
|
|
|
}
|
|
|
|
)
|
2020-02-18 01:44:30 +00:00
|
|
|
}
|
|
|
|
.tabItem {
|
|
|
|
VStack {
|
|
|
|
Image(systemName: "sum")
|
|
|
|
Text("Tags")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.tag(1)
|
2020-02-20 18:25:28 +00:00
|
|
|
|
|
|
|
// SETTINGS
|
2020-02-21 01:24:09 +00:00
|
|
|
NavigationView {
|
|
|
|
SettingsList()
|
|
|
|
.navigationBarTitle(Text("Settings").font(.title))
|
|
|
|
}.tabItem {
|
2020-02-16 18:47:09 +00:00
|
|
|
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-22 13:28:15 +00:00
|
|
|
// .onReceive(timer) { _ in
|
|
|
|
// self.fetch()
|
|
|
|
// }
|
2020-02-18 01:44:30 +00:00
|
|
|
}.onAppear {
|
2020-02-22 13:28:15 +00:00
|
|
|
self.fetchAll()
|
2020-02-16 18:47:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-18 01:44:30 +00:00
|
|
|
|
2020-02-22 13:28:15 +00:00
|
|
|
private func fetchAll() {
|
|
|
|
refreshPlaylists()
|
|
|
|
refreshTags()
|
|
|
|
}
|
|
|
|
|
|
|
|
func refreshPlaylists() {
|
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-20 18:25:28 +00:00
|
|
|
let playlists = json["playlists"].arrayValue
|
|
|
|
// parse playlists
|
|
|
|
.map({ dict in
|
2020-02-23 00:24:02 +00:00
|
|
|
Playlist.fromDict(dictionary: dict)!
|
2020-02-20 18:25:28 +00:00
|
|
|
})
|
|
|
|
// sort
|
|
|
|
.sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
|
|
|
|
// filter playlists for those recently deleted
|
2020-02-19 23:00:23 +00:00
|
|
|
.filter { (rxPlaylist) -> Bool in
|
|
|
|
|
|
|
|
var deleted = false
|
2020-02-20 18:25:28 +00:00
|
|
|
for playlist in self.justDeletedPlaylists {
|
2020-02-19 23:00:23 +00:00
|
|
|
if playlist == rxPlaylist {
|
|
|
|
deleted = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return !deleted
|
|
|
|
}
|
2020-02-20 18:25:28 +00:00
|
|
|
// clear cache of recently deleted playlists
|
|
|
|
self.justDeletedPlaylists = []
|
2020-02-19 23:00:23 +00:00
|
|
|
|
2020-02-20 18:25:28 +00:00
|
|
|
// update state
|
2020-02-19 23:00:23 +00:00
|
|
|
self.liveUser.playlists = playlists
|
2020-02-22 13:28:15 +00:00
|
|
|
self.isRefreshingPlaylists = false
|
2020-02-18 21:43:30 +00:00
|
|
|
}
|
2020-02-19 01:20:19 +00:00
|
|
|
//TODO: do better error checking
|
2020-02-22 13:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func refreshTags() {
|
2020-02-20 18:25:28 +00:00
|
|
|
let tagApi = TagApi.getTags
|
|
|
|
RequestBuilder.buildRequest(apiRequest: tagApi).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")
|
|
|
|
}
|
|
|
|
|
|
|
|
let tags = json["tags"].arrayValue
|
|
|
|
// parse playlists
|
|
|
|
.map({ dict in
|
|
|
|
Tag.fromDict(dictionary: dict)
|
|
|
|
})
|
|
|
|
// sort
|
|
|
|
.sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
|
|
|
|
// filter playlists for those recently deleted
|
|
|
|
.filter { (rxTag) -> Bool in
|
|
|
|
|
|
|
|
var deleted = false
|
|
|
|
for tag in self.justDeletedTags {
|
|
|
|
if tag == rxTag {
|
|
|
|
deleted = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return !deleted
|
|
|
|
}
|
|
|
|
// clear cache of recently deleted playlists
|
|
|
|
self.justDeletedTags = []
|
|
|
|
|
|
|
|
// update state
|
|
|
|
self.liveUser.tags = tags
|
2020-02-22 13:28:15 +00:00
|
|
|
self.isRefreshingTags = false
|
2020-02-20 18:25:28 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|