1.
FormViewModel 프로토콜
protocol FormViewModel {
func updateForm()
}
Swift
복사
UI 요소가 데이터 변화에 맞춰 어떻게 갱신될지 정의한다.
예를 들어, 다음과 같이 사용 가능하다.
extension LoginController: FormViewModel {
func updateForm() {
UIView.animate(withDuration: 0.5) {
self.loginButton.backgroundColor = self.viewModel.buttonBackgroundColor
self.loginButton.isEnabled = self.viewModel.formIsValid
}
UIView.transition(
with: loginButton,
duration: 0.5,
options: .transitionCrossDissolve,
animations: {
self.loginButton.setTitleColor(self.viewModel.buttonTitleColor, for: .normal)
}
)
}
}
Swift
복사
로그인 버튼의 활성화 상태나 배경 색상을 사용자가 입력한 데이터의 유효성에 따라 업데이트할 때 사용하다.
2.
AuthenticationViewModel 프로토콜
protocol AuthenticationViewModel {
var formIsValid: Bool { get }
var buttonBackgroundColor: UIColor { get }
var buttonTitleColor: UIColor { get }
}
Swift
복사
인증 관련 뷰 모델의 기본 구조를 정의한다. 이 프로토콜은 로그인이나 회원가입 폼의 유효성을 판단하고 버튼의 배경 색상과 텍스트 색상을 결정하기 위한 속성을 포함한다.
struct LoginViewModel: AuthenticationViewModel {
var email: String?
var password: String?
var formIsValid: Bool {
return email?.isEmpty == false && password?.isEmpty == false
}
var buttonBackgroundColor: UIColor {
return formIsValid ? UIColor.black : UIColor.lightGray
}
var buttonTitleColor: UIColor {
return formIsValid ? .white : .white.withAlphaComponent(0.67)
}
}
Swift
복사
LoginViewModel은 이메일과 비밀번호의 입력 여부에 따라 폼의 유효성을 판단하고 해당 유효성 결과에 따라 버튼의 색상을 변경한다.
3.
AuthenticationDelegate 프로토콜
protocol AuthenticationDelegate: AnyObject {
func authenticationDidComplete()
}
Swift
복사
이 프로토콜을 통해 인증 과정이 성공적으로 끝난 후에 어떤 동작을 할지를 결정할 수 있다. 예를 들어, 사용자가 로그인 버튼을 누르고 인증이 성공적으로 완료되면 authenticationDidComplete 메서드에가 호출되어 앱에서는 해당 사용자를 메인 화면으로 이동시킬 수 있다.