Created
October 14, 2020 23:23
-
-
Save jordibruin/627d64d978e78def9b7a878bf7d42db4 to your computer and use it in GitHub Desktop.
Simple App Store Review showcase in SwiftUI
This file contains hidden or 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
struct ReviewTabView: View { | |
var reviews: [Review] = [ | |
Review(title: "Great app", reviewer: "Maartje Derks", text: "This app really makes my life so much easier. Can't wait to use it."), | |
Review(title: "Great app", reviewer: "Maartje Derks", text: "This app really makes my life so much easier. Can't wait to use it."), | |
Review(title: "Great app", reviewer: "Maartje Derks", text: "This app really makes my life so much easier. Can't wait to use it."), | |
Review(title: "Great app", reviewer: "Maartje Derks", text: "This app really makes my life so much easier. Can't wait to use it."), | |
] | |
@State var selectedIndex = 0 | |
@State var timer = Timer.publish(every: 4, on: .main, in: .common).autoconnect() | |
func next() { | |
if selectedIndex < reviews.count { | |
selectedIndex = selectedIndex + 1 | |
} else { | |
selectedIndex = 0 | |
} | |
} | |
var body: some View { | |
if #available(iOS 14.0, *) { | |
TabView(selection: $selectedIndex) { | |
ForEach(0..<reviews.count, id: \.self) { index in | |
ReviewView(review: reviews[index]) | |
.tag(index) | |
} | |
}.tabViewStyle(PageTabViewStyle()) | |
.onReceive(timer, perform: { _ in | |
withAnimation { | |
next() | |
} | |
}) | |
} else { | |
Text("Not worth it to recreate on iOS 13") | |
} | |
} | |
} | |
struct CategoriesView_Previews: PreviewProvider { | |
static var previews: some View { | |
ReviewTabView() | |
} | |
} | |
struct ReviewView: View { | |
var review: Review | |
var body: some View { | |
ZStack { | |
Color.black.opacity(0.05) | |
VStack(alignment: .leading) { | |
HStack { | |
VStack(alignment: .leading, spacing: 6) { | |
Text(review.title) | |
.bold() | |
.layoutPriority(3) | |
.fixedSize(horizontal: false, vertical: /*@START_MENU_TOKEN@*/true/*@END_MENU_TOKEN@*/) | |
HStack(spacing: 2) { | |
ForEach(0..<5) { _ in | |
Image(systemName: "star.fill") | |
.foregroundColor(.orange) | |
.font(.subheadline) | |
} | |
Spacer() | |
Text(review.reviewer) | |
.opacity(0.4) | |
} | |
} | |
Spacer() | |
} | |
Text(review.text) | |
.padding(.top, 8) | |
Spacer() | |
} | |
.padding() | |
} | |
.fixedSize(horizontal: false, vertical: true) | |
.cornerRadius(10) | |
.padding(12) | |
} | |
} | |
struct ReviewView_Previews: PreviewProvider { | |
static var previews: some View { | |
ReviewView(review: Review(title: "Title", reviewer: "Reviewer", text: "Review text")) | |
} | |
} | |
struct Review { | |
var title: String | |
var reviewer: String | |
var text: String | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment