You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.5 KiB
63 lines
1.5 KiB
//
|
|
// ListCategoryView.swift
|
|
// TabBarProject
|
|
//
|
|
// Created by Arthur Dambrine on 06/04/2021.
|
|
//
|
|
|
|
import SwiftUI
|
|
|
|
struct Category: Codable, Identifiable {
|
|
let id = UUID()
|
|
let id_categorie: Int
|
|
let nom: String
|
|
}
|
|
|
|
class apiCall {
|
|
func getCategories(completion:@escaping ([Category]) -> ()) {
|
|
guard let url = URL(string: "https://api.art-dambrine.ovh/categories") else { return }
|
|
URLSession.shared.dataTask(with: url) { (data, _, _) in
|
|
let categories = try! JSONDecoder().decode([Category].self, from: data!)
|
|
print(categories)
|
|
|
|
DispatchQueue.main.async {
|
|
completion(categories)
|
|
}
|
|
}
|
|
.resume()
|
|
}
|
|
}
|
|
|
|
|
|
struct ListCategoryView: View {
|
|
@State var categories: [Category] = []
|
|
|
|
var body: some View {
|
|
VStack {
|
|
Button {
|
|
print("buttonClicked")
|
|
apiCall().getCategories { (categories) in
|
|
self.categories = categories
|
|
}
|
|
} label : {
|
|
Text("Afficher les categories")
|
|
}
|
|
|
|
List(categories) { categorie in
|
|
|
|
Text(String(categorie.id_categorie))
|
|
.font(.headline)
|
|
Text(categorie.nom)
|
|
.font(.subheadline)
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
struct ListCategoryView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
ListCategoryView()
|
|
}
|
|
}
|
|
|