package com.hilton.gitatouille;

import java.util.List;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

public class ProgitListAdapter extends BaseExpandableListAdapter {
    private static final int ANIMATION_DEFAULT_DURATION = 400;
    private static final int ANIMATION_DEFAULT_DELAY = 100;
    private static final String TAG = ProgitListAdapter.class.getName();
    private List<ProGitChapter> mProGitChapters;
    private Context mContext;
    private LayoutInflater mViewFactory;
    
    public ProgitListAdapter(Context ctx) {
        mContext = ctx;
        mProGitChapters = ProGit.getChapters();
        mViewFactory = LayoutInflater.from(ctx);
    }
    
    @Override
    public ProGitSection getChild(int groupPosition, int childPosition) {
        return mProGitChapters.get(groupPosition).getSection(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        TextView name = null;
        if (convertView == null) {
            name = (TextView) mViewFactory.inflate(R.layout.progit_child_item, null, false);
        } else {
            name = (TextView) convertView;
        }
        name.setText(getChild(groupPosition, childPosition).mName);
        setupChildAnimation(childPosition, name, parent);
        return name;
    }
    
    @SuppressLint("NewApi")
    private void setupChildAnimation(int position, View view, ViewGroup parent) {
        Log.e(TAG, "setupChildAnimation position " + position);
        view.setAlpha(0);
        Animator flyIn = ObjectAnimator.ofFloat(view, "translationX", -parent.getWidth(), 0);
        Animator alpha = ObjectAnimator.ofFloat(view, "alpha", 0, 1);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(alpha, flyIn);
        int delay = ANIMATION_DEFAULT_DELAY;
        set.setStartDelay(position * delay);
        set.setDuration(ANIMATION_DEFAULT_DURATION);
        set.start();
    }
    
    @Override
    public int getChildrenCount(int groupPosition) {
        return mProGitChapters.get(groupPosition).getSectionCount();
    }

    @Override
    public ProGitChapter getGroup(int groupPosition) {
        return mProGitChapters.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return mProGitChapters.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
    
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        View root = null;
        if (convertView == null) {
            root = mViewFactory.inflate(R.layout.progit_group_item, null, false);
            setupAnimation(groupPosition, root, parent);
        } else {
            root = convertView;
        }
        TextView textView = (TextView) root.findViewById(R.id.name);
        textView.setText(getGroup(groupPosition).mName);
        ImageView indicator = (ImageView) root.findViewById(R.id.indicator);
        indicator.setImageResource(isExpanded ? R.drawable.button_tab_to_close : R.drawable.button_tab_to_open);
        return root;
    }

    @SuppressLint("NewApi")
    private void setupAnimation(int position, View view, ViewGroup parent) {
        Log.e(TAG, "setupAnimation position " + position);
        view.setAlpha(0);
        Animator flyIn = ObjectAnimator.ofFloat(view, "translationY", 500, 0);
        Animator alpha = ObjectAnimator.ofFloat(view, "alpha", 0, 1);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(flyIn, alpha);
        int delay = ANIMATION_DEFAULT_DELAY;
        set.setStartDelay(position * delay);
        set.setDuration(ANIMATION_DEFAULT_DURATION);
        set.start();
    }
    
    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

@@ -1411,32 +1411,60 @@
 osition;%0A    %7D%0A%0A
+    @SuppressLint(%22NewApi%22)%0A
     @Override%0A  
@@ -1899,298 +1899,8 @@
 e);%0A
-        setupChildAnimation(childPosition, name, parent);%0A        return name;%0A    %7D%0A    %0A    @SuppressLint(%22NewApi%22)%0A    private void setupChildAnimation(int position, View view, ViewGroup parent) %7B%0A        Log.e(TAG, %22setupChildAnimation position %22 + position);%0A        view.setAlpha(0);%0A
     
@@ -1935,36 +1935,36 @@
 nimator.ofFloat(
-view
+name
 , %22translationX%22
@@ -2001,317 +2001,79 @@
     
-Animator alpha = ObjectAnimator.ofFloat(view, %22alpha%22, 0, 1);%0A        AnimatorSet set = new AnimatorSet();%0A        set.playTogether(alpha, flyIn);%0A        int delay = ANIMATION_DEFAULT_DELAY;%0A        set.setStartDelay(position * delay);%0A        set.setDuration(ANIMATION_DEFAULT_DURATION);%0A        set.start()
+setupAnimation(childPosition, name, parent, flyIn);%0A        return name
 ;%0A  
@@ -2539,32 +2539,60 @@
 ion;%0A    %7D%0A    %0A
+    @SuppressLint(%22NewApi%22)%0A
     @Override%0A  
@@ -2842,32 +2842,115 @@
 , null, false);%0A
+            Animator flyIn = ObjectAnimator.ofFloat(root, %22translationY%22, 500, 0);%0A
             setu
@@ -2987,16 +2987,23 @@
 , parent
+, flyIn
 );%0A     
@@ -3493,32 +3493,48 @@
 ViewGroup parent
+, Animator flyIn
 ) %7B%0A        Log.
@@ -3610,87 +3610,8 @@
 0);%0A
-        Animator flyIn = ObjectAnimator.ofFloat(view, %22translationY%22, 500, 0);%0A
     

Refactor to remove duplicate code