I would like to do something like this:
<code>struct HandleUrlDropModifier<Content>: ViewModifier where Content: DynamicViewContent {
@Environment(.modelContext) var modelContext
var content: Content
func body(content: Content) -> some View {
content
.dropDestination(for: URL.self) { items, index in
// index is Int
handleUrlDrop(items, at: index)
}
}
func handleUrlDrop(_ items: [URL], at index: Int) {
modelContext.insert(...)
}
}
</code>
<code>struct HandleUrlDropModifier<Content>: ViewModifier where Content: DynamicViewContent {
@Environment(.modelContext) var modelContext
var content: Content
func body(content: Content) -> some View {
content
.dropDestination(for: URL.self) { items, index in
// index is Int
handleUrlDrop(items, at: index)
}
}
func handleUrlDrop(_ items: [URL], at index: Int) {
modelContext.insert(...)
}
}
</code>
struct HandleUrlDropModifier<Content>: ViewModifier where Content: DynamicViewContent {
@Environment(.modelContext) var modelContext
var content: Content
func body(content: Content) -> some View {
content
.dropDestination(for: URL.self) { items, index in
// index is Int
handleUrlDrop(items, at: index)
}
}
func handleUrlDrop(_ items: [URL], at index: Int) {
modelContext.insert(...)
}
}
Is that possible somehow?
What I’ve tried
Writing it as a direct extension doesn’t work, since I want to use an @Environtment
variable:
<code>extension DynamicViewContent {
@Environment(.modelContext) var modelContext // <- not possible
func handleUrlDrop() -> some View {
self
.dropDestination(for: URL.self) { items, index in
modelContext.insert(...)
}
}
}
</code>
<code>extension DynamicViewContent {
@Environment(.modelContext) var modelContext // <- not possible
func handleUrlDrop() -> some View {
self
.dropDestination(for: URL.self) { items, index in
modelContext.insert(...)
}
}
}
</code>
extension DynamicViewContent {
@Environment(.modelContext) var modelContext // <- not possible
func handleUrlDrop() -> some View {
self
.dropDestination(for: URL.self) { items, index in
modelContext.insert(...)
}
}
}
This works for regular Views, but the .dropDestination
action
is different for a DynamicViewContent
:
<code>struct HandleUrlDropModifier: ViewModifier {
@Environment(.modelContext) var modelContext
func body(content: Content) -> some View {
content
.dropDestination(for: URL.self) { items, location in
// location is CGPoint
handleUrlDrop(items)
return true
}
}
func handleUrlDrop(_ items: [URL]) {
modelContext.insert(...)
}
}
</code>
<code>struct HandleUrlDropModifier: ViewModifier {
@Environment(.modelContext) var modelContext
func body(content: Content) -> some View {
content
.dropDestination(for: URL.self) { items, location in
// location is CGPoint
handleUrlDrop(items)
return true
}
}
func handleUrlDrop(_ items: [URL]) {
modelContext.insert(...)
}
}
</code>
struct HandleUrlDropModifier: ViewModifier {
@Environment(.modelContext) var modelContext
func body(content: Content) -> some View {
content
.dropDestination(for: URL.self) { items, location in
// location is CGPoint
handleUrlDrop(items)
return true
}
}
func handleUrlDrop(_ items: [URL]) {
modelContext.insert(...)
}
}