1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 14:50:32 +05:30
yattee/Shared/Home/DropFavorite.swift

44 lines
944 B
Swift
Raw Normal View History

2021-11-02 03:26:18 +05:30
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-09-28 19:57:01 +05:30
guard let current else {
2022-03-26 18:20:01 +05:30
return
}
let from = favorites.firstIndex(of: current)
let to = favorites.firstIndex(of: item)
2022-09-28 19:57:01 +05:30
guard let from, let to else {
2022-03-26 18:20:01 +05:30
return
}
2021-11-02 03:26:18 +05:30
2022-03-26 18:20:01 +05:30
guard favorites[to].id != current.id else {
2021-11-02 03:26:18 +05:30
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
}
}