Skip to content

Commit

Permalink
refactor : Container에서 뷰컨트롤러와 관련 코디네이터를 묶어 관리하고 삭제 작업도 처리하도록 변경 #28
Browse files Browse the repository at this point in the history
  • Loading branch information
bibi6666667 committed Sep 22, 2022
1 parent 0d2db84 commit 6e2fcec
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 63 deletions.
13 changes: 2 additions & 11 deletions IssueTracker/IssueTracker/Container.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,11 @@ class Container {

init(environment: ContainerEnvironment) {
self.environment = environment
// registerObjects()
}

// func registerObjects() {
// registerLoginModel()
// registerLoginViewController()
// registerReposModel()
// registerReposViewController()
// }

// 외부 등록 허용??
// model, viewcontroller, coordinator를 생성 시점에 등록함
func register<T>(_ object: T) {
let key = String(describing: type(of: T.self)) // 해당 클래스의 이름을 key값으로 저장
let key = String(describing: type(of: T.self))
registeredObjects[key] = object
if let viewControllerObject = object as? UIViewController {
print("vc등록")
Expand Down Expand Up @@ -115,7 +107,6 @@ class Container {

func fetchAccessToken(url: URL, completion: @escaping (Bool) -> Void) {
environment.oAuthService.fetchToken(from: url) { [weak self] accessToken in
print("가져온 액세스토큰 : \(accessToken)")
guard let token = accessToken,
let self = self else {
completion(false)
Expand Down
54 changes: 14 additions & 40 deletions IssueTracker/IssueTracker/Coordinator/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,24 @@ class AppCoordinator: NSObject, Coordinator {
}

func showRootViewController() {
print("토큰 : \(container.environment.githubUserDefaults.getToken())")
container.environment.githubUserDefaults.getToken() != nil
? showReposViewController()
: showLoginViewController()
}

func buildRootViewController() -> UIViewController {
var vc = UIViewController()
if container.environment.githubUserDefaults.getToken() != nil {
if let reposVC: ReposViewController = container.resolve() {
vc = reposVC
}
} else {
if let loginVC: LoginViewController = container.resolve() {
vc = loginVC
}
}
return vc
}
// func buildRootViewController() -> UIViewController {
// var vc = UIViewController()
// if container.environment.githubUserDefaults.getToken() != nil {
// if let reposVC: ReposViewController = container.resolve() {
// vc = reposVC
// }
// } else {
// if let loginVC: LoginViewController = container.resolve() {
// vc = loginVC
// }
// }
// return vc
// }

private func showLoginViewController() {
let coordinator = LoginCoordinator(navigationController: navigationController, container: container)
Expand Down Expand Up @@ -98,7 +97,7 @@ class AppCoordinator: NSObject, Coordinator {

private func removeChildCoordinator(child: Coordinator) {
for (index, coordinator) in childCoordinators.enumerated() {
if coordinator === child {
if coordinator === child { // 참조 비교
childCoordinators.remove(at: index)
}
}
Expand Down Expand Up @@ -175,33 +174,8 @@ extension AppCoordinator: UINavigationControllerDelegate {
}

// navStack에 존재하지 않음 = pop됨 : 해당 뷰컨의 coordinator를 childCoordinators에서 지워야 함
// TODO: resolve로부터 받아온 코디네이터가 제대로 삭제되지 않음 - 디버깅 필요
if let coordinator = container.resolvePair(of: fromViewController) {
removeChildCoordinator(child: coordinator)
}

// if fromViewController as? LoginViewController != nil,
// let coordinator: LoginCoordinator = container.resolve() {
// removeChildCoordinator(child: coordinator)
// }
// if fromViewController as? ReposViewController != nil,
// let coordinator: ReposCoordinator = container.resolve() {
// removeChildCoordinator(child: coordinator)
// }
// if fromViewController as? IssueViewController != nil,
// let coordinator: IssueCoordinator = container.resolve() {
// removeChildCoordinator(child: coordinator)
// }
// if fromViewController as? NewIssueViewController != nil,
// let coordinator: NewIssueCoordinator = container.resolve() {
// removeChildCoordinator(child: coordinator)
// }
// if fromViewController as? OptionSelectViewController != nil,
// let coordinator: OptionSelectCoordinator = container.resolve() {
// removeChildCoordinator(child: coordinator)
// }

print("내비게이션 스택 : \(navigationController.viewControllers)")
print("자식 코디네이터들 : \(childCoordinators)")
}
}
12 changes: 7 additions & 5 deletions IssueTracker/IssueTracker/Coordinator/IssueCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,19 @@ class IssueCoordinator: Coordinator {
return
}
// MARK: 동적으로 생성되어야 하는 VC는 Container를 사용할 수 없나?
let issueModel = IssueModel(environment: .init(requestRepositoryIssues: { [weak self] completion in
let model = IssueModel(environment: .init(requestRepositoryIssues: { [weak self] completion in
self?.container.environment.issueService.requestRepositoryIssues(repo: repo, completion: { result in
completion(result)
})
}))
let issueVC = IssueViewController(model: issueModel, repo: repo)
issueVC.delegate = self
viewController = issueVC
let viewcontroller = IssueViewController(model: model, repo: repo)
viewcontroller.delegate = self
self.viewController = viewcontroller
container.register(model)
container.register(viewcontroller)

loadIssues()
self.navigationController.pushViewController(issueVC, animated: true)
self.navigationController.pushViewController(viewcontroller, animated: true)

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class NewIssueCoordinator: Coordinator {
let model = NewIssueModel(environment: modelEnvironment)
let newIssueVC = NewIssueViewController(repo: repo, model: model)

container.register(model)
container.register(newIssueVC)

newIssueVC.delegate = self

navigationController.pushViewController(newIssueVC, animated: true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ class OptionSelectCoordinator: Coordinator {
}

let optionSelectModel = OptionSelectModel(environment: optionSelectModelEnvironment)

let optionSelectVC = OptionSelectViewController(model: optionSelectModel, option: option, repo: repo)

container.register(optionSelectModel)
container.register(optionSelectVC)

optionSelectVC.delegate = self

navigationController.pushViewController(optionSelectVC, animated: true)
Expand Down
3 changes: 0 additions & 3 deletions IssueTracker/IssueTracker/Issue/Service/IssueService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ class IssueService {
}

func requestRepos(completion: @escaping (Result<[Repository], IssueError>) -> Void) {
print("액세스토큰 : \(accessToken)")
let urlString = RequestURL.repos.description
let headers: HTTPHeaders = [
NetworkHeader.acceptV3.getHttpHeader(),
Expand All @@ -144,8 +143,6 @@ class IssueService {
.responseDecodable(of: [Repository].self,
queue: globalThread,
decoder: decoder) { response in
print("응답 : \(response)")
print("결과 : \(response.result)")
switch response.result {
case .success(let data):
completion(.success(data))
Expand Down
3 changes: 0 additions & 3 deletions IssueTracker/IssueTracker/Repos/Model/ReposModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class ReposModel {
private var reposList: [Repository] {
didSet {
updated?(reposList)
print("repos 업데이트됨")
}
}

Expand All @@ -26,13 +25,11 @@ class ReposModel {
}

func fetchViewData(completion: @escaping (Bool) -> Void) {
print("ReposModel 데이터를 가져옵니다")
environment.requestRepos() { [weak self] result in
switch result {
case .success(let repositoryList):
self?.reposList = repositoryList
completion(true)
print("ReposModel 데이터를 가져왔습니다")
case .failure(let error):
completion(false)
print(error)
Expand Down

0 comments on commit 6e2fcec

Please sign in to comment.