Skip to content

Commit

Permalink
reworked feature flags according to feedback and made them persistent
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanmontz committed Nov 16, 2024
1 parent c78f2d5 commit df8b1db
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 44 deletions.
4 changes: 2 additions & 2 deletions Nos/Controller/SensitiveContentController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ actor SensitiveContentController: FileDownloading {

do {
#if DEBUG
let shouldOverrideAnalyzer = featureFlags.isEnabled(.sensitiveContentIncoming)
let shouldOverrideAnalyzer = featureFlags.isEnabled(.sensitiveContentFlagAllAsSensitive)
if shouldOverrideAnalyzer {
try await Task.sleep(nanoseconds: 1 * 1_000_000_000) // simulate time to analyze
updateState(.analyzed(true), for: url)
Expand Down Expand Up @@ -99,7 +99,7 @@ actor SensitiveContentController: FileDownloading {
}

#if DEBUG
let shouldOverrideAnalyzer = featureFlags.isEnabled(.sensitiveContentOutgoing)
let shouldOverrideAnalyzer = featureFlags.isEnabled(.sensitiveContentFlagAllAsSensitive)
if shouldOverrideAnalyzer {
try? await Task.sleep(nanoseconds: 250_000_000) // simulate time to analyze
updateState(.analyzed(true), for: fileURL)
Expand Down
9 changes: 5 additions & 4 deletions Nos/Service/FeatureFlags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ enum FeatureFlag {
/// for the new flow.
case newOnboardingFlow

case sensitiveContentOutgoing
case sensitiveContentIncoming
case sensitiveContentAnalysisEnabled
case sensitiveContentFlagAllAsSensitive
}

/// The set of feature flags used by the app.
Expand All @@ -35,8 +35,9 @@ protocol FeatureFlags {
/// Feature flags and their values.
private var featureFlags: [FeatureFlag: Bool] = [
.newOnboardingFlow: true,
.sensitiveContentOutgoing: false,
.sensitiveContentIncoming: false

.sensitiveContentAnalysisEnabled: true,
.sensitiveContentFlagAllAsSensitive: false
]

/// Returns true if the feature is enabled.
Expand Down
19 changes: 12 additions & 7 deletions Nos/Views/NoteComposer/ImagePickerButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import SwiftUI
import Dependencies

struct ImagePickerButton<Label>: View where Label: View {


@Dependency(\.analytics) private var analytics
@Dependency(\.featureFlags) private var featureFlags

/// The device to be used initially when the user chooses to use the camera.
let cameraDevice: UIImagePickerController.CameraDevice
/// The types of content the user can choose.
Expand All @@ -29,8 +32,6 @@ struct ImagePickerButton<Label>: View where Label: View {
@State
private var imagePickerSource: UIImagePickerController.SourceType?

@Dependency(\.analytics) private var analytics

@EnvironmentObject private var router: Router

private var showImagePicker: Binding<Bool> {
Expand Down Expand Up @@ -158,10 +159,14 @@ struct ImagePickerButton<Label>: View where Label: View {
if let url {
analytics.selectedImage()

let shouldWarn = await SensitiveContentController.shared.shouldWarnUserUploadingFile(at: url)
if shouldWarn {
selectedContentURL = url
showSensitiveContentAlert = true
if featureFlags.isEnabled(.sensitiveContentAnalysisEnabled) {
let shouldWarn = await SensitiveContentController.shared.shouldWarnUserUploadingFile(at: url)
if shouldWarn {
selectedContentURL = url
showSensitiveContentAlert = true
} else {
onCompletion(url)
}
} else {
onCompletion(url)
}
Expand Down
81 changes: 50 additions & 31 deletions Nos/Views/Settings/SensitiveImageSettingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,95 @@ import SwiftUI
/// A debug view to manage testing of sensitive content analysis.
struct SensitiveImageSettingView: View {
@Dependency(\.featureFlags) private var featureFlags
@Dependency(\.userDefaults) private var userDefaults

private let sensitiveContentAnalysisEnabledKey = "sensitiveContentAnalysisEnabled"
private let sensitiveContentAnalysisFlagAllKey = "sensitiveContentAnalysisFlagAll"

private let analyzer = SCSensitivityAnalyzer()

var body: some View {
Form {
Section {
Group {
incomingImagesToggle
outgoingImagesToggle
Text(analyzer.analysisPolicy.description)
.foregroundColor(.primaryTxt)
.font(.clarity(.semibold))
.fixedSize(horizontal: false, vertical: true)
.textCase(nil)
.listRowGradientBackground()

if analyzer.analysisPolicy != .disabled {
Section {
sensitiveContentAnalysisToggle
} header: {
}
} header: {
VStack(alignment: .leading, spacing: 10) {
Text(analyzer.analysisPolicy.description)
.foregroundColor(.primaryTxt)
.font(.clarity(.semibold))
.fixedSize(horizontal: false, vertical: true)

Text("Use the toggles below to override the Sensitive Content Analyzer, once enabled.")
.foregroundColor(.secondaryTxt)
.font(.footnote)
.listRowGradientBackground()

Section {
flagAllToggle
} header: {
VStack(alignment: .leading, spacing: 10) {

Text("Use the toggle below to override the Sensitive Content Analyzer, once enabled.")
.foregroundColor(.secondaryTxt)
.font(.footnote)
}
.textCase(nil)
.listRowInsets(EdgeInsets())
.padding(.top, 30)
.padding(.bottom, 20)
}
.textCase(nil)
.listRowInsets(EdgeInsets())
.padding(.top, 30)
.padding(.bottom, 20)
.listRowGradientBackground()
.disabled(!featureFlags.isEnabled(.sensitiveContentAnalysisEnabled))
}
.listRowGradientBackground()
}
.scrollContentBackground(.hidden)
.background(Color.appBg)
.nosNavigationBar("Sensitive Content")
}

private var incomingImagesToggle: some View {
private var sensitiveContentAnalysisToggle: some View {
VStack {
NosToggle("Flag All Downloaded Images", isOn: shouldFlagIncomingImages)
NosToggle("Sensitive Content Analysis", isOn: isSensitiveContentAnalysisEnabled)

HStack {
Text("When on, all downloaded images will be flagged as sensitive. With it off, the real analyzer will determine sensitivity.") // swiftlint:disable:this line_length
Text("When on, downloaded and uploaded images will be scanned for sensitivity.")
.foregroundColor(.secondaryTxt)
.font(.footnote)
Spacer()
}
}
}

private var outgoingImagesToggle: some View {
private var flagAllToggle: some View {
VStack {
NosToggle("Flag All Uploaded Images", isOn: shouldFlagOutgoingImages)
NosToggle("Flag All Content As Sensitive", isOn: shouldFlagAllContentAsSensitive)

HStack {
Text("When on, all uploaded images will be flagged as sensitive. With it off, the real analyzer will determine sensitivity.") // swiftlint:disable:this line_length
Text("When on, all images will be flagged as sensitive. With it off, the real analyzer will determine sensitivity.") // swiftlint:disable:this line_length
.foregroundColor(.secondaryTxt)
.font(.footnote)
Spacer()
}
}
}

private var shouldFlagIncomingImages: Binding<Bool> {
private var isSensitiveContentAnalysisEnabled: Binding<Bool> {
Binding<Bool>(
get: { featureFlags.isEnabled(.sensitiveContentIncoming) },
set: { featureFlags.setFeature(.sensitiveContentIncoming, enabled: $0) }
get: { featureFlags.isEnabled(.sensitiveContentAnalysisEnabled) },
set: {
featureFlags.setFeature(.sensitiveContentAnalysisEnabled, enabled: $0)
userDefaults.set($0, forKey: sensitiveContentAnalysisEnabledKey)
}
)
}

private var shouldFlagOutgoingImages: Binding<Bool> {
private var shouldFlagAllContentAsSensitive: Binding<Bool> {
Binding<Bool>(
get: { featureFlags.isEnabled(.sensitiveContentOutgoing) },
set: { featureFlags.setFeature(.sensitiveContentOutgoing, enabled: $0) }
get: { featureFlags.isEnabled(.sensitiveContentFlagAllAsSensitive) },
set: {
featureFlags.setFeature(.sensitiveContentFlagAllAsSensitive, enabled: $0)
userDefaults.set($0, forKey: sensitiveContentAnalysisFlagAllKey)
}
)
}
}

0 comments on commit df8b1db

Please sign in to comment.