swift 根据切换是否处于活动状态,使用两个参数过滤列表

eqqqjvef  于 2023-02-03  发布在  Swift
关注(0)|答案(1)|浏览(128)

我想用两个参数“性别”和“部门”来过滤“雇员”列表。但是只有在相应的切换被激活的情况下。我尝试在两个枚举中使用“all”大小写,但是很明显,如果使用“all”大小写,列表中就没有成员了。如果一个切换被停用,以返回相应枚举的所有大小写,最好的解决方案是什么?

import SwiftUI

enum Gender: String, CaseIterable, Identifiable {
    var id: Self { self }
    
    case male = "male"
    case female = "female"
    case other = "other"
    
    case all = "all"
}

enum Department: String, CaseIterable, Identifiable {
    var id: Self { self }

    case engineering = "Engineer"
    case software = "Software"
    case sales = "Sales"
    case hr = "Human Ressources"
    case export = "Export"
    
    case all = "All"
    
}

struct Employe: Identifiable {
    
    let sex: Gender
    let department: Department
    let name: String
    
    var id: String { name }
}

struct ContentView: View {
    
    @State private var filterGender: Gender = .all
    @State private var filterDepartment: Department = .all
    
    @State private var activeGenderFilter: Bool = false
    @State private var activeDepartmentFilter: Bool = false
    
    let employees: [Employe] = [
        .init(sex: .female, department: .engineering, name: "Sarah"),
        .init(sex: .female, department: .engineering, name: "Anna"),
        .init(sex: .male, department: .export, name: "Tom"),
        .init(sex: .male, department: .sales, name: "Stephen"),
        .init(sex: .other, department: .software, name: "Alexis"),
        .init(sex: .female, department: .hr, name: "Julia"),
        .init(sex: .female, department: .sales, name: "Alina"),
        .init(sex: .male, department: .software, name: "Marie"),
        .init(sex: .other, department: .sales, name: "Sam"),
        .init(sex: .male, department: .export, name: "Ed")
    ]
    
    var body: some View {
        NavigationStack {
            
            toggleBar
            
            VStack {
                
                filterBar
                
                List {
                    ForEach((activeGenderFilter || activeDepartmentFilter) ? employees.filter { matchFilter(employe: $0, gender: filterGender, departmend: filterDepartment) }  : employees) { employe in
                        HStack {
                            Text(employe.name)
                            Text("(\(employe.sex.rawValue))")
                            Spacer()
                            Text(employe.department.rawValue)
                            
                        }
                    }
                }
            }
            .navigationTitle("Employees")
        }
    }
    
    func matchFilter(employe: Employe, gender: Gender, departmend: Department) -> Bool {
        return employe.sex == gender && employe.department == departmend
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

extension ContentView {
    private var toggleBar: some View  {
        VStack {
            
            Toggle("Gender", isOn: $activeGenderFilter.animation(.easeInOut))
            Toggle("Department", isOn: $activeDepartmentFilter.animation(.easeInOut))
        }
        .padding()
    }
    
    private var filterBar: some View {
        
        HStack {
            if activeGenderFilter {
                Picker("Gender", selection: $filterGender) {
                    ForEach(Gender.allCases) { gender in
                        Text(gender.rawValue)
                    }
                }
                .pickerStyle(.menu)
                .padding()
            }
            
            Spacer()

            
            if activeDepartmentFilter {
                Picker("Department", selection: $filterDepartment) {
                    ForEach(Department.allCases) { department in
                        Text(department.rawValue)
                    }
                }
                .padding()
                .pickerStyle(.menu)
            }
        }
    }
}
mo49yndu

mo49yndu1#

您需要在匹配逻辑中包括切换属性和.all大小写

func matchFilter(employe: Employe, gender: Gender, departmend: Department) -> Bool {
    return (activeGenderFilter == false || gender == .all || employe.sex == gender) &&
        (activeDepartmentFilter == false || departmend == .all || employe.department == departmend)
}

相关问题