r40 - in trunk: . res/drawable res/layout res/values res/values-fr res/xml src src/fr/ifremer/wlo src/fr/ifremer/wlo/preferences src/fr/ifremer/wlo/storage src/fr/ifremer/wlo/utils
Author: kmorin Date: 2014-01-29 19:51:33 +0100 (Wed, 29 Jan 2014) New Revision: 40 Url: http://forge.codelutin.com/projects/wlo/repository/revisions/40 Log: refs #4183 Liste de favoris Added: trunk/res/drawable/favorite_toggle.xml trunk/res/layout/autocomplete_text_view_with_favorites.xml trunk/src/android/ trunk/src/fr/ifremer/wlo/preferences/MultiSelectItemPreference.java trunk/src/fr/ifremer/wlo/preferences/WloMultiSelectListPreference.java trunk/src/fr/ifremer/wlo/utils/WloAutoCompleteTextViewWithFavorites.java Modified: trunk/AndroidManifest.xml trunk/res/layout/scientific_species_form.xml trunk/res/values-fr/strings.xml trunk/res/values/strings.xml trunk/res/xml/preferences.xml trunk/src/fr/ifremer/wlo/BigFinCommunicationService.java trunk/src/fr/ifremer/wlo/ScientificSpeciesFormActivity.java trunk/src/fr/ifremer/wlo/WloModelEditionActivity.java trunk/src/fr/ifremer/wlo/preferences/SettingsActivity.java trunk/src/fr/ifremer/wlo/storage/DataCache.java Modified: trunk/AndroidManifest.xml =================================================================== --- trunk/AndroidManifest.xml 2014-01-29 13:38:23 UTC (rev 39) +++ trunk/AndroidManifest.xml 2014-01-29 18:51:33 UTC (rev 40) @@ -18,7 +18,7 @@ android:versionCode="1" android:versionName="1.0"> - <uses-sdk minSdkVersion="7" + <uses-sdk minSdkVersion="15" android:targetSdkVersion="15"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> Added: trunk/res/drawable/favorite_toggle.xml =================================================================== --- trunk/res/drawable/favorite_toggle.xml (rev 0) +++ trunk/res/drawable/favorite_toggle.xml 2014-01-29 18:51:33 UTC (rev 40) @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="utf-8"?> + +<selector xmlns:android="http://schemas.android.com/apk/res/android"> + <!-- When selected, use grey --> + <item android:drawable="@android:drawable/btn_star_big_on" + android:state_checked="true" /> + <!-- When not selected, use white--> + <item android:drawable="@android:drawable/btn_star_big_off" + android:state_checked="false"/> + +</selector> \ No newline at end of file Added: trunk/res/layout/autocomplete_text_view_with_favorites.xml =================================================================== --- trunk/res/layout/autocomplete_text_view_with_favorites.xml (rev 0) +++ trunk/res/layout/autocomplete_text_view_with_favorites.xml 2014-01-29 18:51:33 UTC (rev 40) @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="utf-8"?> + +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:orientation="horizontal" + android:layout_width="match_parent" + android:layout_height="match_parent"> + + <fr.ifremer.wlo.utils.WloAutoCompleteTextView + android:id="@+id/autocomplete_text_view" + android:layout_width="0dp" + android:layout_height="wrap_content" + android:layout_weight="1" + android:singleLine="true" + android:hint="@string/undefined"/> + + <ToggleButton android:id="@+id/favorite_button" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:background="@drawable/favorite_toggle" + android:textOff="" + android:textOn=""/> +</LinearLayout> \ No newline at end of file Modified: trunk/res/layout/scientific_species_form.xml =================================================================== --- trunk/res/layout/scientific_species_form.xml 2014-01-29 13:38:23 UTC (rev 39) +++ trunk/res/layout/scientific_species_form.xml 2014-01-29 18:51:33 UTC (rev 40) @@ -19,12 +19,10 @@ android:layout_width="match_parent" android:layout_height="wrap_content"/> - <fr.ifremer.wlo.utils.WloAutoCompleteTextView + <fr.ifremer.wlo.utils.WloAutoCompleteTextViewWithFavorites android:id="@+id/scientific_species_form_name" android:layout_width="match_parent" - android:layout_height="wrap_content" - android:singleLine="true" - android:hint="@string/undefined"/> + android:layout_height="wrap_content"/> <CheckBox android:id="@+id/scientific_species_form_takingActivation" android:text="@string/scientific_species_form_taking_activation" Modified: trunk/res/values/strings.xml =================================================================== --- trunk/res/values/strings.xml 2014-01-29 13:38:23 UTC (rev 39) +++ trunk/res/values/strings.xml 2014-01-29 18:51:33 UTC (rev 40) @@ -85,6 +85,7 @@ <string name="preferences_ichtyometer">Ichtyometer</string> <string name="preferences_referentials">Referentials</string> <string name="preferences_imports">Imports</string> + <string name="preferences_imports">Favorites</string> <string name="preferences_import_summary">%s items currently</string> <string name="preferences_import_ages">Import ages</string> @@ -102,6 +103,9 @@ <string name="preferences_import_no_external_storage_message">No externat storage is available.\nWould you like to import the data without saving the source file?</string> <string name="preferences_importing_referential">Importing referential data</string> + <string name="preferences_favorites_commercial_species">Commercial species</string> + <string name="preferences_favorites_scientific_species">Scientific species</string> + <string name="import_new_element_number">%s new items imported</string> <string name="import_error">An error occurred during the import.\nCheck that the header names of the file you chose are correct.</string> Modified: trunk/res/values-fr/strings.xml =================================================================== --- trunk/res/values-fr/strings.xml 2014-01-29 13:38:23 UTC (rev 39) +++ trunk/res/values-fr/strings.xml 2014-01-29 18:51:33 UTC (rev 40) @@ -80,6 +80,7 @@ <string name="preferences_use_place">Lieu d\'utilisation</string> <string name="preferences_referentials">Référentiels</string> <string name="preferences_imports">Imports</string> + <string name="preferences_favorites">Favoris</string> <string name="preferences_import_summary">%s éléments actuellement</string> <string name="preferences_import_ages">Importer des âges</string> @@ -100,6 +101,9 @@ <string name="import_new_element_number">%s nouveaux éléments importés</string> <string name="import_error">Une erreur s\'est produite durant l\'import.\nVérifiez que les entêtes du fichier que vous avez choisi sont corrects.</string> + <string name="preferences_favorites_commercial_species">Espèces commerciales</string> + <string name="preferences_favorites_scientific_species">Espèces scientifiques</string> + <!-- Contexts --> <string name="add_context">Créer un nouveau contexte</string> <string name="contexts_title">Contextes</string> Modified: trunk/res/xml/preferences.xml =================================================================== --- trunk/res/xml/preferences.xml 2014-01-29 13:38:23 UTC (rev 39) +++ trunk/res/xml/preferences.xml 2014-01-29 18:51:33 UTC (rev 40) @@ -66,6 +66,20 @@ </PreferenceScreen> + <PreferenceScreen + android:key="preferences_favorites" + android:title="@string/preferences_favorites"> + + <fr.ifremer.wlo.preferences.WloMultiSelectListPreference + android:key="preferences_favorites_commercial_species" + android:title="@string/preferences_favorites_commercial_species"/> + + <fr.ifremer.wlo.preferences.WloMultiSelectListPreference + android:key="preferences_favorites_scientific_species" + android:title="@string/preferences_favorites_scientific_species"/> + + </PreferenceScreen> + </PreferenceCategory> </PreferenceScreen> \ No newline at end of file Modified: trunk/src/fr/ifremer/wlo/BigFinCommunicationService.java =================================================================== --- trunk/src/fr/ifremer/wlo/BigFinCommunicationService.java 2014-01-29 13:38:23 UTC (rev 39) +++ trunk/src/fr/ifremer/wlo/BigFinCommunicationService.java 2014-01-29 18:51:33 UTC (rev 40) @@ -19,8 +19,8 @@ /* * #%L * WLO - * $Id:$ - * $HeadURL:$ + * $Id$ + * $HeadURL$ * %% * Copyright (C) 2013 - 2014 Ifremer * %% @@ -554,7 +554,9 @@ // Read from the InputStream bytes = mmInStream.read(buffer); + Log.d(TAG, "received " + new String(buffer, 0, bytes)); // Send the obtained bytes to the UI Activity + sendMessage(MESSAGE_READ, bytes, -1, buffer); } catch (IOException e) { Modified: trunk/src/fr/ifremer/wlo/ScientificSpeciesFormActivity.java =================================================================== --- trunk/src/fr/ifremer/wlo/ScientificSpeciesFormActivity.java 2014-01-29 13:38:23 UTC (rev 39) +++ trunk/src/fr/ifremer/wlo/ScientificSpeciesFormActivity.java 2014-01-29 18:51:33 UTC (rev 40) @@ -25,19 +25,28 @@ */ import android.content.Intent; +import android.content.SharedPreferences; import android.os.Bundle; +import android.preference.PreferenceManager; import android.util.Log; import android.view.View; import android.widget.CheckBox; import android.widget.CompoundButton; +import com.google.common.base.Predicate; +import com.google.common.collect.Collections2; import com.google.common.collect.Lists; +import com.google.common.collect.Sets; import fr.ifremer.wlo.measurement.MeasurementActivity; import fr.ifremer.wlo.models.ScientificSpeciesModel; import fr.ifremer.wlo.models.referentials.ScientificSpecies; +import fr.ifremer.wlo.preferences.MultiSelectItemPreference; import fr.ifremer.wlo.storage.DataCache; import fr.ifremer.wlo.storage.WloSqlOpenHelper; +import fr.ifremer.wlo.utils.WloAutoCompleteTextViewWithFavorites; +import java.util.Collection; import java.util.List; +import java.util.Set; /** * @author kmorin <kmorin@codelutin.com> @@ -76,8 +85,20 @@ // init editors List<ScientificSpecies> scientificSpecies = Lists.newArrayList(DataCache.getAllScientificSpecies(this)); - initAutoCompleteTextView(R.id.scientific_species_form_name, ScientificSpeciesModel.COLUMN_NAME, scientificSpecies); + SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); + final Set<String> favoriteIds = sharedPref.getStringSet(MultiSelectItemPreference.SCIENTIFIC_SPECIES_FAVORITES.getKey(), + Sets.<String>newHashSet()); + Collection<ScientificSpecies> favorites = Collections2.filter(scientificSpecies, new Predicate<ScientificSpecies>() { + @Override + public boolean apply(ScientificSpecies input) { + return favoriteIds.contains(input.getId()); + } + }); + + WloAutoCompleteTextViewWithFavorites actvwf = (WloAutoCompleteTextViewWithFavorites) findViewById(R.id.scientific_species_form_name); + initAutoCompleteTextView(actvwf.getAutoCompleteTextView(), ScientificSpeciesModel.COLUMN_NAME, scientificSpecies, favorites); + CheckBox takingActivationEditor = (CheckBox) findViewById(R.id.scientific_species_form_takingActivation); takingActivationEditor.setChecked(model.isTakingActivation()); takingActivationEditor.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { Modified: trunk/src/fr/ifremer/wlo/WloModelEditionActivity.java =================================================================== --- trunk/src/fr/ifremer/wlo/WloModelEditionActivity.java 2014-01-29 13:38:23 UTC (rev 39) +++ trunk/src/fr/ifremer/wlo/WloModelEditionActivity.java 2014-01-29 18:51:33 UTC (rev 40) @@ -46,6 +46,7 @@ import fr.ifremer.wlo.storage.WloSqlOpenHelper; import fr.ifremer.wlo.utils.BaseTextWatcher; import fr.ifremer.wlo.utils.UIUtils; +import fr.ifremer.wlo.utils.WloAutoCompleteTextViewWithFavorites; import org.apache.commons.lang3.ObjectUtils; import java.beans.PropertyChangeEvent; @@ -246,18 +247,28 @@ } protected <R> AutoCompleteTextView initAutoCompleteTextView(int autoCompleteTextViewId, final String attribute, Collection<R> data) { + return initAutoCompleteTextView(autoCompleteTextViewId, attribute, data, null); + } + + protected <R> AutoCompleteTextView initAutoCompleteTextView(int autoCompleteTextViewId, final String attribute, + Collection<R> data, Collection<R> favorites) { AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById(autoCompleteTextViewId); - initAutoCompleteTextView(autoCompleteTextView, attribute, data); + initAutoCompleteTextView(autoCompleteTextView, attribute, data, favorites); return autoCompleteTextView; } protected <R> void initAutoCompleteTextView(final AutoCompleteTextView autoCompleteTextView, final String attribute, Collection<R> data) { + initAutoCompleteTextView(autoCompleteTextView, attribute, data, null); + } + + protected <R> void initAutoCompleteTextView(final AutoCompleteTextView autoCompleteTextView, final String attribute, + Collection<R> data, Collection<R> favorites) { final Class clazz = model.getClass(); final String firtsLetterUpperCaseAttribute = attribute.substring(0, 1).toUpperCase() + attribute.substring(1); if (!data.isEmpty()) { - ArrayAdapter<R> adapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, Lists.newArrayList(data)); + ArrayAdapter<R> adapter = new WloAutoCompleteTextViewWithFavorites.FavoriteAdapter<R>(this, data, favorites); autoCompleteTextView.setAdapter(adapter); } Copied: trunk/src/fr/ifremer/wlo/preferences/MultiSelectItemPreference.java (from rev 29, trunk/src/fr/ifremer/wlo/preferences/StringPreference.java) =================================================================== --- trunk/src/fr/ifremer/wlo/preferences/MultiSelectItemPreference.java (rev 0) +++ trunk/src/fr/ifremer/wlo/preferences/MultiSelectItemPreference.java 2014-01-29 18:51:33 UTC (rev 40) @@ -0,0 +1,46 @@ +package fr.ifremer.wlo.preferences; + +/* + * #%L + * WLO + * $Id:$ + * $HeadURL:$ + * %% + * Copyright (C) 2013 - 2014 Ifremer + * %% + * 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/gpl-3.0.html>. + * #L% + */ + +/** + * @author kmorin <kmorin@codelutin.com> + * @since 0.1 + */ +public enum MultiSelectItemPreference { + + COMMERCIAL_SPECIES_FAVORITES("preferences_favorites_commercial_species"), + SCIENTIFIC_SPECIES_FAVORITES("preferences_favorites_scientific_species"); + + private String key; + + private MultiSelectItemPreference(String key) { + this.key = key; + } + + public String getKey() { + return key; + } + +} Modified: trunk/src/fr/ifremer/wlo/preferences/SettingsActivity.java =================================================================== --- trunk/src/fr/ifremer/wlo/preferences/SettingsActivity.java 2014-01-29 13:38:23 UTC (rev 39) +++ trunk/src/fr/ifremer/wlo/preferences/SettingsActivity.java 2014-01-29 18:51:33 UTC (rev 40) @@ -32,17 +32,24 @@ import android.content.SharedPreferences; import android.os.Bundle; import android.os.Environment; +import android.preference.MultiSelectListPreference; import android.preference.Preference; import android.preference.PreferenceFragment; import android.text.format.DateFormat; import android.util.Log; import android.widget.Toast; +import com.google.common.base.Functions; import com.google.common.collect.BiMap; +import com.google.common.collect.Collections2; import com.google.common.collect.HashBiMap; import com.google.common.collect.Maps; +import com.google.common.collect.Sets; import fr.ifremer.wlo.MainActivity; import fr.ifremer.wlo.R; import fr.ifremer.wlo.WloBaseActivity; +import fr.ifremer.wlo.models.BaseModel; +import fr.ifremer.wlo.models.referentials.CommercialSpecies; +import fr.ifremer.wlo.models.referentials.ScientificSpecies; import fr.ifremer.wlo.storage.DataCache; import fr.ifremer.wlo.storage.StorageUtils; import fr.ifremer.wlo.storage.WloSqlOpenHelper; @@ -55,8 +62,12 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.util.Collection; +import java.util.Collections; import java.util.Date; +import java.util.HashSet; import java.util.Map; +import java.util.Set; /** * @author kmorin <kmorin@codelutin.com> @@ -157,12 +168,14 @@ } }); } + } @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + Log.d(TAG, "onSharedPreferenceChanged " + key); - Preference connectionPref = findPreference(key); + Preference preference = findPreference(key); String summary = null; ListItemPreference pref = ListItemPreference.getListItemPreference(key); @@ -171,10 +184,14 @@ summary = pref.getEntryForValue(getActivity(), entry); } else { - summary = sharedPreferences.getString(key, ""); + try { + summary = sharedPreferences.getString(key, ""); + } catch (ClassCastException e) { + // ok, that's normal, it is a list so we do not want to have a summary + } } // Set summary to be the user-description for the selected value - connectionPref.setSummary(summary); + preference.setSummary(summary); } @Override @@ -184,24 +201,32 @@ for (StringPreference pref : StringPreference.values()) { String key = pref.getKey(); - Preference connectionPref = findPreference(key); - connectionPref.setSummary(sharedPreferences.getString(key, "")); + Preference preference = findPreference(key); + preference.setSummary(sharedPreferences.getString(key, "")); } for (ListItemPreference pref : ListItemPreference.values()) { String key = pref.getKey(); - Preference connectionPref = findPreference(key); + Preference preference = findPreference(key); String entry = sharedPreferences.getString(key, ""); String summary = pref.getEntryForValue(getActivity(), entry); - connectionPref.setSummary(summary); + preference.setSummary(summary); } + Collection<CommercialSpecies> commercialSpecies = DataCache.getAllCommercialSpecies(getActivity()); + initMultiSelectListPreference(MultiSelectItemPreference.COMMERCIAL_SPECIES_FAVORITES, commercialSpecies); + + Collection<ScientificSpecies> scientificSpecies = DataCache.getAllScientificSpecies(getActivity()); + initMultiSelectListPreference(MultiSelectItemPreference.SCIENTIFIC_SPECIES_FAVORITES, scientificSpecies); + + Log.d(TAG, "registerOnSharedPreferenceChangeListener"); sharedPreferences.registerOnSharedPreferenceChangeListener(this); } @Override public void onPause() { super.onPause(); + Log.d(TAG, "unregisterOnSharedPreferenceChangeListener"); getPreferenceScreen().getSharedPreferences() .unregisterOnSharedPreferenceChangeListener(this); } @@ -346,6 +371,24 @@ } }).start(); } + + protected <M extends BaseModel> void initMultiSelectListPreference(MultiSelectItemPreference pref, Collection<M> refData) { + Log.d(TAG, "initMultiSelectListPreference " + pref.getKey()); + + SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences(); + Set<String> data = sharedPreferences.getStringSet(pref.getKey(), new HashSet<String>()); + Log.d(TAG, "data " + data); + + MultiSelectListPreference preference = + (MultiSelectListPreference) findPreference(pref.getKey()); + Collection<String> entries = Collections2.transform(refData, Functions.toStringFunction()); + preference.setEntries(entries.toArray(new CharSequence[entries.size()])); + Collection<String> entryValues = Collections2.transform(refData, BaseModel.GET_ID_FUNCTION); + preference.setEntryValues(entryValues.toArray(new CharSequence[entryValues.size()])); + + preference.setValues(data); + + } } } \ No newline at end of file Added: trunk/src/fr/ifremer/wlo/preferences/WloMultiSelectListPreference.java =================================================================== --- trunk/src/fr/ifremer/wlo/preferences/WloMultiSelectListPreference.java (rev 0) +++ trunk/src/fr/ifremer/wlo/preferences/WloMultiSelectListPreference.java 2014-01-29 18:51:33 UTC (rev 40) @@ -0,0 +1,59 @@ +package fr.ifremer.wlo.preferences; + +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.MultiSelectListPreference; +import android.util.AttributeSet; + +import java.util.HashSet; +import java.util.Set; + +/** + * A {@link Preference} that displays a list of entries as + * a dialog. + * <p> + * This preference will store a set of strings into the SharedPreferences. + * This set will contain one or more values from the + * {@link #setEntryValues(CharSequence[])} array. + * + * @attr ref android.R.styleable#MultiSelectListPreference_entries + * @attr ref android.R.styleable#MultiSelectListPreference_entryValues + * @since 0.2 + */ +public class WloMultiSelectListPreference extends MultiSelectListPreference { + + public WloMultiSelectListPreference(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public WloMultiSelectListPreference(Context context) { + this(context, null); + } + + /** + * Sets the value of the key. This should contain entries in + * {@link #getEntryValues()}. + * + * @param values The values to set for the key. + */ + public void setValues(Set<String> values) { + Set<String> mValues = getValues(); + mValues.clear(); + mValues.addAll(values); + + SharedPreferences.Editor editor = getPreferenceManager().getSharedPreferences().edit(); + editor.putStringSet(getKey(), mValues); + + try { + editor.apply(); + + } catch (AbstractMethodError unused) { + // The app injected its own pre-Gingerbread + // SharedPreferences.Editor implementation without + // an apply method. + editor.commit(); + } + + } + +} Modified: trunk/src/fr/ifremer/wlo/storage/DataCache.java =================================================================== --- trunk/src/fr/ifremer/wlo/storage/DataCache.java 2014-01-29 13:38:23 UTC (rev 39) +++ trunk/src/fr/ifremer/wlo/storage/DataCache.java 2014-01-29 18:51:33 UTC (rev 40) @@ -92,7 +92,9 @@ public static Collection<CommercialSpecies> getAllCommercialSpecies(Context context) { initCommercialSpecies(context); - return commercialSpecies.values(); + List<CommercialSpecies> result = Lists.newArrayList(commercialSpecies.values()); + Collections.sort(result, HasCode.GET_CODE_COMPARATOR); + return result; } public static CommercialSpecies getCommercialSpeciesById(Context context, String id) { Added: trunk/src/fr/ifremer/wlo/utils/WloAutoCompleteTextViewWithFavorites.java =================================================================== --- trunk/src/fr/ifremer/wlo/utils/WloAutoCompleteTextViewWithFavorites.java (rev 0) +++ trunk/src/fr/ifremer/wlo/utils/WloAutoCompleteTextViewWithFavorites.java 2014-01-29 18:51:33 UTC (rev 40) @@ -0,0 +1,92 @@ +package fr.ifremer.wlo.utils; + +import android.content.Context; +import android.util.AttributeSet; +import android.util.Log; +import android.widget.Adapter; +import android.widget.ArrayAdapter; +import android.widget.CompoundButton; +import android.widget.FrameLayout; +import android.widget.ToggleButton; +import com.google.common.collect.Lists; +import fr.ifremer.wlo.R; +import org.apache.commons.collections.CollectionUtils; + +import java.util.Collection; +import java.util.Collections; + +/** + * @author kmorin <kmorin@codelutin.com> + * @since 0.2 + */ +public class WloAutoCompleteTextViewWithFavorites extends FrameLayout { + + private static final String TAG = "WloAutoCompleteTextViewWithFavorites"; + + protected WloAutoCompleteTextView autoCompleteTextView; + protected ToggleButton toggleButton; + + public WloAutoCompleteTextViewWithFavorites(Context context) { + super(context); + init(context); + } + + public WloAutoCompleteTextViewWithFavorites(Context context, AttributeSet attrs) { + super(context, attrs); + init(context); + } + + public WloAutoCompleteTextViewWithFavorites(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + init(context); + } + + protected void init(Context context) { + inflate(context, R.layout.autocomplete_text_view_with_favorites, this); + autoCompleteTextView = (WloAutoCompleteTextView) findViewById(R.id.autocomplete_text_view); + toggleButton = (ToggleButton) findViewById(R.id.favorite_button); + toggleButton.setChecked(false); + toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + Log.d(TAG, "onCheckedChanged " + isChecked); + Adapter adapter = autoCompleteTextView.getAdapter(); + if (FavoriteAdapter.class.isAssignableFrom(adapter.getClass())) { + FavoriteAdapter favoriteAdapter = (FavoriteAdapter) autoCompleteTextView.getAdapter(); + favoriteAdapter.useFavorites(isChecked); + } + } + }); + } + + public WloAutoCompleteTextView getAutoCompleteTextView() { + return autoCompleteTextView; + } + + public static class FavoriteAdapter<M> extends ArrayAdapter<M> { + + protected Collection<M> allData; + protected Collection<M> favoriteData; + + public FavoriteAdapter(Context context, Collection<M> allData, Collection<M> favoriteData) { + super(context, android.R.layout.simple_dropdown_item_1line, Lists.newArrayList(allData)); + this.allData = Lists.newArrayList(allData); + if (favoriteData != null) { + this.favoriteData = Lists.newArrayList(favoriteData); + } + } + + public void useFavorites(boolean favorites) { + if (CollectionUtils.isNotEmpty(favoriteData)) { + Log.d(TAG, "useFavorites " + favorites); + clear(); + if (favorites) { + addAll(favoriteData); + } else { + addAll(allData); + } + notifyDataSetInvalidated(); + } + } + } +}
participants (1)
-
kmorin@users.forge.codelutin.com