Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- swiftUI
- Human interface guide
- Refactoring
- UITextView
- rxswift
- SWIFT
- map
- UICollectionView
- ribs
- swift documentation
- Xcode
- Protocol
- collectionview
- 리펙터링
- combine
- 애니메이션
- MVVM
- Observable
- Clean Code
- ios
- clean architecture
- RxCocoa
- uiscrollview
- 스위프트
- HIG
- tableView
- 리펙토링
- 리팩토링
- uitableview
- 클린 코드
Archives
- Today
- Total
김종권의 iOS 앱 개발 알아가기
[iOS - swift] 4-4) RIBs 튜토리얼 (LoggedIn에 OffGame 붙이기) 본문
Architecture (swift)/RIBs
[iOS - swift] 4-4) RIBs 튜토리얼 (LoggedIn에 OffGame 붙이기)
jake-kim 2021. 4. 21. 23:33LoggedIn에 OffGame 붙이기, 이동
OffGame 생성
- OffGame RIB 그룹, 파일 생성
- UI: raw.githubusercontent.com/uber/ribs/assets/tutorial_assets/ios/tutorial2-composing-ribs/source/source2.swift
부모인 LoggedIn에 OffGame Builder 프로퍼티 추가
- Router에 새로운 Builder 프로퍼티 추가
// LoggedInRouter.swift
private let offGameBuilder: OffGameBuildable
init(interactor: LoggedInInteractable,
viewController: LoggedInViewControllable,
offGameBuilder: OffGameBuildable) {
self.viewController = viewController
self.offGameBuilder = offGameBuilder
super.init(interactor: interactor)
interactor.router = self
}
- Builder에 변경된 Router내용 적용
// LoggedInBuilder.swift
func build(withListener listener: LoggedInListener) -> LoggedInRouting {
let component = LoggedInComponent(dependency: dependency)
let interactor = LoggedInInteractor()
interactor.listener = listener
let offGameBuilder = OffGameBuilder(dependency: component)
return LoggedInRouter(interactor: interactor,
viewController: component.loggedInViewController,
offGameBuilder: offGameBuilder)
}
- LoggedInComponent+OffGame 생성하여 LoggedinComponent에 OffGameComponent속성 구현
attach, detach 처리
- LoggedIn에서 로그인이 성공하면 offGame이 바로 나와야 하므로, LoggedIn이 didLoad되었을 경우 attach OffGame 하도록 준비
// LoggedInRouter.swift
override func didLoad() {
super.didLoad()
attachOffGame()
}
- attachOffGame() 구현, child참조하고 있는 변수 선언
// LoggedInRouter.swift
private var currentChild: ViewableRouting?
private func attachOffGame() {
let offGame = offGameBuilder.build(withListener: interactor)
self.currentChild = offGame
attachChild(offGame)
viewController.present(viewController: offGame.viewControllable)
}
- LoggedInInterctable에 OffGameListener 프로토콜 구현
// LoggedInRouter.swift
protocol LoggedInInteractable: Interactable, OffGameListener {
var router: LoggedInRouting? { get set }
var listener: LoggedInListener? { get set }
}
- present 기능 추가
- 구현은 RootViewController에서 되어 있으므로, protocol에만 명시하면 가능
- LoggedIn은 viewless이지만 Router에 존재하는 ViewControllable프로토콜을 사용하여 해결
- RootViewController가 LoggedInViewControllable을 구현하고 있으므로 프로토콜 선언만 하면 가능
// LoggedInRouter.swift
protocol LoggedInViewControllable: ViewControllable {
func present(viewController: ViewControllable)
func dismiss(viewController: ViewControllable)
}
- LoggedIn이 detach될 때 LoggedIn의 자식을 dismiss하는 기능
- 호출은 LoggedInInteractor에서 수행
// LoggedInRouter.swift
func cleanupViews() {
if let currentChild = currentChild {
viewController.dismiss(viewController: currentChild.viewControllable)
}
}
OffGame으로 이동
detach current child + attach OffGame
- 부모 Interactor에 OffGame, TicTacToe로 이동하는 기능 선언
// LoggedInInteractor.swift
protocol LoggedInRouting: Routing {
func routeToOffGame() // < 추가
func cleanupViews()
}
- 구현
// LoggedInRouter.swift
func routeToOffGame() {
detachCurrentChild()
attachOffGame()
}
private func detachCurrentChild() {
if let currentChild = currentChild {
detachChild(currentChild)
viewController.dismiss(viewController: currentChild.viewControllable)
}
}
- 게임이 끝난 경우에도 OffGame으로 이동
// LoggedInRouter
func gameDidEnd() {
router?.routeToOffGame()
}
TicTacToe 붙이기
TicTacToe Builder 생성 - 이미 되어있는 상태
- 부모 Router에 TicTacToe Builder 프로퍼티 추가
// LoggedInRouter.swift
private let ticTacToeBuilder: TicTacToeBuildable
init(interactor: LoggedInInteractable,
viewController: LoggedInViewControllable,
offGameBuilder: OffGameBuildable,
ticTacToeBuilder: TicTacToeBuildable) {
self.viewController = viewController
self.offGameBuilder = offGameBuilder
self.ticTacToeBuilder = ticTacToeBuilder
super.init(interactor: interactor)
interactor.router = self
}
- Builder에 변경된 Router내용 적용
func build(withListener listener: LoggedInListener) -> LoggedInRouting {
let component = LoggedInComponent(dependency: dependency)
let interactor = LoggedInInteractor()
interactor.listener = listener
let offGameBuilder = OffGameBuilder(dependency: component)
let ticTacToeBuilder = TicTacToeBuilder(dependency: component)
return LoggedInRouter(
interactor: interactor,
viewController: component.LoggedInViewController,
offGameBuilder: offGameBuilder,
ticTacToeBuilder: ticTacToeBuilder
)
}
- LoggedInComponent+TicTacToe 생성하여 LoggedinComponent에 TicTacToeComponent속성 구현
detach, attach TicTacToe
- TicTacToe로 이동하는 기능 선언
// LoggedInInteractor.swift
protocol LoggedInRouting: Routing {
func routeToOffGame()
func routeToTicTacToe() // < 추가
func cleanupViews()
}
- 구현
// LoggedInRouter.swift
func routeToTicTacToe() {
detachCurrentChild()
let ticTacToe = ticTacToeBuilder.build(withListener: interactor)
currentChild = ticTacToe
attachChild(ticTacToe)
viewController.present(viewController: ticTacToe.viewControllable)
}
- LoggedInInteractable속성에 TicTacToeListener 추가
protocol LoggedInInteractable: Interactable, OffGameListener, TicTacToeListener {
var router: LoggedInRouting? { get set }
var listener: LoggedInListener? { get set }
}
게임 종료 시 OffGame으로 이동
// LoggedInteractor.swift
func gameDidEnd() {
router?.routeToOffGame()
}
'Architecture (swift) > RIBs' 카테고리의 다른 글
[iOS - swift] 4-6) RIBs 튜토리얼 (Deeplinking, Workflows, Actionable item) (0) | 2021.04.26 |
---|---|
[iOS - swift] 4-5) RIBs 튜토리얼 (Dependency Injection, Rx Stream) (0) | 2021.04.22 |
[iOS - swift] 4-3) RIBs 튜토리얼 (viewless RIB) (0) | 2021.04.20 |
[iOS - swift] 4-2) RIBs 튜토리얼 (Listener Interface) (0) | 2021.04.20 |
[iOS - swift] 3. RIBs의 구조, RIB 생성, attach/detach 흐름 (0) | 2021.04.20 |
Comments