AbstractTabScheduleViewInflater.java :  » Framework » festivales » com » projectsexception » festivales » schedule » view » Android Open Source

Android Open Source » Framework » festivales 
festivales » com » projectsexception » festivales » schedule » view » AbstractTabScheduleViewInflater.java
package com.projectsexception.festivales.schedule.view;

import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.projectsexception.festivales.BandActivity;
import com.projectsexception.festivales.R;
import com.projectsexception.festivales.favorites.model.Favorite;
import com.projectsexception.festivales.setup.ProjectResources;
import com.projectsexception.festivales.setup.ShowInfo;
import com.projectsexception.festivales.util.DateFormater;

public abstract class AbstractTabScheduleViewInflater implements TabScheduleViewInflater {
    
    protected Activity activity;
    protected LayoutInflater inflater;
    protected final String[] colors = {"#9FB6CD", "#6C7B8B"};
    
    public AbstractTabScheduleViewInflater(Activity activity) {
        this.activity = activity;
        this.inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    public void inflateDates(List<Date> showDates, Date actualDate) {
        final ViewGroup buttonGroup = (ViewGroup) activity.findViewById(R.id.scroll_dates);
        int buttonId = 1;
        ToggleButton b;
        for (Date date : showDates) {
            b = (ToggleButton) inflater.inflate(R.layout.tab_schedule_date_button, null);
            if (date.equals(actualDate)) {
                b.setChecked(true);
            }
            b.setId(buttonId);
            String text = DateFormater.formatShowDate(date);
            b.setText(text);
            b.setTextOn(text);
            b.setTextOff(text);
            b.setTag(date);
            b.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    ToggleButton activeButton = (ToggleButton) view;
                    ToggleButton button;
                    for (int i = 0 ; i < buttonGroup.getChildCount() ; i++) {
                        button = (ToggleButton) buttonGroup.getChildAt(i);
                        if (button.getId() != activeButton.getId()) {
                            button.setChecked(false);
                        } else {
                            button.setChecked(true);
                        }
                    }
                    Date date = (Date) view.getTag();
                    AbstractTabScheduleViewInflater.this.inflateMainContent(date);
                }
            });
            buttonGroup.addView(b);
            buttonId++;
        }
    }
    
    public abstract void inflateMainContent(Date actualDate);
    
    public void inflateView(Date actualDate) {
        activity.setContentView(R.layout.tab_schedule_view);
        
        ProjectResources res = ProjectResources.getInstance();
        List<Date> showDates = res.getShowDates();
        inflateDates(showDates, actualDate);
        
        inflateMainContent(actualDate);
    }
    
    /**
     * Crea la vista del grupo
     * @param show informacin del concierto
     * @param favorites lista de favoritos
     * @param padding
     * @param layoutParams
     * @return
     */
    protected LinearLayout createBandView(ShowInfo show, List<Favorite> favorites, int[] padding, LayoutParams layoutParams) {
        LinearLayout layoutBand = (LinearLayout) inflater.inflate(R.layout.tab_schedule_band, null);
        if (padding != null) {
            layoutBand.setPadding(padding[0], padding[1], padding[2], padding[3]);
        }
        LinearLayout bandContent = (LinearLayout) layoutBand.findViewById(R.id.band_view);
        if (layoutParams != null) {
            bandContent.setLayoutParams(layoutParams);
        }
        final int showId = show.getId();
        bandContent.setOnClickListener(new OnClickListener() {                
            @Override
            public void onClick(View view) {
                Intent band = new Intent(activity, BandActivity.class);
                band.putExtra(BandActivity.SHOW_ID, showId);
                activity.startActivity(band);
            }
        });
        TextView t = (TextView) layoutBand.findViewById(R.id.band_name);
        t.setText(show.getBand().getName());
        
        final ImageView imageFavorite = (ImageView) layoutBand.findViewById(R.id.band_favorite);
        Favorite showFavorite = getFavorite(show, favorites);
        if (showFavorite == null) {
            showFavorite = new Favorite(show);
            imageFavorite.setImageResource(R.drawable.tab_favorite_off);
        } else {
            imageFavorite.setImageResource(R.drawable.tab_favorite_on);
        }
        imageFavorite.setTag(showFavorite);
        imageFavorite.setOnClickListener(new OnClickListener() {                
            @Override
            public void onClick(View view) {
                ProjectResources res = ProjectResources.getInstance();
                Favorite fav = (Favorite) imageFavorite.getTag();
                if (fav.getId() == 0) {
                    // Quiere decir que no est entre los favoritos
                    imageFavorite.setImageResource(R.drawable.tab_favorite_on);
                    res.saveFavorite(fav);
                } else {
                    // Ya estaba en los favoritos, luego hay que eliminarlo
                    imageFavorite.setImageResource(R.drawable.tab_favorite_off);
                    res.deleteFavorite(fav);
                }
            }
        });
        return layoutBand;
    }
    
    private Favorite getFavorite(ShowInfo showInfo, List<Favorite> favorites) {
        for (Favorite fav : favorites) {
            if (fav.getShowInfo().getId() == showInfo.getId()) {
                return fav;
            }
        }
        return null;
    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.