Skip to content

Commit

Permalink
Percentage label now highlights with emoji button tap
Browse files Browse the repository at this point in the history
  • Loading branch information
marlonjames71 committed Nov 29, 2019
1 parent 7631b67 commit fc01916
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tipsy/Resources/Colors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ extension UIColor {
static let darkJungleGreen = UIColor(red: 0.12, green: 0.12, blue: 0.12, alpha: 1.00)
static let canCan = UIColor(red: 0.83, green: 0.52, blue: 0.61, alpha: 1.00)
static let canCanTwo = UIColor(red: 0.73, green: 0.42, blue: 0.61, alpha: 1.00)
static let tipsyDarkerAccents = UIColor(named: "darkerAccents")!
static let tipsySecondaryLabelColor = UIColor(named: "tipsySecondaryLabelColor")!
}
31 changes: 31 additions & 0 deletions tipsy/ViewControllers/TipViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class TipViewController: UIViewController, UITextFieldDelegate {
}
}

lazy var emojiButtons = [firstEmoji, secondEmoji, thirdEmoji, fourthEmoji].compactMap { $0 }
lazy var percentLabels = [twoPercentLabel, fifteenPercentLabel, twentyPercentLabel, twentyFivePercentLabel].compactMap { $0 }

// MARK: - Outlets (In order on screen)

@IBOutlet weak var tipsyTitleLabel: UILabel!
Expand Down Expand Up @@ -79,6 +82,7 @@ class TipViewController: UIViewController, UITextFieldDelegate {
updateResetButtonEnabled()
screenEdgeGestureRecognizer.edges = .right
HapticFeedback.lightFeedback.prepare()
resetHighlightTipPercentLabels()


let toolbar: UIToolbar = UIToolbar(frame: .zero)
Expand Down Expand Up @@ -149,26 +153,31 @@ class TipViewController: UIViewController, UITextFieldDelegate {
@IBAction func resetButtonTapped(_ sender: UIButton) {
clear()
calculatedTipPercentage = "20"
resetHighlightTipPercentLabels()
}

@IBAction func firstEmojiTapped(_ sender: UIButton) {
HapticFeedback.produceLightFeedback()
calculatedTipPercentage = "2"
highlightLabelForButton(button: sender)
}

@IBAction func secondEmojiTapped(_ sender: UIButton) {
HapticFeedback.produceLightFeedback()
calculatedTipPercentage = "15"
highlightLabelForButton(button: sender)
}

@IBAction func thirdEmojiTapped(_ sender: UIButton) {
HapticFeedback.produceLightFeedback()
calculatedTipPercentage = "20"
highlightLabelForButton(button: sender)
}

@IBAction func fourthEmojiTapped(_ sender: UIButton) {
HapticFeedback.produceLightFeedback()
calculatedTipPercentage = "25"
highlightLabelForButton(button: sender)
}

@IBAction func totalDidChange(_ sender: UITextField) {
Expand All @@ -184,6 +193,7 @@ class TipViewController: UIViewController, UITextFieldDelegate {
}

@IBAction func tipFieldDidChange(_ sender: UITextField) {
percentLabels.forEach { $0.textColor = .tipsySecondaryLabelColor }
tipErrorLabel.isHidden = true
editingTipPercentage = true
calculatedTipPercentage = sender.text ?? ""
Expand Down Expand Up @@ -261,6 +271,27 @@ class TipViewController: UIViewController, UITextFieldDelegate {
}
}

private func highlightLabelForButton(button: UIButton) {
guard let buttonIndex = emojiButtons.firstIndex(of: button) else { return }
let label = percentLabels[buttonIndex]
highlightPercentLabel(label: label)
}

private func highlightPercentLabel(label: UILabel) {
percentLabels.forEach { $0.textColor = .tipsySecondaryLabelColor }
label.textColor = .tipsyDarkerAccents
}

private func resetHighlightTipPercentLabels() {
for label in percentLabels {
if label != twentyPercentLabel {
label.textColor = .tipsySecondaryLabelColor
} else {
label.textColor = .tipsyDarkerAccents
}
}
}

private func showSplitPlatter() {
guard let platterViewController = storyboard?.instantiateViewController(withIdentifier: "PlatterViewController") as? PlatterViewController,
let total = totalOutputLabel.text,
Expand Down

0 comments on commit fc01916

Please sign in to comment.