Hey there, anime fans and SwiftUI developers! Today, weâre going to create a fun and easy-to-use anime app that will display random quotes from your favorite shows using the Animechan API! đ
Get ready to laugh with some light anime jokes and enjoy this 5-minute read. Letâs dive in and build our very own anime-quote-dispenser! đ
Getting Started: Setting Up the Project đ
First, create a new SwiftUI project in Xcode, and give it a fitting nameâââlike âAnimeQuotesâ or âQuoteDĆjĆâ! đ„
Now, letâs get our hands on those juicy anime quotes by using the Animechan API. Weâll be using two endpoints:
Random quote: https://animechan.vercel.app/api/random
Random quote from a specific anime: https://animechan.vercel.app/api/random/anime?title=naruto
More information about the API can be found at https://animechan.vercel.app.
Networking Like a Ninja đ±âđ€
Itâs time to create a networking layer to fetch quotes from the Animechan API. Letâs create a QuoteAPI class:
import Foundation
class QuoteAPI {
let baseURL = "https://animechan.vercel.app/api"
func getRandomQuote(completion: @escaping (AnimeQuote?) -> Void) {
let urlString = "\(baseURL)/random"
fetchData(urlString: urlString, completion: completion)
}
func getRandomQuote(fromAnime title: String, completion: @escaping (AnimeQuote?) -> Void) {
let urlString = "\(baseURL)/random/anime?title=\(title)"
fetchData(urlString: urlString, completion: completion)
}
private func fetchData(urlString: String, completion: @escaping (AnimeQuote?) -> Void) {
guard let url = URL(string: urlString) else {
completion(nil)
return
}
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data, error == nil {
let quote = try? JSONDecoder().decode(AnimeQuote.self, from: data)
completion(quote)
} else {
completion(nil)
}
}.resume()
Great, weâve got our networking ninja ready for action! đ
Building the UI: Displaying Quotes Like a Boss đ±
Letâs create an engaging user interface to display the quotes. Weâll start with a simple QuoteCard view:
import SwiftUI
struct QuoteCard: View {
let quote: AnimeQuote
var body: some View {
VStack(alignment: .leading, spacing: 10) {
Text(quote.anime)
.font(.title)
.bold()
.foregroundColor(.red)
Text("\"\(quote.quote)\"")
.font(.headline)
.italic()
Text("- \(quote.character)")
.font(.subheadline)
.fontWeight(.medium)
.foregroundColor(.gray)
}
.padding()
.background(Color(.systemGray6))
.cornerRadius(10)
.shadow(radius: 5)
}
}
Now, letâs create the main ContentView:
import SwiftUI
struct ContentView: View {
@State private var quote: AnimeQuote?
@State private var animeTitle: String = ""
private let quoteAPI = QuoteAPI()
var body: some View {
VStack {
TextField("Enter anime title", text: $animeTitle)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
if let quote = quote {
QuoteCard(quote: quote)
} else {
Text("No quote yet")
.font(.headline)
.foregroundColor(.gray)
}
Spacer()
Button(action: fetchRandomQuote) {
Text("Get Random Quote")
.bold()
.padding()
.background(Color.red)
.foregroundColor(.white)
.cornerRadius(10)
}
}
.padding()
.onAppear(perform: fetchRandomQuote)
}
private func fetchRandomQuote() {
if animeTitle.isEmpty {
quoteAPI.getRandomQuote { fetchedQuote in
DispatchQueue.main.async {
self.quote = fetchedQuote
}
}
} else {
quoteAPI.getRandomQuote(fromAnime: animeTitle) { fetchedQuote in
DispatchQueue.main.async {
self.quote = fetchedQuote
}
This ContentView contains a TextField for the user to input an anime title, displays the fetched quote using QuoteCard, and has a button to fetch a new quote. Our app is now ready to roll! đ
Wrapping Up: Anime App Extraordinaire đ
Congratulations! Youâve successfully created an anime-quote-fetching SwiftUI app using the Animechan API! đ Your app is now filled with wisdom (and occasional hilarity) from your favorite anime characters! đ€Ł
So go on, give yourself a pat on the back and share your newfound anime-quote knowledge with the world! đ
Comments