Skip to content

Commit e2e2147

Browse files
WIP: Replace ShippingRulesTableViewController with ShippingLocationsView (SwiftUI)
1 parent efb720c commit e2e2147

File tree

3 files changed

+132
-9
lines changed

3 files changed

+132
-9
lines changed

Kickstarter-iOS/Features/PledgeShippingLocation/PledgeShippingLocationViewController.swift

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,28 @@ final class PledgeShippingLocationViewController: UIViewController {
169169
// MARK: - Functions
170170

171171
private func presentShippingRules(
172-
_ project: Project, shippingRules: [ShippingRule], selectedShippingRule: ShippingRule
172+
_: Project, shippingRules: [ShippingRule], selectedShippingRule: ShippingRule
173173
) {
174-
let viewController = ShippingRulesTableViewController.instantiate()
175-
viewController.configureWith(
176-
project, shippingRules: shippingRules,
177-
selectedShippingRule: selectedShippingRule
178-
)
179-
viewController.delegate = self
174+
let viewController = ShippingLocationsViewController(
175+
withLocations: shippingRules.map { $0.location },
176+
selectedLocation: selectedShippingRule.location
177+
) { _ in
178+
// TODO:
179+
}
180+
self.presentViewControllerWithSheetOverlay(viewController, offset: Layout.Sheet.offset)
181+
182+
/*
183+
let viewController = ShippingRulesTableViewController.instantiate()
184+
viewController.configureWith(
185+
project, shippingRules: shippingRules,
186+
selectedShippingRule: selectedShippingRule
187+
)
188+
viewController.delegate = self
180189

181-
let navigationController = UINavigationController(rootViewController: viewController)
190+
let navigationController = UINavigationController(rootViewController: viewController)
182191

183-
self.presentViewControllerWithSheetOverlay(navigationController, offset: Layout.Sheet.offset)
192+
self.presentViewControllerWithSheetOverlay(navigationController, offset: Layout.Sheet.offset)
193+
*/
184194
}
185195
}
186196

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import KDS
2+
import KsApi
3+
import Library
4+
import SwiftUI
5+
6+
public func ShippingLocationsViewController(
7+
withLocations locations: [Location],
8+
selectedLocation: Location?,
9+
onSelectedLocation: @escaping (Location) -> Void
10+
) -> UIViewController {
11+
let sortedLocations = locations.sorted { a, b in
12+
a.localizedName <= b.localizedName
13+
}
14+
15+
let view = ShippingLocationsView(
16+
locations: sortedLocations,
17+
selectedLocation: selectedLocation,
18+
onSelectedLocation: onSelectedLocation
19+
)
20+
21+
return UIHostingController(rootView: view)
22+
}
23+
24+
public struct ShippingLocationsView: View {
25+
let locations: [Location]
26+
@State var selectedLocation: Location?
27+
let onSelectedLocation: (Location) -> Void
28+
29+
@State private var filteredLocations: [Location]? = nil
30+
@State private var searchText: String = ""
31+
32+
func buttonLabel(title: String, isSelected: Bool) -> some View {
33+
HStack(spacing: Constants.buttonLabelSpacing) {
34+
RadioButton(isSelected: isSelected)
35+
Text(title)
36+
.font(InterFont.bodyLG.swiftUIFont())
37+
.foregroundStyle(Colors.Text.primary.swiftUIColor())
38+
}
39+
}
40+
41+
@ViewBuilder var locationsList: some View {
42+
VStack(alignment: .leading, spacing: Constants.spacing) {
43+
ForEach(self.filteredLocations ?? self.locations) { location in
44+
Button {
45+
self.selectedLocation = location
46+
} label: {
47+
self.buttonLabel(
48+
title: location.localizedName,
49+
isSelected: self.selectedLocation?.id == location.id
50+
)
51+
}
52+
.id(location.id)
53+
}
54+
}
55+
.padding(Constants.padding)
56+
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
57+
}
58+
59+
public var body: some View {
60+
NavigationStack {
61+
ScrollViewReader { reader in
62+
ScrollView {
63+
self.locationsList
64+
}
65+
.onAppear {
66+
if let selectedLocation {
67+
reader.scrollTo(selectedLocation.id, anchor: .center)
68+
}
69+
}
70+
}
71+
.searchable(
72+
text: self.$searchText,
73+
placement: .navigationBarDrawer(displayMode: .always),
74+
prompt: Strings.Location_searchbox_placeholder() // TODO: what string
75+
)
76+
.navigationTitle(Strings.Location()) // TODO: what title
77+
.toolbar {
78+
ToolbarItem(placement: .cancellationAction) {
79+
Button(Strings.Cancel()) {
80+
// TODO: do something
81+
}
82+
}
83+
}
84+
}
85+
.onChange(of: self.selectedLocation) { newValue in
86+
if let location = newValue {
87+
self.onSelectedLocation(location)
88+
}
89+
}
90+
.onChange(of: self.searchText) { newValue in
91+
if newValue.isEmpty {
92+
self.filteredLocations = nil
93+
return
94+
}
95+
96+
self.filteredLocations = self.locations.filter { location in
97+
location.localizedName.lowercased().contains(self.searchText.lowercased())
98+
}
99+
}
100+
}
101+
102+
internal enum Constants {
103+
static let padding = Spacing.unit_06
104+
static let spacing = Spacing.unit_06
105+
static let buttonLabelSpacing = Spacing.unit_02
106+
}
107+
}
108+
109+
// TODO: preview

