Skip to content

Instantly share code, notes, and snippets.

@c-villain
Created January 5, 2022 22:01
Show Gist options
  • Save c-villain/bb0dd9c7c1b00115a45674f51dcd9a29 to your computer and use it in GitHub Desktop.
Save c-villain/bb0dd9c7c1b00115a45674f51dcd9a29 to your computer and use it in GitHub Desktop.
List vs LazyVStack
//
// ContentView.swift
// ListVsLazyVStack
//
// Created by Alexander Kraev on 05.01.2022.
//
import SwiftUI
@main
struct ListVsLazyVStackApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct SampleRow: View {
let id: Int
let emojiArray = ["πŸ‘πŸ»", "πŸ€·πŸΌβ€β™‚οΈ", "🧐", "πŸ˜ƒ", "πŸ‘¨πŸ»β€πŸ’»"]
var body: some View {
VStack {
Text("Row \(id)")
Text("Random emoji: \( emojiArray.randomElement() ?? "πŸ˜”" )")
RatingView(rating: Int.random(in: 0..<5))
}
.frame(height: 100)
.padding()
}
init(id: Int) {
print("Loading row \(id)")
self.id = id
}
}
struct RatingView: View {
var rating: Int
var body: some View {
HStack {
ForEach(0..<5) { starIndex in
StarImage(isFilled: rating > starIndex)
}
}
}
}
struct StarImage: View {
var isFilled: Bool
var body: some View {
Image(systemName: "star.fill").foregroundColor( isFilled ? .yellow : .gray)
}
}
struct ContentView: View {
var body: some View {
Text("LazyVStack iOS 14").font(.largeTitle)
ScrollView {
LazyVStack {
ForEach(1...100, id: \.self, content: SampleRow.init)
}
}
/*
// uncomment this for test List rendering on iOS 14
Text("List iOS 14").font(.largeTitle)
List {
ForEach(1...100, id: \.self, content: SampleRow.init)
}
*/
}
}
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