|
8 | 8 | import UIKit |
9 | 9 |
|
10 | 10 | protocol RandomNumberKeyboardDelegate { |
11 | | - func outputData(_ num: Int?) |
| 11 | + func outputData(_ input: KeyboardIput) |
| 12 | +} |
| 13 | + |
| 14 | +enum KeyboardIput { |
| 15 | + enum Order { |
| 16 | + case delete, clear, complete |
| 17 | + } |
| 18 | + |
| 19 | + case number(Int) |
| 20 | + case order(Order) |
12 | 21 | } |
13 | 22 |
|
14 | 23 | class RandomNumberKeyboard: UIView { |
15 | | - @IBOutlet var oneButton: UIButton! |
16 | | - @IBOutlet var twoButton: UIButton! |
17 | | - @IBOutlet var threeButton: UIButton! |
18 | | - @IBOutlet var fourButton: UIButton! |
19 | | - @IBOutlet var fiveButton: UIButton! |
20 | | - @IBOutlet var sixButton: UIButton! |
21 | | - @IBOutlet var sevenButton: UIButton! |
22 | | - @IBOutlet var eightButton: UIButton! |
23 | | - @IBOutlet var nineButton: UIButton! |
24 | | - @IBOutlet var zeroButton: UIButton! |
25 | | - @IBOutlet var deleteButton: UIButton! |
| 24 | + @IBOutlet var numberButtons: [UIButton]! |
| 25 | + @IBOutlet weak var deleteButton: UIButton! |
| 26 | + @IBOutlet weak var clearButton: UIButton! |
| 27 | + @IBOutlet weak var completeButton: UIButton! |
| 28 | + private var allButtons: [UIButton] { |
| 29 | + return numberButtons + [deleteButton, clearButton, completeButton] |
| 30 | + } |
26 | 31 |
|
27 | 32 | static let ideytifier = "RandomNumberKeyboard" |
28 | | - private var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 33 | + private var numbers: [Int?] = [] |
29 | 34 | var delegate: RandomNumberKeyboardDelegate? |
30 | | - |
| 35 | + |
31 | 36 | internal func configure() { |
32 | | - setBackgroundColor(.black) |
33 | | - setFont(UIFont.systemFont(ofSize: 24.0, weight: .bold)) |
34 | | - setTitleColor(.white, for: .normal) |
35 | | - shuffleNumbers() |
| 37 | + addTargetNumberButtons() |
| 38 | + setButtonsCornerRadius(4.0) |
| 39 | + shuffleNumberButtons() |
| 40 | + } |
| 41 | + |
| 42 | + internal func shuffleNumberButtons() { |
| 43 | + numbers = makeRandomNumbers() |
| 44 | + configureNumberButtons() |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// MARK: Configure Methods |
| 49 | + |
| 50 | +private extension RandomNumberKeyboard { |
| 51 | + // generate random numbers |
| 52 | + func makeRandomNumbers() -> [Int?] { |
| 53 | + var numbers: [Int?] = [] |
| 54 | + (0..<numberButtons.count).forEach { |
| 55 | + $0 <= 9 ? numbers.append($0) : numbers.append(nil) |
| 56 | + } |
| 57 | + return numbers.shuffled() |
| 58 | + } |
| 59 | + |
| 60 | + // configure number buttons by random numbers |
| 61 | + func configureNumberButtons() { |
| 62 | + zip(numbers, numberButtons).forEach { number, button in |
| 63 | + guard let number = number else { |
| 64 | + button.setTitle("", for: .normal) |
| 65 | + button.isEnabled = false |
| 66 | + button.backgroundColor = .clear |
| 67 | + return |
| 68 | + } |
| 69 | + button.setTitle(number.toString, for: .normal) |
| 70 | + button.isEnabled = true |
| 71 | + button.backgroundColor = .white |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // add target number buttons |
| 76 | + func addTargetNumberButtons() { |
| 77 | + numberButtons.enumerated().forEach { index, button in |
| 78 | + button.tag = index |
| 79 | + button.addTarget(self, action: #selector(numberButtonTapped), for: .touchUpInside) |
| 80 | + } |
36 | 81 | } |
37 | 82 | } |
38 | 83 |
|
| 84 | +// MARK: Attribute Methods |
| 85 | + |
39 | 86 | internal extension RandomNumberKeyboard { |
40 | 87 | func setBackgroundColor(_ color: UIColor?) { |
41 | 88 | backgroundColor = color |
42 | 89 | } |
43 | 90 |
|
44 | 91 | func setFont(_ font: UIFont) { |
45 | | - [ |
46 | | - zeroButton, |
47 | | - oneButton, |
48 | | - twoButton, |
49 | | - threeButton, |
50 | | - fourButton, |
51 | | - fiveButton, |
52 | | - sixButton, |
53 | | - sevenButton, |
54 | | - eightButton, |
55 | | - nineButton, |
56 | | - ].enumerated().forEach { idx, button in |
57 | | - button?.titleLabel?.font = font |
| 92 | + allButtons.forEach { button in |
| 93 | + button.titleLabel?.font = font |
58 | 94 | } |
59 | 95 | } |
60 | 96 |
|
61 | 97 | func setTitleColor(_ color: UIColor?, for state: UIControl.State) { |
62 | | - [ |
63 | | - zeroButton, |
64 | | - oneButton, |
65 | | - twoButton, |
66 | | - threeButton, |
67 | | - fourButton, |
68 | | - fiveButton, |
69 | | - sixButton, |
70 | | - sevenButton, |
71 | | - eightButton, |
72 | | - nineButton, |
73 | | - ].enumerated().forEach { idx, button in |
74 | | - button?.setTitleColor(color, for: state) |
| 98 | + allButtons.forEach { button in |
| 99 | + button.setTitleColor(color, for: state) |
75 | 100 | } |
76 | 101 | deleteButton.tintColor = color |
77 | 102 | } |
78 | | - |
79 | | - func shuffleNumbers() { |
80 | | - numbers = numbers.shuffled() |
81 | | - |
82 | | - [ |
83 | | - zeroButton, |
84 | | - oneButton, |
85 | | - twoButton, |
86 | | - threeButton, |
87 | | - fourButton, |
88 | | - fiveButton, |
89 | | - sixButton, |
90 | | - sevenButton, |
91 | | - eightButton, |
92 | | - nineButton, |
93 | | - ].enumerated().forEach { idx, button in |
94 | | - button?.setTitle(numbers[idx].toString, for: .normal) |
| 103 | + |
| 104 | + private func setButtonsCornerRadius(_ cornerRadius: Double) { |
| 105 | + allButtons.forEach { button in |
| 106 | + button.layer.cornerRadius = cornerRadius |
95 | 107 | } |
96 | 108 | } |
97 | 109 | } |
98 | 110 |
|
99 | 111 | private extension RandomNumberKeyboard { |
100 | | - @IBAction func oneButtonTapped(_ sender: Any) { |
101 | | - delegate?.outputData(numbers[1]) |
102 | | - } |
103 | | - @IBAction func twoButtonTapped(_ sender: Any) { |
104 | | - delegate?.outputData(numbers[2]) |
105 | | - } |
106 | | - @IBAction func threeButtonTapped(_ sender: Any) { |
107 | | - delegate?.outputData(numbers[3]) |
108 | | - } |
109 | | - @IBAction func fourButtonTapped(_ sender: Any) { |
110 | | - delegate?.outputData(numbers[4]) |
111 | | - } |
112 | | - @IBAction func fiveButtonTapped(_ sender: Any) { |
113 | | - delegate?.outputData(numbers[5]) |
114 | | - } |
115 | | - @IBAction func sixButtonTapped(_ sender: Any) { |
116 | | - delegate?.outputData(numbers[6]) |
117 | | - } |
118 | | - @IBAction func sevenButtonTapped(_ sender: Any) { |
119 | | - delegate?.outputData(numbers[7]) |
120 | | - } |
121 | | - @IBAction func eightButtonTapped(_ sender: Any) { |
122 | | - delegate?.outputData(numbers[8]) |
| 112 | + @objc func numberButtonTapped(sender: UIButton) { |
| 113 | + let index = sender.tag |
| 114 | + guard numbers.count > index, |
| 115 | + let number = numbers[index] else { |
| 116 | + return |
| 117 | + } |
| 118 | + delegate?.outputData(KeyboardIput.number(number)) |
123 | 119 | } |
124 | | - @IBAction func nineButtonTapped(_ sender: Any) { |
125 | | - delegate?.outputData(numbers[9]) |
| 120 | + |
| 121 | + @IBAction func deleteButtonTapped(_ sender: Any) { |
| 122 | + delegate?.outputData(KeyboardIput.order(.delete)) |
126 | 123 | } |
127 | | - @IBAction func zeroButtonTapped(_ sender: Any) { |
128 | | - delegate?.outputData(numbers[0]) |
| 124 | + |
| 125 | + @IBAction func clearButtonTapped(_ sender: Any) { |
| 126 | + delegate?.outputData(KeyboardIput.order(.clear)) |
129 | 127 | } |
130 | | - @IBAction func deleteButtonTapped(_ sender: Any) { |
131 | | - delegate?.outputData(nil) |
| 128 | + @IBAction func completeButtonTapped(_ sender: Any) { |
| 129 | + delegate?.outputData(KeyboardIput.order(.complete)) |
132 | 130 | } |
133 | 131 | } |
134 | 132 |
|
|
0 commit comments