package ca.antonious.viewcelladapter;

import java.util.HashMap;
import java.util.Map;

import ca.antonious.viewcelladapter.viewcells.AbstractViewCell;

/**
 * Created by George on 2016-12-28.
 */

public class BindListenersHelper {
    private static Map<Class<?>, ListenerBinder> cachedListenerBinders = new HashMap<>();

    @SuppressWarnings("unchecked")
    public static void bindListenersTo(AbstractViewCell viewCell, BaseViewHolder viewHolder, ListenerCollection listeners) {
        ListenerBinder listenerBinder = getListenerBinderFor(viewCell);

        if (listenerBinder != null) {
            listenerBinder.bindListeners(viewCell, viewHolder, listeners);
        }
    }

    private static ListenerBinder getListenerBinderFor(AbstractViewCell viewCell) {
        if (cachedListenerBinders.containsKey(viewCell.getClass())) {
            return cachedListenerBinders.get(viewCell.getClass());
        }

        try {
            String listenerBinderName = viewCell.getClass().getCanonicalName() + "_ListenerBinder";
            Class<?> listenerBinderClass = Class.forName(listenerBinderName);
            ListenerBinder listenerBinder = (ListenerBinder) listenerBinderClass.newInstance();
            cachedListenerBinders.put(viewCell.getClass(), listenerBinder);
        } catch (Exception e) {
            e.printStackTrace();
            cachedListenerBinders.put(viewCell.getClass(), null);
        }

        return cachedListenerBinders.get(viewCell.getClass());
    }
}

@@ -75,19 +75,67 @@
 va.util.
-Map
+HashSet;%0Aimport java.util.Map;%0Aimport java.util.Set
 ;%0A%0Aimpor
@@ -363,16 +363,112 @@
 Map%3C%3E();
+%0A    private static Map%3CClass%3C?%3E, Set%3CListenerBinder%3E%3E listenerBinderMappings = new HashMap%3C%3E();
 %0A%0A    @S
@@ -621,32 +621,37 @@
 ners) %7B%0A        
+for (
 ListenerBinder l
@@ -663,18 +663,17 @@
 erBinder
- =
+:
  getList
@@ -678,24 +678,25 @@
 stenerBinder
+s
 For(viewCell
@@ -699,46 +699,20 @@
 Cell
-);%0A%0A        if (listenerBinder != null
+.getClass())
 ) %7B%0A
@@ -822,16 +822,20 @@
  static 
+Set%3C
 Listener
@@ -840,16 +840,17 @@
 erBinder
+%3E
  getList
@@ -859,20 +859,37 @@
 erBinder
+s
 For(
+Class%3C? extends 
 Abstract
@@ -888,32 +888,33 @@
 AbstractViewCell
+%3E
  viewCell) %7B%0A   
@@ -906,16 +906,21 @@
 viewCell
+Class
 ) %7B%0A    
@@ -927,23 +927,18 @@
     if (
-cachedL
+!l
 istenerB
@@ -942,16 +942,23 @@
 erBinder
+Mapping
 s.contai
@@ -967,35 +967,29 @@
 Key(viewCell
-.get
 Class
-()
 )) %7B%0A       
@@ -989,38 +989,98 @@
             
-return cachedL
+createListenerBinderMappingsFor(viewCellClass);%0A        %7D%0A        return l
 istenerBinde
@@ -1072,32 +1072,39 @@
 n listenerBinder
+Mapping
 s.get(viewCell.g
@@ -1093,32 +1093,37 @@
 ngs.get(viewCell
+Class
 .getClass());%0A  
@@ -1128,88 +1128,975 @@
     
+%7D%0A%0A
     
-%7D%0A%0A        try %7B%0A            String listenerBinderName = viewCell.getClass()
+private static void createListenerBinderMappingsFor(Class%3C? extends AbstractViewCell%3E viewCellClass) %7B%0A        Class%3C?%3E currentClass = viewCellClass;%0A        Set%3CListenerBinder%3E listenerBinders = new HashSet%3C%3E();%0A%0A        while (currentClass != null) %7B%0A            ListenerBinder listenerBinder = getListenerBinderFor(currentClass);%0A%0A            if (listenerBinder != null) %7B%0A                listenerBinders.add(listenerBinder);%0A            %7D%0A            %0A            currentClass = currentClass.getSuperclass();%0A        %7D%0A%0A        listenerBinderMappings.put(viewCellClass, listenerBinders);%0A    %7D%0A%0A    private static ListenerBinder getListenerBinderFor(Class%3C?%3E clazz) %7B%0A        if (!cachedListenerBinders.containsKey(clazz)) %7B%0A            loadListenerBinderFor(clazz);%0A        %7D%0A        return cachedListenerBinders.get(clazz);%0A    %7D%0A%0A    private static void loadListenerBinderFor(Class%3C?%3E clazz) %7B%0A        try %7B%0A            String listenerBinderName = clazz
 .get
@@ -2344,35 +2344,21 @@
 ers.put(
-viewCell.getClass()
+clazz
 , listen
@@ -2416,168 +2416,57 @@
     
-e.printStackTrace();%0A            cachedListenerBinders.put(viewCell.getClass(), null);%0A        %7D%0A%0A        return cachedListenerBinders.get(viewCell.getClass());
+cachedListenerBinders.put(clazz, null);%0A        %7D
 %0A   

Call superclass ListenerBinder instances on subclasses