Im trying to put checkmark when menu item selected.I tried give a @State var selectedSort : Int = .zero and give id(selectedSort) but It didn't work. How can I solve this problem ?
This is my code;
@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()
}
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Im displaying url images in my app with WebImage but some of them has non English characters in the path so they are not displaying in view.How can I handle non English characters in Url ımage ?
language
LazyVGrid(columns: Array(repeating: GridItem(.flexible(),spacing: 20), count: data.column ?? 1), spacing:30 ) {
ForEach(data.options ?? [] , id :\.id ){ item in
NavigationLink(destination: ViewParser.create(item: item,index: item.typeArry ?? [])) {
VStack(alignment: .leading, spacing: 15) {
WebImage(url: URL(string: item.image ?? ""))
.resizable()
.aspectRatio(CGFloat(data.ratio ?? 1),contentMode: .fit)
.cornerRadius(isColumnOne ? 12 : 0)
.padding(.horizontal , 1)
}
}
}
}
Im trying to get sort id which is come from my server . when user select sort in menu I should have selected id . But somehow when I selected sort its not triggered.Where do I make mistake ? when I change to selection to UUID then I can get uuıd for every selection but I should get their id which they are string that's why selection is string.
`
@State private var selection = ""
@Binding var sortClicked : Bool
@ObservedObject var productListViewModel = ProductListViewModel()
@State var sortListArray : [ProductListSortAndFilterList]
var body : some View {
HStack(alignment: .center){
Spacer()
Picker(selection: $selection, label: SortView().frame(height:UIScreen.main.bounds.height * 0.050), content: {
ForEach(sortListArray,id:\.id) { item in
if item.id == "sort" {
ForEach(item.sortList ?? [],id:\.id) { data in
Text(data.name ?? "")
.tag(data.id)
}
.onReceive(Just(selection)) { (value) in
print(value)
}
}
}
})
.pickerStyle(MenuPickerStyle())
.padding()
.background(Color(.systemBackground).edgesIgnoringSafeArea(.all))
.cornerRadius(5)
```
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 ;
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
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.
@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()
}