Skip to content
Snippets Groups Projects
Commit c76943a9 authored by Jehu Ombrog's avatar Jehu Ombrog
Browse files

Update Second View Controll, Added API call

parent 7572278f
No related branches found
No related tags found
No related merge requests found
......@@ -69,7 +69,6 @@
618005D82C770F4000E07B3E /* Model */,
615AC3F42C764237009924FB /* AppDelegate.swift */,
615AC3F62C764237009924FB /* SceneDelegate.swift */,
615AC3F82C764237009924FB /* ViewController.swift */,
615AC3FD2C764238009924FB /* Assets.xcassets */,
615AC3FF2C764238009924FB /* LaunchScreen.storyboard */,
615AC4022C764238009924FB /* Info.plist */,
......@@ -96,6 +95,7 @@
618005DB2C770F6500E07B3E /* Controller */ = {
isa = PBXGroup;
children = (
615AC3F82C764237009924FB /* ViewController.swift */,
615AC4102C7663AD009924FB /* CustomTableViewCell.swift */,
618005D62C76ED5B00E07B3E /* SecondViewController.swift */,
618005DC2C770F7400E07B3E /* API */,
......
......@@ -21,4 +21,18 @@ class productController{
return []
}
}
func fetchOneProductDetails(id : String) async throws -> productDetails? {
let urlString = "https://fakestoreapi.com/products/\(id)"
guard let url = URL(string:urlString) else { return nil } // to check that url must have value
do{
let (data , _) = try await URLSession.shared.data(from: url)
let productList = try JSONDecoder().decode(productDetails.self, from: data)
return productList
}catch{
print("DEBUG: Error \(error.localizedDescription)")
return nil
}
}
}
......@@ -12,35 +12,49 @@ class SecondViewController: UIViewController {
@IBOutlet weak var productName : UILabel!
@IBOutlet weak var productPrice : UILabel!
@IBOutlet weak var productImage : UIImageView!
var img : UIImage? {
didSet {
productImage.image = img
}
}
var lblName = ""
var lblPrice = ""
var productDetail : productDetails?
var id = ""
override func viewDidLoad() {
super.viewDidLoad()
productName.text = lblName
productPrice.text = lblPrice
Task {
do {
try await fetchProduct()
} catch {
print("Failed to fetch products: \(error)")
}
}
// Do any additional setup after loading the view.
}
func fetchProduct() async throws {
do {
self.productDetail = try await productController().fetchOneProductDetails(id: id)
productName.text = productDetail?.title
productPrice.text = String(format: "$%.2f", productDetail!.price)
if let imageUrl = URL(string: productDetail!.image) {
ViewController().loadImage(from: imageUrl) { image in
DispatchQueue.main.async {
self.productImage.image = image
}
}
}
/*
// MARK: - Navigation
} catch {
print("Error fetching products: \(error)")
}
}
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
......@@ -43,7 +43,7 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour
}
}
private func loadImage(from url: URL, completion: @escaping (UIImage?) -> Void) {
func loadImage(from url: URL, completion: @escaping (UIImage?) -> Void) {
let task = URLSession.shared.dataTask(with: url) { data, response, error in
if let error = error {
print("Error fetching image: \(error)")
......@@ -66,18 +66,8 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let vc = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController{
let product = productList[indexPath.row]
vc.lblName = product.title
vc.lblPrice = String(format: "$%.2f", product.price)
if let imageUrl = URL(string: product.image) {
loadImage(from: imageUrl) { image in
DispatchQueue.main.async {
vc.img = image
print("DEBUG: image link \(product.image)")
print("DEBUG: UIImage \(image)")
}
}
}
vc.id = String(product.id)
self .navigationController?.pushViewController(vc, animated: true)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment