This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathAIChatMenuVisibilityConfigurable.swift
144 lines (121 loc) · 5.83 KB
/
AIChatMenuVisibilityConfigurable.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// AIChatMenuVisibilityConfigurable.swift
//
// Copyright © 2024 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Combine
import BrowserServicesKit
protocol AIChatMenuVisibilityConfigurable {
/// This property validates remote feature flags and user settings to determine if the shortcut
/// should be presented to the user. s
///
/// - Returns: `true` if the application menu shortcut should be displayed; otherwise, `false`.
var shouldDisplayApplicationMenuShortcut: Bool { get }
/// This property checks the relevant settings to decide if the toolbar shortcut is to be shown.
///
/// - Returns: `true` if the toolbar shortcut should be displayed; otherwise, `false`.
var shouldDisplayToolbarShortcut: Bool { get }
/// This property reflects the current state of the feature flag for the application menu shortcut.
///
/// - Returns: `true` if the remote feature for the application menu shortcut is enabled; otherwise, `false`.
var isFeatureEnabledForApplicationMenuShortcut: Bool { get }
/// This property reflects the current state of the feature flag for the toolbar shortcut.
///
/// - Returns: `true` if the remote feature for the toolbar shortcut is enabled; otherwise, `false`.
var isFeatureEnabledForToolbarShortcut: Bool { get }
/// A publisher that emits a value when either the `shouldDisplayApplicationMenuShortcut` or
/// `shouldDisplayToolbarShortcut` settings, backed by storage, are changed.
///
/// This allows subscribers to react to changes in the visibility settings of the application menu
/// and toolbar shortcuts.
///
/// - Returns: A `PassthroughSubject` that emits `Void` when the values change.
var valuesChangedPublisher: PassthroughSubject<Void, Never> { get }
/// A publisher that is triggered when it is validated that the onboarding should be displayed.
///
/// This property listens to `AIChatOnboardingTabExtension` and triggers the publisher when a
/// notification `AIChatOpenedForReturningUser` is posted.
///
/// - Returns: A `PassthroughSubject` that emits `Void` when the onboarding popover should be displayed.
var shouldDisplayToolbarOnboardingPopover: PassthroughSubject<Void, Never> { get }
/// Marks the toolbar onboarding popover as shown, preventing it from being displayed more than once.
/// This method should be called after the onboarding popover has been presented to the user.
func markToolbarOnboardingPopoverAsShown()
}
final class AIChatMenuConfiguration: AIChatMenuVisibilityConfigurable {
enum ShortcutType {
case applicationMenu
case toolbar
}
private var cancellables = Set<AnyCancellable>()
private var storage: AIChatPreferencesStorage
private let notificationCenter: NotificationCenter
private let remoteSettings: AIChatRemoteSettingsProvider
var valuesChangedPublisher = PassthroughSubject<Void, Never>()
var shouldDisplayToolbarOnboardingPopover = PassthroughSubject<Void, Never>()
var isFeatureEnabledForApplicationMenuShortcut: Bool {
isFeatureEnabledFor(shortcutType: .applicationMenu)
}
var isFeatureEnabledForToolbarShortcut: Bool {
isFeatureEnabledFor(shortcutType: .toolbar)
}
var shouldDisplayToolbarShortcut: Bool {
return isFeatureEnabledForToolbarShortcut && storage.shouldDisplayToolbarShortcut
}
var shouldDisplayApplicationMenuShortcut: Bool {
return isFeatureEnabledForApplicationMenuShortcut && storage.showShortcutInApplicationMenu
}
func markToolbarOnboardingPopoverAsShown() {
storage.didDisplayAIChatToolbarOnboarding = true
}
init(storage: AIChatPreferencesStorage = DefaultAIChatPreferencesStorage(),
notificationCenter: NotificationCenter = .default,
remoteSettings: AIChatRemoteSettingsProvider = AIChatRemoteSettings()) {
self.storage = storage
self.notificationCenter = notificationCenter
self.remoteSettings = remoteSettings
self.subscribeToValuesChanged()
self.subscribeToAIChatLoadedNotification()
}
private func subscribeToAIChatLoadedNotification() {
notificationCenter.publisher(for: .AIChatOpenedForReturningUser)
.sink { [weak self] _ in
guard let self = self else { return }
if !self.storage.didDisplayAIChatToolbarOnboarding && !storage.shouldDisplayToolbarShortcut {
self.shouldDisplayToolbarOnboardingPopover.send()
}
}.store(in: &cancellables)
}
private func subscribeToValuesChanged() {
storage.shouldDisplayToolbarShortcutPublisher
.removeDuplicates()
.sink { [weak self] _ in
self?.valuesChangedPublisher.send()
}.store(in: &cancellables)
storage.showShortcutInApplicationMenuPublisher
.removeDuplicates()
.sink { [weak self] _ in
self?.valuesChangedPublisher.send()
}.store(in: &cancellables)
}
private func isFeatureEnabledFor(shortcutType: ShortcutType) -> Bool {
switch shortcutType {
case .applicationMenu:
return remoteSettings.isApplicationMenuShortcutEnabled
case .toolbar:
return remoteSettings.isToolbarShortcutEnabled
}
}
}