-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombineViewController_HJB.swift
More file actions
153 lines (124 loc) · 4.77 KB
/
CombineViewController_HJB.swift
File metadata and controls
153 lines (124 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// Combine_HJB.swift
// Smashing-Assignment
//
// Created by 홍준범 on 12/26/25.
//
import UIKit
import Combine
import Then
import SnapKit
protocol InputOutputProtocol {
associatedtype Input
associatedtype Output
func transform(input: AnyPublisher<Input, Never>) -> AnyPublisher<Output, Never>
}
class CombineViewModel_HJB: InputOutputProtocol {
enum Input {
case firstTextFieldChanged(String)
case secondTextFieldChanged(String)
case buttonTapped
}
enum Output {
case toggleButton(isEnabled: Bool)
case clearTextFields
}
private let output: PassthroughSubject<Output, Never> = .init()
private var cancellables = Set<AnyCancellable>()
private let firstTextSubject = CurrentValueSubject<String, Never>("")
private let secondTextSubject = CurrentValueSubject<String, Never>("")
func transform(input: AnyPublisher<Input, Never>) -> AnyPublisher<Output, Never> {
input.sink { [weak self] event in
switch event {
case .firstTextFieldChanged(let text):
self?.firstTextSubject.send(text)
case .secondTextFieldChanged(let text):
self?.secondTextSubject.send(text)
case .buttonTapped:
self?.firstTextSubject.send("")
self?.secondTextSubject.send("")
self?.output.send(.clearTextFields)
}
}.store(in: &cancellables)
Publishers.CombineLatest(firstTextSubject, secondTextSubject)
.map { first, second in
return first.count >= 5 && second.count >= 5
}
.sink { [weak self] isEnabled in
self?.output.send(.toggleButton(isEnabled: isEnabled))
}.store(in: &cancellables)
return output.eraseToAnyPublisher()
}
}
class CombineViewController_HJB: UIViewController {
@Published var combineText: String = ""
// private let vm = CombineViewModel_HJB()
private let vm: CombineViewModel_HJB
private let input: PassthroughSubject<CombineViewModel_HJB.Input, Never> = .init()
private var cancellables = Set<AnyCancellable>()
private let homeView = CombineView_HJB()
init(viewModel: CombineViewModel_HJB) {
self.vm = viewModel
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func loadView() {
self.view = homeView
}
override func viewDidLoad() {
super.viewDidLoad()
bind()
addTarget()
}
private func bind() {
homeView.combineTextField.textDidChangePublisher()
.sink { [weak self] text in
self?.input.send(.firstTextFieldChanged(text))
}.store(in: &cancellables)
homeView.secondCombineTextField.textDidChangePublisher()
.sink { [weak self] text in
self?.input.send(.secondTextFieldChanged(text))
}.store(in: &cancellables)
let output = vm.transform(input: input.eraseToAnyPublisher())
output
.receive(on: DispatchQueue.main) //이거 없애서 해보기
.sink { [weak self] event in
switch event {
case .toggleButton(let isEnabled):
self?.homeView.combineButton.isEnabled = isEnabled
self?.homeView.combineButton.backgroundColor = isEnabled ? .systemBlue : .gray
case .clearTextFields:
self?.homeView.combineTextField.text = ""
self?.homeView.secondCombineTextField.text = ""
}
}.store(in: &cancellables)
}
private func addTarget() {
homeView.combineButton.addTarget(self, action: #selector(combineButtonTapped), for: .touchUpInside)
}
@objc
private func combineButtonTapped(_ sender: UIButton) {
input.send(.buttonTapped)
}
}
// private func bind() {
// homeView.combineTextField.textDidChangePublisher()
// .assign(to: &$combineText)
//
// $combineText
// .sink { [weak self] text in
// if text.count >= 5 {
// self?.homeView.combineButton.isEnabled = true
// self?.homeView.combineButton.backgroundColor = .systemBlue
// self?.homeView.infoText.isHidden = true
//
// } else {
// self?.homeView.combineButton.isEnabled = false
// self?.homeView.combineButton.backgroundColor = .gray
// self?.homeView.infoText.isHidden = false
// }
// }
// .store(in: &cancellables)
// }