Skip to content

Commit 3a3f519

Browse files
committed
Add Support Screen presentation logic
1 parent 6cf8bea commit 3a3f519

5 files changed

Lines changed: 66 additions & 60 deletions

File tree

WordPress/Jetpack/Classes/ViewRelated/WordPress-to-Jetpack Migration/Common/MigrationDependencyContainer.swift

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,30 @@ struct MigrationViewControllerFactory {
4444
}
4545
}
4646

47-
// MARK: - View Models
47+
// MARK: - View Controllers
4848

49-
private func makeWelcomeViewModel() -> MigrationWelcomeViewModel {
50-
MigrationWelcomeViewModel(account: makeAccount(), coordinator: coordinator)
49+
private func makeWelcomeViewModel(handlers: ActionHandlers) -> MigrationWelcomeViewModel {
50+
let primaryHandler = { () -> Void in handlers.primary?() }
51+
let secondaryHandler = { () -> Void in handlers.secondary?() }
52+
53+
let actions = MigrationActionsViewConfiguration(
54+
step: .welcome,
55+
primaryHandler: primaryHandler,
56+
secondaryHandler: secondaryHandler
57+
)
58+
59+
return .init(account: makeAccount(), actions: actions)
5160
}
5261

53-
// MARK: - View Controllers
54-
5562
private func makeWelcomeViewController() -> UIViewController {
56-
MigrationWelcomeViewController(viewModel: makeWelcomeViewModel())
63+
let handlers = ActionHandlers()
64+
let viewModel = makeWelcomeViewModel(handlers: handlers)
65+
66+
let viewController = MigrationWelcomeViewController(viewModel: viewModel)
67+
handlers.primary = { [weak coordinator] in coordinator?.transitionToNextStep() }
68+
handlers.secondary = makeSupportViewControllerRouter(with: viewController)
69+
70+
return viewController
5771
}
5872

5973
private func makeNotificationsViewModel() -> MigrationNotificationsViewModel {
@@ -71,4 +85,21 @@ struct MigrationViewControllerFactory {
7185
private func makeDoneViewController() -> UIViewController {
7286
MigrationDoneViewController(viewModel: makeDoneViewModel())
7387
}
88+
89+
// MARK: - Routers
90+
91+
private func makeSupportViewControllerRouter(with presenter: UIViewController) -> () -> Void {
92+
return { [weak presenter] in
93+
let destination = SupportTableViewController(configuration: .currentAccountConfiguration())
94+
presenter?.present(UINavigationController(rootViewController: destination), animated: true)
95+
}
96+
}
97+
98+
// MARK: - Types
99+
100+
private class ActionHandlers {
101+
var primary: (() -> Void)?
102+
var secondary: (() -> Void)?
103+
}
104+
74105
}

WordPress/Jetpack/Classes/ViewRelated/WordPress-to-Jetpack Migration/Common/Navigation/MigrationFlowCoordinator.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ final class MigrationFlowCoordinator: ObservableObject {
88
// related to this property.
99
@Published private(set) var currentStep = MigrationStep.welcome
1010

11-
/// Call this closure to display the support screen
12-
var routeToSupportViewController: (() -> Void)?
13-
1411
func transitionToNextStep() {
1512
Task { [weak self] in
1613
if let nextStep = await Self.nextStep(from: currentStep) {

WordPress/Jetpack/Classes/ViewRelated/WordPress-to-Jetpack Migration/Common/Navigation/MigrationNavigationController.swift

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,7 @@ class MigrationNavigationController: UINavigationController {
5858
navigationBar.compactScrollEdgeAppearance = scrollEdgeAppearance
5959
}
6060
navigationBar.isTranslucent = true
61-
configure(coordinator: coordinator)
62-
}
63-
64-
private func configure(coordinator: MigrationFlowCoordinator) {
65-
coordinator.routeToSupportViewController = { [weak self] in
66-
let destination = SupportTableViewController(configuration: .currentAccountConfiguration())
67-
self?.present(UINavigationController(rootViewController: destination), animated: true)
68-
}
69-
self.listenForStateChanges()
61+
listenForStateChanges()
7062
}
7163

7264
private func listenForStateChanges() {

WordPress/Jetpack/Classes/ViewRelated/WordPress-to-Jetpack Migration/Welcome/MigrationWelcomeViewController.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ final class MigrationWelcomeViewController: UIViewController {
6565
}
6666

6767
private func setupNavigationBar() {
68-
self.navigationItem.rightBarButtonItem = UIBarButtonItem(email: viewModel.gravatarEmail)
68+
self.navigationItem.rightBarButtonItem = UIBarButtonItem(email: viewModel.gravatarEmail) { [weak self] () -> Void in
69+
self?.viewModel.configuration.actionsConfiguration.secondaryHandler?()
70+
}
6971
}
7072

7173
private func setupBottomSheet() {

WordPress/Jetpack/Classes/ViewRelated/WordPress-to-Jetpack Migration/Welcome/MigrationWelcomeViewModel.swift

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,35 @@ final class MigrationWelcomeViewModel {
44

55
// MARK: - Properties
66

7-
var gravatarEmail: String?
8-
9-
let blogListDataSource: BlogListDataSource
10-
7+
let gravatarEmail: String?
118
let configuration: MigrationStepConfiguration
9+
let blogListDataSource: BlogListDataSource
1210

1311
// MARK: - Init
1412

15-
init(account: WPAccount?, coordinator: MigrationFlowCoordinator) {
16-
if let account {
17-
self.gravatarEmail = account.email
18-
}
19-
20-
self.blogListDataSource = BlogListDataSource()
21-
self.blogListDataSource.loggedIn = true
22-
self.blogListDataSource.account = account
23-
24-
let headerConfiguration = MigrationHeaderConfiguration(step: .welcome,
25-
multiSite: blogListDataSource.visibleBlogsCount > 1)
13+
init(gravatarEmail: String?, blogListDataSource: BlogListDataSource, configuration: MigrationStepConfiguration) {
14+
self.gravatarEmail = gravatarEmail
15+
self.configuration = configuration
16+
self.blogListDataSource = blogListDataSource
17+
}
2618

27-
let primaryHandler = { [weak coordinator] () -> Void in
28-
coordinator?.transitionToNextStep()
29-
}
30-
let secondaryHandler = { [weak coordinator] () -> Void in
31-
// Which object is responsible for displaying the support screen?
32-
//
33-
// The following code needs to be executed somewhere:
34-
//
35-
// let destination = SupportTableViewController()
36-
// presentingViewController.present(destination, completion: nil)
37-
//
38-
// Approaches considered:
39-
//
40-
// 1. The View Model shouldn't be responsible for displaying the support screen
41-
// 2. The coordination can't perform the presentation because it doesn't have access to the `presenting` view controller
42-
// 3. I thought of making the `MigrationViewControllerFactory` responsible for creating the `secondaryHandler` and injecting it into this view model
43-
// But that didn't work as well.
44-
//
45-
// Workaround:
46-
//
47-
coordinator?.routeToSupportViewController?()
48-
}
49-
let actionsConfiguration = MigrationActionsViewConfiguration(step: .welcome, primaryHandler: primaryHandler, secondaryHandler: secondaryHandler)
50-
configuration = MigrationStepConfiguration(headerConfiguration: headerConfiguration,
51-
centerViewConfiguration: nil,
52-
actionsConfiguration: actionsConfiguration)
19+
convenience init(account: WPAccount?, actions: MigrationActionsViewConfiguration) {
20+
let blogsDataSource = BlogListDataSource()
21+
blogsDataSource.loggedIn = true
22+
blogsDataSource.account = account
23+
let header = MigrationHeaderConfiguration(
24+
step: .welcome,
25+
multiSite: blogsDataSource.visibleBlogsCount > 1
26+
)
27+
let configuration = MigrationStepConfiguration(
28+
headerConfiguration: header,
29+
centerViewConfiguration: nil,
30+
actionsConfiguration: actions
31+
)
32+
self.init(
33+
gravatarEmail: account?.email,
34+
blogListDataSource: blogsDataSource,
35+
configuration: configuration
36+
)
5337
}
5438
}

0 commit comments

Comments
 (0)