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

쥬스메이커 [STEP 2] 제이티, Ari #119

Merged
merged 17 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
68927c4
feat: 쥬스 주문버튼 터치 시 Alret 표시되는 기능 구현
leeari95 Oct 25, 2021
c8f2ac6
feat: 주문 버튼 터치시 재고가 있는 경우와 없는 경우에 대한 기능 구현
HoneyCoding Oct 25, 2021
e083fff
feat: 과일의 재고가 바뀔 때 마다 화면에 반영되도록 기능 구현
leeari95 Oct 25, 2021
293f811
refactor: HIG 지침에 따라 Alert의 선택지 문구 수정
HoneyCoding Oct 26, 2021
a560ab2
refactor: Fruit타입을 FruitStore 내부에서 외부로 이동
leeari95 Oct 26, 2021
57031d7
refactor: JuiceMakerViewController의 UILabel 프로퍼티 이름 수정
HoneyCoding Oct 26, 2021
eb48bef
refactor: JuiceMakerViewController 내부 메서드명 수정
leeari95 Oct 26, 2021
0331594
refactor: JuiceMakerViewController 내부 메소드 순서 변경
HoneyCoding Oct 26, 2021
8edd150
refactor: orderJuiceButtonTapped 메서드 분리 및 리팩토링
leeari95 Oct 26, 2021
b28fab2
chore: 불필요한 주석 제거
HoneyCoding Oct 26, 2021
5d1c1da
chore: mixFruit 메서드 내부에 에러 문구를 수정
leeari95 Oct 26, 2021
f7bb20f
docs: README.md 파일 내용 추가
HoneyCoding Oct 26, 2021
8bfece8
refactor: 문자열을 모아놓은 enum Text 추가 및 숫자를 문자열로 형변환 하는 코드 수정
HoneyCoding Oct 28, 2021
5f09d5c
refactor: Juice 타입 리팩토링 및 Text 타입 이름 수정
leeari95 Oct 28, 2021
d252b60
refactor: Alert 버튼의 타이틀을 따로 Text 타입으로 구현
HoneyCoding Oct 28, 2021
0484cd1
refactor: FruitStore 내부에 전역변수를 enum Const 타입으로 리팩토링
leeari95 Oct 28, 2021
494468c
refactor: JuiceMakerViewController 내부의 showOutOfStockAlert 메소드의 코드 일부…
HoneyCoding Oct 28, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: 과일의 재고가 바뀔 때 마다 화면에 반영되도록 기능 구현
- Notification.Name changedFruitStockNotification 추가
- FruitStore 타입 내부에 changeAmount 메서드에 Notification 발송 기능 추가
- JuiceMakerViewController 내부에 Label들을 업데이트 할 수 있도록 여러 메서드 추가
  • Loading branch information
leeari95 committed Oct 25, 2021
commit e083fff4f2a9d7a72e49f2996696c955254c266d
56 changes: 55 additions & 1 deletion JuiceMaker/JuiceMaker/Controller/JuiceMakerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,62 @@ class JuiceMakerViewController: UIViewController {

private let juiceMaker = JuiceMaker()

@IBOutlet weak var strawberryStock: UILabel!
@IBOutlet weak var bananaStock: UILabel!
@IBOutlet weak var pineappleStock: UILabel!
@IBOutlet weak var kiwiStock: UILabel!
@IBOutlet weak var mangoStock: UILabel!


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
uploadStocks()

NotificationCenter.default.addObserver(self, selector: #selector(updateFruitStock(notification:)), name: .changedFruitStockNotification, object: nil)
}

func uploadStocks() {
currentStockLabel(fruit: .strawberry, label: strawberryStock)
currentStockLabel(fruit: .banana, label: bananaStock)
currentStockLabel(fruit: .pineapple, label: pineappleStock)
currentStockLabel(fruit: .kiwi, label: kiwiStock)
currentStockLabel(fruit: .mango, label: mangoStock)
}


func currentStockLabel(fruit: FruitStore.Fruit, label: UILabel) {
do {
let stock = try FruitStore.shared.stock(fruit: fruit)
label.text = String(stock)
} catch let error as RequestError {
notificationAlert(message: error.errorDescription)
} catch {
notificationAlert(message: "알 수 없는 에러가 발생했습니다.")
}
}

@objc func updateFruitStock(notification: Notification) {
// 수정된 과일과 과일의 재고갯수를 받아와서 label을 수정
guard let fruit = notification.object as? FruitStore.Fruit else {
notificationAlert(message: "알 수 없는 에러가 발생했습니다.")
return
}
currentStockLabel(fruit: fruit, label: fruitlabel(of: fruit))
}

func fruitlabel(of fruit: FruitStore.Fruit) -> UILabel {
switch fruit {
case .strawberry:
return strawberryStock
case .banana:
return bananaStock
case .pineapple:
return pineappleStock
case .kiwi:
return kiwiStock
case .mango:
return mangoStock
}
}

@IBAction func orderJuiceButtonTapped(_ sender: UIButton) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

모든 button 의 action method 를 개별로 구현할 때와 이렇게 하나에 구현할 때의 장단점은 무엇인가요?

Expand Down Expand Up @@ -41,6 +94,7 @@ class JuiceMakerViewController: UIViewController {

do {
try juiceMaker.mixFruit(juice: juice)
notificationAlert(message: "\(juice) 쥬스 나왔습니다! 맛있게 드세요!")
} catch RequestError.fruitStockOut {
outOfStockAlert()
} catch let error as RequestError {
Expand Down
12 changes: 12 additions & 0 deletions JuiceMaker/JuiceMaker/Model/FruitStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ class FruitStore {
self.fruitBasket = Dictionary(uniqueKeysWithValues: zip(allFruits, fruitscount))
}

func stock(fruit: Fruit) throws -> Int {
guard let fruitStock = fruitBasket[fruit] else {
throw RequestError.fruitNotFound
}
return fruitStock
}

func addFruitStock(fruit: Fruit, count: Int) throws {
try changeAmount(count: count, of: fruit, by: +)
}
Expand All @@ -49,6 +56,8 @@ class FruitStore {
}
let newFruitCount = calculator(oldFruitCount, count)
fruitBasket[fruit] = newFruitCount

NotificationCenter.default.post(name: .changedFruitStockNotification, object: fruit)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NotificationCenter 를 사용해보셨군요
근데 stock 이 줄어들 때는 이벤트 알림을 안받나요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아뇨 줄어들때도 알림을 받습니다!
subtractFruitStock, addFruitStock 메서드가 changeAmount 메서드를 활용하여 stock을 추가하거나 빼는 일을 하고 있습니다😁

}

func hasFruitStock(of fruit: Fruit, count fruitCountSubtracted: Int) -> Bool {
Expand All @@ -58,3 +67,6 @@ class FruitStore {
}


extension Notification.Name {
static let changedFruitStockNotification = Notification.Name("changeFruitStock")
}
7 changes: 7 additions & 0 deletions JuiceMaker/JuiceMaker/View/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,13 @@
</connections>
</barButtonItem>
</navigationItem>
<connections>
<outlet property="bananaStock" destination="gvk-pA-Lw5" id="I4h-3W-xpD"/>
<outlet property="kiwiStock" destination="FZq-de-TJG" id="yU2-Ay-SC3"/>
<outlet property="mangoStock" destination="3Ce-SU-JeH" id="eXr-g0-l78"/>
<outlet property="pineappleStock" destination="ccQ-Dk-PuY" id="lFl-eM-TyK"/>
<outlet property="strawberryStock" destination="Qas-vP-td6" id="wPb-it-h37"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
Expand Down