Skip to content

Commit

Permalink
Merge pull request #15 from JacobHearst/tutorials
Browse files Browse the repository at this point in the history
Add tutorial
  • Loading branch information
JacobHearst authored Sep 8, 2023
2 parents bd7d61e + 66fb56b commit bead4ac
Show file tree
Hide file tree
Showing 31 changed files with 1,169 additions and 0 deletions.
394 changes: 394 additions & 0 deletions SampleCode/ScryfallSearcher/ScryfallSearcher.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"pins" : [
{
"identity" : "scryfallkit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/jacobhearst/scryfallkit",
"state" : {
"revision" : "bc1f742718420a099663b8af1eae2abbeb615ad3",
"version" : "5.2.0"
}
}
],
"version" : 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"images" : [
{
"idiom" : "universal",
"platform" : "ios",
"size" : "1024x1024"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
59 changes: 59 additions & 0 deletions SampleCode/ScryfallSearcher/ScryfallSearcher/CardView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// CardView.swift
//

import SwiftUI
import ScryfallKit

struct CardView: View {
var card: Card

var body: some View {
VStack {
AsyncImage(url: card.getImageURL(type: .normal)) { image in
image
.resizable()
.scaledToFit()
} placeholder: {
Text(card.name)
ProgressView()
}

GroupBox {
HStack {
Text(card.name)
Spacer()
Text(card.manaCost ?? "")
}

Divider()

Text(card.oracleText ?? "")
.padding(.bottom)
Text(card.flavorText ?? "")
.italic()

if let powerAndToughness = card.powerAndToughness {
Divider()
HStack {
Spacer()
Text(powerAndToughness)
}
}
}
}
}
}

extension Card {
var powerAndToughness: String? {
guard let power, let toughness else { return nil }
return "\(power)/\(toughness)"
}
}

//struct SwiftUIView_Previews: PreviewProvider {
// static var previews: some View {
// SwiftUIView()
// }
//}
88 changes: 88 additions & 0 deletions SampleCode/ScryfallSearcher/ScryfallSearcher/ContentView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//
// ContentView.swift
// ScryfallSearcher
//

import SwiftUI
import ScryfallKit

struct ContentView: View {
private let client = ScryfallClient()
private let columns: [GridItem] = Array(repeating: .init(.flexible()), count: 2)

@State private var loading = false
@State private var query = ""
@State private var cards = [Card]()
@State private var error: String?
@State private var showError = false

var body: some View {
ScrollView {
TextField("Search for Magic: the Gathering cards", text: $query)
.textFieldStyle(.roundedBorder)
.autocorrectionDisabled(true)
.textInputAutocapitalization(.never)
.onSubmit {
search(query: query)
}

if loading {
ProgressView()
} else if cards.isEmpty {
Text("Perform a search to view cards")
} else {
LazyVGrid(columns: columns) {
ForEach(cards) { card in
AsyncImage(url: card.getImageURL(type: .normal)) { image in
image
.resizable()
.scaledToFit()
} placeholder: {
Text(card.name)
ProgressView()
}
}
}
}

Spacer()
}
.alert("Error", isPresented: $showError, presenting: error, actions: { _ in }) { error in
Text(error)
}
.refreshable {
guard !query.isEmpty else { return }
search(query: query)
}
.padding()
}

private func search(query: String) {
error = nil
loading = true

Task {
do {
let results = try await client.searchCards(query: query)
await MainActor.run {
cards = results.data
}
} catch {
await MainActor.run {
showError = true
self.error = error.localizedDescription
}
}

await MainActor.run {
loading = false
}
}
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// ScryfallSearcherApp.swift
// ScryfallSearcher
//
// Created by Jacob Hearst on 4/11/23.
//

import SwiftUI

@main
struct ScryfallSearcherApp: App {
var body: some Scene {
WindowGroup {
SearchView()
}
}
}
69 changes: 69 additions & 0 deletions SampleCode/ScryfallSearcher/ScryfallSearcher/SearchView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import SwiftUI
import ScryfallKit

struct SearchView: View {
private let client = ScryfallClient()
private let columns: [GridItem] = Array(repeating: .init(.flexible()), count: 2)

@State private var loading = false
@State private var query = ""
@State private var cards = [Card]()
@State private var error: String?
@State private var showError = false

var body: some View {
NavigationStack {
ScrollView {
TextField("Search for Magic: the Gathering cards", text: $query)
.textFieldStyle(.roundedBorder)
.autocorrectionDisabled(true)
.onSubmit(search)

if loading {
ProgressView()
} else if cards.isEmpty {
Text("Perform a search to view cards")
} else {
LazyVGrid(columns: columns) {
ForEach(cards) { card in
NavigationLink(value: card) {
AsyncImage(url: card.getImageURL(type: .normal)) { image in
image
.resizable()
.scaledToFit()
} placeholder: {
Text(card.name)
ProgressView()
}
}
}
}.navigationDestination(for: Card.self) { CardView(card: $0) }
}
}
}
.padding()
.alert("Error", isPresented: $showError, presenting: error, actions: { _ in }) { error in
Text(error)
}
}

private func search() {
loading = true
error = nil
showError = false

Task {
do {
let results = try await client.searchCards(query: query)
await MainActor.run {
cards = results.data
}
} catch {
self.error = error.localizedDescription
self.showError = true
}

loading = false
}
}
}
1 change: 1 addition & 0 deletions Sources/ScryfallKit/Documentation.docc/ScryfallKit.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ Some recommended starting points:

### Fundamentals

- <doc:SearchTutorial>
- <doc:RetrievingCards>
- <doc:MultiFacedCards>
Loading

0 comments on commit bead4ac

Please sign in to comment.