-
Notifications
You must be signed in to change notification settings - Fork 166
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 3] 제이티, Ari #135
Changes from 16 commits
3d2a6da
1e85463
df7531a
23a4bdc
febd113
bd2ac42
9b545cf
e71fa4f
b9d4c03
4864c3c
93bf54e
484a0a1
2b397db
1fec18c
da64fc6
ccb8a10
ae7582e
3a3c801
82c2aa5
119b6d6
288fe58
d8f244d
ba4de9c
6a7ae09
dc428fe
981c869
abef023
827e98b
4809668
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,13 +7,112 @@ | |
|
||
import UIKit | ||
|
||
// MARK: - Properties and Lifecycle, Transition | ||
class FruitStoreViewController: UIViewController { | ||
|
||
@IBOutlet weak var strawberryStockLabel: UILabel! | ||
@IBOutlet weak var bananaStockLabel: UILabel! | ||
@IBOutlet weak var pineappleStockLabel: UILabel! | ||
@IBOutlet weak var kiwiStockLabel: UILabel! | ||
@IBOutlet weak var mangoStockLabel: UILabel! | ||
|
||
@IBOutlet weak var strawberryStockStepper: UIStepper! | ||
@IBOutlet weak var bananaStockStepper: UIStepper! | ||
@IBOutlet weak var pineappleStockStepper: UIStepper! | ||
@IBOutlet weak var kiwiStockStepper: UIStepper! | ||
@IBOutlet weak var mangoStockStepper: UIStepper! | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
setUpFruitStepperValues() | ||
} | ||
|
||
@IBAction func cancelButtonTapped(_ sender: UIButton) { | ||
self.dismiss(animated: true, completion: nil) | ||
} | ||
} | ||
|
||
// MARK: - Label and Stepper Values Setting | ||
extension FruitStoreViewController { | ||
@IBAction func stepperTapped(_ sender: UIStepper) { | ||
switch sender { | ||
case strawberryStockStepper: | ||
strawberryStockLabel.text = fruitLabelText(value: sender.value) | ||
fruitStockChange(fruit: .strawberry, value: sender.value) | ||
case bananaStockStepper: | ||
bananaStockLabel.text = fruitLabelText(value: sender.value) | ||
fruitStockChange(fruit: .banana, value: sender.value) | ||
case pineappleStockStepper: | ||
pineappleStockLabel.text = fruitLabelText(value: sender.value) | ||
fruitStockChange(fruit: .pineapple, value: sender.value) | ||
case kiwiStockStepper: | ||
kiwiStockLabel.text = fruitLabelText(value: sender.value) | ||
fruitStockChange(fruit: .kiwi, value: sender.value) | ||
case mangoStockStepper: | ||
mangoStockLabel.text = fruitLabelText(value: sender.value) | ||
fruitStockChange(fruit: .mango, value: sender.value) | ||
default: | ||
showNotificationAlert(message: Message.unknownError.description) | ||
} | ||
} | ||
|
||
func fruitLabelText(value: Double) -> String { | ||
return String(format: "%.0f", value) | ||
} | ||
|
||
func setUpFruitStepperValues() { | ||
currentStockStepperValueUpdate(fruit: .strawberry, stepper: strawberryStockStepper) | ||
currentStockStepperValueUpdate(fruit: .banana, stepper: bananaStockStepper) | ||
currentStockStepperValueUpdate(fruit: .pineapple, stepper: pineappleStockStepper) | ||
currentStockStepperValueUpdate(fruit: .kiwi, stepper: kiwiStockStepper) | ||
currentStockStepperValueUpdate(fruit: .mango, stepper: mangoStockStepper) | ||
} | ||
|
||
func currentStockStepperValueUpdate(fruit: Fruit, stepper: UIStepper) { | ||
do { | ||
let stock = try FruitStore.shared.stock(fruit: fruit) | ||
stepper.value = Double(stock) | ||
} catch { | ||
showNotificationAlert(message: Message.unknownError.description) | ||
} | ||
} | ||
} | ||
|
||
// MARK: Stepper Operation | ||
extension FruitStoreViewController { | ||
func fruitStockChange(fruit: Fruit, value: Double) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기도 네이밍 동사로 변경하면 어떨까요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 수정완료했습니다!! 감사합니다! |
||
let oldStockValue: Int | ||
let newStockValue = Int(value) | ||
do { | ||
oldStockValue = try FruitStore.shared.stock(fruit: fruit) | ||
chooseCalculator(fruit: fruit, oldStockValue: oldStockValue, newStockValue: newStockValue) | ||
} catch { | ||
showNotificationAlert(message: Message.unknownError.description) | ||
} | ||
} | ||
|
||
func chooseCalculator(fruit: Fruit, oldStockValue: Int, newStockValue: Int) { | ||
switch oldStockValue < newStockValue { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 로직은 if 로 해도 되지 않나요? switch 를 사용한 이유는요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. switch로 사용한 이유는 if보다 좀더 좋아보여서 사용했는데, 다시 생각을 나눠보니까 if로 해도 상관없다고 판단되서 수정해주었습니다! |
||
case true: | ||
let count = newStockValue - oldStockValue | ||
calculateFruitStock(fruit: fruit, count: count, calculate: FruitStore.shared.addFruitStock) | ||
case false: | ||
let count = oldStockValue - newStockValue | ||
calculateFruitStock(fruit: fruit, count: count, calculate: FruitStore.shared.subtractFruitStock) | ||
} | ||
} | ||
|
||
func calculateFruitStock(fruit: Fruit, count: Int, calculate: (Fruit, Int) throws -> Void ) { | ||
do { | ||
try calculate(fruit, count) | ||
} catch { | ||
showNotificationAlert(message: Message.unknownError.description) | ||
} | ||
} | ||
|
||
func showNotificationAlert(message: String, title: String = Text.ok.title) { | ||
let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert) | ||
let cancel = UIAlertAction(title: title, style: .cancel, handler: nil) | ||
alert.addAction(cancel) | ||
present(alert, animated: true, completion: nil) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,8 @@ | |
|
||
import UIKit | ||
|
||
// MARK: - Properties and Lifecycle | ||
class JuiceMakerViewController: UIViewController { | ||
|
||
private let juiceMaker = JuiceMaker() | ||
|
||
@IBOutlet weak var strawberryStockLabel: UILabel! | ||
|
@@ -27,10 +27,69 @@ class JuiceMakerViewController: UIViewController { | |
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
updateFruitLabels() | ||
|
||
buttonLabelFontSizeFix() | ||
|
||
NotificationCenter.default.addObserver(self, selector: #selector(fruitLabelChanged(notification:)), name: .changedFruitStockNotification, object: nil) | ||
} | ||
} | ||
|
||
// MARK: - Setup Label and Button | ||
extension JuiceMakerViewController { | ||
func buttonLabelFontSizeFix() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 동작은 스토리보드에서 하는 방법을 찾지 못했어요. 코드로 밖에 설정할 수 없겠다고 판단하고 코드로 작업해주었습니다. 🥲 |
||
orderStrawberryBananaJuiceButton.titleLabel?.adjustsFontForContentSizeCategory = true | ||
orderMangoKiwiJuiceButton.titleLabel?.adjustsFontForContentSizeCategory = true | ||
orderStrawberryJuiceButton.titleLabel?.adjustsFontForContentSizeCategory = true | ||
orderBananaJuiceButton.titleLabel?.adjustsFontForContentSizeCategory = true | ||
orderPineappleJuiceButton.titleLabel?.adjustsFontForContentSizeCategory = true | ||
orderKiwiJuiceButton.titleLabel?.adjustsFontForContentSizeCategory = true | ||
orderMangoJuiceButton.titleLabel?.adjustsFontForContentSizeCategory = true | ||
|
||
orderStrawberryBananaJuiceButton.titleLabel?.adjustsFontSizeToFitWidth = true | ||
orderMangoKiwiJuiceButton.titleLabel?.adjustsFontSizeToFitWidth = true | ||
orderStrawberryJuiceButton.titleLabel?.adjustsFontSizeToFitWidth = true | ||
orderBananaJuiceButton.titleLabel?.adjustsFontSizeToFitWidth = true | ||
orderPineappleJuiceButton.titleLabel?.adjustsFontSizeToFitWidth = true | ||
orderKiwiJuiceButton.titleLabel?.adjustsFontSizeToFitWidth = true | ||
orderMangoJuiceButton.titleLabel?.adjustsFontSizeToFitWidth = true | ||
} | ||
|
||
func updateFruitLabels() { | ||
currentStockLabelUpdate(fruit: .strawberry, label: strawberryStockLabel) | ||
currentStockLabelUpdate(fruit: .banana, label: bananaStockLabel) | ||
currentStockLabelUpdate(fruit: .pineapple, label: pineappleStockLabel) | ||
currentStockLabelUpdate(fruit: .kiwi, label: kiwiStockLabel) | ||
currentStockLabelUpdate(fruit: .mango, label: mangoStockLabel) | ||
} | ||
|
||
func currentStockLabelUpdate(fruit: Fruit, label: UILabel) { | ||
do { | ||
let stock = try FruitStore.shared.stock(fruit: fruit) | ||
label.text = stock.description | ||
} catch let error as RequestError { | ||
showNotificationAlert(message: error.errorDescription) | ||
} catch { | ||
showNotificationAlert(message: Message.unknownError.description) | ||
} | ||
} | ||
|
||
func fruitlabel(of fruit: Fruit) -> UILabel { | ||
switch fruit { | ||
case .strawberry: | ||
return strawberryStockLabel | ||
case .banana: | ||
return bananaStockLabel | ||
case .pineapple: | ||
return pineappleStockLabel | ||
case .kiwi: | ||
return kiwiStockLabel | ||
case .mango: | ||
return mangoStockLabel | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Order Juice | ||
extension JuiceMakerViewController { | ||
@IBAction func orderJuiceButtonTapped(_ sender: UIButton) { | ||
let juice: Juice | ||
|
||
|
@@ -70,48 +129,6 @@ class JuiceMakerViewController: UIViewController { | |
} | ||
} | ||
|
||
@objc func fruitLabelChanged(notification: Notification) { | ||
guard let fruit = notification.object as? Fruit else { | ||
showNotificationAlert(message: Message.unknownError.description) | ||
return | ||
} | ||
currentStockLabelUpdate(fruit: fruit, label: fruitlabel(of: fruit)) | ||
} | ||
|
||
func updateFruitLabels() { | ||
currentStockLabelUpdate(fruit: .strawberry, label: strawberryStockLabel) | ||
currentStockLabelUpdate(fruit: .banana, label: bananaStockLabel) | ||
currentStockLabelUpdate(fruit: .pineapple, label: pineappleStockLabel) | ||
currentStockLabelUpdate(fruit: .kiwi, label: kiwiStockLabel) | ||
currentStockLabelUpdate(fruit: .mango, label: mangoStockLabel) | ||
} | ||
|
||
func currentStockLabelUpdate(fruit: Fruit, label: UILabel) { | ||
do { | ||
let stock = try FruitStore.shared.stock(fruit: fruit) | ||
label.text = stock.description | ||
} catch let error as RequestError { | ||
showNotificationAlert(message: error.errorDescription) | ||
} catch { | ||
showNotificationAlert(message: Message.unknownError.description) | ||
} | ||
} | ||
|
||
func fruitlabel(of fruit: Fruit) -> UILabel { | ||
switch fruit { | ||
case .strawberry: | ||
return strawberryStockLabel | ||
case .banana: | ||
return bananaStockLabel | ||
case .pineapple: | ||
return pineappleStockLabel | ||
case .kiwi: | ||
return kiwiStockLabel | ||
case .mango: | ||
return mangoStockLabel | ||
} | ||
} | ||
|
||
func showNotificationAlert(message: String, title: String = Text.ok.title) { | ||
let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert) | ||
let cancel = UIAlertAction(title: title, style: .cancel, handler: nil) | ||
|
@@ -127,10 +144,40 @@ class JuiceMakerViewController: UIViewController { | |
alert.addAction(okAction) | ||
present(alert, animated: true, completion: nil) | ||
} | ||
|
||
} | ||
|
||
// MARK: - Transition | ||
extension JuiceMakerViewController { | ||
private func presentFruitStoreViewController(_ action: UIAlertAction) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 파라미터로 action 은 왜 받는건가요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let okAction = UIAlertAction(title: Text.ok.title, style: .default, handler: presentFruitStoreViewController)
private func presentFruitStoreViewController(_ action: UIAlertAction) {
// 내부 코드 구현
}
let okAction = UIAlertAction(title: Text.ok.title, style: .default) { action in
presentFruitStoreViewController()
}
private func presentFruitStoreViewController() {
// 내부 코드 구현
} |
||
guard let viewController = self.storyboard?.instantiateViewController(withIdentifier: "FruitStoreViewController") else { return } | ||
guard let viewController = self.storyboard?.instantiateViewController(withIdentifier: "FruitStoreViewController") as? UINavigationController else { return } | ||
self.present(viewController, animated: true, completion: nil) | ||
guard let nextViewController = viewController.topViewController as? FruitStoreViewController else { return } | ||
setupNextViewLabel(of: nextViewController) | ||
} | ||
|
||
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { | ||
guard let navigationController = segue.destination as? UINavigationController else { return } | ||
guard let nextViewController = navigationController.topViewController as? FruitStoreViewController else { return } | ||
setupNextViewLabel(of: nextViewController) | ||
} | ||
|
||
func setupNextViewLabel(of nextViewController: FruitStoreViewController) { | ||
nextViewController.loadViewIfNeeded() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
nextViewController.strawberryStockLabel.text = strawberryStockLabel.text | ||
nextViewController.bananaStockLabel.text = bananaStockLabel.text | ||
nextViewController.pineappleStockLabel.text = pineappleStockLabel.text | ||
nextViewController.kiwiStockLabel.text = kiwiStockLabel.text | ||
nextViewController.mangoStockLabel.text = mangoStockLabel.text | ||
} | ||
} | ||
|
||
// MARK: - Notification | ||
extension JuiceMakerViewController { | ||
@objc func fruitLabelChanged(notification: Notification) { | ||
guard let fruit = notification.object as? Fruit else { | ||
showNotificationAlert(message: Message.unknownError.description) | ||
return | ||
} | ||
currentStockLabelUpdate(fruit: fruit, label: fruitlabel(of: fruit)) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
method 는 동사로 시작하는게 좋아요. update로 시작하면 어떨까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
앗... 리마인드 해야겠네요.. 메소드는 동사로 🥲
수정완료했습니다!