Skip to content
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

[REFACTOR] Coordinator Pattern 적용 (#139) #140

Merged
merged 10 commits into from
Oct 14, 2023
204 changes: 200 additions & 4 deletions LionHeart-iOS/LionHeart-iOS.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions LionHeart-iOS/LionHeart-iOS/Application/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import KakaoSDKAuth
class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?
var appCoordinator: AppCoordinator?

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
if let url = URLContexts.first?.url {
Expand All @@ -31,8 +32,10 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
self.window = UIWindow(windowScene: windowScene)
/// 폰트등록
Font.registerFonts()
let splashViewController = SplashViewController(manager: SplashManagerImpl(authService: AuthServiceImpl(apiService: APIService())))
let navigationController = UINavigationController(rootViewController: splashViewController)

let navigationController = UINavigationController()
appCoordinator = AppCoordinator(navigationController: navigationController)
appCoordinator?.start()
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
}
Expand Down
13 changes: 13 additions & 0 deletions LionHeart-iOS/LionHeart-iOS/Global/Enums/TokenState.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// TokenState.swift
// LionHeart-iOS
//
// Created by uiskim on 2023/10/14.
//

import Foundation

enum TokenState {
case valid
case expired
}
13 changes: 13 additions & 0 deletions LionHeart-iOS/LionHeart-iOS/Global/Enums/UserState.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// UserState.swift
// LionHeart-iOS
//
// Created by uiskim on 2023/10/14.
//

import Foundation

enum UserState {
case verified
case nonVerified
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,6 @@ extension UIViewController {
articleDetailViewController.isModalInPresentation = true
articleDetailViewController.modalPresentationStyle = .fullScreen
self.present(articleDetailViewController, animated: true)

}
}
10 changes: 0 additions & 10 deletions LionHeart-iOS/LionHeart-iOS/Global/Literals/DummyModel.swift

This file was deleted.

31 changes: 31 additions & 0 deletions LionHeart-iOS/LionHeart-iOS/Global/Protocols/Coordinator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// Coordinator.swift
// LionHeart-iOS
//
// Created by uiskim on 2023/10/13.
//

import UIKit

protocol Coordinator : AnyObject {
var parentCoordinator: Coordinator? { get set }
var children: [Coordinator] { get set }
var navigationController : UINavigationController { get set }

func start()
}

extension Coordinator {

/// Removing a coordinator inside a children. This call is important to prevent memory leak.
/// - Parameter coordinator: Coordinator that finished.
func childDidFinish(_ coordinator : Coordinator){
// Call this if a coordinator is done.
for (index, child) in children.enumerated() {
if child === coordinator {
children.remove(at: index)
break
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,19 @@ final class LHNavigationBarView: UIView {
private func setBackButtonWithTitle() {
self.titleLabel.text = type.title
self.leftBarItem.setImage(UIImage(systemName: "arrow.backward"), for: .normal)
if type == .onboarding {
self.leftBarItem.addButtonAction { _ in
self.backButtonActionHandler?()
}
return
}
self.leftBarItem.addButtonAction { [weak self] _ in
guard let self else { return }
self.viewController?.navigationController?.popViewController(animated: true)
self.leftBarItem.addButtonAction { _ in
self.backButtonActionHandler?()
}
// if type == .onboarding {
// self.leftBarItem.addButtonAction { _ in
// self.backButtonActionHandler?()
// }
// return
// }
// self.leftBarItem.addButtonAction { [weak self] _ in
// guard let self else { return }
// self.viewController?.navigationController?.popViewController(animated: true)
// }
kimscastle marked this conversation as resolved.
Show resolved Hide resolved
}

private func setButtonWithRightBarItems() {
Expand Down Expand Up @@ -165,7 +168,8 @@ final class LHNavigationBarView: UIView {
self.leftBarItem.setImage(UIImage(systemName: "xmark"), for: .normal)
self.leftBarItem.addButtonAction { [weak self] _ in
guard let self else { return }
self.viewController?.dismiss(animated: true)
self.backButtonActionHandler?()
// self.viewController?.dismiss(animated: true)
kimscastle marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

final class BookmarkMangerImpl: BookmarkManger {
final class BookmarkMangerImpl: BookmarkManager {

private let bookmarkService: BookmarkService

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// ArticleDetailManager.swift
// LionHeart-iOS
//
// Created by uiskim on 2023/10/14.
//

import Foundation

protocol ArticleDetailManager {
func getArticleDetail(articleId: Int) async throws -> [BlockTypeAppData]
func postBookmark(model: BookmarkRequest) async throws
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// ArticleListByCategoryManager.swift
// LionHeart-iOS
//
// Created by uiskim on 2023/10/14.
//

import Foundation

protocol ArticleListByCategoryManager {
func getArticleListByCategory(categoryString: String) async throws -> CurriculumWeekData
func postBookmark(model: BookmarkRequest) async throws
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// BookmarkManager.swift
// LionHeart-iOS
//
// Created by uiskim on 2023/10/14.
//

import Foundation

protocol BookmarkManager {
func getBookmark() async throws -> BookmarkAppData
func postBookmark(model: BookmarkRequest) async throws
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// ChallengeManager.swift
// LionHeart-iOS
//
// Created by uiskim on 2023/10/14.
//

import Foundation

protocol ChallengeManager {
func inquireChallengeInfo() async throws -> ChallengeData
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// CurriculumListManager.swift
// LionHeart-iOS
//
// Created by uiskim on 2023/10/14.
//

import Foundation

protocol CurriculumListManager {
func postBookmark(model: BookmarkRequest) async throws
func getArticleListByWeekInfo(week: Int) async throws -> CurriculumWeekData
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// CurriculumManager.swift
// LionHeart-iOS
//
// Created by uiskim on 2023/10/14.
//

import Foundation

protocol CurriculumManager {
func getCurriculumServiceInfo() async throws -> UserInfoData
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// LoginManager.swift
// LionHeart-iOS
//
// Created by uiskim on 2023/10/14.
//

import Foundation

protocol LoginManager {
func login(type: LoginType, kakaoToken: String) async throws
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// MyPageManager.swift
// LionHeart-iOS
//
// Created by uiskim on 2023/10/14.
//

import Foundation

protocol MyPageManager {
func getMyPage() async throws -> BadgeProfileAppData
func resignUser() async throws
func logout(token: UserDefaultToken) async throws
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// OnboardingManager.swift
// LionHeart-iOS
//
// Created by uiskim on 2023/10/14.
//

import Foundation

protocol OnboardingManager {
func signUp(type: LoginType, onboardingModel: UserOnboardingModel) async throws
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// SplashManager.swift
// LionHeart-iOS
//
// Created by uiskim on 2023/10/14.
//

import Foundation

protocol SplashManager {
func reissueToken(token: Token) async throws -> Token?
func logout(token: UserDefaultToken) async throws
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// TodayManager.swift
// LionHeart-iOS
//
// Created by uiskim on 2023/10/14.
//

import Foundation

protocol TodayManager {
func inquiryTodayArticle() async throws -> TodayArticle
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import UIKit

import SnapKit



final class ArticleCategoryViewController: UIViewController {

weak var coordinator: ArticleCategoryNavigation?

private lazy var navigationBar = LHNavigationBarView(type: .explore, viewController: self)
private lazy var titleLabel = LHLabel(type: .head2, color: .white, lines: 2, basicText: "카테고리별\n아티클 모아보기")
Expand Down Expand Up @@ -67,13 +71,11 @@ private extension ArticleCategoryViewController {

func setAddTarget() {
navigationBar.rightFirstBarItemAction {
let bookmarkViewController = BookmarkViewController(manager: BookmarkMangerImpl(bookmarkService: BookmarkServiceImpl(apiService: APIService())))
self.navigationController?.pushViewController(bookmarkViewController, animated: true)
self.coordinator?.navigationLeftButtonTapped()
}

navigationBar.rightSecondBarItemAction {
let mypageViewController = MyPageViewController(manager: MyPageManagerImpl(mypageService: MyPageServiceImpl(apiService: APIService()), authService: AuthServiceImpl(apiService: APIService())))
self.navigationController?.pushViewController(mypageViewController, animated: true)
self.coordinator?.navigationRightButtonTapped()
}
}

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

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let articleListbyCategoryViewController = ArticleListByCategoryViewController(manager: ArticleListByCategoryMangerImpl(articleService: ArticleServiceImpl(apiService: APIService()), bookmarkService: BookmarkServiceImpl(apiService: APIService())))
articleListbyCategoryViewController.categoryString = dummyCase[indexPath.item].categoryString
self.navigationController?.pushViewController(articleListbyCategoryViewController, animated: true)
coordinator?.articleListCellTapped(categoryName: dummyCase[indexPath.item].categoryString)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class CopyRightTableViewCell: UITableViewCell, TableViewCellRegisterDequeu
private let copyrightLabel = LHLabel(type: .body4, color: .gray600, basicText: "모든 콘텐츠는 제공자와 라이온하트에 저작권이 있습니다.\n저작권법에 의거 무단 전재 및 재배포를 금지합니다.")

//TODO: - inputData가 없는 경우..
var inputData: DummyModel?
// var inputData: DummyModel?

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import UIKit

import SnapKit

protocol ArticleDetailManager {
func getArticleDetail(articleId: Int) async throws -> [BlockTypeAppData]
func postBookmark(model: BookmarkRequest) async throws
}




final class ArticleDetailViewController: UIViewController {

// MARK: - UI Components
weak var coordinator: ArticleDetailModalNavigation?
private let manager: ArticleDetailManager

private lazy var navigationBar = LHNavigationBarView(type: .articleMain, viewController: self)
Expand Down Expand Up @@ -116,9 +116,7 @@ extension ArticleDetailViewController: ViewControllerServiceable {
func handleError(_ error: NetworkError) {
switch error {
case .unAuthorizedError:
guard let window = self.view.window else { return }
let splashViewController = SplashViewController(manager: SplashManagerImpl(authService: AuthServiceImpl(apiService: APIService())))
ViewControllerUtil.setRootViewController(window: window, viewController: splashViewController, withAnimation: false)
coordinator?.checkTokenIsExpired()
case .clientError(_, let message):
LHToast.show(message: "\(message)")
default:
Expand Down Expand Up @@ -180,6 +178,10 @@ private extension ArticleDetailViewController {
}

func setAddTarget() {
navigationBar.backButtonAction {
self.coordinator?.closeButtonTapped()
}

scrollToTopButton.addButtonAction { _ in
let indexPath = IndexPath(row: 0, section: 0)
self.articleTableView.scrollToRow(at: indexPath, at: .top, animated: true)
Expand Down
Loading