Created
June 26, 2020 16:04
-
-
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.
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 | |
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