I have two APIs one is for showing list 2nd is for showing search word list API but both Model is same
code: here if i search word then list is showing correctly but if i tap on one of the cell then it navigates to SentAndInboxChatView but here it sends selectedDataID
nil to the next screen why? how to get selectedDataID while searching list. please guide me
struct MessageListView: View {
@StateObject private var viewModel = AllMessagesListViewModel()
@State private var showingSearchField = false
@State private var searchText = ""
@State private var gotoChat: Bool = false
@State private var selectedDataID: String?
@State var id: String = ""
var body: some View {
ZStack {
Color.hexF4F4FC
.ignoresSafeArea()
VStack(spacing: 0) {
List(viewModel.allMesgs, id: .id) { msg in
Button {
selectedDataID = msg.id
print("selectedDataID... (selectedDataID)")
gotoChat = true
} label: {
messageView(user: msg)
}
.listRowSeparator(.hidden)
.listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
}
.navigationDestination(isPresented: $gotoChat) {
SentAndInboxChatView(idInbox: selectedDataID ?? "", messageType: MessageType.Inbox.rawValue)
.toolbar(.hidden, for: .navigationBar)
}
.listStyle(.plain)
}
.onAppear {
viewModel.fetchAllMessages(id: id) { status in
}
}
Spacer()
}
}
private var searchFieldView: some View {
ZStack {
Rectangle()
.fill(Color.white)
.cornerRadius(10)
HStack {
Image(systemName: "magnifyingglass").foregroundColor(.gray)
.padding(.leading, 6)
TextField("Search", text: $searchText)
.padding(.horizontal, 10)
.padding(.vertical, 8)
.onChange(of: searchText) { newValue in
if newValue.count >= 3 {
viewModel.fetchAllMessagesSearch(id: id, Query: newValue, showHud: false) { status in
}
print("selectedDataID.... (selectedDataID) ")
}
else{
viewModel.fetchAllMessages(id: id, showHud: false) { status in
}
}
}
Spacer()
}
.padding(EdgeInsets(top: 0, leading: 8, bottom: 0, trailing: 8))
}
}
}
o/p
selectedDataID…. nil