I have reviewed some questions on Stack Overflow about drag and drop reorder with SwiftUI, and this one was particularly helpful: SwiftUI | Using onDrag and onDrop to reorder Items within one single LazyGrid?
I'm looking to expand this functionality where I drag something from one list of items to another in my SwifUI app. Let's say I have a Task
list:
//TaskView.swift
ScrollView{
VStack{
ForEach(model.tasks, id: \.self){ task in
Text(task.name)
.onDrag{
NSItemProvider(object: String(task.id) as NSString)
}
}
}
}
...and I also have a Project
list that I can drag a Task
onto to move it to that project:
//ProjectView.swift
ScrollView{
VStack{
ForEach(model.projects, id: \.self){ project in
Text(project.name)
.onDrop(of: [UTType.text], delegate: ProjectDropDelegate(project: project))
}
}
}
The part I'm struggling with is in my ProjectDropDelegate
where I'm trying to determine a couple things:
- What kind of object is being dropped on me? (it must be a task)
- If it's a task, what is its
id
so I can take action on it? (or, ideally, I'd get the wholeTask
object to work with)
I can't figure out how to make my NSItemProvider
in .onDrag
use anything other than a string and still work with my SwiftUI drag/drop functionality. For what it's worth, my Task
and Project
objects are Core Data classes.
How can I make NSItemProvider
contain key-value pairs so I can pass a type identifier string like myapp.task
(for #1 above) and an id
(for #2)?