Code: with this code if i type any word in search and if i check filtered data form selectedTab = 0 to 1, 2… then with eg
for eg.. in student tab i got 6 filtered profileViewStu then if i select Staff tab here if i got 2 filtered profileViewStaff then in staff tab count showing two profiles but that 2 are previous first 2 profileViewStu data showing why?? i need to show profileViewStaff data… where am i wrong? in staff tab why student profile showing? how to fix this.
struct SearchView: View {
@State private var searchKeyword: String = ""
@State private var selectedIndex: Int = 0
@State private var selectedTab: Int = 0
@State private var optionsArray = ["Module", "Student", "Staff"]
@State private var filteredStudents: [Student] = []
@State private var filteredStaff: [Staff] = []
var body: some View {
ZStack {
Color.hexF4F4FC
VStack(spacing: 0) {
headerView()
tabView()
contentView()
}
}
.onAppear {
viewModel.fetchStudentList(scholarType: 2) { status in
filterDataStu()
}
viewModelStaff.fetchSatffList { status in
filterDataStaff()
}
}
}
private func filterDataStu() {
filteredStudents = viewModel.students.filter { student in
student.name?.lowercased().contains(searchKeyword.lowercased()) ?? false
}
}
private func filterDataStaff() {
filteredStaff = viewModelStaff.staffs.filter { staff in
staff.name?.lowercased().contains(searchKeyword.lowercased()) ?? false
}
}
@ViewBuilder func headerView() -> some View {
VStack {
HStack {
VStack {
ZStack {
HStack {
Image(systemName: "magnifyingglass")
.foregroundColor(.gray)
.padding(.leading, 6)
TextField("Search...", text: $searchKeyword)
.onChange(of: searchKeyword) { newValue in
filterDataStu()
filterDataStaff()
}
Spacer()
Button(action: {
searchKeyword = ""
filteredStudents = []
filteredStaff = []
}) {
Image(systemName: "xmark.circle.fill")
}
}
.padding(EdgeInsets(top: 0, leading: 8, bottom: 0, trailing: 8))
}
.padding(.top, 10)
}
.frame(height: 60)
Spacer()
}
.padding(.leading, 10)
}
}
@ViewBuilder func tabView() -> some View {
ScrollViewReader { reader in
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 0) {
LazyHGrid(rows: [GridItem(.flexible(), spacing: 0)]) {
ForEach(0..<optionsArray.count, id: .self) { i in
let data = optionsArray[i]
Button {
withAnimation {
selectedTab = i
filterDataStu()
filterDataStaff()
}
} label: {
VStack {
Text(data)
.foregroundStyle(i == selectedTab ? Color.brandColor : Color.hex1E1D0E)
Spacer()
ZStack {
Divider()
.background(Color(hex: 0xC6C6C8).opacity(0.2))
if selectedTab == i {
Divider()
.background(Color.brandColor)
}
}
}
.frame(height: 30)
.animation(.default, value: selectedTab)
}
.buttonStyle(.plain)
.id(i)
}
}
}
}
.frame(height: 55)
}
}
@ViewBuilder func contentView() -> some View {
ScrollView {
LazyVGrid(columns: [GridItem(.fixed(screenWidth/2), spacing: 0), GridItem(.fixed(screenWidth/2), spacing: 0)], spacing: 0) {
if selectedTab == 1 {
ForEach(filteredStudents.indices, id: .self) { i in
let data = filteredStudents[i]
Button {
} label: {
profileViewStu(student: data)
}
.frame(width: screenWidth/2, height: screenWidth/2)
}
} else if selectedTab == 2 {
ForEach(filteredStaff.indices, id: .self) { i in
let data = filteredStaff[i]
Button {
} label: {
profileViewStaff(staff: data)
}
.frame(width: screenWidth/2, height: screenWidth/2)
}
}
}
}
}
@ViewBuilder func profileViewStu(student: Student) -> some View {
//student profile design
}
@ViewBuilder func profileViewStaff(staff: Staff) -> some View {
//staff profile design
}
}
o/p: in staff of i need staff profile.. same vice versa in staffs to student happens.. where am i wrong??.. plz guide