๐Ÿ“‘ Project

[Project] WishListApp #2 / UI ์ž‘์—… ๋ฐ ๋ฐ์ดํ„ฐ ์ ์šฉํ•˜๊ธฐ

EarthSea 2024. 4. 15. 23:38

 

 

์˜ค๋Š˜์€ ์œ„์‹œ ๋ฆฌ์ŠคํŠธ์˜ UI์ž‘์—…๊ณผ ํ™”๋ฉด์— Data๋ฅผ ๋„์šฐ๋Š” ์ž‘์—…์„ ํ•˜์˜€์Šต๋‹ˆ๋‹ค!

๊ทธ๋Ÿผ ์‹œ์ž‘ํ•ด๋ณผ๊นŒ์šง?! โœจ

 

WishListApp UI

WishListApp์˜ ๋ฉ”์ธํ™”๋ฉด์€ ๊ทธ์ € ๊น”๋”ํ•˜๊ณ  ์‹ฌํ”Œํ–ˆ์œผ๋ฉด ์ข‹๊ฒ ๋‹ค๋ผ๊ณ  ์ƒ๊ฐ์„ ํ–ˆ์–ด์š”!

๊ทธ๋ž˜์„œ ํ‰์†Œ์— ์ œ๊ฐ€ ์ฆ๊ฒจ์“ฐ๋Š” ์•ฑ์ธ " 29CM "์˜ ์ƒํ’ˆ ํ™”๋ฉด๊ณผ ๋น„์Šทํ•˜๊ฒŒ ๋งŒ๋“ค์–ด๋ณด์ž๊ณ  ์ƒ๊ฐํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! 

 

29CM ์•ฑ์˜ ์ƒํ’ˆ ํŽ˜์ด์ง€ ์บก์ณ ํ™”๋ฉด

 

๋‚ด๊ฐ€ ๊ตฌํ˜„ํ•œ Main.storyBoard

 

์•„๋ž˜ Add to Wish ๋ฒ„ํŠผ๊ณผ Next to Item ์ด ์žˆ๋Š” View๋Š” ๊ณ ์ •์ธ ์ƒํƒœ๋กœ Description์ด ๊ธธ์–ด์ง€๊ฒŒ ๋˜๋ฉด View๊ฐ€ ์Šคํฌ๋กค์ด ๋˜๊ฒŒ๋” ๊ตฌํ˜„์„ ํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค! ๊ธ€์”จ์ฒด๋Š” ๋‹ค๋ฅด์ง€๋งŒ ๊ทธ๋ž˜๋„ ์ •๋ง ๊น”๋”ํ•œ UI ๊ฐ™์•„์š”!ใ…Žใ…Ž

 

 

 

 

WishListApp Data ์ ์šฉํ•˜๊ธฐ

 

URLSession์„ ํ†ตํ•ด ๋ณ€ํ™˜ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ProductDataManager์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค!

let dataManager = ProductDataManager()

 

๋žœ๋คํ•œ id ๊ฐ’์„ ํ˜•์„ฑํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด randomID ๋ณ€์ˆ˜์™€ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์—ˆ์–ด์š”~!

var randomid = 0

func setRandomID() {
    guard let randomId = (1...100).randomElement() else { return }
    randomid = randomId
}

 

์ด์ œ dataManager๋ฅผ ํ†ตํ•ด getProductData์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ’์„ ๋ฐ์ดํ„ฐ๋ฅผ ๊บผ๋‚ด์˜ต์‹œ๋‹ค!

func setDataAndLabel() {
    dataManager.getProductData(id: randomid ) { product in
        DispatchQueue.main.async {
            if let product = product {
                dump(product)
                // ๋ฐ์ดํ„ฐ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ
            }
        }
    }
}

์ด ๋•Œ, ๋ฐ์ดํ„ฐ๋ฅผ UI์ž‘์—…์— ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” DispatchQueue.main.async ๋ฅผ ๊ผญ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค! ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์“ฐ๋ ˆ๋“œ์—์„œ ์ฒ˜๋ฆฌ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฉ”์ธ ์“ฐ๋ ˆ๋“œ๋กœ ์•ˆ์ „ํ•˜๊ฒŒ ์ „์†กํ•ด์ค๋‹ˆ๋‹ค.

 

๋ฐ์ดํ„ฐ๋ฅผ UI์— ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ๋ฅผ ์žฌ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋”ฐ๋กœ ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์–ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

