FMDCategoryAdapter.java :  » Web » fmydroid » com » estiuka » dev » fmydroid » view » Android Open Source

Android Open Source » Web » fmydroid 
fmydroid » com » estiuka » dev » fmydroid » view » FMDCategoryAdapter.java
/*
 * FMyDroid - an Android application for the FMyLife website.
 * Copyright (C) 2009  Kelykos
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.estiuka.dev.fmydroid.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.estiuka.dev.fmydroid.R;
import com.estiuka.dev.fmydroid.control.FMDAppController;
import com.estiuka.dev.fmydroid.control.FMDError;
import com.estiuka.dev.fmydroid.fmylife.FMDCategories;

/**
 * public adapter class to display the categories {@link FMDCategoryScreen}
 * 
 * @author kelykos
 * 
 */
public class FMDCategoryAdapter extends ArrayAdapter<String> implements
    IFMDDataReceiver {

  /**
   * list of categories
   */
  private FMDCategories mCategories;

  /**
   * application controller
   */
  private FMDAppController mAppController;

  /**
   * activity to be notified when the categories have been updated
   */
  private FMDActivity mActivity = null;

  /**
   * constructor
   * 
   * @param context
   * @param textViewResourceId
   */
  public FMDCategoryAdapter(FMDAppController appController, Context context,
      int textViewResourceId) {
    super(context, textViewResourceId);
    mAppController = appController;
    mCategories = appController.getEngine().getCategories();
    mActivity = appController.getActivity();
  }

  /**
   * getter for the application categories
   * 
   * @return
   */
  public FMDCategories getCategories() {
    return mCategories;
  }

  /**
   * return true if there are some categories
   * 
   * @return
   */
  public boolean hasCategories() {
    return !mCategories.isEmpty();
  }

  /**
   * load the categories if they have not expired
   * 
   * @param expirationDate
   *            to determinate if we need to load the categories from the
   *            resources or from the preferences
   * @return true if the categories could have been loaded
   */
  public boolean loadNonExpiredCategories(long expirationDate) {
    long timeNow = System.currentTimeMillis();
    // we have no category in the preferences so we load them from the resource array
    if (expirationDate == -1) {
      if (mCategories.isEmpty()) {
        mAppController.getEngine().loadCategoriesFromResources();
        setCategoryPreferences();
      }
      setData();
      return true;
    } else if (expirationDate > timeNow) {
      if (mCategories.isEmpty()) {
        mAppController.getEngine().loadCategoriesFromPrefs();
      }
      setData();
      return true;
    }
    return false;
  }

  /**
   * load the categories regardless they may have expired or not
   * 
   * @param expirationDate
   *            to determinate if we need to load the categories from the
   *            resources or from the preferences
   */
  public void loadCategories(long expirationDate) {
    if (expirationDate == -1) {
      if (mCategories.isEmpty()) {
        mAppController.getEngine().loadCategoriesFromResources();
      }
    } else {
      if (mCategories.isEmpty()) {
        mAppController.getEngine().loadCategoriesFromPrefs();
      }
    }
    setData();
  }

  /**
   * method for updating the categories
   */
  public void updateCategories() {
    mActivity.displayProgressDialog(R.string.please_wait,
        R.string.updating_categories);
    mAppController.retrieveFMLData(this);
  }

  /**
   * set the preferences regarding the categories
   */
  private void setCategoryPreferences() {
    mAppController.setCategories(mCategories.getCategoriesAsString());
    mAppController.setLocalizedCategories(mCategories
        .getLocalizedCategoriesAsString());
    long expirationTime = System.currentTimeMillis();
    expirationTime += (3600000 * 24 * 3); // 3 days
    mAppController.setCategoriesExpiration(expirationTime);
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
      LayoutInflater inflater = (LayoutInflater) getContext()
          .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      view = inflater.inflate(
          R.layout.fmd_categoryscreen_list_item_layout, null);
    }
    String category = getItem(position);
    if (category != null) {
      TextView textView = (TextView) view
          .findViewById(R.id.fmd_categoryscreen_list_item);
      textView.setText(category);
    }
    return view;
  }

  private void setData() {
    clear();
    notifyDataSetChanged();
    for (int i = 0; i < mCategories.count(); ++i) {
      add(mCategories.getLocalizedCategory(i));
    }
    notifyDataSetChanged();
  }

  @Override
  public void onDataReceived(Object newData, int requestType, int error) {
    FMDActivity activity = mActivity;
    if (error == FMDError.NO_ERROR) {
      FMDCategories categories = (FMDCategories) newData;
      clear();
      notifyDataSetChanged();
      if ((categories != null) && (!categories.isEmpty())) {
        for (int i = 0; i < categories.count(); ++i) {
          add(categories.getLocalizedCategory(i));
        }
        notifyDataSetChanged();
        // reset the preferences
        mCategories = categories;
        setCategoryPreferences();
      }
      activity.dismissDialog();
    } else {
      activity.dismissDialog();
      if(error != FMDError.UNKNOWN_CATEGORY_ERROR){
        activity.displayErrorDialog(error, true);
      } else {
        activity.displayToastNotification(FMDError.GetErrorString(error));
        updateCategories();
      }
    }
  }
}
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.