Swiftui: Detecting onMove Start

Viewed 131

Goal: I would like to be able to adjust the stepper in editMode but when the user starts to move the title row either drag the stepper along with it or hide all the stepper rows until the move is completed.

Current: When user moves a title row it goes between all rows instead of just the title rows.

Tried: I looked into the onMoved documentation & it seems you can only do something once the move is finished. I also tried using longpress & tap gestures but they only seem to work on the text itself not the row. I am not sure if this is currently possible.

Code:

struct ContentView: View {
    @ObservedObject var items = ItemGroup()
    @State private var editMode = EditMode.active
    
    var body: some View {
        NavigationView{
            List {
                
                ForEach(Array(items.group.enumerated()), id: \.element.id) { index, item in
                    Text(items.group[index].item)
                        .deleteDisabled(!editMode.isEditing)

                    
                    if editMode.isEditing {
                        let stepper = items.group[index].stepper
                        
                        Stepper("Stepper: \(stepper)", value: $items.group[index].stepper, in: 1...10)
                            .moveDisabled(true)
                            .deleteDisabled(true)
                    }
                    
                }
                .onMove(perform: onMove)
                
            }
            .navigationBarTitle("List")
            .navigationBarItems(leading: EditButton(), trailing: addButton)
            .environment(\.editMode, $editMode)
            
        }
    }
    
    
    
    private var addButton: some View {
        switch editMode {
        case .active:
            return AnyView(Button(action: onAdd) { Image(systemName: "plus") })
        default:
            return AnyView(EmptyView())
        }
    }
    
    func onAdd() {
        let newItem = SingleItem(item: "Word - \(items.group.count)", supplement: [""], stepper: 1, current: 1)
        self.items.group.append(newItem)
    }
    
    
    private func onMove(source: IndexSet, destination: Int) {
        items.group.move(fromOffsets: source, toOffset: destination)
    }
}

struct SingleItem: Identifiable {
    let id = UUID()
    var item: String
    var supplement: [String]
    var stepper: Int
    var current: Int
}

class ItemGroup: ObservableObject{
    @Published var group = [SingleItem]()
}
0 Answers
Related