Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alert with binding #145

Merged
merged 12 commits into from
Apr 5, 2024
Merged

Conversation

randomeizer
Copy link
Contributor

@randomeizer randomeizer commented Mar 28, 2024

Created alert(item:...) method overloads for View. These allow providing an optional Bindable<Item?> value to the alert(item: ...) parameter, then providing other closures for the title, actions, and message, each passed an honest item, if it's not nil.

For example, a model:

@Observable
class FeatureModel {
  struct ConfirmDelete: Equatable {}

  var confirmDelete: ConfirmDelete?

  func deleteButtonTapped() {
    self.confirmDelete = .init()
  }
  
  func deleteConfirmButtonTapped() {
    // do deletion
    confirmDelete = nil
  }

  func deleteCancelButtonPressed() {
    confirmDelete = nil
  }
}

...and a View:

struct ContentView: View {
  @ObservedObject var model: FeatureModel

  var body: some View {
    Button("Delete Everything") {
      model.deleteButtonTapped()
    }
    List {
      // ...
    }
    .alert("Are you sure?", item: self.$model.confirmDelete) { _ in
      Button("Delete", role: .destructive) {
        model.deleteConfirmButtonTapped()
      }
      Button("Nevermind", role: .cancel) {
        model.deleteCancelButtonPressed()
      }
    } message: { _ in
      Text("Deleting this item cannot be undone.")
    }
  }
}

- Deprecated the `alert(title:unwrapping:...)` and `confirmationDialog(title:unwrapping:...)` functions in `SwiftUINavigation`
- Deprecated the read-only `Bindable.isPresent()` in `SwiftUINavigation.
- Added `alert(_:title:actions:message)` functions to `SwiftUINavigationCore` to expose them to TCA.
- Added `confirmationDialog(_:title:titleVisibility:actions:message)` functions to `SwiftUINavigationCore` to expose them to TCA.
- Added read/write `Bindable.isPresent()` in `SwiftUINavigationCore`.
- Added/updated documentation
- Reordered parameters to more closely match the built-in ones.
- Added overloads without `message` closures.
- Updated documentation
@randomeizer
Copy link
Contributor Author

Feedback wanted, particularly on:

Parameter Order

Should title or item go first? I've split the difference, trying to follow established order in existing overloads. Basically:

  1. If title is a closure, item goes first, then title, then the rest.
  2. If title is a String/LocalizableStringKey, then that goes first, then item, then the rest.

This applies across the alert and confirmationDialog overloads.

isPresent()

Currently, I've deprecated the old read-only isPresent() function in SwiftUINavigation for older versions, and added a new read/write isPresent() in SwiftUINavigationCore, to allow access from TCA.

Not sure if this is the right approach, of if we should just have a new, differently-named (maybe private?) func/var for use by alert/confirmationDialog.

Thoughts?

@stephencelis stephencelis changed the title Assert with binding Alert with binding Apr 4, 2024
Copy link
Member

@stephencelis stephencelis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @randomeizer! Thanks for taking this on! A few comments below that should be easy to tackle, and then I think it'll be good to merge!

Sources/SwiftUINavigationCore/Alert.swift Outdated Show resolved Hide resolved
Sources/SwiftUINavigationCore/Binding.swift Outdated Show resolved Hide resolved
@randomeizer
Copy link
Contributor Author

@stephencelis I believe I've addressed your comments. Maybe double-check the Binding migration - I wasn't 100% certain that I extracted all the relevant elements of Binding into Core, but it does compile and my app that uses this code works as expected.

@@ -1,12 +0,0 @@
# ``SwiftUINavigationCore``
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know why this file got deleted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I assumed that I had added all the documentation, my bad.

@stephencelis stephencelis merged commit 3fff790 into pointfreeco:main Apr 5, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants