-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathExperimentalFeaturesDataProvider.swift
65 lines (53 loc) · 3.06 KB
/
ExperimentalFeaturesDataProvider.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
import Foundation
import WordPressUI
import UIKit
class ExperimentalFeaturesDataProvider: ExperimentalFeaturesViewModel.DataProvider {
let flags: [OverridableFlag] = [
FeatureFlag.authenticateUsingApplicationPassword,
FeatureFlag.newGutenberg,
FeatureFlag.newGutenbergThemeStyles,
FeatureFlag.newGutenbergPlugins,
]
private let flagStore = FeatureFlagOverrideStore()
func loadItems() throws -> [WordPressUI.Feature] {
flags.map { flag in
WordPressUI.Feature(name: flag.description, key: flag.key)
}
}
func value(for feature: WordPressUI.Feature) -> Bool {
let flag = flag(for: feature)
return flagStore.overriddenValue(for: flag) ?? flag.originalValue
}
func didChangeValue(for feature: WordPressUI.Feature, to newValue: Bool) {
flagStore.override(flag(for: feature), withValue: newValue)
if feature.key == FeatureFlag.newGutenberg.key && !newValue {
let alert = UIAlertController(title: Strings.editorFeedbackTitle, message: Strings.editorFeedbackMessage, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: Strings.editorFeedbackDecline, style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: Strings.editorFeedbackAccept, style: .default, handler: { _ in
let feedbackViewController = SubmitFeedbackViewController(source: "experimental_features", feedbackPrefix: "Editor")
self.presentViewController(feedbackViewController)
}))
self.presentViewController(alert)
}
}
private func flag(for feature: WordPressUI.Feature) -> OverridableFlag {
guard let flag = flags.first(where: { $0.key == feature.key }) else {
preconditionFailure("Invalid Feature Flag")
}
return flag
}
private func presentViewController(_ viewController: UIViewController) {
DispatchQueue.main.async {
if let windowScene = UIViewController.topViewController?.view.window?.windowScene,
let topController = windowScene.windows.first?.rootViewController {
topController.present(viewController, animated: true, completion: nil)
}
}
}
enum Strings {
static let editorFeedbackTitle = NSLocalizedString("experimentalFeatures.editorFeedbackTitle", value: "Share Feedback", comment: "Title for the alert asking for feedback")
static let editorFeedbackMessage = NSLocalizedString("experimentalFeatures.editorFeedbackMessage", value: "Are you willing to share feedback on the experimental editor?", comment: "Message for the alert asking for feedback on the experimental editor")
static let editorFeedbackDecline = NSLocalizedString("experimentalFeatures.editorFeedbackDecline", value: "Not now", comment: "Dismiss button title for the alert asking for feedback")
static let editorFeedbackAccept = NSLocalizedString("experimentalFeatures.editorFeedbackAccept", value: "Send feedback", comment: "Accept button title for the alert asking for feedback")
}
}