Kickstarter.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,6 +1671,7 @@
16711671
E158030B2D836C370000BAB3 /* SearchQuery_EmptyResults.json in Resources */ = {isa = PBXBuildFile; fileRef = E15803052D83632F0000BAB3 /* SearchQuery_EmptyResults.json */; };
16721672
E16794282B7EAA5200064063 /* OAuthTokenExchange.swift in Sources */ = {isa = PBXBuildFile; fileRef = E16794272B7EAA5200064063 /* OAuthTokenExchange.swift */; };
16731673
E167942A2B85136900064063 /* OAuthTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E16794292B85136900064063 /* OAuthTests.swift */; };
1674+
E16D0B0C2ECCD3AA004F28CC /* ShippingLocationsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E16D0B0B2ECCD3A3004F28CC /* ShippingLocationsView.swift */; };
16741675
E16ECA702C245A34002A1D25 /* PagedContainerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E16ECA6F2C245A34002A1D25 /* PagedContainerViewController.swift */; };
16751676
E16ECA722C245A51002A1D25 /* PagedContainerViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = E16ECA712C245A51002A1D25 /* PagedContainerViewModel.swift */; };
16761677
E170B9112B20E83B001BEDD7 /* MockGraphQLClient+CombineTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E170B9102B20E83B001BEDD7 /* MockGraphQLClient+CombineTests.swift */; };
@@ -3436,6 +3437,7 @@
34363437
E15E22CD2E54B91B00EE6BB0 /* KDS */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = KDS; sourceTree = "<group>"; };
34373438
E16794272B7EAA5200064063 /* OAuthTokenExchange.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OAuthTokenExchange.swift; sourceTree = "<group>"; };
34383439
E16794292B85136900064063 /* OAuthTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OAuthTests.swift; sourceTree = "<group>"; };
3440+
E16D0B0B2ECCD3A3004F28CC /* ShippingLocationsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShippingLocationsView.swift; sourceTree = "<group>"; };
34393441
E16ECA6F2C245A34002A1D25 /* PagedContainerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PagedContainerViewController.swift; sourceTree = "<group>"; };
34403442
E16ECA712C245A51002A1D25 /* PagedContainerViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PagedContainerViewModel.swift; sourceTree = "<group>"; };
34413443
E170B9102B20E83B001BEDD7 /* MockGraphQLClient+CombineTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MockGraphQLClient+CombineTests.swift"; sourceTree = "<group>"; };
@@ -4859,6 +4861,7 @@
48594861
19450C5228C82CEF00C60F97 /* ShippingRules */ = {
48604862
isa = PBXGroup;
48614863
children = (
4864+
E16D0B0B2ECCD3A3004F28CC /* ShippingLocationsView.swift */,
48624865
19450C5328C82D6500C60F97 /* Views */,
48634866
19450C5528C82D7100C60F97 /* Datasource */,
48644867
19450C5628C82D7900C60F97 /* Controller */,
@@ -9253,6 +9256,7 @@
92539256
A72C3AB71D00FB1F0075227E /* DiscoveryExpandedSelectableRow.swift in Sources */,
92549257
063D2D0A2846767F00CEDE33 /* PledgeLocalPickupView.swift in Sources */,
92559258
37059843226F79A700BDA6E3 /* PledgeShippingLocationViewController.swift in Sources */,
9259+
E16D0B0C2ECCD3AA004F28CC /* ShippingLocationsView.swift in Sources */,
92569260
01940B291D467ECE0074FCE3 /* HelpWebViewController.swift in Sources */,
92579261
8AA3DB35250AE46D009AC8EA /* SettingsAccountViewModel.swift in Sources */,
92589262
60E1BB352C74F7E4007D723B /* EstimatedShippingCheckoutView.swift in Sources */,

0 commit comments

Comments
 (0)