Fix enabling feature flags on iOS 15

This commit is contained in:
Shadowfacts 2024-08-15 21:52:58 -07:00
parent ff433c4270
commit 54fadaa270
1 changed files with 69 additions and 16 deletions

View File

@ -18,7 +18,6 @@ struct AdvancedPrefsView : View {
@State private var mastodonCacheSize: Int64 = 0 @State private var mastodonCacheSize: Int64 = 0
@State private var cloudKitStatus: CKAccountStatus? @State private var cloudKitStatus: CKAccountStatus?
@State private var isShowingFeatureFlagAlert = false @State private var isShowingFeatureFlagAlert = false
@State private var featureFlagName = ""
var body: some View { var body: some View {
List { List {
@ -32,23 +31,9 @@ struct AdvancedPrefsView : View {
.listStyle(.insetGrouped) .listStyle(.insetGrouped)
.appGroupedListBackground(container: PreferencesNavigationController.self) .appGroupedListBackground(container: PreferencesNavigationController.self)
.onTapGesture(count: 3) { .onTapGesture(count: 3) {
featureFlagName = ""
isShowingFeatureFlagAlert = true isShowingFeatureFlagAlert = true
} }
.alert("Enable Feature Flag", isPresented: $isShowingFeatureFlagAlert) { .modifier(FeatureFlagAlertModifier(showing: $isShowingFeatureFlagAlert))
TextField("Flag Name", text: $featureFlagName)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
Button("Cancel", role: .cancel) {}
Button("Enable") {
if let flag = FeatureFlag(rawValue: featureFlagName) {
preferences.enabledFeatureFlags.insert(flag)
}
}
} message: {
Text("Warning: Feature flags are intended for development and debugging use only. They are experimental and subject to change at any time.")
}
.navigationBarTitle(Text("Advanced")) .navigationBarTitle(Text("Advanced"))
} }
@ -251,6 +236,74 @@ extension StatusContentType {
} }
} }
private struct FeatureFlagAlertModifier: ViewModifier {
@Binding var showing: Bool
@ObservedObject private var preferences = Preferences.shared
@State private var featureFlagName = ""
func body(content: Content) -> some View {
if #available(iOS 16.0, *) {
content
.onChange(of: showing) {
if $0 {
featureFlagName = ""
}
}
.alert("Enable Feature Flag", isPresented: $showing) {
textField
Button("Cancel", role: .cancel) {}
Button("Enable", action: enableFlag)
} message: {
warning
}
} else {
content
.sheet(isPresented: $showing) {
NavigationView {
List {
Section {
textField
} footer: {
warning
}
}
.navigationTitle("Enable Feature Flag")
.navigationBarTitleDisplayMode(.inline)
.listStyle(.insetGrouped)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") {
showing = false
}
}
ToolbarItem(placement: .confirmationAction) {
Button("Enable", action: self.enableFlag)
}
}
}
.navigationViewStyle(.stack)
}
}
}
private var textField: some View {
TextField("Flag Name", text: $featureFlagName)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
}
private var warning: Text {
Text("Warning: Feature flags are intended for development and debugging use only. They are experimental and subject to change at any time.")
}
private func enableFlag() {
if let flag = FeatureFlag(rawValue: featureFlagName) {
preferences.enabledFeatureFlags.insert(flag)
showing = false
}
}
}
#if DEBUG #if DEBUG
struct AdvancedPrefsView_Previews : PreviewProvider { struct AdvancedPrefsView_Previews : PreviewProvider {
static var previews: some View { static var previews: some View {