ios – Utilizing Sheet to edit and take away objects from listing in SwiftUI

Spread the love


I’ve a requirement that when the consumer clicks component of the listing, a sheet ought to open with data 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 very best code I found out:

import SwiftUI


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

#Preview {
    MyTest1()
}

struct ListView: View {
    @StateObject non-public var listItems = StateItems.objects
    @State non-public var showingSheet = false
    @State non-public var sheetView: SheetView? = nil
    
    var physique: some View {
        VStack {
            Listing {
                ForEach(listItems.objects, 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 objects")
                print(listItems.objects)
            } , label: {
                Textual content("Print objects")
            })
        }
    }
}

struct SheetView: View {
    let itemToRemove: String
    
    @StateObject non-public var listItems = StateItems.objects
    @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(.crimson)
                    .foregroundColor(.white)
                    .clipShape(Capsule())
                    .body(maxWidth: .infinity)
            })
        }
    }
}

class StateItems {
    static let objects = ListItems()
}

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

It really works nearly effective besides when the consumer clicks the final merchandise within the listing on the very first time, the sheet is frozen. Tips on how to resolve this?

I would like resolution working each with sheet and fullScreenCover.
Word that my objects in ListItems can not implement Identifiable interface as a result of it comes from exterior supply (I take advantage of kotlin multiplatform). If that’s the case, then it needs to be mapped to a different listing which is refreshed based mostly on adjustments in objects property.

Leave a Reply

Your email address will not be published. Required fields are marked *