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 pathPinningManager.swift
149 lines (119 loc) · 5.31 KB
/
PinningManager.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
145
146
147
148
149
//
// PinningManager.swift
//
// Copyright © 2022 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 Foundation
import NetworkProtection
enum PinnableView: String {
case autofill
case bookmarks
case downloads
case homeButton
case networkProtection
case aiChat
}
protocol PinningManager {
func togglePinning(for view: PinnableView)
func isPinned(_ view: PinnableView) -> Bool
func wasManuallyToggled(_ view: PinnableView) -> Bool
func pin(_ view: PinnableView)
func unpin(_ view: PinnableView)
func shortcutTitle(for view: PinnableView) -> String
}
final class LocalPinningManager: PinningManager {
static let shared = LocalPinningManager(networkProtectionFeatureActivation: NetworkProtectionKeychainTokenStore())
static let pinnedViewChangedNotificationViewTypeKey = "pinning.pinnedViewChanged.viewType"
@UserDefaultsWrapper(key: .pinnedViews, defaultValue: [])
private var pinnedViewStrings: [String]
@UserDefaultsWrapper(key: .manuallyToggledPinnedViews, defaultValue: [])
private var manuallyToggledPinnedViewsStrings: [String]
private let networkProtectionFeatureActivation: NetworkProtectionFeatureActivation
init(networkProtectionFeatureActivation: NetworkProtectionFeatureActivation) {
self.networkProtectionFeatureActivation = networkProtectionFeatureActivation
}
func togglePinning(for view: PinnableView) {
flagAsManuallyToggled(view)
if isPinned(view) {
pinnedViewStrings.removeAll(where: { $0 == view.rawValue })
} else {
pinnedViewStrings.append(view.rawValue)
}
NotificationCenter.default.post(name: .PinnedViewsChanged, object: nil, userInfo: [
Self.pinnedViewChangedNotificationViewTypeKey: view.rawValue
])
}
/// Do not call this for user-initiated toggling. This is only meant to be used for scenarios in which certain conditions
/// may require a view to be pinned.
///
func pin(_ view: PinnableView) {
guard !isPinned(view) else {
return
}
pinnedViewStrings.append(view.rawValue)
NotificationCenter.default.post(name: .PinnedViewsChanged, object: nil, userInfo: [
Self.pinnedViewChangedNotificationViewTypeKey: view.rawValue
])
}
/// Do not call this for user-initiated toggling. This is only meant to be used for scenarios in which certain conditions
/// may require a view to be unpinned.
///
func unpin(_ view: PinnableView) {
guard isPinned(view) else {
return
}
manuallyToggledPinnedViewsStrings.removeAll(where: { $0 == view.rawValue })
pinnedViewStrings.removeAll(where: { $0 == view.rawValue })
NotificationCenter.default.post(name: .PinnedViewsChanged, object: nil, userInfo: [
Self.pinnedViewChangedNotificationViewTypeKey: view.rawValue
])
}
func isPinned(_ view: PinnableView) -> Bool {
return pinnedViewStrings.contains(view.rawValue)
}
func shortcutTitle(for view: PinnableView) -> String {
switch view {
case .autofill: return isPinned(.autofill) ? UserText.hideAutofillShortcut : UserText.showAutofillShortcut
case .bookmarks: return isPinned(.bookmarks) ? UserText.hideBookmarksShortcut : UserText.showBookmarksShortcut
case .downloads: return isPinned(.downloads) ? UserText.hideDownloadsShortcut : UserText.showDownloadsShortcut
case .homeButton: return ""
case .networkProtection:
return isPinned(.networkProtection) ? UserText.hideNetworkProtectionShortcut : UserText.showNetworkProtectionShortcut
case .aiChat:
return isPinned(.aiChat) ? UserText.hideAIChatShortcut : UserText.showAIChatShortcut
}
}
// MARK: - Recording Manual Toggling
/// This method is useful for knowing if the view was manually toggled.
/// It's particularly useful for initializing a pin to a certain value at a certain point during the execution of code,
/// only if the user hasn't explicitly specified a desired state.
/// As an example: this is used in the VPN for pinning the icon to the navigation bar the first time the
/// feature is enabled.
///
func wasManuallyToggled(_ view: PinnableView) -> Bool {
manuallyToggledPinnedViewsStrings.contains(view.rawValue)
}
/// Flags a view as having been manually pinned / unpinned by the user.
///
private func flagAsManuallyToggled(_ view: PinnableView) {
var set = Set(manuallyToggledPinnedViewsStrings)
set.insert(view.rawValue)
manuallyToggledPinnedViewsStrings = Array(set)
}
}
// MARK: - NSNotification
extension NSNotification.Name {
static let PinnedViewsChanged = NSNotification.Name("pinning.pinnedViewsChanged")
}