Home IOS Development ios – Utilizing Sheet to edit and take away gadgets from record in SwiftUI

ios – Utilizing Sheet to edit and take away gadgets from record in SwiftUI

0
ios – Utilizing Sheet to edit and take away gadgets from record in SwiftUI

[ad_1]

I’ve a requirement that when the consumer clicks aspect of the record, a sheet ought to open with info of the chosen merchandise and with the button to delete it. I’ve had some issues with dismissing sheet and so forth. and that is the perfect code I discovered:

import SwiftUI


struct MyTest1: View {
    @StateObject non-public var listItems = StateItems.gadgets
    @Atmosphere(.dismiss) var dismiss
    
    non-public var isEmptyList: Bool {
        get {
            listItems.gadgets.depend == 0
        }
    }
    var physique: some View {
        VStack {
            if isEmptyList {
                Textual content("No gadgets in record!")
            } else {
                ListView()
            }
        }
        .padding()
    }
}

#Preview {
    MyTest1()
}

struct ListView: View {
    @StateObject non-public var listItems = StateItems.gadgets
    @State non-public var showingSheet = false
    @State non-public var sheetView: SheetView? = nil
    
    var physique: some View {
        VStack {
            Checklist {
                ForEach(listItems.gadgets, id: .self) { merchandise in
                    Button(motion: {
                        showingSheet = true
                        sheetView = SheetView(itemToRemove: merchandise, isPresented: $showingSheet)
                    }) {
                        Textual content(merchandise)
                    }
                    .sheet(isPresented: Binding<Bool>(
//                        get: { (sheetView.itemToRemove == merchandise) && showingSheet },
                        get: { showingSheet },
                        set: { newIsPresented in
                            print("setting isPresented?  (newIsPresented)")
                            showingSheet = newIsPresented
                            sheetView = SheetView(itemToRemove: "no merchandise", isPresented: $showingSheet)
//                            listItems.objectWillChange.ship()
                        }
                    ), onDismiss: {
                        print("dismiss!")
                    }) {
                        sheetView
                    }
                }
                .onDelete(carry out: { indexSet in
                    listItems.delete(at: indexSet)
                })
            }
            Button(motion: {
                print("printin all gadgets")
                print(listItems.gadgets)
            } , label: {
                Textual content("Print gadgets")
            })
        }
    }
}

struct SheetView: View {
    let itemToRemove: String
    
    @StateObject non-public var listItems = StateItems.gadgets
    @Atmosphere(.dismiss) var dismiss
    @Binding var isPresented: Bool
    
    var physique: some View {
        VStack {
            Button(position: .damaging, motion: {
                listItems.delete(merchandise: itemToRemove)
                dismiss()
                isPresented = false
            }, label: {
                Label("delete (itemToRemove)", systemImage: "trash")
                    .padding()
                    .background(.pink)
                    .foregroundColor(.white)
                    .clipShape(Capsule())
                    .body(maxWidth: .infinity)
            })
        }
    }
}

class StateItems {
    static let gadgets = ListItems()
}

class ListItems: ObservableObject {
    @Printed var gadgets = ["Item 1", "Item 2", "item 3 ;-)"]
    
    func delete(merchandise: String) {
        gadgets = gadgets.filter { $0 != merchandise }
        print(gadgets)
    }
    
    func delete(at offsets: IndexSet) {
        offsets.forEach { (i) in
            let itemToDelete = gadgets[i]
            print(itemToDelete)
            delete(merchandise: itemToDelete)
        }
        
    }
    
}

It really works nearly positive besides when the consumer clicks the final merchandise within the record on the very first time, the sheet is frozen. How you can resolve this?

I want answer working each with sheet and fullScreenCover.
Be aware that my gadgets in ListItems can not implement Identifiable interface as a result of it comes from exterior supply (I exploit kotlin multiplatform). In that case, then it needs to be mapped to a different record which is refreshed based mostly on modifications in gadgets property.

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here