Skip to content

Commit 2a60c8a

Browse files
committed
Add Support Screen presentation logic
1 parent 6cf8bea commit 2a60c8a

5 files changed

Lines changed: 60 additions & 60 deletions

File tree

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

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,25 @@ 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(supportViewControllerRouter: ViewControllerRouter) -> MigrationWelcomeViewModel {
50+
let primaryHandler = { [weak coordinator] () -> Void in
51+
coordinator?.transitionToNextStep()
52+
}
53+
let secondaryHandler = { () -> Void in
54+
supportViewControllerRouter.handler?()
55+
}
56+
let actions = MigrationActionsViewConfiguration(step: .welcome, primaryHandler: primaryHandler, secondaryHandler: secondaryHandler)
57+
return MigrationWelcomeViewModel(account: makeAccount(), actions: actions)
5158
}
5259

53-
// MARK: - View Controllers
54-
5560
private func makeWelcomeViewController() -> UIViewController {
56-
MigrationWelcomeViewController(viewModel: makeWelcomeViewModel())
61+
let supportViewControllerRouter = ViewControllerRouter()
62+
let viewModel = makeWelcomeViewModel(supportViewControllerRouter: supportViewControllerRouter)
63+
let viewController = MigrationWelcomeViewController(viewModel: viewModel)
64+
supportViewControllerRouter.handler = makeSupportViewControllerRouter(with: viewController)
65+
return viewController
5766
}
5867

5968
private func makeNotificationsViewModel() -> MigrationNotificationsViewModel {
@@ -71,4 +80,20 @@ struct MigrationViewControllerFactory {
7180
private func makeDoneViewController() -> UIViewController {
7281
MigrationDoneViewController(viewModel: makeDoneViewModel())
7382
}
83+
84+
// MARK: - Routers
85+
86+
private func makeSupportViewControllerRouter(with presenter: UIViewController) -> () -> Void {
87+
return { [weak presenter] in
88+
let destination = SupportTableViewController(configuration: .currentAccountConfiguration())
89+
presenter?.present(UINavigationController(rootViewController: destination), animated: true)
90+
}
91+
}
92+
93+
// MARK: - Types
94+
95+
private class ViewControllerRouter {
96+
var handler: (() -> Void)?
97+
}
98+
7499
}

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)