SwiftUI: Move item from List from section A to section B by "draging"

Viewed 248

Im trying to achieve something simple: drag an item from a section A to section B, the problem is that the onMove action return an Int not a Index so i can't get the new section and the new row index.

func onMove(perform action: ((IndexSet, Int) -> Void)?) -> some DynamicViewContent

Can anyone tell me if this is possible ?

Here's a sample code:

struct ContentView: View {
  
  @State var categories: [Tree<String>] = [
      .init(
          value: "Clothing",
          children: [
              .init(value: "Hoodies"),
              .init(value: "Jackets"),
              .init(value: "Joggers"),
              .init(value: "Jumpers"),
              .init(
                  value: "Jeans",
                  children: [
                      .init(value: "Regular"),
                      .init(value: "Slim")
                  ]
              ),
          ]
      ),
      .init(
          value: "Shoes",
          children: [
              .init(value: "Boots"),
              .init(value: "Sliders"),
              .init(value: "Sandals"),
              .init(value: "Trainers"),
          ]
      )
  ]
  
    var body: some View {
        List {
            ForEach(categories, id: \.self) { section in
                Section(header: Text(section.value)) {
                    OutlineGroup(
                        section.children ?? [],
                        id: \.value,
                        children: \.children
                    ) { tree in
                        Text(tree.value)
                            .font(.subheadline)
                    }
                }
            }
            .onMove(perform: onMove)

        }.listStyle(SidebarListStyle())
    }
  
  private func onMove(source: IndexSet, destination: Int) {
    categories.move(fromOffsets: source, toOffset: destination)
  }
}
0 Answers
Related