yattee/Shared/Favorites/DropFavorite.swift

44 lines
966 B
Swift
Raw Normal View History

2021-11-01 21:56:18 +00:00
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
}
2022-03-26 12:50:01 +00:00
guard let current = current else {
return
}
let from = favorites.firstIndex(of: current)
let to = favorites.firstIndex(of: item)
guard let from = from, let to = to else {
return
}
2021-11-01 21:56:18 +00:00
2022-03-26 12:50:01 +00:00
guard favorites[to].id != current.id else {
2021-11-01 21:56:18 +00:00
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
}
}