-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
AppMenu.swift
158 lines (135 loc) · 8.41 KB
/
AppMenu.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
150
151
152
153
154
155
156
157
158
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0
import Shared
import Account
extension PhotonActionSheetProtocol {
//Returns a list of actions which is used to build a menu
//OpenURL is a closure that can open a given URL in some view controller. It is up to the class using the menu to know how to open it
func getLibraryActions(vcDelegate: PageOptionsVC) -> [PhotonActionSheetItem] {
let bookmarks = PhotonActionSheetItem(title: .AppMenuBookmarks, iconString: "menu-panel-Bookmarks") { _, _ in
let bvc = vcDelegate as? BrowserViewController
bvc?.showLibrary(panel: .bookmarks)
}
let history = PhotonActionSheetItem(title: .AppMenuHistory, iconString: "menu-panel-History") { _, _ in
let bvc = vcDelegate as? BrowserViewController
bvc?.showLibrary(panel: .history)
}
let downloads = PhotonActionSheetItem(title: .AppMenuDownloads, iconString: "menu-panel-Downloads") { _, _ in
let bvc = vcDelegate as? BrowserViewController
bvc?.showLibrary(panel: .downloads)
}
let readingList = PhotonActionSheetItem(title: .AppMenuReadingList, iconString: "menu-panel-ReadingList") { _, _ in
let bvc = vcDelegate as? BrowserViewController
bvc?.showLibrary(panel: .readingList)
}
return [bookmarks, history, downloads, readingList]
}
// Not part of AppMenu, but left for future use.
func getHomeAction(vcDelegate: Self.PageOptionsVC) -> [PhotonActionSheetItem] {
guard let tab = self.tabManager.selectedTab else { return [] }
let openHomePage = PhotonActionSheetItem(title: .AppMenuOpenHomePageTitleString, iconString: "menu-Home") { _, _ in
let page = NewTabAccessors.getHomePage(self.profile.prefs)
if page == .homePage, let homePageURL = HomeButtonHomePageAccessors.getHomePage(self.profile.prefs) {
tab.loadRequest(PrivilegedRequest(url: homePageURL) as URLRequest)
} else if let homePanelURL = page.url {
tab.loadRequest(PrivilegedRequest(url: homePanelURL) as URLRequest)
}
TelemetryWrapper.recordEvent(category: .action, method: .tap, object: .home)
}
return [openHomePage]
}
func getSettingsAction(vcDelegate: Self.PageOptionsVC) -> [PhotonActionSheetItem] {
// This method is being called when we the user sees the menu, not just when it's constructed.
// In that case, we can let sendExposureEvent default to true.
let variables = Experiments.shared.getVariables(featureId: .nimbusValidation)
// Get the title and icon for this feature from nimbus.
// We need to provide defaults if Nimbus doesn't provide them.
let title = variables.getText("settings-title") ?? .AppMenuSettingsTitleString
let icon = variables.getString("settings-icon") ?? "menu-Settings"
let openSettings = PhotonActionSheetItem(title: title, iconString: icon) { _, _ in
let settingsTableViewController = AppSettingsTableViewController()
settingsTableViewController.profile = self.profile
settingsTableViewController.tabManager = self.tabManager
settingsTableViewController.settingsDelegate = vcDelegate
let controller = ThemedNavigationController(rootViewController: settingsTableViewController)
// On iPhone iOS13 the WKWebview crashes while presenting file picker if its not full screen. Ref #6232
if UIDevice.current.userInterfaceIdiom == .phone {
controller.modalPresentationStyle = .fullScreen
}
controller.presentingModalViewControllerDelegate = vcDelegate
TelemetryWrapper.recordEvent(category: .action, method: .open, object: .settings)
// Wait to present VC in an async dispatch queue to prevent a case where dismissal
// of this popover on iPad seems to block the presentation of the modal VC.
DispatchQueue.main.async {
vcDelegate.present(controller, animated: true, completion: nil)
}
}
return [openSettings]
}
func getOtherPanelActions(vcDelegate: PageOptionsVC) -> [PhotonActionSheetItem] {
var items: [PhotonActionSheetItem] = []
let noImageEnabled = NoImageModeHelper.isActivated(profile.prefs)
let imageModeTitle: String = noImageEnabled ? .AppMenuShowImageMode : .AppMenuNoImageMode
let iconString = noImageEnabled ? "menu-ShowImages" : "menu-NoImageMode"
let noImageMode = PhotonActionSheetItem(title: imageModeTitle, iconString: iconString, isEnabled: noImageEnabled) { action,_ in
NoImageModeHelper.toggle(isEnabled: action.isEnabled, profile: self.profile, tabManager: self.tabManager)
if noImageEnabled {
TelemetryWrapper.recordEvent(category: .action, method: .tap, object: .blockImagesDisabled)
} else {
TelemetryWrapper.recordEvent(category: .action, method: .tap, object: .blockImagesEnabled)
}
}
items.append(noImageMode)
let nightModeEnabled = NightModeHelper.isActivated(profile.prefs)
let nightModeTitle: String = nightModeEnabled ? .AppMenuTurnOffNightMode : .AppMenuTurnOnNightMode
let nightMode = PhotonActionSheetItem(title: nightModeTitle, iconString: "menu-NightMode", isEnabled: nightModeEnabled) { _, _ in
NightModeHelper.toggle(self.profile.prefs, tabManager: self.tabManager)
if nightModeEnabled {
TelemetryWrapper.recordEvent(category: .action, method: .tap, object: .nightModeEnabled)
} else {
TelemetryWrapper.recordEvent(category: .action, method: .tap, object: .nightModeDisabled)
}
// If we've enabled night mode and the theme is normal, enable dark theme
if NightModeHelper.isActivated(self.profile.prefs), LegacyThemeManager.instance.currentName == .normal {
LegacyThemeManager.instance.current = DarkTheme()
NightModeHelper.setEnabledDarkTheme(self.profile.prefs, darkTheme: true)
}
// If we've disabled night mode and dark theme was activated by it then disable dark theme
if !NightModeHelper.isActivated(self.profile.prefs), NightModeHelper.hasEnabledDarkTheme(self.profile.prefs), LegacyThemeManager.instance.currentName == .dark {
LegacyThemeManager.instance.current = NormalTheme()
NightModeHelper.setEnabledDarkTheme(self.profile.prefs, darkTheme: false)
}
}
items.append(nightMode)
return items
}
func syncMenuButton(showFxA: @escaping (_ params: FxALaunchParams?, _ flowType: FxAPageType,_ referringPage: ReferringPage) -> Void) -> PhotonActionSheetItem? {
//profile.getAccount()?.updateProfile()
let action: ((PhotonActionSheetItem, UITableViewCell) -> Void) = { action,_ in
let fxaParams = FxALaunchParams(query: ["entrypoint": "browsermenu"])
showFxA(fxaParams, .emailLoginFlow, .appMenu)
TelemetryWrapper.recordEvent(category: .action, method: .tap, object: .signIntoSync)
}
let rustAccount = RustFirefoxAccounts.shared
let needsReauth = rustAccount.accountNeedsReauth()
guard let userProfile = rustAccount.userProfile else {
return PhotonActionSheetItem(title: .AppMenuBackUpAndSyncData, iconString: "menu-sync", handler: action)
}
let title: String = {
if rustAccount.accountNeedsReauth() {
return .FxAAccountVerifyPassword
}
return userProfile.displayName ?? userProfile.email
}()
let iconString = needsReauth ? "menu-warning" : "placeholder-avatar"
var iconURL: URL? = nil
if let str = rustAccount.userProfile?.avatarUrl, let url = URL(string: str) {
iconURL = url
}
let iconType: PhotonActionSheetIconType = needsReauth ? .Image : .URL
let iconTint: UIColor? = needsReauth ? UIColor.Photon.Yellow60 : nil
let syncOption = PhotonActionSheetItem(title: title, iconString: iconString, iconURL: iconURL, iconType: iconType, iconTint: iconTint, handler: action)
return syncOption
}
}