diff --git a/Tusker/Screens/Preferences/AdvancedPrefsView.swift b/Tusker/Screens/Preferences/AdvancedPrefsView.swift index 05021cd5..5ff4e57e 100644 --- a/Tusker/Screens/Preferences/AdvancedPrefsView.swift +++ b/Tusker/Screens/Preferences/AdvancedPrefsView.swift @@ -18,7 +18,6 @@ struct AdvancedPrefsView : View { @State private var mastodonCacheSize: Int64 = 0 @State private var cloudKitStatus: CKAccountStatus? @State private var isShowingFeatureFlagAlert = false - @State private var featureFlagName = "" var body: some View { List { @@ -32,23 +31,9 @@ struct AdvancedPrefsView : View { .listStyle(.insetGrouped) .appGroupedListBackground(container: PreferencesNavigationController.self) .onTapGesture(count: 3) { - featureFlagName = "" isShowingFeatureFlagAlert = true } - .alert("Enable Feature Flag", isPresented: $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.") - } + .modifier(FeatureFlagAlertModifier(showing: $isShowingFeatureFlagAlert)) .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 struct AdvancedPrefsView_Previews : PreviewProvider { static var previews: some View {