Created
May 13, 2021 14:43
-
-
Save christianselig/4e7f95b3a28d0e316729ecfa21d8fbb0 to your computer and use it in GitHub Desktop.
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 | |
struct Setting { | |
enum SettingIcon { | |
case system(name: String) | |
case custom(name: String) | |
} | |
enum SettingType: Equatable { | |
case toggle | |
case menu(settings: [MenuSetting]) | |
case viewController(creationBlock: (() -> UIViewController)) | |
static func ==(lhs: SettingType, rhs: SettingType) -> Bool { | |
switch (lhs, rhs) { | |
case (.toggle, .toggle): | |
return true | |
case (.menu(let settings1), .menu(let settings2)): | |
return settings1 == settings2 | |
case (.viewController, .viewController): | |
// Always assume two view controller creation blocks are inequal for simplicity | |
return false | |
default: | |
return false | |
} | |
} | |
} | |
let title: String | |
let key: String | |
let icon: SettingIcon | |
let type: SettingType | |
} | |
struct MenuSetting: Equatable { | |
let title: String | |
let isChecked: Bool | |
} | |
var settings: [Setting] = [ | |
Setting(title: "Dogs Can Fly", key: "DogsCanFly", icon: .system(name: "airplane"), type: .toggle), | |
Setting(title: "Dog Name", key: "DogName", icon: .system(name: "textformat"), type: .menu(settings: [ | |
MenuSetting(title: "Rover", isChecked: true), | |
MenuSetting(title: "Phyllis", isChecked: false), | |
MenuSetting(title: "Rosetta", isChecked: false), | |
MenuSetting(title: "Bartholimeu", isChecked: false), | |
])) | |
] | |
struct ContentView: View { | |
var body: some View { | |
NavigationView { | |
Form { | |
Section { | |
ForEach(settings, id: \.key) { (setting) in | |
// Works | |
// Text(setting.title) | |
// Does not work | |
switch setting.type { | |
case .toggle: | |
Text("Toggle") | |
case .setting(let menu): | |
Text("setting") | |
case .viewController (let creationBlock): | |
Text("Meep") | |
} | |
} | |
} | |
} | |
.navigationBarTitle("Settings") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment