koral-- / android-gif-drawable

Views and Drawable for displaying animated GIFs on Android
Other
9.54k stars 1.78k forks source link

MultiCallback class must have own Handler #216

Open jonasasx opened 8 years ago

jonasasx commented 8 years ago

If a Drawble, which has a MultiCallback, schedules a task, this task will be executed in every Drawable.Callback member of MultiCallback. It will be correctly to set a Handler to schedule tasks in constructor or setter of MultiCallback. Handler must be single per every Drawable.

koral-- commented 8 years ago

Why Handler should be used there?

jonasasx commented 8 years ago

Not Handler exactly, it may be any main-thread executor. But, Android's View class uses Handler to schedule Drawable's tasks. Unfortunately, Android's Handler doesn't implements Java's abstract interface java.util.concurrent.Executor.

koral-- commented 8 years ago

I meant why any additional mechanism should be used?

jonasasx commented 8 years ago

If Drawable wants to schedule some task, it can use it's Callback. But if this Callback has MultiCallback implementation, the task will be executed by each of the members of MultiCallback. For example, my Drawable wants to implement some animation. It calls Drawable#getCallback().scheduleDrawable(Drawable, Runnable, long), where Runnable has some counter (increment). If Drawable#getCallback() returns MultiCallback, the task will be executed many times (the counter will be incremented many times) by only single call of scheduleDrawable. Drawable doesn't expect such behavior.

koral-- commented 8 years ago

OK, I see now, however there are use cases where executing Runnable on each callback is intended. Eg. I have a single instance of custom GifDrawable subclass which I want do display in several custom ImageViews which do some extra action apart from displaying frames which is fired when Runnable is scheduled. For example several fade-out steps while displaying given frame. In such case View could change its alpha inside scheduleDrawable(). That action must be executed for each View not only for one.

Another important issue is that callback implementation may decide to not execute Runnable eg. View calls first verifyDrawable() and then optionally enqueues Runnable.

So to be complete it can be configurable eg. in MultiCallback constructor if Runnables are dispatched to each child callback or executed once as you said.

jonasasx commented 8 years ago

You can implement both of these: single executor and executors from Callback members.