TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

How to update item value after editing?

Forums > SwiftUI

When I edit DetailView in EditingView new values look good between each other, but when I back to my ListView, everything without new values. In consol, I see that elements in the array aren't changed. Help me please.

struct ClientsListView: View {
    @EnvironmentObject var clientsModel: ClientViewModel

    var body: some View {
        VStack(alignment: .leading){
            ForEach(clientsModel.clientsModel){ client in
                NavigationLink(
                    destination: ClientDetailHost(client: client),
                    label: {
                        ClientRowView(name: client.name, address: client.address, email: client.email)
                    })
                Divider()

            }
        }.padding()
    }
}

struct ClientsListView_Previews: PreviewProvider {
    static var previews: some View {
        ClientsListView()
            .environmentObject(ClientViewModel())
    }
}
struct ClientDetailEditing: View {
    @Binding var client: Client
    @Environment(\.editMode) var editMode

    var body: some View {
        VStack{
            AddClientRow(itemName: "Name", iconName: "person", keyboardType: .namePhonePad ,bindingItem: $client.name)
            AddClientRow(itemName: "Email", iconName: "envelope", keyboardType: .emailAddress ,bindingItem: $client.email)
            AddClientRow(itemName: "Mobile", iconName: "phone.fill", keyboardType: .phonePad ,bindingItem: $client.mobile)
            AddClientRow(itemName: "Phone", iconName: "phone", keyboardType: .phonePad ,bindingItem: $client.phone)
            AddClientRow(itemName: "Address", iconName: "map", keyboardType: .default ,bindingItem: $client.address)
            AddClientRow(itemName: "Shipping Address", iconName: "shippingbox", keyboardType: .default ,bindingItem: $client.billingAddress)

        }
    }
}
struct ClientDetailHost: View {
    @Environment(\.editMode) var editMode
    @Environment(\.presentationMode) var presentationMode
    @EnvironmentObject var clientsModel: ClientViewModel
    @State private var draftClient = Client.default
    @State var client: Client
    @State var isBackHidden: Bool = false

    var body: some View {
        VStack{
            if editMode?.wrappedValue == .inactive {
                ClientDetailView(client: client)

            } else {
                ClientDetailEditing(client: $draftClient)
                    .onAppear {
                        draftClient = client
                        isBackHidden.toggle()
                        print("onAppear")
                        print(client)
                    }
                    .onDisappear {
                        client = draftClient
                        isBackHidden.toggle()
                        print("onDisappear")
                        print(client)
                    }
            }

            if editMode?.wrappedValue == .active {
                Button("Cancel") {
                draftClient = clientsModel.client
                editMode?.animation().wrappedValue = .inactive}
                Spacer()
                }

            }
        .navigationBarBackButtonHidden(isBackHidden)
        .navigationBarItems(leading: Text(""), trailing: EditButton())
    }
}
struct ClientDetailView: View {
    @EnvironmentObject var clientsModel: ClientViewModel
    let client: Client

    @State var clientName: String = ""
    @State var clientEmail: String = ""
    @State var clientPhone: String = ""
    @State var clientMobile: String = ""
    @State var clientAddress: String = """
                                        """
    @State var clientBillingAddress: String = """
                                                """
    @State var isSave: Bool = false
    @Environment(\.presentationMode) var presentationMode

    var body: some View {
        ScrollView{
            VStack{
                Text("\(client.name)")
                    .font(/*@START_MENU_TOKEN@*/.title/*@END_MENU_TOKEN@*/)
                    .bold()
                    .padding(.vertical)
                HStack(alignment: .center){
                    ActionItem(imageName: "message", text: "message")
                    ActionItem(imageName: "phone", text: "call")
                    ActionItem(imageName: "envelope", text: "mail")
                    ActionItem(imageName: "square.and.pencil", text: "edit")
                }
                Divider()

                VStack(alignment: .leading){
                ClientInfoItem(itemname: "mobile", itemvalue: "\(client.mobile)")
                ClientInfoItem(itemname: "email", itemvalue: "\(client.email)")
                ClientInfoItem(itemname: "phone", itemvalue: "\(client.phone)")
                ClientInfoAddress(itemname: "address", itemvalue: "\(client.address)")
                ClientInfoAddress(itemname: "billing address", itemvalue: "\(client.billingAddress)")
                }

            }.padding(.horizontal)
        }
        .navigationBarBackButtonHidden(false)
        .navigationBarTitleDisplayMode(.inline)

    }
}

struct ActionItem: View {
    var imageName: String
    var text: String
    var body: some View {
        VStack{
            ZStack{
            RoundedRectangle(cornerRadius: 10)
                .frame(width: 70, height: 70)
                .foregroundColor(Color(.systemGray6))
                VStack{
            Image(systemName: imageName)
                .font(.title)
                .foregroundColor(/*@START_MENU_TOKEN@*/.blue/*@END_MENU_TOKEN@*/)
                //.padding(.vertical,2)
            Text(text)
                .font(.caption)

                }
            }.padding(.all, 7)
        }

    }
}

struct ClientInfoItem: View {
    var itemname: String
    var itemvalue: String

    var body: some View{
        ZStack(alignment: .leading){
        RoundedRectangle(cornerRadius: 10)
            .frame(height: 70)
            .foregroundColor(Color(.systemGray6))
        VStack(alignment: .leading){
            Text(itemname)
            Spacer()
            Text(itemvalue)
                .foregroundColor(.blue)
        }.padding()
        }
        .font(.title3)
    }
}

struct ClientInfoAddress: View {
    var itemname: String
    var itemvalue: String

    var body: some View{
//        ZStack(alignment: .leading){
//            RoundedRectangle(cornerRadius: 10)
//                .foregroundColor(Color(.systemGray6))
        VStack(alignment: .leading){
            Text(itemname)
                .padding(.vertical, 3)
            Text(itemvalue)
                .font(.callout)
                .foregroundColor(.blue)
                .bold()
        }
        .font(.title3)
        .padding(.vertical, 2)
    }
}

3      

hi,

i don't see the complete description of the model, and i cannot see whether a client is a struct or a class.

but my guess:

  • your ClientsListView is observing its clientsModel for changes.
  • when you commit changes to a client in the ClientDetailHost view via the .onDisappear() modifier, you are only resetting the local client variable to draftClient.
  • i don't see how that makes a change to the client model itself, and so the ClientsListView is not going to be updated.

you should probably implement a method in your model to update an exisiting client's data from a modified draftClient, in such a way that the change causes the model to send out a change message on what i'm guessing is a @Published var clientsModel: [Client] variable in the model?

hope that's a start,

DMG

3      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.