All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.bluebill.mobile.android.taxonomy.browser.TaxonomyBrowserController Maven / Gradle / Ivy

/***********************************************************************************************************************
 *
 * blueBill Mobile - open source birdwatching
 * ==========================================
 *
 * Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
 * http://bluebill.tidalwave.it/mobile/
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************
 *
 * $Id: TaxonomyBrowserController.java,v 97140ea1539e 2010/06/11 14:13:55 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.taxonomy.browser;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.bluebill.taxonomy.Taxon;
import it.tidalwave.bluebill.taxonomy.Taxonomy.Type;
import it.tidalwave.bluebill.mobile.taxonomy.TaxonomyPreferences;
import it.tidalwave.bluebill.mobile.android.taxonomy.TaxonIntentHelper;
import it.tidalwave.bluebill.mobile.android.taxonomy.TaxonomyAdapter;
import it.tidalwave.mobile.android.ui.ControlFlow;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
public class TaxonomyBrowserController
  {
    private static final String CLASS = TaxonomyBrowserController.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);

    @Nonnull
    private final Activity activity;

    @Nonnull
    private final Context context;

    @Nonnull
    private final SharedPreferences sharedPreferences;

    @Nonnull
    private final TaxonomyPreferences preferences;

    @Nonnull
    private final Type type;

    private final List taxa = new ArrayList();

    private Locale lastSortLocale;

    @Nonnull
    private final TaxonomyAdapter adapter;

    private ControlFlow controlFlow;

    public final static int PICK = 1;

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private final OnItemClickListener taxonBrowserListener = new OnItemClickListener()
      {
        @Override
        public void onItemClick (final @Nonnull AdapterView a,
                                 final @Nonnull View view,
                                 final @Nonnegative int position,
                                 final @Nonnegative long id)
          {
            final Taxon taxon = (Taxon)adapter.getItem(position);

            try
              {
                final Intent intent = TaxonIntentHelper.intentFor(taxon);

                if (type.equals(Type.SPECIES)) // final one
                  {
                    controlFlow.toNextStep(intent);
                  }
                else
                  {
                    controlFlow.toNextStep(intent, ControlFlow.USE_INTENT_FILTER);
                  }
              }
            catch (NotFoundException e)
              {
                throw new RuntimeException(e);
              }
          }
      };

    /*******************************************************************************************************************
     *
     * Recomputes the data whenever preference have been changed (e.g. because the locale(s) have changed).
     *
     ******************************************************************************************************************/
    private final OnSharedPreferenceChangeListener preferenceChangeListener = new OnSharedPreferenceChangeListener()
      {
        public void onSharedPreferenceChanged (final @Nonnull SharedPreferences sp, final @Nonnull String string)
          {
            lastSortLocale = null; // mark "dirty"
          }
      };

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public TaxonomyBrowserController (final @Nonnull Activity activity,
                                      final @Nonnull TaxonomyPreferences preferences,
                                      final @Nonnull Type type,
                                      final @Nonnull List taxa)
      {
        this.activity = activity;
        this.context = activity.getBaseContext();
        this.preferences = preferences;
        this.type = type;
        this.taxa.clear();
        this.taxa.addAll(taxa);
        adapter = new TaxonomyAdapter(context, preferences, this.taxa);
        sharedPreferences = Locator.find(SharedPreferences.class);
        sharedPreferences.registerOnSharedPreferenceChangeListener(preferenceChangeListener);
        controlFlow = ControlFlow.forActivity(activity);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void dispose()
      {
        sharedPreferences.unregisterOnSharedPreferenceChangeListener(preferenceChangeListener);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public ListAdapter getTaxonBrowserAdapter()
      {
        return adapter;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public OnItemClickListener getTaxonBrowserListener()
      {
        return taxonBrowserListener;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void ensureTaxaAreSorted()
      {
        if ((lastSortLocale == null) || (!lastSortLocale.equals(Locale.getDefault())))
          {
            lastSortLocale = Locale.getDefault();
            logger.info("starting sort %d taxa...", taxa.size());
            final long time = System.currentTimeMillis();
            Collections.sort(taxa, preferences.getTaxonComparator());
            logger.info(">>>> taxa sorted in %d msec", System.currentTimeMillis() - time);

            if (adapter != null)
              {
                adapter.notifyDataSetChanged();
              }
          }
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy