Skip to content

Commit

Permalink
Merge branch 'main' into large_file_upload_error
Browse files Browse the repository at this point in the history
  • Loading branch information
pelumy authored Sep 12, 2024
2 parents 4be1b8c + 542e1bb commit 0b5eef9
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added more instructions to the changelog file.
- Added some logging when a content warning is displayed. [cleanstr#53](https://github.com/planetary-social/cleanstr/issues/53)
- Minor refactor of Event+CoreDataClass. [#1443](https://github.com/planetary-social/nos/issues/1443)
- Refactored feature flag and added a feature flag toggle for “Enable new moderation flow” to Staging builds. [#1496](https://github.com/planetary-social/nos/issues/1496)

## [0.1.26] - 2024-09-09Z

Expand Down
11 changes: 11 additions & 0 deletions Nos/Assets/Localization/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -3739,6 +3739,17 @@
}
}
},
"enableNewModerationFlow" : {
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Enable new moderation flow"
}
}
}
},
"enterCode" : {
"extractionState" : "manual",
"localizations" : {
Expand Down
41 changes: 28 additions & 13 deletions Nos/Service/FeatureFlags.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import Foundation
import Dependencies

/// The set of feature flags used by the app.
protocol FeatureFlags {
/// Feature flags for enabling experimental or beta features.
enum FeatureFlag {
/// Whether the new media display should be enabled or not.
/// - Note: See [#1177](https://github.com/planetary-social/nos/issues/1177) for details on the new media display.
var newMediaDisplayEnabled: Bool { get }
case newMediaDisplay
/// Whether the new moderation flow should be enabled or not.
/// - Note: See [#1489](https://github.com/planetary-social/nos/issues/1489) for details on the new moderation flow.
case newModerationFlow
}

// MARK: - Additional requirements for debug mode
/// The set of feature flags used by the app.
protocol FeatureFlags {
/// Retrieves the value of the specified feature flag.
func isEnabled(_ feature: FeatureFlag) -> Bool

// MARK: - Additional requirements for debug mode
#if DEBUG || STAGING
/// Sets the value of `newMediaDisplayEnabled`.
func setNewMediaDisplayEnabled(_ enabled: Bool)
/// Sets the value of the specified feature flag.
func setFeature(_ feature: FeatureFlag, enabled: Bool)
#endif
}

Expand All @@ -22,13 +30,20 @@ class DefaultFeatureFlags: FeatureFlags, DependencyKey {

private init() {}

private(set) var newMediaDisplayEnabled = false
}
/// Feature flags and their values.
private var featureFlags: [FeatureFlag: Bool] = [
.newMediaDisplay: false,
.newModerationFlow: false
]

/// Returns true if the feature is enabled.
func isEnabled(_ feature: FeatureFlag) -> Bool {
featureFlags[feature] ?? false
}

#if DEBUG || STAGING
extension DefaultFeatureFlags {
func setNewMediaDisplayEnabled(_ enabled: Bool) {
newMediaDisplayEnabled = enabled
#if DEBUG || STAGING
func setFeature(_ feature: FeatureFlag, enabled: Bool) {
featureFlags[feature] = enabled
}
#endif
}
#endif
2 changes: 1 addition & 1 deletion Nos/Views/Note/CompactNoteView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ struct CompactNoteView: View {
.allowsHitTesting(!note.isPreview)
}
if note.kind == EventKind.text.rawValue, showLinkPreviews, !note.contentLinks.isEmpty {
if featureFlags.newMediaDisplayEnabled {
if featureFlags.isEnabled(.newMediaDisplay) {
GalleryView(urls: note.contentLinks, metadata: note.inlineMetadata)
} else {
LinkPreviewCarousel(links: note.contentLinks)
Expand Down
22 changes: 18 additions & 4 deletions Nos/Views/Settings/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,28 @@ extension SettingsView {
/// Whether the new media display is enabled.
private var isNewMediaDisplayEnabled: Binding<Bool> {
Binding<Bool>(
get: { featureFlags.newMediaDisplayEnabled },
set: { featureFlags.setNewMediaDisplayEnabled($0) }
get: { featureFlags.isEnabled(.newMediaDisplay) },
set: { featureFlags.setFeature(.newMediaDisplay, enabled: $0) }
)
}

/// A toggle for the new media display that allows the user to turn the feature on or off.
private var newMediaFeatureToggle: some View {
NosToggle(isOn: isNewMediaDisplayEnabled, labelText: .localizable.enableNewMediaDisplay)
}

/// Whether the new moderation flow is enabled.
private var isNewModerationFlowEnabled: Binding<Bool> {
Binding<Bool>(
get: { featureFlags.isEnabled(.newModerationFlow) },
set: { featureFlags.setFeature(.newModerationFlow, enabled: $0) }
)
}

/// A toggle for the new moderation flow that allows the user to turn the feature on or off.
private var newModerationFlowToggle: some View {
NosToggle(isOn: isNewModerationFlowEnabled, labelText: .localizable.enableNewModerationFlow)
}
}
#endif

Expand All @@ -283,6 +296,7 @@ extension SettingsView {
/// Controls that will appear when the app is built for STAGING.
@MainActor private var stagingControls: some View {
newMediaFeatureToggle
newModerationFlowToggle
}
}
#endif
Expand All @@ -293,7 +307,7 @@ extension SettingsView {
@MainActor private var debugControls: some View {
Group {
newMediaFeatureToggle

newModerationFlowToggle
Text(.localizable.sampleDataInstructions)
.foregroundColor(.primaryTxt)
Button(String(localized: .localizable.loadSampleData)) {
Expand Down
14 changes: 11 additions & 3 deletions NosTests/Service/MockFeatureFlags.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
/// A set of feature flag values used for testing that can be customized.
class MockFeatureFlags: FeatureFlags {
var newMediaDisplayEnabled = false
/// Mock feature flags and their values.
private var featureFlags: [FeatureFlag: Bool] = [
.newMediaDisplay: false,
.newModerationFlow: false
]

func setNewMediaDisplayEnabled(_ enabled: Bool) {
newMediaDisplayEnabled = enabled
func isEnabled(_ feature: FeatureFlag) -> Bool {
featureFlags[feature] ?? false
}

func setFeature(_ feature: FeatureFlag, enabled: Bool) {
featureFlags[feature] = enabled
}
}

0 comments on commit 0b5eef9

Please sign in to comment.