user1151874
user1151874

Reputation: 279

SwiftUI does not perform onInsert when the list is empty?

I have 2 lists on the UI, 1 list is allow user to drag to another list. I have problem when the "selected product list" is empty. It does not perform the .onInsert function.

It have no problem when the array "selectedProduct" is not empty when it was rendering.
How can i to allow user to drag and drop when the other list is completely empty?

List(selection: $selection)
{
     HStack(alignment: .center)
     {
       Spacer().frame(width: 200)
       Text("Product Name")
           .foregroundColor(blue)
           .lineLimit(1)
       Spacer()
       Text("Price")
           .foregroundColor(blue)
       Spacer()
       Text("Unit")
            .foregroundColor(blue)
       Spacer()
      }

   ForEach(selectedProduct, id:\.id)
   {
       rider in
       SelectedProductView()    
   }
   .onInsert(of: ["public.data"])
   {
       self.addProduct(position: $0, itemProviders: $1, top: true)
   }
}

Upvotes: 3

Views: 948

Answers (1)

Asperi
Asperi

Reputation: 258247

The .onInsert works on DynamicViewContent, so when your selectedProduct is empty there is no content, so nowhere to insert.

There two possibilities as I see to solve this situation:

1) implement support for drop on List and make it active when selectedProduct is empty to make possible to add first element in dynamic content

2) implement stub product-item (like, empty row showing "drop here" text) that is explicitly added to selectedProduct when it becomes empty of real products, thus making content not empty to allow standard .onInsert work for you.

Upvotes: 4

Related Questions