func setData(_ product: Product) {
    
    if let url = URL(string: product.thumbnail) {
        URLSession.shared.dataTask(with: url) { data, response, error in
            if let data = data, error == nil {
                DispatchQueue.main.async {
                    self.productImageView.image = UIImage(data: data)
                }
            }
        }.resume()
    }
    productBrandLabel.text = product.brand
    productTitleLabel.text = product.title
    productPriceLabel.text = "\(product.price) ์›"
    productDescriptionLabel.text = product.description
}

์ด ๋•Œ, url ์ฃผ์†Œ๋กœ ์ด๋ฃจ์–ด์ง„ ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” URLSession์„ ํ†ตํ•œ data๋ฅผ ๋นผ๋‚ด๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด์ฃผ์—ˆ์–ด์•ผ ํ–ˆ์–ด์š”!

 

setDataAndLabel() ํ•จ์ˆ˜์˜ // ๋ฐ์ดํ„ฐ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ ์— ํ•ด๋‹น setData(product) ํ•จ์ˆ˜๋ฅผ ๋„ฃ์–ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

์ด์ œ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์—ˆ์œผ๋ฉด ์‹คํ–‰์„ ์‹œ์ผœ์•ผ๊ฒ ์ฃ ?!

viewDidLoad()์— ํ•จ์ˆ˜๋ฅผ ๋„ฃ์–ด์„œ ์ดˆ๊ธฐํ™”๋ฉด ๊ตฌํ˜„์„ ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

override func viewDidLoad() {
    super.viewDidLoad()
    setRandomID()
    setDataAndLabel()
}

 

 

์ถ”๊ฐ€๋กœ Next to Item ๋ฒ„ํŠผ์ด ํด๋ฆญ๋˜์—ˆ์„ ๋•Œ, ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€ํ™”์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ randomID๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜์™€ ํ•ด๋‹น data๋ฅผ ๊ฐ€์ ธ์™€์„œ ui๋ฅผ ๊ทธ๋ฆฌ๋Š” ํ•จ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ์–ด์š”!

@IBAction func nextToItemTapped(_ sender: UIButton) {
    setRandomID()
    setDataAndLabel()
}

 

 

 

ViewController์˜ ์ „์ฒด์ฝ”๋“œ

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var productImageView: UIImageView!
    @IBOutlet weak var productBrandLabel: UILabel!
    @IBOutlet weak var productTitleLabel: UILabel!
    @IBOutlet weak var productPriceLabel: UILabel!
    @IBOutlet weak var productDescriptionLabel: UILabel!
    @IBOutlet weak var addToWishButton: UIButton!
    
    let dataManager = ProductDataManager()
    
    var randomid = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setRandomID()
        setDataAndLabel()
    }
    
    func setRandomID() {
        guard let randomId = (1...100).randomElement() else { return }
        randomid = randomId
    }
    
    func setDataAndLabel() {
        dataManager.getProductData(id: randomid ) { product in
            DispatchQueue.main.async {
                if let product = product {
                    dump(product)
                    self.setData(product)
                }
            }
        }
    }
    
    func setData(_ product: Product) {
        
        if let url = URL(string: product.thumbnail) {
            URLSession.shared.dataTask(with: url) { data, response, error in
                if let data = data, error == nil {
                    DispatchQueue.main.async {
                        self.productImageView.image = UIImage(data: data)
                    }
                }
            }.resume()
        }
        productBrandLabel.text = product.brand
        productTitleLabel.text = product.title
        productPriceLabel.text = "\(product.price) ์›"
        productDescriptionLabel.text = product.description
    }
    
    @IBAction func addToWishTapped(_ sender: UIButton) {
        
    }
    
    @IBAction func nextToItemTapped(_ sender: UIButton) {
        setRandomID()
        setDataAndLabel()
    }
}

 

 

 

 

๊ตฌํ˜„ ํ™”๋ฉด

 

 

 

์˜ค๋Š˜์€ ์—ฌ๊ธฐ๊นŒ์ง€~!

๋‚ด์ผ์€ Add to Wish ๋ฒ„ํŠผ์ด ํด๋ฆญ๋˜์—ˆ์„ ๋•Œ, ํ•ด๋‹น ๊ฐ’์„ CoreData์— ์ €์žฅํ•˜๋Š” ๋กœ์ง์„ ๊ตฌํ˜„ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค~!