Skip to content

Commit

Permalink
Verticals: Give flexibility by adding separate configure functions an…
Browse files Browse the repository at this point in the history
…d reload mechanism (#412)
  • Loading branch information
ninarg authored Sep 3, 2024
1 parent 0e6d90d commit c9dea12
Show file tree
Hide file tree
Showing 20 changed files with 144 additions and 64 deletions.
28 changes: 23 additions & 5 deletions CharcoalDemo/CharcoalDemo/DemoTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,23 @@ extension DemoTableViewController: UITableViewDelegate {
charcoalViewController.freeTextFilterDataSource = self
charcoalViewController.freeTextFilterDelegate = self
charcoalViewController.searchLocationDataSource = searchLocationDataSource
charcoalViewController.filterContainer = row.setup?.filterContainer
if let setup = row.setup {
charcoalViewController.filterContainer = setup.filterContainer
charcoalViewController.configure(with: setup.verticals)
charcoalViewController.updateReloadVerticalsButton(isVisible: setup.showVerticalsReloadButton)
}
charcoalViewController.selectionDelegate = self
} else if let viewController = viewController as? DrawerPresentationViewController {
let charcoalViewController = viewController.charcoalViewController
charcoalViewController.selectionDelegate = self
charcoalViewController.freeTextFilterDataSource = self
charcoalViewController.freeTextFilterDelegate = self
charcoalViewController.searchLocationDataSource = searchLocationDataSource
charcoalViewController.filterContainer = row.setup?.filterContainer
if let setup = row.setup {
charcoalViewController.filterContainer = setup.filterContainer
charcoalViewController.configure(with: setup.verticals)
charcoalViewController.updateReloadVerticalsButton(isVisible: setup.showVerticalsReloadButton)
}
charcoalViewController.selectionDelegate = self

viewController.transitioningDelegate = viewController.transition
Expand All @@ -79,10 +87,11 @@ extension DemoTableViewController: CharcoalViewControllerSelectionDelegate {
return
}

if let submarket = setup.markets.first(where: { $0.id == vertical.id }) {
setup.current = submarket
if let subVertical = setup.verticals.first(where: { $0.id == vertical.id }) {
setup.current = subVertical
viewController.isLoading = true
viewController.filterContainer = setup.filterContainer
viewController.configure(with: setup.verticals)
viewController.isLoading = false
}
}
Expand All @@ -99,8 +108,17 @@ extension DemoTableViewController: CharcoalViewControllerSelectionDelegate {
print("🔥 Did select external filter with key '\(key)' and value '\(value ?? "nil")'")
}

func charcoalViewControllerDidSelectReloadVerticals(_ viewController: CharcoalViewController) {
let verticals: [DemoVertical] = .multiple
if let setup = currentRow?.setup {
setup.verticals = verticals
setup.current = verticals.first
}
viewController.updateReloadVerticalsButton(isVisible: false)
viewController.configure(with: verticals)
}

func charcoalViewController(_ viewController: CharcoalViewController, didChangeSelection selection: [URLQueryItem], origin: SelectionChangeOrigin) {}
func charcoalViewController(_ viewController: CharcoalViewController, didSelect selection: CharcoalViewController.MapSelection) {}
}

extension DemoTableViewController: CharcoalViewControllerTextEditingDelegate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ extension FilterContainer {
rootFilters: [Filter] = defaultRootFilters(),
freeTextFilter: Filter? = .freeText,
inlineFilter: Filter? = defaultInlineFilter(),
numberOfResults: Int = 123,
numberOfVerticals: Int = 4,
lastVerticalIsExternal: Bool = true
numberOfResults: Int = 123
) -> FilterContainer {
let container = FilterContainer(rootFilters: rootFilters, freeTextFilter: freeTextFilter, inlineFilter: inlineFilter, numberOfResults: numberOfResults)
container.verticals = DemoVertical.create(numberOfVerticals, lastVerticalIsExternal: lastVerticalIsExternal)
return container
FilterContainer(rootFilters: rootFilters, freeTextFilter: freeTextFilter, inlineFilter: inlineFilter, numberOfResults: numberOfResults)
}

static func defaultRootFilters(isContextFilters: Bool = false, includePolygonSearch: Bool = true) -> [Filter] {
Expand Down
11 changes: 4 additions & 7 deletions CharcoalDemo/CharcoalDemo/Models/DemoSections.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ enum DemoSections: CaseIterable {
]
case .verticals:
return [
Row(title: "Single vertical", filterContainer: .singleVertical),
Row(title: "Multiple verticals", filterContainer: .multipleVerticals),
Row(title: "Single vertical", filterContainer: .standard, verticals: .none),
Row(title: "Multiple verticals", filterContainer: .standard, verticals: .multiple),
Row(title: "Failed to load verticals", filterContainer: .standard, verticals: .none, showVerticalsReloadButton: true)
]
case .misc:
return [
Expand All @@ -44,11 +45,7 @@ enum DemoSections: CaseIterable {
}

extension FilterContainer {
static var singleVertical: FilterContainer {
create(numberOfVerticals: 0)
}

static var multipleVerticals: FilterContainer {
static var standard: FilterContainer {
create()
}

Expand Down
12 changes: 11 additions & 1 deletion CharcoalDemo/CharcoalDemo/Models/DemoVertical.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,18 @@ class DemoVertical: Vertical {
self.isCurrent = isCurrent
self.isExternal = isExternal
}
}

extension Array where Element == DemoVertical {
static var none: [DemoVertical] {
create(0)
}

static var multiple: [DemoVertical] {
create(4)
}

static func create(_ count: Int, lastVerticalIsExternal: Bool) -> [DemoVertical] {
static func create(_ count: Int = 4, lastVerticalIsExternal: Bool = true) -> [DemoVertical] {
guard count >= 1 else { return [] }
return (1 ... count).map {
let isExternal = lastVerticalIsExternal && count > 1 && ($0 % count == 0)
Expand Down
15 changes: 13 additions & 2 deletions CharcoalDemo/CharcoalDemo/Models/Row.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,18 @@ struct Row {
self.usingBottomSheet = usingBottomSheet
}

init(title: String, type: UIViewController.Type = CharcoalViewController.self, filterContainer: FilterContainer) {
self.init(title: title, type: type, setup: Setup(filterContainer: filterContainer), usingBottomSheet: true)
init(
title: String,
type: UIViewController.Type = CharcoalViewController.self,
filterContainer: FilterContainer,
verticals: [DemoVertical] = .multiple,
showVerticalsReloadButton: Bool = false
) {
self.init(
title: title,
type: type,
setup: Setup(filterContainer: filterContainer, verticals: verticals, showVerticalsReloadButton: showVerticalsReloadButton),
usingBottomSheet: true
)
}
}
10 changes: 6 additions & 4 deletions CharcoalDemo/CharcoalDemo/Setup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import UIKit

class Setup {
var filterContainer: FilterContainer
let markets: [DemoVertical]
var verticals: [DemoVertical]
let showVerticalsReloadButton: Bool

var current: DemoVertical? {
didSet {
Expand All @@ -16,9 +17,10 @@ class Setup {
}
}

init(filterContainer: FilterContainer) {
defer { current = markets.first }
init(filterContainer: FilterContainer, verticals: [DemoVertical], showVerticalsReloadButton: Bool = false) {
defer { current = verticals.first }
self.filterContainer = filterContainer
markets = (filterContainer.verticals as? [DemoVertical]) ?? []
self.verticals = verticals
self.showVerticalsReloadButton = showVerticalsReloadButton
}
}
18 changes: 15 additions & 3 deletions CharcoalDemo/SnapshotTests/FilterContainerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@ class FilterContainerTests: XCTestCase {

private func snapshot(
_ filterContainer: FilterContainer,
verticals: [DemoVertical] = .multiple,
showReloadVerticalsButton: Bool = false,
record recording: Bool = false,
testName: String = #function
) {
assertSnapshots(matching: filterContainer, delay: 0.1, record: recording, testName: testName)
assertSnapshots(
matching: filterContainer,
verticals: verticals,
showReloadVerticalsButton: showReloadVerticalsButton,
delay: 0.1,
record: recording,
testName: testName
)
}

override func setUp() {
Expand All @@ -23,10 +32,13 @@ class FilterContainerTests: XCTestCase {
// MARK: - Tests

func testSingleVertical() {
snapshot(.singleVertical)
snapshot(.standard, verticals: .none)
}
func testMultipleVerticals() {
snapshot(.multipleVerticals)
snapshot(.standard, verticals: .multiple)
}
func testFailedToLoadVerticals() {
snapshot(.standard, verticals: .none, showReloadVerticalsButton: true)
}
func testContextFilter() {
snapshot(.contextFilter)
Expand Down
12 changes: 10 additions & 2 deletions CharcoalDemo/SnapshotTests/XCTestCaseExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ extension XCTestCase {

func assertSnapshots(
matching filterContainer: FilterContainer,
verticals: [DemoVertical],
showReloadVerticalsButton: Bool = false,
includeIPad: Bool = true,
delay: TimeInterval? = nil,
record recording: Bool = false,
file: StaticString = #file,
testName: String = #function
) {
let viewController = getViewController(for: filterContainer)
let viewController = getViewController(for: filterContainer, verticals: verticals, showReloadVerticalsButton: showReloadVerticalsButton)
assertSnapshots(
matching: viewController,
device: .iPhone,
Expand All @@ -57,9 +59,15 @@ extension XCTestCase {
}
}

private func getViewController(for filterContainer: FilterContainer) -> UIViewController {
private func getViewController(
for filterContainer: FilterContainer,
verticals: [DemoVertical],
showReloadVerticalsButton: Bool
) -> UIViewController {
let viewController = CharcoalViewController()
viewController.filterContainer = filterContainer
viewController.configure(with: verticals)
viewController.updateReloadVerticalsButton(isVisible: showReloadVerticalsButton)
return viewController
}
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 24 additions & 14 deletions Sources/Charcoal/CharcoalViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public protocol CharcoalViewControllerSelectionDelegate: AnyObject {
func charcoalViewController(_ viewController: CharcoalViewController,
didChangeSelection selection: [URLQueryItem],
origin: SelectionChangeOrigin)
func charcoalViewController(_ viewController: CharcoalViewController,
didSelect selection: CharcoalViewController.MapSelection)
func charcoalViewControllerDidSelectReloadVerticals(_ viewController: CharcoalViewController)
}

public protocol CharcoalViewControllerMapDelegate: AnyObject {
Expand Down Expand Up @@ -54,12 +53,6 @@ public final class CharcoalViewController: UINavigationController {
selectedFilters(in: filterContainer?.allFilters)
}

public enum MapSelection {
case openPolygonSearch
case openRadiusSearch
case initialArea
}

// MARK: - Private properties

private var selectionHasChanged = false
Expand All @@ -72,8 +65,9 @@ public final class CharcoalViewController: UINavigationController {
}()

private var rootFilterViewController: RootFilterViewController?

private var calloutOverlay: CalloutOverlay?
private var verticals: [Vertical]?
private var isReloadVerticalsButtonVisible: Bool?

// MARK: - Lifecycle

Expand Down Expand Up @@ -135,6 +129,17 @@ public final class CharcoalViewController: UINavigationController {
}
}

public func configure(with verticals: [Vertical]) {
self.verticals = verticals
updateReloadVerticalsButton(isVisible: false)
rootFilterViewController?.configure(with: verticals)
}

public func updateReloadVerticalsButton(isVisible: Bool) {
isReloadVerticalsButtonVisible = isVisible
rootFilterViewController?.updateReloadVerticalsButton(isVisible: isVisible)
}

// MARK: - Private

private func configure(with filterContainer: FilterContainer?) {
Expand All @@ -152,6 +157,12 @@ public final class CharcoalViewController: UINavigationController {
rootFilterViewController?.rootDelegate = self
rootFilterViewController?.freeTextFilterDataSource = freeTextFilterDataSource
rootFilterViewController?.freeTextFilterDelegate = freeTextFilterDelegate
if let verticals {
rootFilterViewController?.configure(with: verticals)
}
if let isReloadVerticalsButtonVisible {
rootFilterViewController?.updateReloadVerticalsButton(isVisible: isReloadVerticalsButtonVisible)
}
setViewControllers([rootFilterViewController].compactMap({ $0 }), animated: false)
}
}
Expand Down Expand Up @@ -220,6 +231,10 @@ extension CharcoalViewController: RootFilterViewControllerDelegate {
selectionDelegate?.charcoalViewController(self, didSelect: vertical)
}

func rootFilterViewControllerDidSelectReloadVerticals(_ viewController: RootFilterViewController) {
selectionDelegate?.charcoalViewControllerDidSelectReloadVerticals(self)
}

private func handleFilterSelectionChange(from origin: SelectionChangeOrigin) {
if let filterContainer = filterContainer {
let queryItems = selectionStore.queryItems(for: filterContainer)
Expand Down Expand Up @@ -365,11 +380,6 @@ extension CharcoalViewController: MapFilterViewControllerDelegate {
public func mapFilterViewControllerDidDismiss(_ mapFilterViewController: MapFilterViewController) {
mapDelegate?.charcoalViewControllerDidDismissMapSearch(self)
}

public func mapFilterViewController(_ mapFilterViewController: MapFilterViewController,
didSelect selection: CharcoalViewController.MapSelection) {
selectionDelegate?.charcoalViewController(self, didSelect: selection)
}
}

// MARK: - CalloutOverlayDelegate
Expand Down
10 changes: 0 additions & 10 deletions Sources/Charcoal/Filters/Map/MapFilterViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import UIKit

public protocol MapFilterViewControllerDelegate: AnyObject {
func mapFilterViewController(_ mapFilterViewController: MapFilterViewController,
didSelect selection: CharcoalViewController.MapSelection)
func mapFilterViewControllerDidDismiss(_ mapFilterViewController: MapFilterViewController)
}

Expand Down Expand Up @@ -177,10 +175,6 @@ public class MapFilterViewController: FilterViewController {
mapPolygonFilterViewController : mapRadiusFilterViewController
display(selectedViewController)
updateToggleButtonLabel()

let openedSearch: CharcoalViewController.MapSelection =
selectedViewController == mapRadiusFilterViewController ? .openRadiusSearch : .openPolygonSearch
mapFilterDelegate?.mapFilterViewController(self, didSelect: openedSearch)
}

private func updateToggleButtonLabel() {
Expand Down Expand Up @@ -222,10 +216,6 @@ extension MapFilterViewController: MapPolygonFilterViewControllerDelegate {
delegate?.filterViewControllerWillEndTextEditing(self)
}

func mapPolygonFilterViewControllerDidSelectInitialArea(_ mapPolygonFilterViewController: MapPolygonFilterViewController) {
mapFilterDelegate?.mapFilterViewController(self, didSelect: .initialArea)
}

func mapPolygonFilterViewControllerDidSelectFilter(_ mapPolygonFilterViewController: MapPolygonFilterViewController) {
mapRadiusFilterViewController.resetFilterValues()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import UIKit

protocol MapPolygonFilterViewControllerDelegate: AnyObject {
func mapPolygonFilterViewControllerDidSelectFilter(_ mapPolygonFilterViewController: MapPolygonFilterViewController)
func mapPolygonFilterViewControllerDidSelectInitialArea(_ mapPolygonFilterViewController: MapPolygonFilterViewController)
func mapPolygonFilterViewController(_ mapPolygonFilterViewController: MapPolygonFilterViewController, searchIsEnabled: Bool)
func mapPolygonFilterViewControllerWillBeginTextEditing(_ mapPolygonFilterViewController: MapPolygonFilterViewController)
func mapPolygonFilterViewControllerWillEndTextEditing(_ mapPolygonFilterViewController: MapPolygonFilterViewController)
Expand Down Expand Up @@ -529,7 +528,6 @@ extension MapPolygonFilterViewController: MapPolygonFilterViewDelegate {
mapPolygonFilterView.drawPolygon(with: annotations)
mapPolygonFilterView.configure(for: .polygonSelection)
updateFilterValues()
delegate?.mapPolygonFilterViewControllerDidSelectInitialArea(self)
}

func mapPolygonFilterViewDidSelectLocationButton(_ mapPolygonFilterView: MapPolygonFilterView) {
Expand Down
Loading

0 comments on commit c9dea12

Please sign in to comment.