-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomeViewModel.swift
125 lines (113 loc) · 3.97 KB
/
HomeViewModel.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import Foundation
import UIKit
import GOVKit
import RecentActivity
struct HomeViewModel {
let analyticsService: AnalyticsServiceInterface
let configService: AppConfigServiceInterface
let notificationService: NotificationServiceInterface
let topicWidgetViewModel: TopicsWidgetViewModel
let feedbackAction: () -> Void
let notificationsAction: () -> Void
let recentActivityAction: () -> Void
let tokenAction: () -> Void
let urlOpener: URLOpener
let searchService: SearchServiceInterface
let activityService: ActivityServiceInterface
lazy var searchEnabled = featureEnabled(.search)
lazy var searchViewModel: SearchViewModel = SearchViewModel(
analyticsService: analyticsService,
searchService: searchService,
activityService: activityService,
urlOpener: urlOpener
)
var widgets: [WidgetView] {
get async {
await [
notificationsWidget,
// feedbackWidget, // see https://govukverify.atlassian.net/browse/GOVUKAPP-1220
recentActivityWidget,
tokenWidget,
topicsWidget
].compactMap { $0 }
}
}
@MainActor
private var notificationsWidget: WidgetView? {
get async {
guard await notificationService.shouldRequestPermission
else { return nil }
let title = String.home.localized("homeWidgetTitle")
let viewModel = UserFeedbackViewModel(
title: title,
action: notificationsAction
)
let content = InformationView(viewModel: viewModel, shouldHideChevron: true)
let widget = WidgetView(useContentAccessibilityInfo: true)
widget.backgroundColor = UIColor.govUK.fills.surfaceCardBlue
widget.addContent(content)
return widget
}
}
@MainActor
private var feedbackWidget: WidgetView {
let title = String.home.localized("feedbackWidgetTitle")
let viewModel = UserFeedbackViewModel(
title: title,
action: feedbackAction
)
let content = InformationView(viewModel: viewModel, shouldHideChevron: false)
let widget = WidgetView(useContentAccessibilityInfo: true)
widget.backgroundColor = UIColor.govUK.fills.surfaceCardBlue
widget.addContent(content)
return widget
}
@MainActor
private var recentActivityWidget: WidgetView? {
guard featureEnabled(.recentActivity)
else { return nil }
let title = String.home.localized(
"recentActivityWidgetTitle"
)
let viewModel = RecentActivityWidgetViewModel(
title: title,
action: recentActivityAction
)
let content = RecentActivtyWidget(
viewModel: viewModel
)
let widget = WidgetView(useContentAccessibilityInfo: true)
widget.addContent(content)
return widget
}
@MainActor
private var topicsWidget: WidgetView? {
guard featureEnabled(.topics)
else { return nil }
let content = TopicsWidgetView(
viewModel: topicWidgetViewModel
)
let widget = WidgetView(decorateView: false)
widget.addContent(content)
return widget
}
@MainActor
private var tokenWidget: WidgetView {
let title = "Token Test"
let viewModel = UserFeedbackViewModel(
title: title,
action: tokenAction
)
let content = InformationView(viewModel: viewModel, shouldHideChevron: false)
let widget = WidgetView(useContentAccessibilityInfo: true)
widget.backgroundColor = UIColor.govUK.fills.surfaceCardBlue
widget.addContent(content)
return widget
}
private func featureEnabled(_ feature: Feature) -> Bool {
configService.isFeatureEnabled(key: feature)
}
func trackECommerce() {
topicWidgetViewModel.trackECommerce()
}
}