Skip to content

Instantly share code, notes, and snippets.

@ishabazz
Created June 26, 2020 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ishabazz/adeff378e594fcafc1879627f2829875 to your computer and use it in GitHub Desktop.
Save ishabazz/adeff378e594fcafc1879627f2829875 to your computer and use it in GitHub Desktop.
Trying to figure out how to show derived Views from different data types.
import SwiftUI
enum ComponentType{
case sandwich
case smoothie
case soup
}
class Component: Identifiable, Hashable {
static func == (lhs: Component, rhs: Component) -> Bool {
return lhs.id == rhs.id
}
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
var order:UInt
let type:ComponentType
let id:String
init(type:ComponentType, order:UInt = 0, id:String = UUID().uuidString){
self.type = type
self.order = order
self.id = UUID().uuidString
}
func view() -> some View{
Text("Default")
}
}
class SandwichComponent: Component{
init(order:UInt = 0){
super.init(type: .sandwich)
}
func view() -> some View{
Text("Sandwhich")
}
}
class SoupComponent: Component{
init(order:UInt = 1){
super.init(type: .soup, order: order)
}
func view() -> some View{
TextField("Placeholder", text: .constant("SOUP!!!!"))
}
}
class SmoothieComponent: Component{
init(order:UInt = 0){
super.init(type: .smoothie)
}
func view() -> some View{
Image(systemName: "heart")
}
}
struct LunchList:View {
var items:[Component] = [SandwichComponent(),SmoothieComponent(),SoupComponent()]
var body: some View{
VStack{
List {
ForEach(items,id:\.self){ item in
item.view()
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment