Mixonomer-iOS/Mixonomer/Views/Playlist/PlaylistList.swift

110 lines
3.8 KiB
Swift
Raw Normal View History

//
// PlaylistList.swift
2022-08-07 13:31:15 +01:00
// Mixonomer
//
// Created by Andy Pack on 25/04/2020.
// Copyright © 2020 Sarsoo. All rights reserved.
//
import SwiftUI
import ToastUI
struct PlaylistList: View {
@EnvironmentObject var liveUser: LiveUser
@State private var showAdd = false // State for showing add modal view
@State private var showingToast = false
@State private var toastText = ""
@State private var toastSuccess = true
var body: some View {
NavigationView {
List{
if liveUser.user?.spotify_linked == false {
Text("Spotify isn't linked, login to the web client to pair")
Button(action: {
UIApplication.shared.open(URL(string: "https://mixonomer.sarsoo.xyz/")!)
}) {
Text("Go-to Mixonomer Web")
.foregroundColor(.blue)
}
}
else if liveUser.playlists.count > 0 {
ForEach(liveUser.playlists.indices, id:\.self) { idx in
PlaylistRow(playlist: self.$liveUser.playlists[idx])
}
.onDelete { indexSet in
indexSet.forEach { index in
let api = PlaylistApi.deletePlaylist(name: self.liveUser.playlists[index].name)
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
if self.liveUser.check_network_response(response: response) {
} else {
2022-08-09 17:42:01 +01:00
}
}
}
self.liveUser.playlists.remove(atOffsets: indexSet)
}
}else {
Text("No Playlists")
}
}
.refreshable
{
self.liveUser.refresh_playlists(onSuccess: {
toastText = "Refreshed!"
toastSuccess = true
showingToast = true
}, onFailure: {
toastText = "Refresh Failed"
toastSuccess = false
showingToast = true
})
}
.navigationBarTitle(Text("Playlists 📻"))
.navigationBarItems(trailing:
Button(
action: { self.showAdd = true },
label: { Text("Add") }
).sheet(isPresented: $showAdd) {
AddPlaylistSheet(playlists: self.$liveUser.playlists, username: self.$liveUser.username)
}
)
2022-08-12 10:04:03 +01:00
.toast(isPresented: $showingToast, dismissAfter: 1.0){
if toastSuccess {
ToastView(toastText)
.toastViewStyle(.success)
}
else {
ToastView(toastText)
.toastViewStyle(.failure)
}
}
.toastDimmedBackground(false)
}
}
}
struct PlaylistList_Previews: PreviewProvider {
static var previews: some View {
Group {
PlaylistList()
.environmentObject(LiveUser(playlists: [], tags: [], username: "user", loggedIn: false))
PlaylistList()
.environmentObject(LiveUser(playlists: [], tags: [], username: "user", loggedIn: false, user: User()))
}
}
}