r58 - in trunk: . res/layout res/values res/values-fr res/xml src/fr/ifremer/wlo/preferences
Author: kmorin Date: 2014-03-01 21:07:03 +0100 (Sat, 01 Mar 2014) New Revision: 58 Url: http://codelutin.com/projects/wlo/repository/revisions/58 Log: fixes #4598 [Favoris] am?\195?\169liorer l'ergonomie de la mise en favori des esp?\195?\168ces commerciales et scientifiques Added: trunk/res/layout/favorite_selection.xml trunk/src/fr/ifremer/wlo/preferences/MultiSelectionActivity.java Removed: trunk/src/fr/ifremer/wlo/preferences/WloMultiSelectListPreference.java Modified: trunk/AndroidManifest.xml trunk/res/layout/category_creation_form.xml trunk/res/values-fr/strings.xml trunk/res/values/strings.xml trunk/res/xml/preferences.xml trunk/src/fr/ifremer/wlo/preferences/SettingsActivity.java Modified: trunk/AndroidManifest.xml =================================================================== --- trunk/AndroidManifest.xml 2014-03-01 16:46:23 UTC (rev 57) +++ trunk/AndroidManifest.xml 2014-03-01 20:07:03 UTC (rev 58) @@ -121,6 +121,10 @@ android:label="@string/category_creation_title" android:configChanges="orientation|screenSize|keyboardHidden"/> + <activity android:name=".preferences.MultiSelectionActivity" + android:label="@string/favaorite_selection_title" + android:configChanges="orientation|screenSize|keyboardHidden"/> + <activity android:name=".utils.filechooser.FileDialog" android:configChanges="orientation|screenSize|keyboardHidden"/> Modified: trunk/res/layout/category_creation_form.xml =================================================================== --- trunk/res/layout/category_creation_form.xml 2014-03-01 16:46:23 UTC (rev 57) +++ trunk/res/layout/category_creation_form.xml 2014-03-01 20:07:03 UTC (rev 58) @@ -5,38 +5,38 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - <ScrollView android:layout_width="match_parent" - android:layout_height="0dp" - android:layout_weight="1" - android:fillViewport="true"> + <ScrollView android:layout_width="match_parent" + android:layout_height="0dp" + android:layout_weight="1" + android:fillViewport="true"> - <LinearLayout android:orientation="vertical" - android:layout_width="match_parent" - android:layout_height="wrap_content"> - - <TextView android:text="@string/category_label" + <LinearLayout android:orientation="vertical" android:layout_width="match_parent" - android:layout_height="wrap_content"/> + android:layout_height="wrap_content"> - <EditText android:id="@+id/category_label" - android:layout_width="match_parent" - android:layout_height="wrap_content"/> + <TextView android:text="@string/category_label" + android:layout_width="match_parent" + android:layout_height="wrap_content"/> - <Button android:id="@+id/import_qualitative_values_button" - android:text="@string/import_qualitative_values" + <EditText android:id="@+id/category_label" android:layout_width="match_parent" + android:layout_height="wrap_content"/> + + <Button android:id="@+id/import_qualitative_values_button" + android:text="@string/import_qualitative_values" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:onClick="importQualitativeValues"/> + + <TextView android:id="@+id/qualitative_values_nb_label" + android:layout_width="match_parent" android:layout_height="wrap_content" - android:onClick="importQualitativeValues"/> + android:visibility="gone"/> - <TextView android:id="@+id/qualitative_values_nb_label" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:visibility="gone"/> + </LinearLayout> - </LinearLayout> + </ScrollView> - </ScrollView> - <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" Added: trunk/res/layout/favorite_selection.xml =================================================================== --- trunk/res/layout/favorite_selection.xml (rev 0) +++ trunk/res/layout/favorite_selection.xml 2014-03-01 20:07:03 UTC (rev 58) @@ -0,0 +1,39 @@ +<?xml version="1.0" encoding="utf-8"?> + +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:orientation="vertical" + android:layout_width="match_parent" + android:layout_height="match_parent"> + + <fr.ifremer.wlo.utils.WloAutoCompleteTextView + android:id="@+id/favorite_search" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:singleLine="true" + android:hint="@string/select_new_favorite"/> + + <ListView android:id="@android:id/list" + android:layout_width="match_parent" + android:layout_height="0dp" + android:layout_weight="1"/> + + <LinearLayout android:orientation="horizontal" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:gravity="bottom"> + + <Button android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_weight="1" + android:text="@android:string/cancel" + android:onClick="cancel" /> + + <Button android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_weight="1" + android:text="@android:string/ok" + android:onClick="validate" /> + + </LinearLayout> + +</LinearLayout> \ No newline at end of file Modified: trunk/res/values/strings.xml =================================================================== --- trunk/res/values/strings.xml 2014-03-01 16:46:23 UTC (rev 57) +++ trunk/res/values/strings.xml 2014-03-01 20:07:03 UTC (rev 58) @@ -111,6 +111,9 @@ <string name="preferences_favorites_commercial_species">Commercial species</string> <string name="preferences_favorites_scientific_species">Scientific species</string> + <string name="favaorite_selection_title">Favorites</string> + <string name="select_new_favorite">Select a new favorite</string> + <string name="favorite_nb">%s favorites</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-03-01 16:46:23 UTC (rev 57) +++ trunk/res/values-fr/strings.xml 2014-03-01 20:07:03 UTC (rev 58) @@ -107,6 +107,9 @@ <string name="preferences_favorites_commercial_species">Espèces commerciales</string> <string name="preferences_favorites_scientific_species">Espèces scientifiques</string> + <string name="favaorite_selection_title">Favoris</string> + <string name="select_new_favorite">Choisissez un nouveau favori</string> + <string name="favorite_nb">%s favoris</string> <string name="preferences_calcified_parts_takings">Prélèvement des pièces calcifiées</string> <string name="preferences_import_calcified_parts_takings">Import des paramètres de l\'algorithme</string> Modified: trunk/res/xml/preferences.xml =================================================================== --- trunk/res/xml/preferences.xml 2014-03-01 16:46:23 UTC (rev 57) +++ trunk/res/xml/preferences.xml 2014-03-01 20:07:03 UTC (rev 58) @@ -66,13 +66,11 @@ 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"/> + <Preference 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"/> + <Preference android:key="preferences_favorites_scientific_species" + android:title="@string/preferences_favorites_scientific_species"/> </PreferenceScreen> Added: trunk/src/fr/ifremer/wlo/preferences/MultiSelectionActivity.java =================================================================== --- trunk/src/fr/ifremer/wlo/preferences/MultiSelectionActivity.java (rev 0) +++ trunk/src/fr/ifremer/wlo/preferences/MultiSelectionActivity.java 2014-03-01 20:07:03 UTC (rev 58) @@ -0,0 +1,109 @@ +package fr.ifremer.wlo.preferences; + + +import android.app.ListActivity; +import android.content.Intent; +import android.os.Bundle; +import android.view.View; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.ListView; +import com.google.common.base.Function; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import fr.ifremer.wlo.R; +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.utils.BaseModelArrayAdapter; +import fr.ifremer.wlo.utils.WloAutoCompleteTextView; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * @author Kevin Morin (Code Lutin) + * @since x.x + */ +public class MultiSelectionActivity extends ListActivity implements AdapterView.OnItemLongClickListener { + + public static final String INTENT_FAVORITE_LIST = "favoriteList"; + public static final String INTENT_UNIVERSE_CLASS = "universeClass"; + + protected ArrayAdapter favoriteAdapter; + protected ArrayAdapter universeAdapter; + + protected ArrayList<String> favoriteIds = new ArrayList<>(); + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setContentView(R.layout.favorite_selection); + + List<BaseModel> universe = new ArrayList<>(); + Class universeClass = (Class) getIntent().getSerializableExtra(INTENT_UNIVERSE_CLASS); + if (ScientificSpecies.class.equals(universeClass)) { + universe.addAll(DataCache.getAllScientificSpecies(this)); + + } else if (CommercialSpecies.class.equals(universeClass)) { + universe.addAll(DataCache.getAllCommercialSpecies(this)); + } + + final Map<String, BaseModel> modelsById = Maps.uniqueIndex(universe, BaseModel.GET_ID_FUNCTION); + + favoriteIds = getIntent().getStringArrayListExtra(INTENT_FAVORITE_LIST); + List<BaseModel> favorites = new ArrayList<>(Lists.transform(favoriteIds, new Function<String, BaseModel>() { + @Override + public BaseModel apply(String input) { + return modelsById.get(input); + } + })); + favoriteAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, favorites); + setListAdapter(favoriteAdapter); + + final WloAutoCompleteTextView search = (WloAutoCompleteTextView) findViewById(R.id.favorite_search); + + universe.removeAll(favorites); + universeAdapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, universe); + search.setAdapter(universeAdapter); + + search.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView<?> parent, View view, int position, long id) { + BaseModel selected = (BaseModel) parent.getItemAtPosition(position); + favoriteAdapter.add(selected); + favoriteIds.add(selected.getId()); + search.setText(null); + universeAdapter.remove(selected); + } + }); + + getListView().setOnItemLongClickListener(this); + } + + + + public void cancel(View view) { + setResult(RESULT_CANCELED); + finish(); + } + + public void validate(View view) { + Intent result = new Intent(); + result.putExtra(INTENT_FAVORITE_LIST, favoriteIds); + setResult(RESULT_OK, result); + finish(); + } + + @Override + public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { + BaseModel selected = (BaseModel) parent.getItemAtPosition(position); + favoriteAdapter.remove(selected); + favoriteIds.remove(selected.getId()); + universeAdapter.add(selected); + return true; + } +} Modified: trunk/src/fr/ifremer/wlo/preferences/SettingsActivity.java =================================================================== --- trunk/src/fr/ifremer/wlo/preferences/SettingsActivity.java 2014-03-01 16:46:23 UTC (rev 57) +++ trunk/src/fr/ifremer/wlo/preferences/SettingsActivity.java 2014-03-01 20:07:03 UTC (rev 58) @@ -24,7 +24,6 @@ * #L% */ -import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.Context; @@ -37,7 +36,6 @@ import android.preference.Preference; import android.preference.PreferenceFragment; import android.util.Log; -import android.view.View; import android.widget.Toast; import com.google.common.base.Functions; import com.google.common.collect.BiMap; @@ -48,7 +46,6 @@ import fr.ifremer.wlo.R; import fr.ifremer.wlo.WloBaseActivity; import fr.ifremer.wlo.models.BaseModel; -import fr.ifremer.wlo.models.categorization.CategoryModel; import fr.ifremer.wlo.models.referentials.CommercialSpecies; import fr.ifremer.wlo.models.referentials.ScientificSpecies; import fr.ifremer.wlo.storage.DataCache; @@ -63,9 +60,11 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; @@ -87,6 +86,8 @@ protected static final int REQUEST_IMPORT_VESSELS = 7; protected static final int REQUEST_IMPORT_CALCIFIED_PARTS_TAKINGS = 8; protected static final int REQUEST_CREATE_CATEGORY = 9; + protected static final int REQUEST_FAVORITES_COMMERCIAL_SPECIES = 10; + protected static final int REQUEST_FAVORITES_SCIENTIFIC_SPECIES = 11; @Override @@ -181,6 +182,15 @@ } }); + // favorites + initMultiSelectListPreference(MultiSelectItemPreference.COMMERCIAL_SPECIES_FAVORITES.getKey(), + CommercialSpecies.class, + REQUEST_FAVORITES_COMMERCIAL_SPECIES); + + initMultiSelectListPreference(MultiSelectItemPreference.SCIENTIFIC_SPECIES_FAVORITES.getKey(), + ScientificSpecies.class, + REQUEST_FAVORITES_SCIENTIFIC_SPECIES); + } @Override @@ -225,12 +235,6 @@ 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); } @@ -246,33 +250,40 @@ @Override public void onActivityResult(final int requestCode, final int resultCode, final Intent data) { Context context = getActivity(); - if (resultCode == RESULT_OK - && requestCode >= REQUEST_IMPORT_COMMERCIAL_SPECIES && requestCode <= REQUEST_IMPORT_CALCIFIED_PARTS_TAKINGS) { + if (resultCode == RESULT_OK) { + if (requestCode >= REQUEST_IMPORT_COMMERCIAL_SPECIES + && requestCode <= REQUEST_IMPORT_CALCIFIED_PARTS_TAKINGS) { - if (!StorageUtils.isExternalStorageWritable()) { - AlertDialog dialog = new AlertDialog.Builder(context) - .setTitle(R.string.preferences_import_no_external_storage_title) - .setMessage(R.string.preferences_import_no_external_storage_message) - .setCancelable(false) - .setNegativeButton(R.string.no, UIUtils.getCancelClickListener()) - .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - importData(requestCode, data, false); - } - }).create(); - dialog.show(); + if (!StorageUtils.isExternalStorageWritable()) { + AlertDialog dialog = new AlertDialog.Builder(context) + .setTitle(R.string.preferences_import_no_external_storage_title) + .setMessage(R.string.preferences_import_no_external_storage_message) + .setCancelable(false) + .setNegativeButton(R.string.no, UIUtils.getCancelClickListener()) + .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + importData(requestCode, data, false); + } + }).create(); + dialog.show(); - } else { - importData(requestCode, data, true); + } else { + importData(requestCode, data, true); + } + + } else if (requestCode == REQUEST_CREATE_CATEGORY) { + int nb = DataCache.getAllCategories(context).size(); + Preference categoryPref = findPreference("import_categories"); + categoryPref.setSummary(getString(R.string.preferences_import_summary, nb)); + + } else if (requestCode == REQUEST_FAVORITES_SCIENTIFIC_SPECIES) { + saveMultiSelectListPreference(data, MultiSelectItemPreference.SCIENTIFIC_SPECIES_FAVORITES.getKey()); + + } else if (requestCode == REQUEST_FAVORITES_COMMERCIAL_SPECIES) { + saveMultiSelectListPreference(data, MultiSelectItemPreference.COMMERCIAL_SPECIES_FAVORITES.getKey()); } - } else if (resultCode == RESULT_OK && requestCode == REQUEST_CREATE_CATEGORY) { - Toast.makeText(context, R.string.category_created, Toast.LENGTH_LONG).show(); - int nb = DataCache.getAllCategories(context).size(); - Preference categoryPref = findPreference("import_categories"); - categoryPref.setSummary(getString(R.string.preferences_import_summary, nb)); - } else { super.onActivityResult(requestCode, resultCode, data); } @@ -388,22 +399,45 @@ }).start(); } - protected <M extends BaseModel> void initMultiSelectListPreference(MultiSelectItemPreference pref, Collection<M> refData) { - Log.d(TAG, "initMultiSelectListPreference " + pref.getKey()); + protected <M extends BaseModel> void initMultiSelectListPreference(String prefKey, + final Class clazz, + final int requestCode) { + Log.d(TAG, "initMultiSelectListPreference " + prefKey); + Preference preference = findPreference(prefKey); SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences(); - Set<String> data = sharedPreferences.getStringSet(pref.getKey(), new HashSet<String>()); - Log.d(TAG, "data " + data); + final ArrayList<String> data = new ArrayList<>(sharedPreferences.getStringSet(prefKey, + new HashSet<String>())); - 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.setSummary(getString(R.string.favorite_nb, data.size())); + preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + Intent intent = new Intent(getActivity(), MultiSelectionActivity.class); + intent.putExtra(MultiSelectionActivity.INTENT_UNIVERSE_CLASS, clazz); + intent.putExtra(MultiSelectionActivity.INTENT_FAVORITE_LIST, data); + startActivityForResult(intent, requestCode); + return true; + } + }); + } - preference.setValues(data); + protected void saveMultiSelectListPreference(Intent data, String key) { + Preference pref = findPreference(key); + Set<String> favoriteIds = new HashSet<>( + data.getStringArrayListExtra(MultiSelectionActivity.INTENT_FAVORITE_LIST)); + SharedPreferences.Editor editor = getPreferenceManager().getSharedPreferences().edit(); + editor.putStringSet(key, favoriteIds); + try { + editor.apply(); + } catch (AbstractMethodError unused) { + // The app injected its own pre-Gingerbread + // SharedPreferences.Editor implementation without + // an apply method. + editor.commit(); + } + pref.setSummary(getString(R.string.favorite_nb, favoriteIds.size())); } } Deleted: trunk/src/fr/ifremer/wlo/preferences/WloMultiSelectListPreference.java =================================================================== --- trunk/src/fr/ifremer/wlo/preferences/WloMultiSelectListPreference.java 2014-03-01 16:46:23 UTC (rev 57) +++ trunk/src/fr/ifremer/wlo/preferences/WloMultiSelectListPreference.java 2014-03-01 20:07:03 UTC (rev 58) @@ -1,83 +0,0 @@ -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% - */ - -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(); - } - - } - -}
participants (1)
-
kmorin@users.forge.codelutin.com