Created
January 5, 2022 22:01
-
-
Save c-villain/bb0dd9c7c1b00115a45674f51dcd9a29 to your computer and use it in GitHub Desktop.
List vs LazyVStack
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
// | |
// 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