Skip to content

Commit

Permalink
[MERGE] Splash, Auth, ArticleCategory Coordinator에 Adaptor Pattern적용(#…
Browse files Browse the repository at this point in the history
…156)

[REFACTOR] Splash, Auth, ArticleCategory Coordinator에 Adaptor Pattern적용(#156)
  • Loading branch information
kimscastle authored Oct 19, 2023
2 parents 258e421 + 001b4ab commit 44d93f3
Show file tree
Hide file tree
Showing 38 changed files with 500 additions and 278 deletions.
56 changes: 42 additions & 14 deletions LionHeart-iOS/LionHeart-iOS.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// ArticleCategoryAdaptor.swift
// LionHeart-iOS
//
// Created by uiskim on 2023/10/17.
//

import Foundation

typealias EntireArticleCategoryNavigation = ArticleCategoryNavigation & ArticleListByCategoryNavigation

final class ArticleCategoryAdaptor: EntireArticleCategoryNavigation {

let coordinator: ArticleCategoryCoordinator
init(coordinator: ArticleCategoryCoordinator) {
self.coordinator = coordinator
}

func articleListCellTapped(categoryName: String) {
self.coordinator.showArticleListbyCategoryViewController(categoryName: categoryName)
}

func articleListByCategoryCellTapped(articleID: Int) {
self.coordinator.showArticleDetailViewController(articleID: articleID)
}

func checkTokenIsExpired() {
self.coordinator.exitApplication()
}

func navigationRightButtonTapped() {
self.coordinator.showMypageViewController()
}

func navigationLeftButtonTapped() {
self.coordinator.showBookmarkViewController()
}

func backButtonTapped() {
self.coordinator.pop()
}
}
38 changes: 38 additions & 0 deletions LionHeart-iOS/LionHeart-iOS/Scenes/Adaptor/AuthAdaptor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// AuthAdaptor.swift
// LionHeart-iOS
//
// Created by uiskim on 2023/10/17.
//

import Foundation

typealias EntireAuthNaviation = LoginNavigation & OnboardingNavigation & CompleteOnbardingNavigation

final class AuthAdaptor: EntireAuthNaviation {

let coordinator: AuthCoordinator
init(coordinator: AuthCoordinator) {
self.coordinator = coordinator
}

func checkUserIsVerified(userState: UserState, kakaoToken: String?) {
self.coordinator.showTabbarOrOnboardingViewController(userState: userState, kakaoToken: kakaoToken)
}

func onboardingCompleted(data: UserOnboardingModel) {
self.coordinator.showOnboardingCompleteViewController(data: data)
}

func startButtonTapped() {
self.coordinator.showTabbarController()
}

func checkTokenIsExpired() {
self.coordinator.exitApplication()
}

func backButtonTapped() {
self.coordinator.pop()
}
}
27 changes: 27 additions & 0 deletions LionHeart-iOS/LionHeart-iOS/Scenes/Adaptor/SplashAdaptor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// SplashAdaptor.swift
// LionHeart-iOS
//
// Created by uiskim on 2023/10/17.
//

import Foundation

typealias EntireSplashNavigation = SplashNavigation

final class SplashAdaptor: EntireSplashNavigation {

let coordinator: SplashCoordinator
init(coordinator: SplashCoordinator) {
self.coordinator = coordinator
}

func checkToken(state: TokenState) {
switch state {
case .valid:
coordinator.showTabbarViewContoller()
case .expired:
coordinator.showLoginViewController()
}
}
}
6 changes: 4 additions & 2 deletions LionHeart-iOS/LionHeart-iOS/Scenes/Adaptor/TodayAdaptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
// Created by uiskim on 2023/10/16.
//

import UIKit
import Foundation

final class TodayAdaptor: TodayNavigation {
typealias EntireTodayNavigation = TodayNavigation

final class TodayAdaptor: EntireTodayNavigation {

let coordinator: TodayCoordinator
init(coordinator: TodayCoordinator) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import SnapKit

final class ArticleCategoryViewController: UIViewController, ArticleCategoryViewControllerable {

weak var coordinator: ArticleCategoryNavigation?
var navigator: ArticleCategoryNavigation

private lazy var navigationBar = LHNavigationBarView(type: .explore, viewController: self)
private lazy var titleLabel = LHLabel(type: .head2, color: .white, lines: 2, basicText: "카테고리별\n아티클 모아보기")
Expand All @@ -31,6 +31,15 @@ final class ArticleCategoryViewController: UIViewController, ArticleCategoryView
setDelegate()
setCollectionView()
}

init(navigator: ArticleCategoryNavigation) {
self.navigator = navigator
super.init(nibName: nil, bundle: nil)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

private extension ArticleCategoryViewController {
Expand Down Expand Up @@ -69,11 +78,11 @@ private extension ArticleCategoryViewController {

func setAddTarget() {
navigationBar.rightFirstBarItemAction {
self.coordinator?.navigationLeftButtonTapped()
self.navigator.navigationLeftButtonTapped()
}

navigationBar.rightSecondBarItemAction {
self.coordinator?.navigationRightButtonTapped()
self.navigator.navigationRightButtonTapped()
}
}

Expand Down Expand Up @@ -108,7 +117,7 @@ extension ArticleCategoryViewController: UICollectionViewDataSource {
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
coordinator?.articleListCellTapped(categoryName: dummyCase[indexPath.item].categoryString)
navigator.articleListCellTapped(categoryName: dummyCase[indexPath.item].categoryString)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,9 @@ import UIKit

import SnapKit

protocol ArticleListByCategoryViewControllerable where Self: UIViewController {
var coordinator: ArticleListByCategoryNavigation? { get set }
var categoryString: String? { get set }
}

final class ArticleListByCategoryViewController: UIViewController, ArticleListByCategoryViewControllerable {

weak var coordinator: ArticleListByCategoryNavigation?
var navigator: ArticleListByCategoryNavigation
private let manager: ArticleListByCategoryManager

private lazy var navigationBar = LHNavigationBarView(type: .exploreEachCategory, viewController: self)
Expand All @@ -30,8 +25,9 @@ final class ArticleListByCategoryViewController: UIViewController, ArticleListBy
}
}

init(manager: ArticleListByCategoryManager) {
init(manager: ArticleListByCategoryManager, navigator: ArticleListByCategoryNavigation) {
self.manager = manager
self.navigator = navigator
super.init(nibName: nil, bundle: nil)
}

Expand Down Expand Up @@ -99,7 +95,7 @@ private extension ArticleListByCategoryViewController {

func setAddTarget() {
navigationBar.backButtonAction {
self.coordinator?.backButtonTapped()
self.navigator.backButtonTapped()
}
}
}
Expand All @@ -116,7 +112,7 @@ extension ArticleListByCategoryViewController: ViewControllerServiceable {
case .fetchImageError:
LHToast.show(message: "Image Error")
case .unAuthorizedError:
coordinator?.checkTokenIsExpired()
navigator.checkTokenIsExpired()
case .clientError(_, let message):
LHToast.show(message: message)
case .serverError:
Expand Down Expand Up @@ -152,6 +148,6 @@ extension ArticleListByCategoryViewController: UITableViewDataSource {

extension ArticleListByCategoryViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.coordinator?.articleListByCategoryCellTapped(articleID: articleListData[indexPath.row].articleId)
self.navigator.articleListByCategoryCellTapped(articleID: articleListData[indexPath.row].articleId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
import UIKit

protocol ArticleCategoryViewControllerable where Self: UIViewController {
var coordinator: ArticleCategoryNavigation? {get set}
var navigator: ArticleCategoryNavigation {get set}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// ArticleListByCategoryViewControllerable.swift
// LionHeart-iOS
//
// Created by uiskim on 2023/10/17.
//

import UIKit

protocol ArticleListByCategoryViewControllerable where Self: UIViewController {
var navigator: ArticleListByCategoryNavigation { get set }
var categoryString: String? { get set }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
import UIKit

protocol CompleteOnbardingViewControllerable where Self: UIViewController {
var coordinator: CompleteOnbardingNavigation? { get set }
var navigator: CompleteOnbardingNavigation { get set }
var userData: UserOnboardingModel? { get set }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
import UIKit

protocol LoginViewControllerable where Self: UIViewController {
var coordinator: LoginNavigation? { get set }
var navigator: LoginNavigation { get set }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
import UIKit

protocol OnboardingViewControllerable where Self: UIViewController {
var coordinator: OnboardingNavigation? { get set }
var navigator: OnboardingNavigation { get set }
func setKakaoAccessToken(_ token: String?)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
import UIKit

protocol SplashViewControllerable where Self: UIViewController {
var coordinator: SplashNavigation? { get set }
var navigator: SplashNavigation { get set }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
import UIKit

protocol TodayViewControllerable where Self: UIViewController {
var adaptor: TodayNavigation { get set }
var navigator: TodayNavigation { get set }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//
// ArticleCategoryCoordinator.swift
// LionHeart-iOS
//
// Created by uiskim on 2023/10/13.
//

import UIKit

final class ArticleCategoryCoordinatorImpl: ArticleCategoryCoordinator {

weak var parentCoordinator: Coordinator?

private let factory: ArticleCategortFactory

var children: [Coordinator] = []

var navigationController: UINavigationController

init(navigationController: UINavigationController, factory: ArticleCategortFactory) {
self.navigationController = navigationController
self.factory = factory
}

func start() {
showArticleCategoryViewController()
}

func showArticleCategoryViewController() {
let articleCategoryAdaptor = ArticleCategoryAdaptor(coordinator: self)
let articleCategoryViewController = factory.makeArticleCategoryViewController(navigator: articleCategoryAdaptor)
self.navigationController.pushViewController(articleCategoryViewController, animated: true)
}

func showArticleDetailViewController(articleID: Int) {
let articleCoordinator = ArticleCoordinator(
navigationController: navigationController,
factory: ArticleFactoryImpl(),
articleId: articleID
)
articleCoordinator.parentCoordinator = self
children.append(articleCoordinator)
articleCoordinator.start()
}

func showArticleListbyCategoryViewController(categoryName: String) {
let articleListbyCategoryViewController = factory.makeArticleListByCategoryViewController(coordinator: self)
articleListbyCategoryViewController.categoryString = categoryName
self.navigationController.pushViewController(articleListbyCategoryViewController, animated: true)
}

func showBookmarkViewController() {
let bookmarkFactory = BookmarkFactoryImpl()
let bookmarkCoordinator = BookmarkCoordinator(navigationController: navigationController, factory: bookmarkFactory)
bookmarkCoordinator.start()
children.append(bookmarkCoordinator)
}

func showMypageViewController() {
let mypageCoordinator = MypageCoordinator(
navigationController: navigationController,
factory: MyPageFactoryImpl()
)
mypageCoordinator.start()
children.append(mypageCoordinator)
}

func pop() {
self.navigationController.popViewController(animated: true)
}

func exitApplication() {
UIApplication.shared.perform(#selector(NSXPCConnection.suspend))
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
exit(0)
}
}
}
Loading

0 comments on commit 44d93f3

Please sign in to comment.