Last active
December 19, 2021 15:06
-
-
Save alessionossa/543a18a55423d98fc415be9edebbddb5 to your computer and use it in GitHub Desktop.
ContentView samples for https://stackoverflow.com/questions/61560644/get-enum-publisher-with-combine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import Combine | |
struct ContentView: View { | |
enum SectionPane: Equatable, Identifiable, CustomStringConvertible { | |
case servers | |
case snippets | |
var description: String { | |
switch self { | |
case .servers: | |
return "server" | |
case .snippets: | |
return "snippet" | |
} | |
} | |
var id: SectionPane { self } | |
} | |
@State var selectedPane: SectionPane? = nil | |
// BUG workaround, suggested at https://stackoverflow.com/questions/61003652/selection-in-navigationlink-is-not-working | |
@State var highlightedPane: SectionPane? = nil | |
// Subscribe to highlightedPane update to update active var | |
private var isActivePublisher: AnyPublisher<String, Never> { | |
return highlightedPane?.publisher | |
.map { $0.description } | |
.eraseToAnyPublisher() ?? | |
Just("").eraseToAnyPublisher() | |
} | |
@State private var active: String = "" | |
var body: some View { | |
NavigationView { | |
VStack(alignment: .leading) { | |
// Current selection label | |
Text("Selected: \(active)") | |
NavigationLink(destination: Text("first view").onAppear{ self.highlightedPane = SectionPane.servers }, tag: SectionPane.servers, selection: $selectedPane) { | |
Text("First") | |
} | |
NavigationLink(destination: Text("second view").onAppear{ self.highlightedPane = SectionPane.snippets }, tag: SectionPane.snippets, selection: $selectedPane) { | |
Text("Second") | |
} | |
Spacer() | |
}.onReceive(isActivePublisher, perform: { selection in | |
self.active = selection | |
}) | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import Combine | |
struct ContentView: View { | |
@State var selectedPane: String? = nil | |
// BUG workaround, suggested at https://stackoverflow.com/questions/61003652/selection-in-navigationlink-is-not-working | |
@State var highlightedPane: String? = nil | |
// Subscribe to highlightedPane update to update active var | |
private var isActivePublisher: AnyPublisher<String, Never> { | |
return highlightedPane?.publisher | |
.collect() | |
.map {String($0)} | |
.eraseToAnyPublisher() ?? | |
Just("").eraseToAnyPublisher() | |
} | |
@State private var active: String = "" | |
var body: some View { | |
NavigationView { | |
VStack(alignment: .leading) { | |
// Current selection label | |
Text("Selected: \(active)") | |
NavigationLink(destination: Text("first view").onAppear{ self.highlightedPane = "servers" }, tag: "servers", selection: $selectedPane) { | |
Text("First") | |
} | |
NavigationLink(destination: Text("second view").onAppear{ self.highlightedPane = "snippets" }, tag: "snippets", selection: $selectedPane) { | |
Text("Second") | |
} | |
Spacer() | |
}.onReceive(isActivePublisher, perform: { selection in | |
self.active = selection | |
}) | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment