Skip to content

Instantly share code, notes, and snippets.

@SwiftyAlex
Created June 30, 2021 21:36
Show Gist options
  • Save SwiftyAlex/1bb10c29f2fa10149d50ab7ead9e5daf to your computer and use it in GitHub Desktop.
Save SwiftyAlex/1bb10c29f2fa10149d50ab7ead9e5daf to your computer and use it in GitHub Desktop.
//
// ContentView.swift
// Searchable
//
// Created by Alex Logan on 27/06/2021.
//
import SwiftUI
struct Coffee: Hashable {
let name: String
let imageUrl: URL?
static let all: [Coffee] = [
.cortado, .flatWhite, .mocha, .latte, .icedLatte, .hotChocolate
]
// These images are all provided by unsplash.
static let cortado = Coffee(name: "Cortado", imageUrl: URL(string: "https://images.unsplash.com/photo-1515283709260-ee29296f1534?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=573&q=80"))
static let flatWhite = Coffee(name: "Flat White", imageUrl: URL(string: "https://images.unsplash.com/photo-1517236837508-2b3fca41f2f9?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1268&q=80"))
static let mocha = Coffee(name: "Mocha", imageUrl: URL(string: "https://images.unsplash.com/photo-1521813475821-5e3f5bc3c7a6?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1575&q=80"))
static let latte = Coffee(name: "Latte", imageUrl: URL(string: "https://images.unsplash.com/photo-1582202737800-8ab9a8cc6e6a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1268&q=80"))
static let icedLatte = Coffee(name: "Iced Latte", imageUrl: URL(string: "https://images.unsplash.com/photo-1558122104-355edad709f6?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1600&q=80"))
static let hotChocolate = Coffee(name: "Hot Chocolate", imageUrl: URL(string:"https://images.unsplash.com/photo-1542990253-0d0f5be5f0ed?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1268&q=80"))
}
struct CoffeeView: View {
let coffee: Coffee
var body: some View {
VStack(alignment: .center) {
Spacer()
AsyncImage(url: coffee.imageUrl) { image in
image
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 200, height: 200, alignment: .center)
.clipShape(RoundedRectangle(cornerRadius: 12, style: .continuous))
} placeholder: {
placeholder
}
Spacer()
}
.navigationTitle(Text(coffee.name))
}
var placeholder: some View {
Color.secondary
.frame(width: 200, height: 200)
.clipShape(RoundedRectangle(cornerRadius: 12, style: .continuous))
.overlay(ProgressView())
.progressViewStyle(.circular)
}
}
struct ContentView: View {
@State var selectedCoffee: Coffee = Coffee.flatWhite
@State var selectionIndex: Int = 0
var body: some View {
NavigationView {
List {
ForEach(Coffee.all, id: \.self) { coffee in
NavigationLink(destination: CoffeeView(coffee: coffee)) {
Text(coffee.name).font(.headline)
}
}
}
.listStyle(SidebarListStyle())
.navigationTitle(Text("Coffees"))
Text("Select a coffee")
}
}
}
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