Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code from "Designing an API Client in Swift"
enum APIEnvironment: String, CaseIterable {
case dev
case test
case stage
case prod
}
struct ContentView: View {
@State private var selection: APIEnvironment? = env
var body: some View {
List(selection: $selection) {
ForEach(APIEnvironment.allCases, id: \.self, content: makeView)
}
.environment(\.editMode, .constant(EditMode.active))
.navigationBarItems(trailing: Button("Save") {
DebugSettings.shared.environment = selection!
fatalError("Restarting the app")
})
.disabled(selection == nil)
.navigationBarTitle("Environment")
}
private func makeView(for env: APIEnvironment) -> some View {
VStack(alignment: .leading) {
Text(env.rawValue)
.font(.headline)
Text(env.host)
.font(.footnote)
.foregroundColor(.secondary)
}.tag(env)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment