생성일: 2022년 1월 21일 오후 10:26
class MainTabController: UITabBarController {
//MARK: - LifeCycle
private var user: User? {
didSet {
guard let user = user else { return }
configureViewControllers(withUser: user)
}
}
override func viewDidLoad() {
super.viewDidLoad()
checkIfUserIsLoggedIn()
fetchUser()
}
//MARK: - API
func fetchUser() {
UserService.fetchUser { user in
self.user = user
}
}
func checkIfUserIsLoggedIn() {
if Auth.auth().currentUser == nil {
DispatchQueue.main.async {
let controller = LoginController()
controller.delegate = self
let nav = UINavigationController(rootViewController: controller)
nav.modalPresentationStyle = .fullScreen
self.present(nav, animated: true, completion: nil)
}
}
}
... 중략 ...
//MARK: - Helpers
func configureViewControllers(withUser user: User) {
view.backgroundColor = .white
let layout = UICollectionViewFlowLayout()
let feed = templateNavigationController(unselectedImage: #imageLiteral(resourceName: "home_unselected"), selectedImage: #imageLiteral(resourceName: "home_selected"), rootViewController: FeedController(collectionViewLayout: layout))
let search = templateNavigationController(unselectedImage: #imageLiteral(resourceName: "search_unselected"), selectedImage: #imageLiteral(resourceName: "search_selected"), rootViewController: SearchController())
let imageSelector = templateNavigationController(unselectedImage: #imageLiteral(resourceName: "plus_unselected"), selectedImage: #imageLiteral(resourceName: "plus_unselected"), rootViewController: ImageSelectorController())
let notifications = templateNavigationController(unselectedImage: #imageLiteral(resourceName: "like_unselected"), selectedImage: #imageLiteral(resourceName: "like_selected"), rootViewController: NotificationController())
let profileController = ProfileController(user: user)
let profile = templateNavigationController(unselectedImage: #imageLiteral(resourceName: "profile_unselected"), selectedImage: #imageLiteral(resourceName: "profile_selected"), rootViewController: profileController)
viewControllers = [feed, search, imageSelector, notifications, profile]
tabBar.tintColor = .black
}
}
//MARK: - AuthenticationDelegate
extension MainTabController: AuthenticationDelegate {
func authenticationDidComplete() {
fetchUser()
self.dismiss(animated: true, completion: nil)
}
}
class ProfileController: UICollectionViewController {
//MARK: - Properties
private var user: User
//MARK: - Lifecycle
init(user: User) {
self.user = user
super.init(collectionViewLayout: UICollectionViewFlowLayout())
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
... 중략 ...
}
protocol AuthenticationDelegate: AnyObject {
func authenticationDidComplete()
}
class LoginController: UIViewController {
//MARK: - Properties
private var viewModel = LoginViewModel()
weak var delegate: AuthenticationDelegate?
... 중략 ...
//MARK: - Actions
@objc func handleLogin() {
guard let email = emailTextField.text else { return }
guard let password = passwordTextField.text else { return }
AuthService.logUserIn(withEmail: email, password: password) { result, error in
if let error = error {
print("DEBUG: Failed to log user in \(error.localizedDescription)")
return
}
// 로그인 화면 dismiss하고 로그인한 계정 정보 fetchUser() 시키기
self.delegate?.authenticationDidComplete()
}
}
@objc func handleShowSignUp(){
let controller = RegistrationController()
// RegistrationController 에서도 회원가입이 완료되면 dismiss하고 회원가입한 계정 정보 fetchUser() 하기 위해 delegate 설정
controller.delegate = delegate
navigationController?.pushViewController(controller, animated: true)
}
... 중략 ...
}
class RegistrationController: UIViewController {
//MARK: - Properties
private var viewModel = RegistrationViewModel()
private var profileImage: UIImage?
weak var delegate: AuthenticationDelegate?
... 중략 ...
//MARK: - Actions
@objc func handleSignUp() {
guard let email = emailTextField.text else { return }
guard let password = passwordTextField.text else { return }
guard let fullname = fullnameTextField.text else { return }
guard let username = usernameTextField.text else { return }
guard let profileImage = self.profileImage else { return }
let credentials = AuthCredentials(email: email, password: password, fullname: fullname, username: username, profileImage: profileImage)
AuthService.registerUser(withCredeitial: credentials) { error in
if let error = error {
print("DEBUG: Failed to register user \(error.localizedDescription)")
return
}
// 회원가입 화면 dismiss하고 로그인한 계정 정보 fetchUser() 시키기
self.delegate?.authenticationDidComplete()
}
}
... 중략 ...
}