LayoutKit icon indicating copy to clipboard operation
LayoutKit copied to clipboard

LayoutAdapter with automatic animations

Open Rep2 opened this issue 7 years ago • 2 comments

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.

animationsexample

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.

Rep2 avatar May 16 '17 16:05 Rep2

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 :-) )

staguer avatar May 26 '17 02:05 staguer

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.

Rep2 avatar May 31 '17 11:05 Rep2