๊ฐ๋ฐ ์ค์ธ ์ฑ์์ MVP๋ฅผ ์ํ ๊ธฐ๋ฅ ๊ตฌํ์ ํ๋ ์ค์ ๋ฉ๋ชจ๋ฆฌ ๋์๋ฅผ ํ์ธํ ์ ์ด ์๋ค.
๋น์์๋ ๋น ๋ฅธ ๊ฐ๋ฐ์ด ํ์ํ๊ธฐ ๋๋ฌธ์
์ ํด์ง ์ผ์ ์ ๋ง์น๊ณ ํด๊ฒฐํ๊ธฐ๋ก ํ๋ค.
์์ผ๋ก๋ ๋ฐ๋ก ํ ๊ฑฐ๋ค. ์๊ฐ๋ณด๋ค ์ค๋ ๊ฑธ๋ ธ๋ค : (
์๋ฌดํผ
๋ฉ๋ชจ๋ฆฌ ๊ทธ๋ํ๋ฅผ ๋ณด๋ฉด์ ์ด๋ฐ์ ๋ฐ ์ํฉ์ ํ ์คํธ๋ฅผ ํด๋ณธ ๊ฒฐ๊ณผ
Coordinator์์ ์์ฑ๋ ViewController์ ViewModel, ์๋ฆผ ํ๋ฉด ๋ฑ
๋ฉ๋ชจ๋ฆฌ์์ deinit ๋์ง ์๋ ์ด์๋ก ํ์ธ๋๋ค.
์ด์ํ๋ค.
์ฝ๋๋ฅผ ์ฌ๊ธฐ์ ๊ธฐ ๋ฏ์ด๋ด๋
์ฝํ ์ฐธ์กฐ๋ฅผ ํตํด ์บก์ณํด ์ ์ฌ์ฉํ๊ณ ์์ด ์ด์์์ ๋งํ ๋ ์์ด ๋ณด์ด์ง ์์๋ค.
Profile Instrument๋ฅผ ์ด์ฉํด ๋ฉ๋ชจ๋ฆฌ ๋์๊ฐ ๋ฐ์ํ๊ณ ์๋์ง ํ์ธํ์ง๋ง ์ ์๋์ ์ค์ด๋ผ๊ณ ํ์๋๊ณ ์์๋ค.
์ฝ๋๋ฅผ ์ด๋ป๊ฒ ์์ฑํ๊ธธ๋ Profile Instrument์์๋ ์ ์์ด๋ผ๊ณ ํ์๋๋์ง ์ด์ด๊ฐ ์๋ค.
๋ฉ๋ชจ๋ฆฌ ๊ทธ๋ํ๋ฅผ ์ดํด๋ณด๊ธฐ๋ก ํ๋ค.
๋ฉ๋ชจ๋ฆฌ ๋์๊ฐ ๋๊ณ ์๋ ์ง์ ์ ์ฐพ์๋ค. ( ์บก์ฒํด๋์ด์ผ ํ๋๋ฐ ์ด๋ฏธ์ง๊ฐ ์๋ค ๐ )
RxSwift.๋ธ๋ผ๋ธ๋ผ ํ๋ ๊ณณ์์ ๊ฐํ ์ฐธ์กฐ๊ฐ ๋ฐ์ํด
ViewController, ViewModel ๋ฑ์ด ๋ฉ๋ชจ๋ฆฌ์์ ํด์ ๋์ง ์๊ณ ์์๋ค.
[ ์ฐธ๊ณ ์ฌํญ ]
- ๋ฉ๋ชจ๋ฆฌ ๊ทธ๋ํ์์ ๊ตต์ ์ ์ ๊ฐํ ์ฐธ์กฐ๋ฅผ, ๊ฐ๋ ์ ์ ๊ฐํ ์ฐธ์กฐ ํน์ ์ฝํ ์ฐธ์กฐ์์ ์๋ ค์ค๋ค.
ViewModel์ Input, Output ๋ฐฉ์์ ํตํด์ ๊ฐ๋ฐํ๋๋ฐ
์ฌ๊ธฐ์ Input์ ์ธ์๋ฅผ ๋ฃ์ ๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค.
์๋๋ ์์ ์ฝ๋๋ค.
let input = EditProfileViewModel.Input(
editPhotoSign: ...,
gender: ...,
introduceText: ...,
saveSign: saveButton.rx.tap
.map { _ in
return UpdateUserInformation(
profilePictrue: ...,
nickname: self.nicknameTextField.text ?? ...,
gender: self.womanButton.getStatus(),
bio: self.introduceTextField.text ?? ...)
}.asSignal(onErrorJustReturn: nil))
๋ณด์ด๋ ๊ฒ์ฒ๋ผ map์ ํตํด์ UpdateUserInformation์ ๋๊ฒจ์ฃผ๊ณ ์๋ค.
์ฌ๊ธฐ์ self ํค์๋๋ฅผ ์ฌ์ฉํ๊ณ , ์ด๋ฅผ ์ฝํ ์ฐธ์กฐ๋ก ๋ณ๊ฒฝํด์ผ ํ๋ค.
์ฆ, ํด๋ก์ ๋ก ์บก์ฒํ ๋, ์ธ์คํด์ค๋ฅผ ์บก์ฒํ๊ฒ ๋๋ฉด์ ํด๋ก์ ์ ์ธ์คํด์ค๊ฐ ๊ฐํ ์ฐธ์กฐ ์ํ์ ๋ฐ์์ํจ ๊ฒ์ด๋ค.
let input = EditProfileViewModel.Input(
editPhotoSign: ...,
gender: ...,
introduceText: ...,
saveSign: saveButton.rx.tap
.withUnretained(self)
.map { owner, _ in
return UpdateUserInformation(
profilePictrue: "",
nickname: owner.nicknameTextField.text ?? ...,
gender: owner.womanButton.getStatus(),
bio: owner.introduceTextField.text ?? ...)
}.asSignal(onErrorJustReturn: nil))
๋ฐ๋ผ์ ์์ ๊ฐ์ด ์์ ํ๋ค.
์์ ๋น์ทํ ์ผ์ด์ค๋ค์ด ์์ด ๋ชจ๋ ์์ ํ๊ณ , ๋คํํ ๋ฉ๋ชจ๋ฆฌ ๋์๊ฐ ์ด๋ ์ ๋ ํด๊ฒฐ์ด ๋์๋ค.
์์ง๋ ์ด๋์ ๊ฐ ๋์๊ฐ ๋ฐ์ํ๊ณ ์๋ค.
์์ ๋งํ๋ฏ์ด ๋์๊ฐ ๋ฐ์ํ๋ ๊ณณ์ ViewController์ ViewModel ๋ง๊ณ ๋ Alert๋ ์ปค์คํ ํ ์๋ฆผ ํ๋ฉด์ด ์์๋ค.
Coordinator ๋ด๋ถ์์ ์์ฑ๋ ํ๋ฉด์ ํ์๋๊ณ , ํ๋ฉด์์ ์ฌ๋ผ์ง ๋, ๋ฉ๋ชจ๋ฆฌ์์ ํด์ ๋์ด์ผ ํ์ง๋ง ๊ทธ๋ ์ง ์์๋ค.
Coordinator์์ ํ๋กํ ํ๋ฉด์ ๋ณด์ฌ์ฃผ๋ ์ฝ๋๋ฅผ ์ดํด๋ณด์.
func showProfileBottomSheet(nickname: String) {
let viewController = getProfileBottomSheet(nickname: nickname)
viewController.modalPresentationStyle = .overFullScreen
viewController.getViewTapGesture().subscribe(onNext: { [weak self] _ in
self?.navigationController.unsetBackground()
viewController.dismiss(animated: true)
})
.disposed(by: disposeBag)
navigationController.setBackground()
navigationController.present(viewController, animated: true)
}
ํ๋กํ ํ๋ฉด ์ธ์คํด์ค๋ฅผ ์์ฑ ๋ฐ ํ ๋นํ๊ณ ํ๋ฉด์ ๋ณด์ฌ์ฃผ๋ ์ฝ๋๋ค.
ํ๋ฉด ์ธ๋ถ๋ฅผ ํด๋ฆญํ์ ๊ฒฝ์ฐ, ํ์ฌ NavigationController์ ๋ฐฑ๊ทธ๋ผ์ด๋ ์ปฌ๋ฌ๋ฅผ ์ ๊ฑฐํ๋ ์ฝ๋๊ฐ subscribe ์์ ์์ฑ๋์ด ์๋ค.
์์ ์ฝ๋์ ๋ฌธ์ ์ ์ ๋ญ๊น.
weak๋ฅผ ํตํด self(Coordinator)๋ฅผ ์ฐธ์กฐํด ์ฌ์ฉํ๊ณ ์์ด์ ๋ฌธ์ ๊ฐ ๋ ๊ฑฐ๋ผ๊ณ ๋ ์๊ฐ ๋ชปํ์๋ค.
๋ฌธ์ ๋ ํ๋กํ ํ๋ฉด์ ์ธ์คํด์ค์๋ค.
getProfileBottomSheet ๋ด๋ถ ์ฝ๋์๋ self ํค์๋๊ฐ ์ฌ์ฉ๋๊ณ ์๊ธฐ๋ ํ๊ณ
subscribe ํด๋ก์ ๋ด๋ถ์ ๊ฐํ ์ฐธ์กฐ์ ํํ๋ก dismiss ์ฝ๋๊ฐ ์คํ๋๊ณ ์๊ธฐ ๋๋ฌธ์ด์๋ค.
๋ฐ๋ผ์ ๋ค์๊ณผ ๊ฐ์ด ์์ ํด์ผ ํ๋ค.
func showProfileBottomSheet(nickname: String) {
let viewController = getProfileBottomSheet(nickname: nickname)
viewController.modalPresentationStyle = .overFullScreen
viewController.getViewTapGesture().subscribe(onNext: { [weak self, weak viewController] _ in
self?.navigationController.unsetBackground()
viewController?.dismiss(animated: true)
})
.disposed(by: disposeBag)
navigationController.setBackground()
navigationController.present(viewController, animated: true)
}
์์ ๋ ๊ฐ์ง๋ฅผ ๋ชจ๋ ์์ ํ ๋ค์, ๋์ผํ ๋์์ ํ์ ๋์ ๋ฉ๋ชจ๋ฆฌ๋ ์๋์ ๊ฐ๋ค.
ํ .. ์ ๋๋ก ์์ ๋ ๊ฒ ๊ฐ์๋ฐ
์์ง ํ ๊ฐ์ง ์ฐ์ฐํ ๊ฒ ์๋ค. ์๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ฉ๋ชจ๋ฆฌ๊ฐ ์ ๋ ๊ฒ ๋์๋ ์ถ๋ค.
๋ญ๊ฐ ์์ ํ ๊ฒ ์์ผ๋ฉด ์์ ํ๊ณ ํฌ์คํ ํด์ผ๊ฒ ๋ค.
์๋ฌด์ชผ๋ก ๋ฉ๋ชจ๋ฆฌ ๋์๋ ํด๊ฒฐ ์๋ฃ.
์ถ์ ์ดํ์ ํ๋ณด์ฐจ ๊ธ ์์ฑํ๋ฌ ์ค๊ฒ ์ต๋๋ค.
๊ทธ๋ผ ์ด๋ง ๐๐ป๐๐ป๐๐ป