CustomPicker.swift (2166B)
1 // 2 // CustomPicker.swift 3 // damus 4 // 5 // Created by Eric Holguin on 1/22/23. 6 // 7 8 import SwiftUI 9 10 let RECTANGLE_GRADIENT = LinearGradient(gradient: Gradient(colors: [ 11 DamusColors.purple, 12 DamusColors.blue 13 ]), startPoint: .leading, endPoint: .trailing) 14 15 struct CustomPicker<SelectionValue: Hashable, Content: View>: View { 16 17 @Environment(\.colorScheme) var colorScheme 18 19 @Namespace var picker 20 @Binding var selection: SelectionValue 21 @ViewBuilder let content: Content 22 23 public var body: some View { 24 let contentMirror = Mirror(reflecting: content) 25 let blocksCount = Mirror(reflecting: contentMirror.descendant("value")!).children.count 26 HStack { 27 ForEach(0..<blocksCount, id: \.self) { index in 28 let tupleBlock = contentMirror.descendant("value", ".\(index)") 29 let text = Mirror(reflecting: tupleBlock!).descendant("content") as! Text 30 let tag = Mirror(reflecting: tupleBlock!).descendant("modifier", "value", "tagged") as! SelectionValue 31 32 Button { 33 withAnimation(.spring()) { 34 selection = tag 35 } 36 } label: { 37 text 38 .padding(EdgeInsets(top: 15, leading: 0, bottom: 10, trailing: 0)) 39 .font(.system(size: 14, weight: .heavy)) 40 } 41 .background( 42 Group { 43 if tag == selection { 44 Rectangle().fill(RECTANGLE_GRADIENT).frame(height: 2.5) 45 .matchedGeometryEffect(id: "selector", in: picker) 46 .cornerRadius(2.5) 47 } 48 }, 49 alignment: .bottom 50 ) 51 .frame(maxWidth: .infinity) 52 .accentColor(tag == selection ? textColor() : .gray) 53 } 54 } 55 .background(Color(UIColor.systemBackground)) 56 } 57 58 func textColor() -> Color { 59 colorScheme == .light ? DamusColors.black : DamusColors.white 60 } 61 }