RIB๊ณผ ํ†ต์‹ ํ•˜๊ธฐ

์ƒ์„ฑ์‹œ์— ์–ด๋– ํ•œ ํ๋ฆ„์œผ๋กœ ๋™์ž‘ํ•˜๋Š”์ง€ ์•Œ์•˜๋‹ค๋ฉด, ์ด๋ฒˆ์—๋Š” RIB๊ฐ„์˜ ์†Œํ†ต์„ ์•Œ์•„๋ณผ ์ฐจ๋ก€๋‹ค. ์ผ๋‹จ ์œ„์—์„œ ํ•˜์œ„ RIB์˜ Interactor๊ฐ€ Listener๋ผ๋Š” Protocol์˜ ์š”์†Œ๋ฅผ ์ฑ„์šฐ๊ณ , ์ƒ์œ„ RIB์˜ Interactor๊ฐ€ ์ด๋ฅผ ์ฑ„ํƒํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ํ–ˆ๋˜ ๊ฒƒ์„ ๊ธฐ์–ตํ•ด๋ณด์ž.

// OffGameInteractor
protocol OffGameListener: AnyObject {
    func startGame(with gameBuilder: GameBuildable)
}
 
// GameInteractor
public protocol GameListener: AnyObject {
    func gameDidEnd(with winner: PlayerType?)
}
 
// LoggedInInteractor
protocol LoggedInInteractable: Interactable, OffGameListener, GameListener {
    var router: LoggedInRouting? { get set }
    var listener: LoggedInListener? { get set }
}
 
final class LoggedInInteractor: Interactor, LoggedInInteractable {
 
    // MARK: - OffGameListener
    func startGame(with gameBuilder: GameBuildable) {
        router?.routeToGame(with: gameBuilder)
    }
 
    // MARK: - GameListener
    func gameDidEnd(with winner: PlayerType?) {
        router?.routeToOffGame(with: games)
    }
 
}

์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•ด ํ•˜๋‚˜์˜ ์œ„์น˜์— ์—ฌ๋Ÿฌ ํŒŒ์ผ์— ์žˆ๋˜ Listener๋“ค์„ ๊ฐ€์ ธ์™”๋‹ค. ๋˜ ์‹ค์ œ tutorial๊ณผ๋Š” ์•ฝ๊ฐ„์€ ๋‹ค๋ฅผ์ง€ ๋ชจ๋ฅธ๋‹ค. ํ•˜์ง€๋งŒ ์œ„์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค. ํ•˜์œ„ RIB์—์„œ ํ†ต์‹ ํ•˜๊ณ  ์‹ถ์€ ๊ฒƒ๋“ค์ด ์žˆ๋‹ค๋ฉด Listener์— ์ •์˜ํ•˜๊ณ , ์ด๋ฅผ ์ƒ์œ„ RIB์˜ Interactable Protocol์ด ์ฑ„ํƒํ•˜๋„๋ก ํ•˜๊ณ , ๊ฒฐ๊ณผ์ ์œผ๋กœ ์ด Interactable ํ”„๋กœํ† ์ฝœ์„ Interactor๊ฐ€ ์ค€์ˆ˜ํ•˜๋„๋ก ํ•˜์—ฌ ํ†ต์‹ ์„ ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•œ๋‹ค.

๊ทธ๋Ÿผ ๊ทธ๋ฆผ์„ ํ†ตํ•ด ๋”ฐ๋ผ๊ฐ€๋ฉด์„œ ๋จธ๋ฆฟ์†์— ๊ตฌ์ฒดํ™”์ž‘์—…์„ ํ•ด๋ณด์ž. Child RIB์—์„œ ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €๊ณ , Another RIB์— ์žˆ๋Š” ํ™”๋ฉด์ด ๋„์›Œ์ ธ์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด๋‹ค.

  1. Child RIB์˜ View์—์„œ Interaction์ด ์ผ์–ด๋‚œ๋‹ค. PresentableListener๋ฅผ ์ค€์ˆ˜ํ•˜๊ณ  ์žˆ๋Š” Interactor์— ์žˆ๋Š” method๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
  2. Interactor๋Š” ํ•ด๋‹น ์š”์ฒญ์€ ์ƒ์œ„ RIB์—์„œ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋ฏ€๋กœ Listener Protocol์„ ์ค€์ˆ˜ํ•˜๊ณ  ์žˆ๋Š” ๋ถ€๋ชจ RIB์˜ Interactor ๊ตฌํ˜„์ฒด์— ์š”์ฒญํ•œ๋‹ค.
  3. Parent RIB์˜ Interactor๋Š” ์ด๋ฅผ ์ค€์ˆ˜ํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์š”์ฒญ์„ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•œ๋‹ค.
  4. ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•œ ํ›„, Router์—๊ฒŒ ํ•˜์œ„ RIB์œผ๋กœ ๊ฐ€์•ผํ•œ๋‹ค๊ณ  ์•Œ๋ฆฐ๋‹ค.
  5. Router๋Š” Child RIB์„ detachํ•œ๋‹ค. (์ด ๋•Œ, Child RIB์„ ๋งŒ๋“ค์—ˆ์„ ๋•Œ ๋ฐ›์€ router ๊ฐ์ฒด๋ฅผ ๋„ฃ์–ด ํ•ด์ œํ•œ๋‹ค.)
  6. View์— ํ˜„์žฌ ๋ณด์—ฌ์ง€๊ณ  ์žˆ๋Š” ํ™”๋ฉด์„ dismiss ํ•ด์•ผ ํ•œ๋‹ค๊ณ  ์š”์ฒญํ•œ๋‹ค.
  7. Another RIB์„ ๋งŒ๋“ค๊ณ  Attachํ•œ๋‹ค.
  8. ์ด ๋•Œ, ๋™์  ์˜์กด์„ฑ์ด ํ•„์š”ํ•˜๋‹ค๋ฉด build() ํ•จ์ˆ˜์˜ ์ธ์ˆ˜๋กœ ๋„ฃ์–ด ๋ณด๋‚ธ๋‹ค.
  9. Another RIB์˜ Builder๋Š” component๋ฅผ ๋งŒ๋“ค๊ณ , View๋ฅผ ๋งŒ๋“ ๋‹ค.
  10. ๋งŒ๋“  View๋ฅผ Interactor์— ์ฃผ์ž…ํ•˜๊ณ  Router๋„ ๋งŒ๋“ ๋‹ค.
  11. Router๋Š” ์ƒ์„ฑ๋˜๋Š” ์‹œ์ ์— View์˜ present๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.