Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json 업로드 로직 수정 #161

Open
bdrsky2010 opened this issue Mar 17, 2024 · 5 comments
Open

Json 업로드 로직 수정 #161

bdrsky2010 opened this issue Mar 17, 2024 · 5 comments
Assignees
Milestone

Comments

@bdrsky2010
Copy link
Contributor

bdrsky2010 commented Mar 17, 2024

DrinkUploadViewModel.swift

  • 외부에서 호출되지 않는 메서드 private으로 변경
  • RawDrink protocol 추가
protocol RawDrink: Codable {
	var category: String { get }
	var type: String { get }
	var name: String { get }
	var amount: String { get }
	var price: Int { get }
	var alcohol: Double { get }
	var country: String { get }
}
  • 각 Raw 모델에 RawDrink protocol 채택 및 private struct로 변경
private struct RawBeer: RawDrink {}
private struct RawTraditional: RawDrink {}
private struct RawWine: RawDrink {}
private struct RawWhiskey: RawDrink {}
  • DrinkJsonType private enum 으로 변경
private enum DrinkJsonType: CaseIterable {}
  • 모든 이미지 fetch 함수 작성
private func fetchImagesURL<T: RawDrink>(drinkData: [T], imagesURL: inout [URL?]) async {
	await withTaskGroup(of: (Int, URL?).self) { group in
		for (index, drink) in drinkData.enumerated() {
			group.addTask {
				do {
					// TODO: get image file name
					let url = try await self.fireStorageService.fetchImageURL(folder: .drink, fileName: "")
					return (index, url)
				} catch {
					print("error :: fetchImageURL", error.localizedDescription)
					return (index, nil)
				}
			}
		}
		
		var result = [(Int, URL?)]()
		
		for await downloadURL in group {
			result.append(downloadURL)
		}
		imagesURL = result.sorted(by: { $0.0 < $1.0 }).map { $0.1 }
	}
}
  • 위 메서드 호출하는 부분
private func mapFirebaseDrink(drinkType: DrinkJsonType) async -> [DrinkField]? {
	guard let jsonData = jsonLoad(drinkType: drinkType) else { return nil }
		
	var imagesURL = [URL?]()
		
	switch drinkType {
	case .beer:
		guard let drinkData = try? JSONDecoder().decode([RawBeer].self, from: jsonData) else {
			print("error decoding \(drinkType)")
			return nil
		}
		await fetchImagesURL(drinkData: drinkData, imagesURL: &imagesURL)
		return drinkData.enumerated().map {
			return DrinkField(drinkImageURL: imagesURL[$0.offset], category: $0.element.category, type: $0.element.type,
							  name: $0.element.name, amount: $0.element.amount, price: $0.element.price, alcohol: $0.element.alcohol,
							  country: $0.element.country, province: nil, aroma: $0.element.aroma, taste: $0.element.taste,
							  finish: $0.element.finish, sweet: nil, sour: nil, refresh: nil, body: nil,
							  carbonated: nil, material: nil, wellMatched: $0.element.wellMatched, rating: 0)
			} // ...
@bdrsky2010 bdrsky2010 added this to the Firebase milestone Mar 17, 2024
@bdrsky2010 bdrsky2010 self-assigned this Mar 17, 2024
@bdrsky2010
Copy link
Contributor Author

사진 이미지 파일 이름 get 메서드 작성 필요!

bdrsky2010 added a commit that referenced this issue Mar 17, 2024
bdrsky2010 added a commit that referenced this issue Mar 17, 2024
@bdrsky2010
Copy link
Contributor Author

DrinkUploadViewModel.swift

  • inout 방식에서 return 받는 방식으로 변경
private func fetchImagesURL<T: RawDrink>(drinkData: [T], imagesURL: inout [URL?]) async {
	await withTaskGroup(of: (Int, URL?).self) { group in
		for (index, drink) in drinkData.enumerated() {
			group.addTask {
				do {
					// TODO: get image file name
					let url = try await self.fireStorageService.fetchImageURL(folder: .drink, fileName: "")
					return (index, url)
				} catch {
					print("error :: fetchImageURL", error.localizedDescription)
					return (index, nil)
				}
			}
		}
		
		var result = [(Int, URL?)]()
		
		for await downloadURL in group {
			result.append(downloadURL)
		}
		imagesURL = result.sorted(by: { $0.0 < $1.0 }).map { $0.1 }
	}
}
private func fetchImagesURL<T: RawDrink>(drinkData: [T]) async -> [URL?] {
	await withTaskGroup(of: (Int, URL?).self) { group in
		for (index, drink) in drinkData.enumerated() {
			group.addTask {
				do {
					// TODO: get image file name
					let url = try await self.fireStorageService.fetchImageURL(folder: .drink, fileName: "")
					return (index, url)
				} catch {
					print("error :: fetchImageURL", error.localizedDescription)
					return (index, nil)
				}
			}
		}
		
		var result = [(Int, URL?)]()
		
		for await downloadURL in group {
			result.append(downloadURL)
		}
		return result.sorted(by: { $0.0 < $1.0 }).map { $0.1 }
	}
}

@bdrsky2010
Copy link
Contributor Author

bdrsky2010 commented Mar 18, 2024

DrinkUploadViewModel.swift

  • JsonDrinkType에서 DrinkType으로 변환하는 메서드 추가
private func getDrinkType(drinkJsonType: DrinkJsonType) -> DrinkType {
	switch drinkJsonType {
	case .beer:
		return .beer
	case .wine:
		return .wine
	case .traditional:
		return .traditional
	case .whiskey:
		return .whiskey
	}
}
  • fetchImageURL 메서드에 위 메서드 추가
private func fetchImagesURL<T: RawDrink>(drinkJsonType: DrinkJsonType, drinkData: [T]) async -> [URL?] {
        let category = getDrinkType(drinkJsonType: drinkJsonType)

	return await withTaskGroup(of: (Int, URL?).self) { group in
		for (index, drink) in drinkData.enumerated() {
			group.addTask {
				do {
					if let fileName = Formatter.getImageName(category: category, detailedCategory: drink.type) {
						let url = try await self.fireStorageService.fetchImageURL(folder: .drink, fileName: "")
						return (index, url)
					} else {
						print("error :: getImageName")
						return (index, nil)
					}
				} catch {
					print("error :: fetchImageURL", error.localizedDescription)
					return (index, nil)
				}
			}
		}
		
		var result = [(Int, URL?)]()
		
		for await downloadURL in group {
			result.append(downloadURL)
		}
		return result.sorted(by: { $0.0 < $1.0 }).map { $0.1 }
	}
}

@bdrsky2010
Copy link
Contributor Author

사진 이미지 파일 이름 get 메서드 작성 필요!

작성완료

@bdrsky2010
Copy link
Contributor Author

  • 각 case에 적합하지 맞지않는 return 값을 갖고 있어 수정.
private enum DrinkJsonType: CaseIterable {
	case beer, wine, traditional, whiskey
	
	var jsonName: String {
		switch self {
		case .beer:
			return "BeerTest"
		case .traditional:
			return "TraditionalTest"
		case .whiskey:
			return "WhiskeyTest"
		case .wine:
			return "WineTest"
		}
	}
}

bdrsky2010 added a commit that referenced this issue Mar 21, 2024
[Edit] enum DrinkJsonType case별로 맞지 않은 return 값을 갖고 있어 수정 #161
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant