-
Notifications
You must be signed in to change notification settings - Fork 1
[Refactor] Observable 적용 + 피드백 반영 #33
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
Open
yurim830
wants to merge
10
commits into
main
Choose a base branch
from
refactor/Observable
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ff027a1
[Add, Feat] #32 - Observable 생성
yurim830 ad69b5a
[Rename] #32 - Observable -> ObservablePattern
yurim830 bbed871
[Rename] #32 - LoginStatus -> LoginModel
yurim830 aa74917
[Refactor] #32 - LoginStatus 타입 변경 class -> struct
yurim830 1cbbd9c
[Add, Refactor] #32 - LoginDTO가 서버 통신에만 사용되도록 LoginInfo 생성
yurim830 2184f9c
[Refactor] #32 - VM이 VC 직접참조하는 것 삭제
yurim830 e495121
[Refactor] #32 - 뷰 바인딩 개선(Observable 활용)
yurim830 c848de0
[Fix] #32 - setSavedInfo 호출
yurim830 e91e9fe
[Chore] #32 - setSavedInfo 메서드 수정
yurim830 d0676fc
[Refactor] #32 - TextField 캡슐화
yurim830 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // | ||
| // Observable.swift | ||
| // 35-seminar | ||
| // | ||
| // Created by 김유림 on 1/1/25. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| final class ObservablePattern<T: Equatable> { | ||
|
|
||
| var value: T? { | ||
| didSet { | ||
| self.listener?(value) | ||
| } | ||
| } | ||
|
|
||
| init(_ value: T?) { | ||
| self.value = value | ||
| } | ||
|
|
||
| private var listener: ((T?) -> Void)? | ||
|
|
||
| func bind(_ listener: @escaping (T?) -> Void) { | ||
| listener(value) | ||
| self.listener = listener | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,11 @@ class LoginViewController: BaseViewController { | |
| view = loginView | ||
| } | ||
|
|
||
| override func viewDidLoad() { | ||
| super.viewDidLoad() | ||
| conductAutoLogin() | ||
| } | ||
|
|
||
| override func setDelegate() { | ||
| loginView.usernameTextField.delegate = self | ||
| } | ||
|
|
@@ -32,36 +37,72 @@ class LoginViewController: BaseViewController { | |
| } | ||
|
|
||
| override func bind() { | ||
| // auto login | ||
| let userData = UserDefaultsManager.fetchUserData() | ||
| loginViewModel.autoLogin() | ||
|
|
||
| loginView.bind(username: userData.username, | ||
| password: userData.password, | ||
| autoLogin: userData.autoLogin) | ||
| loginViewModel.usernameBinding.bind { [weak self] username in | ||
| guard let self = self else { return } | ||
| loginView.usernameTextField.text = username | ||
| } | ||
|
|
||
| if userData.autoLogin { | ||
| DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { | ||
| self.conductLogin() | ||
| } | ||
| loginViewModel.passwordBinding.bind { [weak self] password in | ||
| guard let self = self else { return } | ||
| loginView.passwordTextField.text = password | ||
| } | ||
|
|
||
| loginViewModel.isAutoLogin.bind { [weak self] isAutoLogin in | ||
| guard let self = self, | ||
| let isAutoLogin = isAutoLogin else { return } | ||
| loginView.updateAutoLoginCheckButton(autoLogin: isAutoLogin) | ||
| } | ||
|
|
||
| loginViewModel.isLoginSuccess.bind { [weak self] isLoginSuccess in | ||
| guard let self = self, | ||
| let isLoginSuccess = isLoginSuccess else { return } | ||
| isLoginSuccess ? navigateToMainScreen() : EasyAlert.showAlert(title: "로그인 실패", | ||
| message: loginViewModel.loginErrorMessage, | ||
| vc: self) | ||
| } | ||
| } | ||
|
|
||
| private func handleLoginInfo() -> LoginInfo? { | ||
| guard let username = loginView.getUsername(), | ||
| let password = loginView.getPassword(), | ||
|
Comment on lines
+68
to
+69
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 password = loginView.passwordTextField.text으로 접근해주면 돼용 |
||
| !username.isEmpty, // TextField가 비어있으면 nil이 아니라 ""이기 때문에 필요. | ||
| !password.isEmpty else { | ||
| return nil | ||
| } | ||
|
|
||
| return LoginInfo(username: username, password: password) | ||
| } | ||
|
|
||
| private func conductLogin() { | ||
| guard let loginData: LoginDTO = loginView.returnInputs() else { | ||
| guard let loginInfo = handleLoginInfo() else { | ||
| EasyAlert.showAlert( | ||
| title: "로그인 실패", | ||
| message: "username과 password를 정확히 입력하세요.", | ||
| vc: self) | ||
| return | ||
| } | ||
|
|
||
| loginViewModel.login(strongSelf: self, loginData: loginData) | ||
| loginViewModel.login(loginInfo) | ||
| } | ||
|
|
||
| private func conductAutoLogin() { | ||
| if loginViewModel.isAutoLogin.value ?? false { | ||
| conductLogin() | ||
| } | ||
| } | ||
|
|
||
| private func navigateToMainScreen() { | ||
| let tabBarController = TabBarController() | ||
| tabBarController.modalPresentationStyle = .fullScreen | ||
| self.present(tabBarController, animated: true) | ||
| } | ||
|
|
||
| @objc func tappedAutoLoginButton() { | ||
| let autoLogin = UserDefaultsManager.fetchAutoLogin() | ||
| UserDefaultsManager.updateAutoLogin(autoLogin: !autoLogin) | ||
| loginView.updateAutoLoginCheckButton(autoLogin: !autoLogin) | ||
| loginViewModel.isAutoLogin.value = !autoLogin | ||
| } | ||
|
|
||
| @objc func tappedLoginButton() { | ||
|
|
@@ -73,12 +114,14 @@ class LoginViewController: BaseViewController { | |
| let registerVC = RegisterViewController() | ||
| self.present(registerVC, animated: true) | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
| // MARK: - Extensions | ||
|
|
||
| extension LoginViewController: UITextFieldDelegate { | ||
|
|
||
| func textField(_ textField: UITextField, | ||
| shouldChangeCharactersIn range: NSRange, | ||
| replacementString string: String) -> Bool { | ||
|
|
@@ -93,4 +136,5 @@ extension LoginViewController: UITextFieldDelegate { | |
| } | ||
| return true | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
이렇게 처리해줘도 좋지만, 굳이 이렇게 처리할 필요는 없어요 ~!
저는 개인적으로 뷰컨에서 loginView.passwordTextField.text 으로 불러오는 방식을 조금 더 선호합니다! (그러면 뷰에서 passwordTextField가 private이 아니게만 조정해주면 됩니당) 굳이 멍청해야 하는 뷰가 데이터 전달 함수를 가지고 있을 필요가 없다구 생각해요
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.
오호.. 이렇게 생각하는 것도 일리가 있네요..! @cirtuare
혹시 캡슐화를 강화하기 위해 loginView.usernameTextField를 private으로 제한하고
getUsername(), setUsername(text:), setUsernameDelegate(_:) 메소드를 만드는 것에 대해서는 어떻게 생각하시나요?
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.
사실 일회성이라면 굳이.. 싶긴 한 부분이지만, getUsername / setUsername 메소드를 쓸 일이 많으면 이렇게 짤 것 같아요 !!
또는 extension으로, textfield의 text를 get / set 하는 함수를 만들어두고 두고두고 활용하는 방식을 도입해도 될 것 같습니다. 전 뷰에 걸쳐 textfield의 text를 많이 가져오고 세팅하니까요 ~ !!
Uh oh!
There was an error while loading. Please reload this page.
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.
오호... 답변 감사합니다!! 도움이 됐어요😁