damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

CustomPicker.swift (1642B)


      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>: View {
     16     
     17     let tabs: [(String, SelectionValue)]
     18     @Environment(\.colorScheme) var colorScheme
     19 
     20     @Namespace var picker
     21     @Binding var selection: SelectionValue
     22 
     23     public var body: some View {
     24         HStack {
     25             ForEach(tabs, id: \.1) { (text, tag) in
     26                 Button {
     27                     withAnimation(.spring()) {
     28                         selection = tag
     29                     }
     30                 } label: {
     31                     Text(text).padding(EdgeInsets(top: 15, leading: 0, bottom: 10, trailing: 0))
     32                         .font(.system(size: 14, weight: .heavy))
     33                         .tag(tag)
     34                 }
     35                 .background(
     36                     Group {
     37                         if tag == selection {
     38                             Rectangle().fill(RECTANGLE_GRADIENT).frame(height: 2.5)
     39                                 .matchedGeometryEffect(id: "selector", in: picker)
     40                                 .cornerRadius(2.5)
     41                         }
     42                     },
     43                     alignment: .bottom
     44                 )
     45                 .frame(maxWidth: .infinity)
     46                 .accentColor(tag == selection ? textColor() : .gray)
     47             }
     48         }
     49     }
     50     
     51     func textColor() -> Color {
     52         colorScheme == .light ? DamusColors.black : DamusColors.white
     53     }
     54 }