Skip to content

Commit 4a2b34f

Browse files
Merge pull request #24 from surfstudio/SPT-1241-clear-coordinator-after-vc-closed
SPT-1241 DebugScreen coordinato call completionHandler after controller is closed from outside
2 parents 7f0df90 + cfdf34c commit 4a2b34f

6 files changed

Lines changed: 57 additions & 35 deletions

File tree

DebugScreen/Common/Base Classes/BaseCoordinator.swift

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,19 @@ class BaseCoordinator: Coordinator {
1212

1313
var childCoordinators: [Coordinator] = []
1414

15-
// MARK: - Internal Methods
15+
// MARK: - Initialization
16+
17+
init() {
18+
debugPrint("🎂 \(self) created")
19+
}
20+
21+
// MARK: - Deinitialization
22+
23+
deinit {
24+
debugPrint("❗️\(self) deinitialized")
25+
}
26+
27+
// MARK: - Methods
1628

1729
func start() {
1830
start(with: nil)
@@ -26,13 +38,16 @@ class BaseCoordinator: Coordinator {
2638
return
2739
}
2840
childCoordinators.append(coordinator)
41+
debugPrint("\(coordinator) added into \(self)")
2942
}
3043

3144
func removeDependency(_ coordinator: Coordinator?) {
3245
guard
3346
!childCoordinators.isEmpty,
3447
let coordinator = coordinator
35-
else { return }
48+
else {
49+
return
50+
}
3651

3752
for (index, element) in childCoordinators.enumerated() {
3853
if element === coordinator {
@@ -54,15 +69,14 @@ class BaseCoordinator: Coordinator {
5469
childCoordinators.removeAll()
5570
}
5671

57-
// MARK: - Private Methods
72+
}
5873

59-
private func haveDependency(_ coordinator: Coordinator) -> Bool {
60-
for element in childCoordinators {
61-
if element === coordinator {
62-
return true
63-
}
64-
}
65-
return false
74+
// MARK: - Private Methods
75+
76+
private extension BaseCoordinator {
77+
78+
func haveDependency(_ coordinator: Coordinator) -> Bool {
79+
childCoordinators.contains { $0 === coordinator }
6680
}
6781

6882
}

DebugScreen/Flows/DebugScreenCoordinator.swift

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ final class DebugScreenCoordinator: BaseCoordinator {
1313
// MARK: - Private properties
1414

1515
private let router = MainRouter()
16-
private let navigationController = UINavigationController()
1716

1817
// MARK: - Properties
1918

@@ -22,49 +21,48 @@ final class DebugScreenCoordinator: BaseCoordinator {
2221
// MARK: - Methods
2322

2423
override func start() {
25-
var components: MainModuleComponents = MainModuleConfigurator().configure()
26-
navigationController.setViewControllers([components.view], animated: false)
27-
navigationController.modalPresentationStyle = .overFullScreen
28-
29-
components.output.didModuleClosed = { [weak self] in
30-
self?.navigationController.dismiss(animated: true,
31-
completion: self?.completionHandler)
32-
}
33-
34-
components.output.didActionOptionsShowed = { [weak self] model in
35-
self?.showCacheCleaningActions(model: model)
36-
}
37-
38-
router.present(navigationController)
24+
showMainScreen()
3925
}
26+
4027
}
4128

4229
// MARK: - Private methods
4330

4431
private extension DebugScreenCoordinator {
4532

33+
func showMainScreen() {
34+
let (view, output) = MainModuleConfigurator().configure()
35+
output.didModuleClosed = { [weak self] in
36+
self?.router.dismissModule()
37+
}
38+
output.didActionOptionsShowed = { [weak self] model in
39+
self?.showCacheCleaningActions(model: model)
40+
}
41+
output.didModuleDismissed = { [weak self] in
42+
self?.completionHandler?()
43+
}
44+
router.present(view)
45+
}
46+
4647
func showCacheCleaningActions(model: ActionsProviderModel) {
4748
let actionsSheet = UIAlertController(
4849
title: nil,
4950
message: model.message,
5051
preferredStyle: .actionSheet
5152
)
52-
5353
model.actions.forEach { action in
5454
actionsSheet.addAction(UIAlertAction(
5555
title: action.title,
5656
style: .destructive) { _ in
5757
action.block()
5858
})
5959
}
60-
6160
actionsSheet.addAction(UIAlertAction(
6261
title: L10n.DebugScreenCoordinator.cancelAction,
6362
style: .cancel,
6463
handler: nil
6564
))
66-
67-
self.navigationController.present(actionsSheet, animated: true, completion: nil)
65+
router.present(actionsSheet)
6866
}
6967

7068
}

DebugScreen/Flows/MainScreen/Configurator/MainModuleConfigurator.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ final class MainModuleConfigurator {
1515

1616
func configure() -> MainModuleComponents {
1717
let viewController = UIViewController.instantiate(ofType: MainViewController.self)
18-
viewController.modalPresentationStyle = .overFullScreen
19-
2018
let presenter = MainModulePresenter()
2119

2220
presenter.view = viewController
2321
viewController.output = presenter
2422

25-
return (viewController, presenter)
23+
let navController = UINavigationController(rootViewController: viewController)
24+
navController.modalPresentationStyle = .overFullScreen
25+
26+
return (navController, presenter)
2627
}
2728

2829
}

DebugScreen/Flows/MainScreen/Presenter/MainModuleInput.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77

88
import Foundation
99

10-
protocol MainModuleInput { }
10+
protocol MainModuleInput: AnyObject { }

DebugScreen/Flows/MainScreen/Presenter/MainModuleOutput.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
import Foundation
99

10-
protocol MainModuleOutput {
11-
/// Call then clouse module
10+
protocol MainModuleOutput: AnyObject {
11+
/// Call then close module
1212
var didModuleClosed: (() -> Void)? { get set }
1313
/// Call then press clear cache button
1414
var didActionOptionsShowed: ((ActionsProviderModel) -> Void)? { get set }
15+
/// Call after module deinit from memory
16+
var didModuleDismissed: (() -> Void)? { get set }
1517
}

DebugScreen/Flows/MainScreen/Presenter/MainModulePresenter.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@ final class MainModulePresenter: MainModuleOutput {
1313

1414
var didModuleClosed: (() -> Void)?
1515
var didActionOptionsShowed: ((ActionsProviderModel) -> Void)?
16+
var didModuleDismissed: (() -> Void)?
1617

1718
// MARK: - Properties
1819

1920
weak var view: MainViewInput?
2021

22+
// MARK: - Deinitialization
23+
24+
deinit {
25+
didModuleDismissed?()
26+
}
27+
2128
}
2229

2330
// MARK: - MainModuleInput

0 commit comments

Comments
 (0)