-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from JacobHearst/tutorials
Add tutorial
- Loading branch information
Showing
31 changed files
with
1,169 additions
and
0 deletions.
There are no files selected for viewing
394 changes: 394 additions & 0 deletions
394
SampleCode/ScryfallSearcher/ScryfallSearcher.xcodeproj/project.pbxproj
Large diffs are not rendered by default.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
.../ScryfallSearcher/ScryfallSearcher.xcodeproj/project.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
...cher/ScryfallSearcher.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
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
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> |
14 changes: 14 additions & 0 deletions
14
...cher/ScryfallSearcher.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
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
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 | ||
} |
11 changes: 11 additions & 0 deletions
11
...Code/ScryfallSearcher/ScryfallSearcher/Assets.xcassets/AccentColor.colorset/Contents.json
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"colors" : [ | ||
{ | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...leCode/ScryfallSearcher/ScryfallSearcher/Assets.xcassets/AppIcon.appiconset/Contents.json
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
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 | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
SampleCode/ScryfallSearcher/ScryfallSearcher/Assets.xcassets/Contents.json
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
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
59
SampleCode/ScryfallSearcher/ScryfallSearcher/CardView.swift
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
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
88
SampleCode/ScryfallSearcher/ScryfallSearcher/ContentView.swift
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
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() | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...e/ScryfallSearcher/ScryfallSearcher/Preview Content/Preview Assets.xcassets/Contents.json
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
SampleCode/ScryfallSearcher/ScryfallSearcher/ScryfallSearcherApp.swift
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
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
69
SampleCode/ScryfallSearcher/ScryfallSearcher/SearchView.swift
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
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 | ||
} | ||
} | ||
} |
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
Oops, something went wrong.