package circlebinder.common.checklist;

import android.content.Context;
import android.widget.Spinner;

import net.ichigotake.common.widget.OnItemSelectedEventListener;
import net.ichigotake.common.widget.OnItemSelectedListener;

import java.util.List;

public final class ChecklistSelectorContainer {

    private final Context context;
    private final Spinner selector;
    private final OnItemSelectedListener<ChecklistColor> onItemSelectedListener;

    public ChecklistSelectorContainer(Spinner selector) {
        this.context = selector.getContext();
        this.selector = selector;
        this.onItemSelectedListener = new OnItemSelectedListener<ChecklistColor>();
    }

    public void addOnItemSelectedListener(OnItemSelectedEventListener<ChecklistColor> listener) {
        onItemSelectedListener.addOnItemSelectedListener(listener);
    }

    public void initialize(List<ChecklistColor> selectorItems, ChecklistColor selectedColor) {
        ChecklistSelectorBinder binder = new ChecklistSelectorBinder();
        ChecklistSelectorAdapter adapter = new ChecklistSelectorAdapter(context, binder);
        adapter.clear();

        int position = 0;
        int presetPosition = 0;
        for (ChecklistColor color : selectorItems) {
            adapter.add(color);
            position++;
            if (color.equals(selectedColor)) {
                presetPosition = position;
            }
        }
        selector.setAdapter(adapter);
        selector.setSelection(presetPosition);
        selector.setOnItemSelectedListener(onItemSelectedListener);
    }

}

@@ -1281,32 +1281,8 @@
 r);%0A
-            position++;%0A
     
@@ -1373,32 +1373,56 @@
 ;%0A            %7D%0A
+            position++;%0A
         %7D%0A      

fix invalid updaet when checklist changed