Add Edit favorites

This commit is contained in:
Arkadiusz Fal
2022-11-12 01:47:31 +01:00
parent 2c004b81fe
commit ab5afbc9a4
9 changed files with 143 additions and 181 deletions

View File

@@ -1,43 +0,0 @@
import Foundation
import SwiftUI
struct DropFavorite: DropDelegate {
let item: FavoriteItem
@Binding var favorites: [FavoriteItem]
@Binding var current: FavoriteItem?
func dropEntered(info _: DropInfo) {
guard item != current else {
return
}
guard let current else {
return
}
let from = favorites.firstIndex(of: current)
let to = favorites.firstIndex(of: item)
guard let from, let to else {
return
}
guard favorites[to].id != current.id else {
return
}
favorites.move(
fromOffsets: IndexSet(integer: from),
toOffset: to > from ? to + 1 : to
)
}
func dropUpdated(info _: DropInfo) -> DropProposal? {
DropProposal(operation: .move)
}
func performDrop(info _: DropInfo) -> Bool {
current = nil
return true
}
}

View File

@@ -1,11 +0,0 @@
import Foundation
import SwiftUI
struct DropFavoriteOutside: DropDelegate {
@Binding var current: FavoriteItem?
func performDrop(info _: DropInfo) -> Bool {
current = nil
return true
}
}

View File

@@ -56,16 +56,6 @@ struct FavoriteItemView: View {
resource?.addObserver(store)
resource?.loadIfNeeded()
}
#if !os(tvOS)
.onDrag {
dragging = item
return NSItemProvider(object: item.id as NSString)
}
.onDrop(
of: [UTType.text],
delegate: DropFavorite(item: item, favorites: $favorites, current: $dragging)
)
#endif
}
}
.onChange(of: accounts.current) { _ in

View File

@@ -23,6 +23,9 @@ struct BrowsingSettings: View {
@EnvironmentObject<AccountsModel> private var accounts
@State private var homeHistoryItemsText = ""
#if os(macOS)
@State private var presentingEditFavoritesSheet = false
#endif
var body: some View {
Group {
@@ -67,9 +70,6 @@ struct BrowsingSettings: View {
}
#endif
Toggle("Show Open Videos quick actions", isOn: $showOpenActionsInHome)
if !accounts.isEmpty {
Toggle("Show Favorites", isOn: $showFavoritesInHome)
}
HStack {
Text("Recent history")
TextField("Recent history", text: $homeHistoryItemsText)
@@ -85,6 +85,36 @@ struct BrowsingSettings: View {
}
}
.multilineTextAlignment(.trailing)
if !accounts.isEmpty {
Toggle("Show Favorites", isOn: $showFavoritesInHome)
Group {
#if os(macOS)
Button {
presentingEditFavoritesSheet = true
} label: {
Text("Edit Favorites...")
}
.sheet(isPresented: $presentingEditFavoritesSheet) {
VStack(alignment: .leading) {
Button("Done") {
presentingEditFavoritesSheet = false
}
.padding()
.keyboardShortcut(.cancelAction)
EditFavorites()
}
.frame(width: 500, height: 300)
}
#else
NavigationLink(destination: LazyView(EditFavorites())) {
Text("Edit Favorites...")
}
#endif
}
.disabled(!showFavoritesInHome)
}
}
}

View File

@@ -0,0 +1,105 @@
import Defaults
import SwiftUI
struct EditFavorites: View {
@EnvironmentObject<PlaylistsModel> private var playlistsModel
private var model = FavoritesModel.shared
@Default(.favorites) private var favorites
var body: some View {
Group {
List {
Section(header: Text("Favorites")) {
if favorites.isEmpty {
Text("Favorites is empty")
.foregroundColor(.secondary)
}
ForEach(favorites) { item in
HStack {
Text(label(item))
Spacer()
HStack(spacing: 30) {
Button {
model.moveUp(item)
} label: {
Label("Move Up", systemImage: "arrow.up")
}
Button {
model.moveDown(item)
} label: {
Label("Move Down", systemImage: "arrow.down")
}
Button {
model.remove(item)
} label: {
Label("Remove", systemImage: "trash")
}
}
#if !os(tvOS)
.buttonStyle(.borderless)
#endif
}
}
}
#if os(tvOS)
.padding(.trailing, 40)
#endif
#if os(tvOS)
Divider()
.padding(20)
#endif
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
}
}
}
}
#if os(tvOS)
.padding(.trailing, 40)
#endif
}
.labelStyle(.iconOnly)
.frame(alignment: .leading)
#if os(tvOS)
.frame(width: 1000)
#endif
}
.navigationTitle("Favorites")
}
func label(_ item: FavoriteItem) -> String {
if case let .playlist(id) = item.section {
return playlistsModel.find(id: id)?.title ?? "Playlist"
}
return item.section.label
}
}
struct EditFavorites_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
EditFavorites()
}
.injectFixtureEnvironmentObjects()
}
}

View File

@@ -26,11 +26,6 @@ struct PopularView: View {
#endif
}
#if !os(tvOS)
.toolbar {
ToolbarItem(placement: .automatic) {
FavoriteButton(item: FavoriteItem(section: .popular))
}
}
.background(
Button("Refresh") {
resource?.load()

View File

@@ -39,11 +39,6 @@ struct SubscriptionsView: View {
}
#if !os(tvOS)
.toolbar {
ToolbarItem(placement: .automatic) {
FavoriteButton(item: FavoriteItem(section: .subscriptions))
}
}
.background(
Button("Refresh") {
loadResources(force: true)