mergesort / Boutique

✨ A magical persistence library (and so much more) for state-driven iOS and Mac apps ✨
https://build.ms/boutique/docs
MIT License
920 stars 45 forks source link

Add support for `Animation` on methods #28

Closed grdsdev closed 2 years ago

grdsdev commented 2 years ago

Hi, @mergesort thanks for this awesome library.

I was trying to find a way of animating changes on SwiftUI, but wasn't able to.

Would be nice if this library had something like:

public func add(
    _ items: [Item], 
    removingExistingItems existingItemsStrategy: ItemRemovalStrategy<Item>? = nil,
+   animation: Animation? = nil
) async throws

And then in the implementation side would be just a matter of:

await MainActor.run { [currentValuesDictionary] in
+    withAnimation(animation) {
        self.items = Array(currentValuesDictionary.values)
+    }
}

What do you think about this?

mergesort commented 2 years ago

Hey @grsouza, thanks for the suggestion but the cool thing is we don't need this, animations work out the box with Boutique already! When using Boutique you treat everything you're doing in Boutique like you're working with an array, because you are working with just an array!

Have you checked out the demo project? I ask because it actually include an animation, all done using standard SwiftUI animations. (Note: I've made the animation a stored property, you could just do .easeInOut(duration: 0.35) in line here, that has nothing to do with Boutique but something in this app that I was too lazy to fix something.)

In this case the array backing the data is images, so by appending .animation(animation, value: self.images) as a ViewModifier whenever images changes, it will automatically update in an animated fashion. If you prefer using withAnimation at the call-site that changes images that's possible too! Though my preference is to always use the .animation() modifier because it creates the least potential side effects, and allows more specificity around animations.

Lemme know if that makes sense, and if that takes care of your use case as I believe it should.

mergesort commented 2 years ago

I'm closing out this issue because I think what I wrote should help you with what you need @grsouza. If it doesn't feel free to comment! But I do think what I wrote is a more idiomatic approach than adding withAnimation to the add function.