Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion App/Sources/App/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ struct ContentView: View {
Button("Do something", role: .destructive) {
print("Did something!")
}
} onSave: { variables in
} dismiss: { variables in
print(variables)
isPresentingConfigEditor = false
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ metadata.
`ConfigVariableContent`, `CodableValueRepresentation`, `RegisteredConfigVariable`,
and `ConfigVariableSecrecy`
- **Sources/DevConfiguration/Metadata/**: `ConfigVariableMetadata` and metadata key types
(`DisplayNameMetadataKey`, `RequiresRelaunchMetadataKey`)
(`DisplayNameMetadataKey`)
- **Sources/DevConfiguration/Access Reporting/**: EventBus-based access and decoding events

### Key Documents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,12 @@ struct ConfigVariableListView<ViewModel: ConfigVariableListViewModeling, CustomS
.toolbar { toolbarContent }
.alert(localizedStringResource("editorView.saveAlert.title"), isPresented: $viewModel.isShowingSaveAlert) {
Button(localizedStringResource("editorView.saveAlert.saveButton")) {
viewModel.save()
dismiss()
viewModel.saveAndDismiss { dismiss() }
}
.keyboardShortcut(.defaultAction)

Button(localizedStringResource("editorView.saveAlert.dontSaveButton"), role: .destructive) {
dismiss()
viewModel.dismissWithoutSaving { dismiss() }
}

Button(localizedStringResource("editorView.saveAlert.cancelButton"), role: .cancel) {}
Expand Down Expand Up @@ -116,8 +115,7 @@ extension ConfigVariableListView {

ToolbarItem(placement: .confirmationAction) {
Button {
viewModel.save()
dismiss()
viewModel.saveAndDismiss { dismiss() }
} label: {
Label(localizedStringResource("editorView.saveButton"), systemImage: "checkmark")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ final class ConfigVariableListViewModel: ConfigVariableListViewModeling {
/// The document that owns the variable data.
private let document: EditorDocument

/// The closure to call with the changed variables when the user saves.
private let onSave: ([RegisteredConfigVariable]) -> Void
/// An optional closure to call when the editor is dismissed.
///
/// It receives the registered variables whose overrides changed, or an empty array if the user dismissed without
/// saving.
private let dismiss: (([RegisteredConfigVariable]) -> Void)?

var searchText = ""
var isShowingSaveAlert = false
Expand All @@ -32,10 +35,12 @@ final class ConfigVariableListViewModel: ConfigVariableListViewModeling {
///
/// - Parameters:
/// - document: The editor document.
/// - onSave: A closure called with the registered variables whose overrides changed when the user saves.
init(document: EditorDocument, onSave: @escaping ([RegisteredConfigVariable]) -> Void) {
/// - dismiss: An optional closure called when the editor is dismissed. It receives the registered variables whose
/// overrides changed, or an empty array if the user dismissed without saving. If `nil`, the view's environment
/// dismiss action is used instead.
init(document: EditorDocument, dismiss: (([RegisteredConfigVariable]) -> Void)?) {
self.document = document
self.onSave = onSave
self.dismiss = dismiss
}


Expand Down Expand Up @@ -95,15 +100,30 @@ final class ConfigVariableListViewModel: ConfigVariableListViewModeling {
if isDirty {
isShowingSaveAlert = true
} else {
dismiss()
dismissWithoutSaving(dismiss)
}
}


func save() {
func saveAndDismiss(_ dismiss: () -> Void) {
let changedKeys = document.changedKeys
document.save()
onSave(changedKeys.compactMap { document.registeredVariables[$0] })
let changedVariables = changedKeys.compactMap { document.registeredVariables[$0] }

if let dismiss = self.dismiss {
dismiss(changedVariables)
} else {
dismiss()
}
}


func dismissWithoutSaving(_ dismiss: () -> Void) {
if let dismiss = self.dismiss {
dismiss([])
} else {
dismiss()
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,26 @@ protocol ConfigVariableListViewModeling: Observable {

/// Requests dismissal of the editor.
///
/// If the working copy has unsaved changes, this presents the save alert. Otherwise, it calls the dismiss closure
/// immediately.
/// If the working copy has unsaved changes, this presents the save alert. Otherwise, it dismisses without saving.
///
/// - Parameter dismiss: A closure that dismisses the editor view.
/// - Parameter dismiss: A closure that dismisses the editor view using the environment's dismiss action.
func requestDismiss(_ dismiss: () -> Void)

/// Saves the working copy to the editor override provider.
func save()
/// Saves the working copy and dismisses the editor.
///
/// This saves the document and calls the consumer's dismiss closure with the changed variables. If no consumer
/// dismiss closure was provided, it calls the provided `dismiss` closure instead.
///
/// - Parameter dismiss: A closure that dismisses the editor view using the environment's dismiss action.
func saveAndDismiss(_ dismiss: () -> Void)

/// Dismisses the editor without saving.
///
/// This calls the consumer's dismiss closure with an empty array. If no consumer dismiss closure was provided, it
/// calls the provided `dismiss` closure instead.
///
/// - Parameter dismiss: A closure that dismisses the editor view using the environment's dismiss action.
func dismissWithoutSaving(_ dismiss: () -> Void)

/// Requests clearing all overrides by presenting the clear confirmation alert.
func requestClearAllOverrides()
Expand Down
39 changes: 23 additions & 16 deletions Sources/DevConfiguration/Editor/ConfigVariableEditor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import SwiftUI
/// A SwiftUI view that presents the configuration variable editor.
///
/// `ConfigVariableEditor` is initialized with a ``ConfigVariableReader`` that has editor support enabled and an
/// `onSave` closure that receives the registered variables whose overrides changed.
/// optional `dismiss` closure that receives the registered variables whose overrides changed.
///
/// The consumer is responsible for presentation (sheet, full-screen cover, navigation push, etc.).
/// The consumer is responsible for presentation (sheet, full-screen cover, navigation push, etc.). If no `dismiss`
/// closure is provided, the editor uses the environment's dismiss action.
///
/// .sheet(isPresented: $isEditorPresented) {
/// ConfigVariableEditor(reader: reader) { changedVariables in
/// // Handle changed variables
/// // Handle changed variables and dismiss
/// }
/// }
///
Expand All @@ -29,8 +30,8 @@ import SwiftUI
/// customSectionTitle: "Actions"
/// ) {
/// Button("Reset All") { … }
/// } onSave: { changedVariables in
/// // Handle changed variables
/// } dismiss: { changedVariables in
/// // Handle changed variables and dismiss
/// }
public struct ConfigVariableEditor<CustomSection: View>: View {
/// The list view model created from the reader.
Expand All @@ -50,18 +51,20 @@ public struct ConfigVariableEditor<CustomSection: View>: View {
/// `true`, the view is empty.
/// - customSectionTitle: The title for the custom section.
/// - customSection: A view builder that produces custom content to display in a section at the top of the list.
/// - onSave: A closure called with the registered variables whose overrides changed when the user saves.
/// - dismiss: An optional closure called when the editor is dismissed. It receives the registered variables whose
/// overrides changed, or an empty array if dismissed without saving. If `nil`, the environment's dismiss action
/// is used.
public init(
reader: ConfigVariableReader,
customSectionTitle: LocalizedStringKey,
@ViewBuilder customSection: () -> CustomSection,
onSave: @escaping ([RegisteredConfigVariable]) -> Void,
dismiss: (([RegisteredConfigVariable]) -> Void)? = nil,
) {
self.init(
reader: reader,
customSectionTitle: Text(customSectionTitle),
customSection: customSection,
onSave: onSave,
dismiss: dismiss,
)
}

Expand All @@ -73,16 +76,18 @@ public struct ConfigVariableEditor<CustomSection: View>: View {
/// `true`, the view is empty.
/// - customSectionTitle: A `Text` view to use as the title for the custom section.
/// - customSection: A view builder that produces custom content to display in a section at the top of the list.
/// - onSave: A closure called with the registered variables whose overrides changed when the user saves.
/// - dismiss: An optional closure called when the editor is dismissed. It receives the registered variables whose
/// overrides changed, or an empty array if dismissed without saving. If `nil`, the environment's dismiss action
/// is used.
public init(
reader: ConfigVariableReader,
customSectionTitle: Text,
@ViewBuilder customSection: () -> CustomSection,
onSave: @escaping ([RegisteredConfigVariable]) -> Void,
dismiss: (([RegisteredConfigVariable]) -> Void)? = nil,
) {
self.customSectionTitle = customSectionTitle
self.customSection = customSection()
self._viewModel = Self.makeViewModel(reader: reader, onSave: onSave)
self._viewModel = Self.makeViewModel(reader: reader, dismiss: dismiss)
}


Expand All @@ -99,7 +104,7 @@ public struct ConfigVariableEditor<CustomSection: View>: View {

private static func makeViewModel(
reader: ConfigVariableReader,
onSave: @escaping ([RegisteredConfigVariable]) -> Void,
dismiss: (([RegisteredConfigVariable]) -> Void)?,
) -> State<ConfigVariableListViewModel?> {
guard let editorOverrideProvider = reader.editorOverrideProvider else {
return State(initialValue: nil)
Expand All @@ -117,7 +122,7 @@ public struct ConfigVariableEditor<CustomSection: View>: View {
undoManager: UndoManager(),
)

return State(initialValue: ConfigVariableListViewModel(document: document, onSave: onSave))
return State(initialValue: ConfigVariableListViewModel(document: document, dismiss: dismiss))
}
}

Expand All @@ -128,14 +133,16 @@ extension ConfigVariableEditor where CustomSection == EmptyView {
/// - Parameters:
/// - reader: The configuration variable reader. If the reader was not created with `isEditorEnabled` set to
/// `true`, the view is empty.
/// - onSave: A closure called with the registered variables whose overrides changed when the user saves.
/// - dismiss: An optional closure called when the editor is dismissed. It receives the registered variables whose
/// overrides changed, or an empty array if dismissed without saving. If `nil`, the environment's dismiss action
/// is used.
public init(
reader: ConfigVariableReader,
onSave: @escaping ([RegisteredConfigVariable]) -> Void,
dismiss: (([RegisteredConfigVariable]) -> Void)? = nil,
) {
self.customSectionTitle = Text(verbatim: "")
self.customSection = EmptyView()
self._viewModel = Self.makeViewModel(reader: reader, onSave: onSave)
self._viewModel = Self.makeViewModel(reader: reader, dismiss: dismiss)
}
}

Expand Down

This file was deleted.

10 changes: 0 additions & 10 deletions Sources/DevConfiguration/Resources/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -370,16 +370,6 @@
}
}
}
},
"requiresRelaunchMetadata.keyDisplayText" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Requires Relaunch"
}
}
}
}
},
"version" : "1.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ struct ConfigVariableDetailViewModelTests: RandomValueGenerating {
// set up
var metadata = ConfigVariableMetadata()
metadata.displayName = randomAlphanumericString()
metadata.requiresRelaunch = randomBool()
metadata.isEditable = randomBool()
let destinationTypeName = randomAlphanumericString()
let isSecret = randomBool()
Expand Down
Loading
Loading