import SwiftUI private extension Binding where Value == SortCriteria { func value() -> Binding { return Binding(get:{ self.wrappedValue.rawValue }, set: { self.wrappedValue = .init(rawValue: $0)!}) } } struct SortSelectionView: View { let hasMatches: Bool @Binding var isPresented: Bool @Binding var sortType: SortCriteria @Binding var sortAscending: Bool @Binding var showGridView: Bool var body: some View { VStack(alignment: .leading, spacing: 3) { HStack { Text("List Settings").font(.title2).bold() Spacer() Button(action: { isPresented = false }) { Image(systemSymbol: .xmarkCircleFill) .foregroundColor(.gray) .font(.system(size: 26)) } } .padding(.bottom) Text("Sort by") .font(.footnote) .textCase(.uppercase) .foregroundColor(.secondary) Picker("Sort type", selection: $sortType.value()) { Text(SortCriteria.id.text).tag(SortCriteria.id.rawValue) Text(SortCriteria.name.text).tag(SortCriteria.name.rawValue) Text(SortCriteria.count.text).tag(SortCriteria.count.rawValue) if hasMatches { Text(SortCriteria.match.text).tag(SortCriteria.match.rawValue) } } .pickerStyle(SegmentedPickerStyle()) .padding(.bottom) Text("Sort order") .font(.footnote) .textCase(.uppercase) .foregroundColor(.secondary) Picker("Sort order", selection: $sortAscending) { Text("Ascending").tag(true) Text("Descending").tag(false) } .pickerStyle(SegmentedPickerStyle()) .padding(.bottom) HStack { Spacer() Button(action: showGrid) { HStack { Image(systemSymbol: .circleHexagongrid) Text("Show grid") } }.padding() Spacer() } Spacer() } .padding(.horizontal) } private func showGrid() { showGridView = true isPresented = false } } struct SortSelectionView_Previews: PreviewProvider { static var previews: some View { SortSelectionView( hasMatches: true, isPresented: .constant(true), sortType: .constant(.id), sortAscending: .constant(false), showGridView: .constant(false)) .previewLayout(.fixed(width: 375, height: 250)) } }