Skip to content

Commit 4a2a817

Browse files
New options, haptic feedback and more
1 parent ba1915c commit 4a2a817

File tree

3 files changed

+69
-37
lines changed

3 files changed

+69
-37
lines changed

Sources/SFSymbolsPicker/SFSymbolsPicker.swift

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,40 @@
66
//
77

88
import SwiftUI
9-
9+
import Foundation
1010

1111
public struct SFSymbolsPicker: View {
1212

13+
// MARK: - View properties
14+
1315
@Binding public var isPresented: Bool
1416
@Binding public var icon: String
1517
let category: Category
18+
let axis: Axis.Set
19+
let haptic: Bool
1620

17-
public init(isPresented: Binding<Bool>, icon: Binding<String>, category: Category) {
21+
/// Show a picker to select SF Symbols
22+
/// - Parameters:
23+
/// - isPresented: A binding to a Boolean value that determines whether to present the sheet that you create in the modifier's
24+
/// - icon: A binding to a String value that determines which icon has been selected
25+
/// - category: Custom enum Category that determines which type of icons should be displayed
26+
/// - axis: ScrollView axis (vertical by default)
27+
/// - haptic: If true a small haptic feedback is generated when an icon is selected (true by default)
28+
public init(isPresented: Binding<Bool>, icon: Binding<String>, category: Category, axis: Axis.Set = .horizontal, haptic: Bool = true) {
1829
self._isPresented = isPresented
1930
self._icon = icon
2031
self.category = category
32+
self.axis = axis
33+
self.haptic = haptic
2134
}
35+
36+
37+
// MARK: - View body
2238

2339
public var body: some View {
40+
2441
if isPresented {
25-
ScrollView(.vertical) {
42+
ScrollView(self.axis) {
2643

2744
LazyVGrid(columns: [GridItem(.adaptive(minimum: 70))], spacing: 20) {
2845

@@ -31,9 +48,18 @@ public struct SFSymbolsPicker: View {
3148
Image(systemName: icon)
3249
.font(.system(size: 25))
3350
.animation(.linear)
34-
51+
.foregroundColor(self.icon == icon ? Color.blue : Color.primary)
3552
.onTapGesture {
36-
self.icon = icon
53+
54+
// Assign binding value
55+
withAnimation {
56+
self.icon = icon
57+
}
58+
59+
// Generate haptic
60+
if self.haptic {
61+
self.impactFeedback(style: .medium)
62+
}
3763
}
3864

3965
}.padding(.top, 5)
@@ -42,41 +68,19 @@ public struct SFSymbolsPicker: View {
4268
}
4369

4470
}
45-
}
46-
47-
public enum Category: String, CaseIterable, Identifiable {
48-
public var id: String { rawValue }
4971

50-
case communication = "Communication"
51-
case weather = "Weather"
52-
case objects = "Objects"
53-
case devices = "Devices"
54-
case games = "Games"
55-
case connectivity = "Connectivity"
56-
case transport = "Transport"
57-
case people = "People"
58-
case nature = "Nature"
59-
case edit = "Edit"
60-
case text = "Text"
61-
case multimedia = "Multimedia"
62-
case keyboard = "Keyboard"
63-
case commerce = "Commerce"
64-
case time = "Time"
65-
case health = "Health"
66-
case forms = "Forms"
67-
case arrows = "Arrows"
68-
case indices = "Indices"
69-
case math = "Math"
70-
71-
case none = ""
72+
private func impactFeedback(style: UIImpactFeedbackGenerator.FeedbackStyle) {
73+
let generator = UIImpactFeedbackGenerator(style: style)
74+
generator.prepare()
75+
generator.impactOccurred()
76+
}
7277
}
7378

7479

75-
#if DEBUG
80+
81+
7682
struct SFSymbolsPicker_Previews: PreviewProvider {
77-
7883
static var previews: some View {
79-
SFSymbolsPicker(isPresented: .constant(false), icon: .constant(""), category: .commerce)
84+
SFSymbolsPicker(isPresented: .constant(false), icon: .constant(""), category: .commerce, axis: .horizontal)
8085
}
8186
}
82-
#endif

Sources/SFSymbolsPicker/Symbols.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,33 @@
77

88
import Foundation
99

10+
public enum Category: String, CaseIterable, Identifiable {
11+
public var id: String { rawValue }
12+
13+
case communication = "Communication"
14+
case weather = "Weather"
15+
case objects = "Objects"
16+
case devices = "Devices"
17+
case games = "Games"
18+
case connectivity = "Connectivity"
19+
case transport = "Transport"
20+
case people = "People"
21+
case nature = "Nature"
22+
case edit = "Edit"
23+
case text = "Text"
24+
case multimedia = "Multimedia"
25+
case keyboard = "Keyboard"
26+
case commerce = "Commerce"
27+
case time = "Time"
28+
case health = "Health"
29+
case forms = "Forms"
30+
case arrows = "Arrows"
31+
case indices = "Indices"
32+
case math = "Math"
33+
34+
case none = ""
35+
}
36+
1037
let symbols : [String: [String]] = [
1138
"Communication": ["mic","mic.fill","mic.circle","mic.circle.fill","mic.slash","mic.slash.fill","bubble.right","bubble.right.fill","bubble.left","bubble.left.fill","exclamationmark.bubble","exclamationmark.bubble.fill","quote.bubble","quote.bubble.fill","t.bubble","t.bubble.fill","text.bubble","text.bubble.fill","captions.bubble","captions.bubble.fill","plus.bubble","plus.bubble.fill","rectangle.3.offgrid.bubble.left","rectangle.3.offgrid.bubble.left.fill","ellipsis.bubble","ellipsis.bubble.fill","phone.bubble.left","phone.bubble.left.fill","bubble.middle.bottom","bubble.middle.bottom.fill","bubble.middle.top","bubble.middle.top.fill","bubble.left.and.bubble.right","bubble.left.and.bubble.right.fill","phone","phone.fill","phone.circle","phone.circle.fill","phone.badge.plus","phone.fill.badge.plus","phone.connection","phone.fill.connection","phone.arrow.up.right","phone.fill.arrow.up.right","phone.arrow.down.left","phone.fill.arrow.down.left","phone.arrow.right","phone.fill.arrow.right","phone.down","phone.down.fill","phone.down.circle","phone.down.circle.fill","envelope","envelope.fill","envelope.circle","envelope.circle.fill","envelope.arrow.triangle.branch","envelope.arrow.triangle.branch.fill","envelope.open","envelope.open.fill","envelope.badge","envelope.badge.fill","waveform","waveform.circle","waveform.circle.fill"],
1239
"Weather": ["sun.min","sun.min.fill","sun.max","sun.max.fill","sunrise","sunrise.fill","sunset","sunset.fill","sun.dust","sun.dust.fill","sun.haze","sun.haze.fill","moon","moon.fill","moon.circle","moon.circle.fill","sparkles","moon.stars","moon.stars.fill","cloud","cloud.fill","cloud.drizzle","cloud.drizzle.fill","cloud.rain","cloud.rain.fill","cloud.heavyrain","cloud.heavyrain.fill","cloud.fog","cloud.fog.fill","cloud.hail","cloud.hail.fill","cloud.snow","cloud.snow.fill","cloud.sleet","cloud.sleet.fill","cloud.bolt","cloud.bolt.fill","cloud.bolt.rain","cloud.bolt.rain.fill","cloud.sun","cloud.sun.fill","cloud.sun.rain","cloud.sun.rain.fill","cloud.sun.bolt","cloud.sun.bolt.fill","cloud.moon","cloud.moon.fill","cloud.moon.rain","cloud.moon.rain.fill","cloud.moon.bolt","cloud.moon.bolt.fill","smoke","smoke.fill","wind","wind.snow","snow","tornado","tropicalstorm","hurricane","thermometer.sun","thermometer.sun.fill","thermometer.snowflake","thermometer","aqi.low","aqi.medium","aqi.high"],

Sources/SFSymbolsPicker/UsageExample.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ struct UsageExample: View {
3030
}
3131
})
3232

33-
SFSymbolsPicker(isPresented: $isPresented, icon: $icon, category: .games)
34-
33+
SFSymbolsPicker(isPresented: $isPresented, icon: $icon, category: .games, axis: .vertical, haptic: true)
34+
3535
}
3636
.navigationTitle("SFSymbolsPicker")
37+
3738
}
3839
}
3940
}

0 commit comments

Comments
 (0)