Skip to content

Commit 84423df

Browse files
author
Trent Guillory
committed
Rename some variables, clean up examples.
1 parent 0a3a94b commit 84423df

File tree

6 files changed

+63
-39
lines changed

6 files changed

+63
-39
lines changed

SnapToScrollDemo/Sources/Example3/Example3ContentView.swift

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,52 @@
1-
//
2-
// Example3ContentView.swift
3-
// Example3ContentView
4-
//
5-
// Created by Trent Guillory on 9/1/21.
6-
//
7-
81
import SnapToScroll
92
import SwiftUI
103

4+
// MARK: - Example3ContentView
5+
116
struct Example3ContentView: View {
12-
13-
@State private var selectedGettingStartedIndex: Int = 0
14-
7+
8+
// MARK: Internal
9+
1510
var body: some View {
16-
11+
1712
VStack {
1813

1914
Text("Getting Started")
2015
.font(.system(size: 22, weight: .semibold, design: .rounded))
2116
.foregroundColor(.white)
2217
.frame(maxWidth: .infinity, alignment: .leading)
2318
.padding([.top, .leading], 32)
24-
19+
2520
HStackSnap(alignment: .center(32)) {
26-
21+
2722
ForEach(GettingStartedModel.exampleModels) { viewModel in
2823

29-
GettingStartedView(selectedIndex: $selectedGettingStartedIndex, viewModel: viewModel)
24+
GettingStartedView(
25+
selectedIndex: $selectedGettingStartedIndex,
26+
viewModel: viewModel)
3027
.snapAlignmentHelper(id: viewModel.id)
3128
}
3229
} onSwipe: { index in
33-
30+
3431
selectedGettingStartedIndex = index
3532
}
3633
.frame(height: 200)
3734
.padding(.top, 4)
3835
}
3936
.padding([.top, .bottom], 64)
40-
.background(LinearGradient(colors: [Color("Cream"), Color("LightPink")], startPoint: .top, endPoint: .bottom))
37+
.background(LinearGradient(
38+
colors: [Color("Cream"), Color("LightPink")],
39+
startPoint: .top,
40+
endPoint: .bottom))
4141
}
42+
43+
// MARK: Private
44+
45+
@State private var selectedGettingStartedIndex: Int = 0
4246
}
4347

48+
// MARK: - Example3ContentView_Previews
49+
4450
struct Example3ContentView_Previews: PreviewProvider {
4551
static var previews: some View {
4652
Example3ContentView()

SnapToScrollDemo/Sources/Example3/GettingStartedModel.swift

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
import Foundation
22

33
struct GettingStartedModel: Identifiable {
4-
4+
55
static let exampleModels: [GettingStartedModel] = [
6-
.init(id: 0, systemImage: "camera.aperture", title: "Snap a Pic", body: "We feature the viewfinder front and center in this throwback app."),
7-
.init(id: 1, systemImage: "camera.filters", title: "Filter it Up", body: "Add filters - from detailed colorization to film effects."),
8-
.init(id: 2, systemImage: "paperplane", title: "Send It", body: "Share your photos with your contacts. Or the entire world."),
9-
.init(id: 3, systemImage: "sparkles", title: "Be Awesome", body: "You're clearly already doing this. Just wanted to remind you. 😉")
6+
.init(
7+
id: 0,
8+
systemImage: "camera.aperture",
9+
title: "Snap a Pic",
10+
body: "We feature the viewfinder front and center in this throwback app."),
11+
.init(
12+
id: 1,
13+
systemImage: "camera.filters",
14+
title: "Filter it Up",
15+
body: "Add filters - from detailed colorization to film effects."),
16+
.init(
17+
id: 2,
18+
systemImage: "paperplane",
19+
title: "Send It",
20+
body: "Share your photos with your contacts. Or the entire world."),
21+
.init(
22+
id: 3,
23+
systemImage: "sparkles",
24+
title: "Be Awesome",
25+
body: "You're clearly already doing this. Just wanted to remind you. 😉"),
1026
]
11-
27+
1228
let id: Int
1329
let systemImage: String
1430
let title: String
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
import SwiftUI
22

3-
// MARK: - TagView
3+
// MARK: - GettingStartedView
44

55
struct GettingStartedView: View {
6-
6+
77
@Binding var selectedIndex: Int
8-
8+
99
let viewModel: GettingStartedModel
10-
10+
1111
var body: some View {
12-
12+
1313
VStack(alignment: .leading) {
14-
14+
1515
Image(systemName: viewModel.systemImage)
1616
.foregroundColor(isSelected ? Color("LightPink") : .gray)
1717
.font(.system(size: 32))
1818
.padding(.bottom, 2)
19-
19+
2020
Text(viewModel.title)
2121
.fontWeight(.semibold)
2222
.foregroundColor(.black)
2323
.frame(maxWidth: .infinity, alignment: .leading)
2424
.padding(.bottom, 1)
2525
.opacity(0.8)
26-
26+
2727
Text(viewModel.body)
2828
.foregroundColor(.black)
2929
.multilineTextAlignment(.leading)
@@ -34,18 +34,20 @@ struct GettingStartedView: View {
3434
.cornerRadius(12)
3535
.opacity(isSelected ? 1 : 0.8)
3636
}
37-
37+
3838
var isSelected: Bool {
39-
39+
4040
return selectedIndex == viewModel.id
4141
}
4242
}
4343

44-
// MARK: - TagView_Previews
44+
// MARK: - GettingStartedView_Previews
4545

4646
struct GettingStartedView_Previews: PreviewProvider {
4747
static var previews: some View {
48-
49-
GettingStartedView(selectedIndex: .constant(0), viewModel: GettingStartedModel.exampleModels.first!)
48+
49+
GettingStartedView(
50+
selectedIndex: .constant(0),
51+
viewModel: GettingStartedModel.exampleModels.first!)
5052
}
5153
}

Sources/HStackSnap.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public struct HStackSnap<Content: View>: View {
3737
coordinateSpace: coordinateSpace,
3838
content: content,
3939
onSwipe: swipeEventHandler)
40-
.environmentObject(GeometryEnvironment(itemWidth: alignment.shouldSetWidth ? calculatedItemWidth(parentWidth: geometry.size.width, offset: alignment.scrollOffset) : .none))
40+
.environmentObject(SizeOverride(itemWidth: alignment.shouldSetWidth ? calculatedItemWidth(parentWidth: geometry.size.width, offset: alignment.scrollOffset) : .none))
4141
}
4242
}
4343

Sources/Model/GeometryEnvironment.swift renamed to Sources/Model/SizeOverride.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Foundation
22
import SwiftUI
33

4-
class GeometryEnvironment: ObservableObject {
4+
class SizeOverride: ObservableObject {
55

66
init(itemWidth: CGFloat?) {
77

Sources/Views/SnapAlignmentHelper.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import SwiftUI
55

66
struct SnapAlignmentHelper<ID: Hashable>: ViewModifier {
77

8-
@EnvironmentObject var geometryData: GeometryEnvironment
8+
@EnvironmentObject var sizeOverride: SizeOverride
99

1010
var id: ID
1111
var coordinateSpace: String?
1212

1313
func body(content: Content) -> some View {
1414

15-
switch geometryData.itemWidth {
15+
switch sizeOverride.itemWidth {
1616

1717
case let .some(value):
1818

0 commit comments

Comments
 (0)