Im trying to update my product list in UI when I select different sort type in Menu selection.When i select new sort type im sending sort id to server and server should give me new product list and with that list my UI should update.But somehow my UI is not updating I don't know why.
This is my viewModel ;
This is the function im making request in view when onAppear
This is the where I passing sort id when Menu clicked.
This is my viewModel ;
Code Block class ProductListViewModel : ObservableObject { var productList = PassthroughSubject<ProductListResponse,Never>() var cancellable = Set<AnyCancellable>() var storeResponse = InfoResponse() var currentPage = 1 var perPage = 12 func getProductListResponse(typeId : String , type : String, sortId : String) { let tsoftFilter = """ [{"key" : "\(type)", "value" : "\(typeId)"}] """ print("tsoft filter is \(tsoftFilter)") print("sort id is \(sortId)") let serviceParams = [ "store_id" : Config.productListId, "page" : currentPage, "per_page" : perPage, "tsoft_filters" : "\(tsoftFilter)", "sort" : Int(sortId) ] as [String : AnyObject] getProductList(params: serviceParams).sink { (error) in print("ERROR İS -> \(error)") } receiveValue: { (ProductListResponse) in self.productList.send(ProductListResponse) }.store(in: &cancellable) } }
This is the function im making request in view when onAppear
Code Block l func getProductList() { DispatchQueue.main.async { productViewModel.productList.sink { (ProductListResponse) in isSelected = false isAnimating = false productViewModel.currentPage += 1 productListArray.append(contentsOf: ProductListResponse.data?.data ?? []) sortListArray.append(contentsOf: ProductListResponse.data?.extra ?? []) }.store(in: &cancellable) } productViewModel.getProductListResponse(typeId: typeId, type: type, sortId: sortId) }
This is the where I passing sort id when Menu clicked.
Code Block l @State private var selection = 0 @Binding var sortClicked : Bool @ObservedObject var productListViewModel = ProductListViewModel() @State var sortListArray : [ProductListSortAndFilterList] var function: () -> Void @Binding var sortId : String var body : some View { HStack(alignment: .center){ Spacer() Menu { ForEach(sortListArray,id:\.id){ item in if item.id == "sort" { ForEach(item.sortList ?? [],id:\.id) { data in Button(action: { sortId = (data.id ?? "") self.function() print("selected item is : \(data.id!)") }) { Text(data.name ?? "") .tag(data.id) } } } } } label: { SortView() }