Skip to content

Instantly share code, notes, and snippets.

@kean
Created November 21, 2021 03:06
Show Gist options
  • Save kean/846eba91cf3471071760ec0db3ddc23e to your computer and use it in GitHub Desktop.
Save kean/846eba91cf3471071760ec0db3ddc23e to your computer and use it in GitHub Desktop.
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