Skip to content

Instantly share code, notes, and snippets.

@azamsharp
Created June 30, 2020 20:57
Show Gist options
  • Save azamsharp/561e26e430b20d67f6193fd148fd60c3 to your computer and use it in GitHub Desktop.
Save azamsharp/561e26e430b20d67f6193fd148fd60c3 to your computer and use it in GitHub Desktop.
//
// ContentView.swift
// Shared
//
// Created by Mohammad Azam on 6/30/20.
//
import SwiftUI
struct Category: Identifiable {
let id: UUID = UUID()
let name: String
let products: [Product]
}
extension Category {
static func all() -> [Category] {
let animals = [Product(name: "🐢"),Product(name: "🐰")]
let fruits = [Product(name: "πŸ‘"), Product(name: "πŸ‰"), Product(name: "🍌")]
return [Category(name: "Animals", products: animals), Category(name: "Fruits", products: fruits)]
}
}
struct Product: Identifiable {
let id: UUID = UUID()
let name: String
}
struct ContentView: View {
var body: some View {
List {
ForEach(Category.all()) { category in
Section(header: Text(category.name)) {
OutlineGroup(category, children: \.products) { product in
Text(product.name)
}
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment