LayoutKit
LayoutKit copied to clipboard
LayoutAdapter with automatic animations
I found this code quite useful in my project, but it might not be something you would find is in scope of LayoutKit. If so fell free to close this PR.
Currently ReloadableViewLayoutAdapter
uses layoutProvider: @escaping (Void) -> T
to create new layouts every time reload is called.
Instead of recreating all layouts, it is possible to use BatchUpdates
to create new Layouts and Section only when needed. This improves performance and simplifies layout creation. Also, if BatchUpdates
is used to animate content change, but all of the layouts are recreated, only animated cells will have newly created layouts which can lead to weird behaviour.
Furthermore, it is possible to create BatchUpdates
from model being presented. Using this two functionalities it is possible to simply present model and have animations calculated and performed automatically.
For example, all of the animations bellow are done by presenting model using LayoutAdapterWithAutomaticBatchUpdates
without any custom calculations.
This is done by calling
public func reload(
viewModel: [[Any]],
elementCompareCallback: @escaping (Any, Any) -> Bool = { _ in return false },
layoutProvider: @escaping (IndexPath) -> Layout,
animated: Bool = true,
completion: (() -> Void)? = nil)
on LayoutAdapterWithAutomaticBatchUpdates
. Method calculates BatchUpdates
based on model change, uses BatchUpdates
to calculate layouts change and presents them using ReloadableViewLayoutAdapter.reload
.
Hi @Rep2 , this looks very useful and interesting! Thank you for submitting this PR. I'm still reading through it and thinking about it and I'll have more questions later.
(Meanwhile I just have one minor comment that class LayoutAdapterCacheHandler
is repeated in two files in this PR :-) )
Thanks for the response. I agree with and understand your position. I have no problem if you modify and/or use this code outside of this PR.
If you will proceed with this idea feel free to add me to the future PRs. I will respond to the comments as soon as I find the time.