@@ -24,48 +24,61 @@ struct ContentView: View {
2424 /// Required collection of items confirming to `Pill`
2525 /// which will be used for tracking which objects
2626 /// are selected
27- @State private var selectedColors : [ ColorPill ] = [ ]
27+ @State private var selectedGenres : [ Genre ] = [ ]
2828
2929 var body : some View {
3030 NavigationView {
31- TabView {
32-
33- /// Example view where pills wrap to new line and only occupy
34- /// necessary space
35- ExampleBuilder ( selectedColors: $selectedColors, content: {
36- PillPickerView ( items: colorPills, selectedPills: $selectedColors)
37- . pillStackStyle ( . wrap)
38- } )
39- . tag ( 0 )
40- . navigationTitle ( " Wrapping pills " )
41-
42- /// Example view where pills do not wrap to new line and occupy
43- /// set amount of space horizontally and vertically
44- ExampleBuilder ( selectedColors: $selectedColors, content: {
45- PillPickerView ( items: colorPills, selectedPills: $selectedColors)
46- . pillStackStyle ( . noWrap)
47- } )
48- . tag ( 1 )
49- . navigationTitle ( " Static pills " )
31+ VStack {
32+ TabView {
33+
34+ /// Example view where pills wrap to new line and only occupy
35+ /// necessary space
36+ ExampleBuilder ( selectedItems: $selectedGenres, content: {
37+ PillPickerView ( items: genres, selectedPills: $selectedGenres)
38+ . pillStackStyle ( . wrap)
39+ } )
40+ . tag ( 0 )
41+ . navigationTitle ( " Wrapping pills " )
42+
43+ /// Example view where pills do not wrap to new line and occupy
44+ /// set amount of space horizontally and vertically
45+ ExampleBuilder ( selectedItems: $selectedGenres, content: {
46+ PillPickerView ( items: genres, selectedPills: $selectedGenres)
47+ . pillStackStyle ( . noWrap)
48+ } )
49+ . tag ( 1 )
50+ . navigationTitle ( " Static pills " )
51+ }
52+ . tabViewStyle ( . page)
53+ . tint ( . accentColor)
5054 }
51- . tabViewStyle ( . page)
52- . tint ( . accentColor)
55+ . toolbar ( content: {
56+ ToolbarItem ( placement: . navigationBarTrailing, content: {
57+ Button ( action: {
58+ withAnimation {
59+ selectedGenres. removeAll ( )
60+ }
61+ } , label: {
62+ Text ( " Clear All " )
63+ } )
64+ } )
65+ } )
5366 }
5467 }
5568}
5669
57- struct ExampleBuilder < V : View > : View {
70+ struct ExampleBuilder < T , V > : View where T : Pill , V : View {
5871
5972 typealias ContentGenerator = ( ) -> V
6073
61- @Binding var selectedColors : [ ColorPill ]
74+ @Binding var selectedItems : [ T ]
6275
6376 var content : ContentGenerator
6477
6578 var body : some View {
6679 ScrollView ( showsIndicators: false ) {
6780 HStack {
68- Text ( " Select Your Favorite Colors " )
81+ Text ( " Select Your Favorite Genres " )
6982 . font ( . system( size: 26 , weight: . semibold, design: . rounded) )
7083 Spacer ( )
7184 }
@@ -74,17 +87,26 @@ struct ExampleBuilder<V : View>: View {
7487 /// PillPickerView usage example
7588 content ( )
7689
77- Text ( " Selected Colors : " )
90+ Text ( " Selected Genres : " )
7891 . font ( . system( size: 20 , weight: . semibold, design: . rounded) )
7992 . padding ( . top, 30 )
8093
81- HStack ( spacing: 10 ) {
82- ForEach ( selectedColors, id: \. self) { colorPill in
83- RoundedRectangle ( cornerRadius: 40 )
84- . foregroundColor ( colorPill. color)
85- . frame ( width: 40 , height: 40 )
94+ VStack ( spacing: 10 ) {
95+ ForEach ( selectedItems, id: \. self) { item in
96+ HStack {
97+ Text ( item. title)
98+ . font ( . system( size: 16 , weight: . semibold, design: . rounded) )
99+ . foregroundColor ( . white)
100+ }
101+ . padding ( )
102+ . frame ( maxWidth: . infinity)
103+ . background (
104+ RoundedRectangle ( cornerRadius: 20 )
105+ . foregroundColor ( . accentColor)
106+ )
86107 }
87108 }
109+ . padding ( . bottom, 60 )
88110
89111 Spacer ( )
90112 }
@@ -94,18 +116,30 @@ struct ExampleBuilder<V : View>: View {
94116
95117/// Sample model conforming to the `Pill` protocol.
96118/// An element must have a `title` attribute.
97- struct ColorPill : Pill {
119+ struct Genre : Pill {
98120 let title : String
99- let color : Color
100121}
101122
102123/// Collection of items conforming to `Pill`
103- let colorPills : [ ColorPill ] = [
104- ColorPill ( title: " Red " , color: . red) ,
105- ColorPill ( title: " Green " , color: . green) ,
106- ColorPill ( title: " Blue " , color: . blue) ,
107- ColorPill ( title: " Yellow " , color: . yellow) ,
108- ColorPill ( title: " Orange " , color: . orange) ,
109- ColorPill ( title: " Purple " , color: . purple) ,
110- ColorPill ( title: " Pink " , color: . pink) ,
124+ let genres : [ Genre ] = [
125+ Genre ( title: " Action " ) ,
126+ Genre ( title: " Adventure " ) ,
127+ Genre ( title: " Comedy " ) ,
128+ Genre ( title: " Drama " ) ,
129+ Genre ( title: " Fantasy " ) ,
130+ Genre ( title: " Horror " ) ,
131+ Genre ( title: " Mystery " ) ,
132+ Genre ( title: " Romance " ) ,
133+ Genre ( title: " Sci-Fi " ) ,
134+ Genre ( title: " Thriller " ) ,
135+ Genre ( title: " Western " ) ,
136+ Genre ( title: " Animation " ) ,
137+ Genre ( title: " Documentary " ) ,
138+ Genre ( title: " Historical " ) ,
139+ Genre ( title: " Musical " ) ,
140+ Genre ( title: " War " ) ,
141+ Genre ( title: " Crime " ) ,
142+ Genre ( title: " Family " ) ,
143+ Genre ( title: " Sports " ) ,
144+ Genre ( title: " Biography " )
111145]
0 commit comments