-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathProductsTopBannerFactory.swift
82 lines (72 loc) · 3.59 KB
/
ProductsTopBannerFactory.swift
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import UIKit
import Yosemite
import protocol WooFoundation.Analytics
struct ProductsTopBannerFactory {
enum BannerType {
case general
var title: String {
Localization.title
}
var info: String {
switch self {
case .general:
return Localization.topBannerInfo
}
}
var feedbackContext: WooAnalyticsEvent.FeedbackContext {
switch self {
case .general:
return .productsGeneral
}
}
}
/// Creates a products tab top banner asynchronously based on the app settings.
/// - Parameters:
/// - isExpanded: whether the top banner is expanded by default.
/// - type: sets banner content and related analytics events.
/// - expandedStateChangeHandler: called when the top banner expanded state changes.
/// - onGiveFeedbackButtonPressed: called the give feedback button is pressed.
/// - onDismissButtonPressed: called the dismiss button is pressed
/// - onCompletion: called when the view controller is created and ready for display.
static func topBanner(isExpanded: Bool,
stores: StoresManager = ServiceLocator.stores,
analytics: Analytics = ServiceLocator.analytics,
type: BannerType,
expandedStateChangeHandler: @escaping () -> Void,
onGiveFeedbackButtonPressed: @escaping () -> Void,
onDismissButtonPressed: @escaping () -> Void,
onCompletion: @escaping (TopBannerView) -> Void) {
let title = type.title
let infoText = type.info
let icon: UIImage = .megaphoneIcon
let giveFeedbackAction = TopBannerViewModel.ActionButton(title: Localization.giveFeedback) { _ in
analytics.track(event: .featureFeedbackBanner(context: type.feedbackContext, action: .gaveFeedback))
onGiveFeedbackButtonPressed()
}
let dismissAction = TopBannerViewModel.ActionButton(title: Localization.dismiss) { _ in
analytics.track(event: .featureFeedbackBanner(context: type.feedbackContext, action: .dismissed))
onDismissButtonPressed()
}
let actions = [giveFeedbackAction, dismissAction]
let viewModel = TopBannerViewModel(title: title,
infoText: infoText,
icon: icon,
isExpanded: isExpanded,
topButton: .chevron(handler: expandedStateChangeHandler),
actionButtons: actions)
let topBannerView = TopBannerView(viewModel: viewModel)
topBannerView.translatesAutoresizingMaskIntoConstraints = false
onCompletion(topBannerView)
}
}
private extension ProductsTopBannerFactory {
enum Localization {
static let title = NSLocalizedString("New features available!",
comment: "The title of the top banner on the Products tab.")
static let topBannerInfo = "" // The info of the top banner on the Products tab.
static let giveFeedback =
NSLocalizedString("Give feedback",
comment: "The title of the button to give feedback about products beta features on the banner on the products tab")
static let dismiss = NSLocalizedString("Dismiss", comment: "The title of the button to dismiss the banner on the products tab")
}
}