ios-dev/comments/Kities.swift

60 lines
1.2 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

//
// Kities.swift
// comments
//
// Created by Георгий Сигалев on 11.03.2024.
//
import Foundation
import SwiftUI
import Alamofire
import SDWebImage
import SDWebImageSwiftUI
struct CatResponse: Identifiable, Decodable {
var id: String
var url: String
var width: Int
var height: Int
}
func fetchKities() async -> CatResponse? {
let result = await AF.request("https://api.thecatapi.com/v1/images/search").serializingDecodable([CatResponse].self).result
var data: [CatResponse] = []
do {
try data = result.get()
} catch {
return nil
}
return data[0]
}
struct KityView: View {
@State var url: String?
@Environment(\.dismiss) private var dismiss
@ViewBuilder
var body: some View{
VStack {
if url != nil {
WebImage(url: URL(string: url!))
}
else {
Text("Загрузка...")
}
}.task {
guard let cat = (await fetchKities()) else {
dismiss()
return
}
self.url = cat.url
}
}
}