Skip to content

Send in $destination into Adapter #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ public struct CountryListAdapter: View {

private var onCountrySelected: ((Country) -> Void)?
private var onAboutThisAppSelected: (() -> Void)?
@Binding var destination: Destinations?

public init(onCountrySelected: ((Country) -> Void)? = nil,
onAboutThisAppSelected: (() -> Void)? = nil) {
self.onCountrySelected = onCountrySelected
self.onAboutThisAppSelected = onAboutThisAppSelected
public init(destination: Binding<Destinations?>) {
self._destination = destination
}

public var body: some View {
Expand All @@ -36,10 +35,17 @@ public struct CountryListAdapter: View {
.onReceive(viewModel.$navigationDestination, perform: { country in
guard let country = country else { return }

self.onCountrySelected?(country)
self.destination = Destinations.details(regionCode: country.regionCode)
})
.onReceive(Just($destination), perform: { newValue in
if newValue.wrappedValue == nil && viewModel.navigationDestination != nil {
viewModel.navigationDestination = nil
}
})
.pss_notify(item: $viewModel.error, alertBuilder: {
$0.asFloatingAlert(viewModel: viewModel, onAboutThisAppSelected: onAboutThisAppSelected)
$0.asFloatingAlert(viewModel: viewModel, onAboutThisAppSelected: {
self.destination = Destinations.aboutThisApp
})
})
}
}
13 changes: 13 additions & 0 deletions Packages/Feature/Sources/Feature/Destinations.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// File.swift
//
//
// Created by Eric Silverberg on 12/28/22.
//

import Foundation

public enum Destinations {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe naming this enum singular (instead of plural) would eliminate any confusion when expecting parameters of this type:

// 👍 
func navigateTo(destination: Destination) { ... }

// ❓ 
func navigateTo(destination: Destinations) { ... } // it feels like we are going to navigate to multiple places

case details(regionCode: String)
case aboutThisApp
}
11 changes: 1 addition & 10 deletions Shared/TravelAdvisoriesNavHost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ public struct TravelAdvisoriesNavHost: View {

@State var destination: Destinations?

enum Destinations {
case details(regionCode: String)
case aboutThisApp
}

public init(resolver: Swinject.Resolver = InjectSettings.resolver!) {
self.resolver = resolver
}
Expand All @@ -45,11 +40,7 @@ public struct TravelAdvisoriesNavHost: View {
}

@ViewBuilder func buildBaseView() -> some View {
CountryListAdapter(onCountrySelected: { country in
self.destination = Destinations.details(regionCode: country.regionCode)
}) {
self.destination = Destinations.aboutThisApp
}
CountryListAdapter(destination: $destination)
}

@ViewBuilder func buildChildViewFromState() -> some View {
Expand Down