yattee/Shared/Settings/EditFavorites.swift

119 lines
3.5 KiB
Swift
Raw Normal View History

2022-11-12 00:47:31 +00:00
import Defaults
import SwiftUI
struct EditFavorites: View {
private var playlistsModel = PlaylistsModel.shared
2022-11-12 00:47:31 +00:00
private var model = FavoritesModel.shared
@Default(.favorites) private var favorites
var body: some View {
Group {
2022-11-12 01:39:44 +00:00
#if os(tvOS)
ScrollView {
VStack {
editor
2022-11-12 00:47:31 +00:00
}
2022-11-12 01:39:44 +00:00
}
.frame(width: 1000)
#else
List {
editor
}
#endif
}
.navigationTitle("Favorites")
}
2022-11-12 00:47:31 +00:00
2022-11-12 01:39:44 +00:00
var editor: some View {
Group {
Section(header: Text("Favorites")) {
if favorites.isEmpty {
Text("Favorites is empty")
.foregroundColor(.secondary)
}
ForEach(favorites) { item in
HStack {
Text(label(item))
2022-11-12 00:47:31 +00:00
2022-11-12 01:39:44 +00:00
Spacer()
HStack(spacing: 30) {
Button {
model.moveUp(item)
} label: {
Label("Move Up", systemImage: "arrow.up")
}
2022-11-12 00:47:31 +00:00
2022-11-12 01:39:44 +00:00
Button {
model.moveDown(item)
} label: {
Label("Move Down", systemImage: "arrow.down")
}
Button {
model.remove(item)
} label: {
Label("Remove", systemImage: "trash")
2022-11-12 00:47:31 +00:00
}
}
2022-11-12 01:39:44 +00:00
#if !os(tvOS)
.buttonStyle(.borderless)
#endif
2022-11-12 00:47:31 +00:00
}
}
2022-11-12 01:39:44 +00:00
}
#if os(tvOS)
.padding(.trailing, 40)
#endif
2022-11-12 00:47:31 +00:00
2022-11-12 01:39:44 +00:00
#if os(tvOS)
Divider()
.padding(20)
#endif
2022-11-12 00:47:31 +00:00
2022-11-12 01:39:44 +00:00
if !model.addableItems().isEmpty {
2022-11-12 00:47:31 +00:00
Section(header: Text("Available")) {
ForEach(model.addableItems()) { item in
HStack {
Text(label(item))
Spacer()
Button {
model.add(item)
} label: {
Label("Add to Favorites", systemImage: "heart")
#if os(tvOS)
.font(.system(size: 30))
#endif
}
2022-11-12 01:39:44 +00:00
#if !os(tvOS)
.buttonStyle(.borderless)
#endif
2022-11-12 00:47:31 +00:00
}
}
}
#if os(tvOS)
.padding(.trailing, 40)
#endif
}
}
2022-11-12 01:39:44 +00:00
.labelStyle(.iconOnly)
2022-11-12 00:47:31 +00:00
}
func label(_ item: FavoriteItem) -> String {
if case let .playlist(id) = item.section {
return playlistsModel.find(id: id)?.title ?? "Playlist".localized()
2022-11-12 00:47:31 +00:00
}
return item.section.label.localized()
2022-11-12 00:47:31 +00:00
}
}
struct EditFavorites_Previews: PreviewProvider {
static var previews: some View {
2022-11-12 01:39:44 +00:00
EditFavorites()
.injectFixtureEnvironmentObjects()
2022-11-12 00:47:31 +00:00
}
}