Skip to content

Commit

Permalink
Merge pull request #136 from rising-jun/feat-homecellimage
Browse files Browse the repository at this point in the history
feat - 이미지 화면에 보여주기 기능 구현 - #135
  • Loading branch information
rising-jun committed Apr 26, 2022
2 parents bff5768 + 9cc1ce4 commit 9927984
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 11 deletions.
5 changes: 5 additions & 0 deletions SideDishProject/SideDishProject/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
Expand Down
8 changes: 3 additions & 5 deletions SideDishProject/SideDishProject/model/Product.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,19 @@ struct Product{
let description: String
let stock: Int
let category: DishCategory
let imageUrl: String
let event: [ProductEvent]

init(name: String, price: Int, description: String, stock: Int, category: DishCategory, event: [ProductEvent]) {
init(name: String, price: Int, description: String, stock: Int, category: DishCategory, imageUrl: String, event: [ProductEvent]) {
self.id = UUID()
self.name = name
self.price = price
self.description = description
self.stock = stock
self.category = category
self.imageUrl = imageUrl
self.event = event
}

func getTitle() -> String{
return name
}
}

enum DishCategory: CaseIterable{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,71 @@ enum ProductRepositoryError: Error {
}

protocol ProductRepository {
func fetchAll(completion: @escaping (Result<[Product], ProductRepositoryError>) -> Void)
func fetchAll(completion: @escaping (Result<[DishCategory: [Product]], ProductRepositoryError>) -> Void)
func fetchList(by category: DishCategory, completion: @escaping (Result<[Product], ProductRepositoryError>) -> Void)
func fetchImage(completion: @escaping (Result<[DishCategory : [Data]] , ProductRepositoryError>) -> Void)
}

final class MockProductRepository {
private let products: [Product]
private var dishes: [DishCategory: [Product]] = [:]

init() {
self.products = MockProductFactory(makeCount: 10).makeRandomProducts()
}
}
extension MockProductRepository: ProductRepository{

func fetchImage(completion: @escaping (Result<[DishCategory : [Data]] , ProductRepositoryError>) -> Void) {
var imageData: [DishCategory : [Data]] = [:]
let group = DispatchGroup()
for product in products{
guard let index = dishes[product.category]?.firstIndex(where: { factor in
if factor.id == product.id{
return true
}
return false
}) else { return }
group.enter()
guard let url = URL(string: product.imageUrl) else { return }
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data else {
completion(.failure(.fetchError))
return
}
if imageData[product.category] == nil{
guard let count = self.dishes[product.category]?.count else { return }
imageData[product.category] = [Data](repeating: Data(), count: count)
}
imageData[product.category]?[Int(exactly: index)!] = data
group.leave()
}.resume()
}
let queueForGroup = DispatchQueue(label: "endQueue", attributes: .concurrent)
group.notify(queue: queueForGroup) {
completion(.success(imageData))
}
}

private func makeAllDishes(){
dishes[.main] = products.filter{$0.category == .main}
dishes[.soup] = products.filter{$0.category == .soup}
dishes[.side] = products.filter{$0.category == .side}
}

func fetchAll(completion: @escaping (Result<[Product], ProductRepositoryError>) -> Void) {
completion(.success(products))
}
func fetchAll(completion: @escaping (Result<[DishCategory : [Product]], ProductRepositoryError>) -> Void) {
makeAllDishes()
completion(.success(dishes))
}


func fetchList(by category: DishCategory, completion: @escaping (Result<[Product], ProductRepositoryError>) -> Void){
let filteredResults = self.products.filter{ $0.category == category }
completion(.success(filteredResults))
}


}
20 changes: 17 additions & 3 deletions SideDishProject/SideDishProject/usecase/HomeUsecase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ final class HomeUsecase{
guard let self = self else { return }
switch result{
case .success(let products):
self.makeAllDishes(products: products)
self.delegate?.updateDishComment(comments: [DishCategory.main.comment, DishCategory.soup.comment, DishCategory.side.comment])
self.delegate?.updateAllDishes(dishes: products)
self.updateImageData()
case .failure(let error):
self.delegate?.updateFail(error: error)
}
Expand All @@ -40,14 +42,26 @@ final class HomeUsecase{
dishes[.main] = products.filter{$0.category == .main}
dishes[.soup] = products.filter{$0.category == .soup}
dishes[.side] = products.filter{$0.category == .side}
delegate?.updateAllDishes(dishes: dishes)
delegate?.updateDishComment(comments: [DishCategory.main.comment, DishCategory.soup.comment, DishCategory.side.comment])


}

private func updateImageData(){
repository.fetchImage { result in
switch result{
case .success(let data):
self.delegate?.updateImageData(imageData: data)
case .failure(let error):
self.delegate?.updateFail(error: error)
}
}
}
}

protocol ProductModelDelegate: AnyObject{
func updateDishComment(comments: [String])
func updateAllDishes(dishes: [DishCategory : [Product]])
func updateFail(error: Error)
func updateImageData(imageData: [DishCategory : [Data]])
func selected(id: UniqueID)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Foundation
struct MockProductFactory{
private let randomNames: [DishCategory : [String]] = [.main : ["삼겹살", "김치볶음밥", "제육볶음"], .soup : ["김치찌게", "된장찌게", "미역국"], .side : ["멸치볶음", "", "콩자반"]]
private let randomDiscriptions: [String] = ["ebony가 직접 만들었습니다." , "rosa가 직접 만들었습니다.", "구찌가 극찬하였습니다.", "데일이 먹고 울었습니다."]
private let dishImageURLs: [String] = ["http://public.codesquad.kr/jk/storeapp/data/main/510_ZIP_P_0047_T.jpg","http://public.codesquad.kr/jk/storeapp/data/main/510_ZIP_P_0047_D1.jpg", "http://public.codesquad.kr/jk/storeapp/data/main/510_ZIP_P_0047_D2.jpg", "http://public.codesquad.kr/jk/storeapp/data/main/510_ZIP_P_0047_D3.jpg"]
private var events: [ProductEvent] = [.eventPrice(disCount: 0.15), .launchingPrice(disCount: 0.2), .none(disCount: 1.0)]
private let makeCount: Int

Expand All @@ -33,7 +34,13 @@ struct MockProductFactory{
let stock = randomStock()
let name = randomName(cateogory: category)
let productEvents = randomEvents()
return Product(name: name, price: price, description: discription, stock: stock, category: category, event: productEvents)
let imageUrl = randomImageUrl()
return Product(name: name, price: price, description: discription, stock: stock, category: category, imageUrl: imageUrl, event: productEvents)
}

private func randomImageUrl() -> String{
guard let url = dishImageURLs.randomElement() else { return "" }
return url
}

private func randomEvents() -> [ProductEvent]{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
import UIKit

final class DishCollectionDataSource: NSObject{

private var dishes: [DishCategory : [Product]] = [:]
private var dishComments: [String] = []
private var dishImages: [DishCategory : [Data]] = [:]
func setDishes(dishes: [DishCategory : [Product]]){
self.dishes = dishes
}

func setDishImages(images: [DishCategory : [Data]]){
self.dishImages = images
}

func setDishComments(dishComments: [String]){
self.dishComments = dishComments
}
Expand All @@ -32,6 +36,10 @@ extension DishCollectionDataSource: UICollectionViewDataSource{
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: DishCollectionViewCell.identifier, for: indexPath) as? DishCollectionViewCell else { return UICollectionViewCell()}
guard let product = dishes[DishCategory.allCases[indexPath.section]]?[indexPath.item] else { return cell }
cell.updateUIProperty(with: product)
if !(dishImages.isEmpty){
guard let data = dishImages[DishCategory.dishKind(section: indexPath.section)]?[indexPath.row] else { return cell }
cell.updateImage(data: data)
}
return cell
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,8 @@ final class DishCollectionViewCell: UICollectionViewCell{
}
isContainEvent ? setNonEventUIConstraint() : setEventUIConstraint()
}

func updateImage(data: Data){
dishImageView.image = UIImage(data: data)
}
}
6 changes: 6 additions & 0 deletions SideDishProject/SideDishProject/view/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,10 @@ final class HomeView: UIView{
collectionView.dataSource = dataSource
collectionView.delegate = delegate
}

func collectionViewReloadData(){
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
}
5 changes: 5 additions & 0 deletions SideDishProject/SideDishProject/view/HomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ final class HomeViewController: UIViewController {

}
extension HomeViewController: ProductModelDelegate{
func updateImageData(imageData: [DishCategory : [Data]] ) {
dishCollectionDataSource.setDishImages(images: imageData)
homeView.collectionViewReloadData()
}

func selected(id: UniqueID) {
presentDetailViewController(uniqueId: id)
}
Expand Down

0 comments on commit 9927984

Please sign in to comment.