diff options
author | Bananeweizen <bananeweizen@gmx.de> | 2014-02-12 21:32:24 +0100 |
---|---|---|
committer | Bananeweizen <bananeweizen@gmx.de> | 2015-03-02 20:19:15 +0100 |
commit | 113d7eb40a3f6486f2ef2b82e4c4920f871e9c51 (patch) | |
tree | 5b751a5e26dda5e4fbcb9fe6074ff9f5ae4f1319 /main | |
parent | b6ed572616383b57bc2956f6b41e2853724c23f3 (diff) | |
download | cgeo-113d7eb40a3f6486f2ef2b82e4c4920f871e9c51.zip cgeo-113d7eb40a3f6486f2ef2b82e4c4920f871e9c51.tar.gz cgeo-113d7eb40a3f6486f2ef2b82e4c4920f871e9c51.tar.bz2 |
new: filters as activity, not dialog
This makes it possible to replace certain filters by better matching
controls over time.
Diffstat (limited to 'main')
18 files changed, 446 insertions, 208 deletions
diff --git a/main/AndroidManifest.xml b/main/AndroidManifest.xml index e026b5c..372e4b9 100644 --- a/main/AndroidManifest.xml +++ b/main/AndroidManifest.xml @@ -975,6 +975,11 @@ </activity> + <activity + android:name=".filter.FilterActivity_" + android:label="@string/caches_filter_title" > + </activity> + <!-- provide enhanced meta data for caches (and waypoints) when invoking Locus from c:geo --> <provider android:name="menion.android.locus.LocusDataStorageProvider" diff --git a/main/res/layout/filter_activity.xml b/main/res/layout/filter_activity.xml new file mode 100644 index 0000000..8858452 --- /dev/null +++ b/main/res/layout/filter_activity.xml @@ -0,0 +1,30 @@ +<?xml version="1.0" encoding="UTF-8"?> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="fill_parent" + android:layout_height="fill_parent" + android:orientation="vertical" + tools:context=".filter.FilterActivity" > + + <ScrollView + android:id="@+id/scrollView" + android:layout_width="match_parent" + android:layout_height="wrap_content" > + + <LinearLayout + android:id="@+id/filters" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:orientation="vertical" > + + </LinearLayout> + </ScrollView> + + + <ExpandableListView + android:id="@+id/filterList" + android:layout_width="match_parent" + android:layout_height="fill_parent" > + </ExpandableListView> + +</LinearLayout>
\ No newline at end of file diff --git a/main/res/layout/filter_list_child.xml b/main/res/layout/filter_list_child.xml new file mode 100644 index 0000000..7978269 --- /dev/null +++ b/main/res/layout/filter_list_child.xml @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="utf-8"?> +<TextView xmlns:android="http://schemas.android.com/apk/res/android" + android:id="@android:id/text1" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_gravity="start" + android:layout_marginTop="6dip" + android:textAlignment="viewStart" + android:textAppearance="?android:attr/textAppearanceLarge" /> diff --git a/main/res/layout/grid_image.xml b/main/res/layout/grid_image.xml new file mode 100644 index 0000000..4e145f5 --- /dev/null +++ b/main/res/layout/grid_image.xml @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="utf-8"?> +<ImageView xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:padding="0dp" + android:scaleType="fitCenter" + android:src="@drawable/type_traditional" + tools:context=".filter.FilterActivity" > + +</ImageView>
\ No newline at end of file diff --git a/main/res/layout/images_gridview.xml b/main/res/layout/images_gridview.xml new file mode 100644 index 0000000..b3993af --- /dev/null +++ b/main/res/layout/images_gridview.xml @@ -0,0 +1,15 @@ +<?xml version="1.0" encoding="utf-8"?> +<GridView xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:tools="http://schemas.android.com/tools" + android:id="@+id/gridView" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:columnWidth="80dp" + android:gravity="center" + android:horizontalSpacing="0dp" + android:numColumns="auto_fit" + android:stretchMode="spacingWidth" + android:verticalSpacing="0dp" + tools:listitem="@layout/grid_image" > + +</GridView> diff --git a/main/res/menu/filter_options.xml b/main/res/menu/filter_options.xml new file mode 100644 index 0000000..60733a6 --- /dev/null +++ b/main/res/menu/filter_options.xml @@ -0,0 +1,12 @@ +<?xml version="1.0" encoding="utf-8"?> +<menu xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto" > + + <item + android:id="@+id/menu_reset_filter" + android:icon="@drawable/ic_menu_delete" + android:title="@string/caches_filter_clear" + app:showAsAction="ifRoom|withText"> + </item> + +</menu>
\ No newline at end of file diff --git a/main/src/cgeo/geocaching/CacheListActivity.java b/main/src/cgeo/geocaching/CacheListActivity.java index 5bcdd42..433c902 100644 --- a/main/src/cgeo/geocaching/CacheListActivity.java +++ b/main/src/cgeo/geocaching/CacheListActivity.java @@ -21,7 +21,7 @@ import cgeo.geocaching.enumerations.StatusCode; import cgeo.geocaching.export.FieldnoteExport; import cgeo.geocaching.export.GpxExport; import cgeo.geocaching.files.GPXImporter; -import cgeo.geocaching.filter.FilterUserInterface; +import cgeo.geocaching.filter.FilterActivity; import cgeo.geocaching.filter.IFilter; import cgeo.geocaching.list.AbstractList; import cgeo.geocaching.list.ListNameMemento; @@ -809,12 +809,7 @@ public class CacheListActivity extends AbstractListActivity implements FilteredA */ @Override public void showFilterMenu(final View view) { - new FilterUserInterface(this).selectFilter(new Action1<IFilter>() { - @Override - public void call(@Nullable final IFilter selectedFilter) { - setFilter(selectedFilter); - } - }); + FilterActivity.selectFilter(this); } private void setComparator(final CacheComparator comparator) { @@ -1059,6 +1054,10 @@ public class CacheListActivity extends AbstractListActivity implements FilteredA new GPXImporter(this, listId, importGpxAttachementFinishedHandler).importGPX(uri, null, getDisplayName(uri)); } } + else if (requestCode == FilterActivity.REQUEST_SELECT_FILTER && resultCode == Activity.RESULT_OK) { + final int[] filterIndex = data.getIntArrayExtra(FilterActivity.EXTRA_FILTER_RESULT); + setFilter(FilterActivity.getFilterFromPosition(filterIndex[0], filterIndex[1])); + } refreshCurrentList(); } diff --git a/main/src/cgeo/geocaching/filter/AbstractFilter.java b/main/src/cgeo/geocaching/filter/AbstractFilter.java index 20575a6..4d521c5 100644 --- a/main/src/cgeo/geocaching/filter/AbstractFilter.java +++ b/main/src/cgeo/geocaching/filter/AbstractFilter.java @@ -47,4 +47,9 @@ abstract class AbstractFilter implements IFilter { public String toString() { return getName(); } + + @Override + public int getImageId() { + return 0; + } } diff --git a/main/src/cgeo/geocaching/filter/FilterActivity.java b/main/src/cgeo/geocaching/filter/FilterActivity.java new file mode 100644 index 0000000..13a2263 --- /dev/null +++ b/main/src/cgeo/geocaching/filter/FilterActivity.java @@ -0,0 +1,154 @@ +package cgeo.geocaching.filter; + +import butterknife.ButterKnife; +import butterknife.InjectView; + +import cgeo.geocaching.R; +import cgeo.geocaching.activity.AbstractActionBarActivity; +import cgeo.geocaching.filter.FilterRegistry.FactoryEntry; +import cgeo.geocaching.utils.Log; + +import org.androidannotations.annotations.EActivity; +import org.androidannotations.annotations.OptionsItem; +import org.androidannotations.annotations.OptionsMenu; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; + +import android.app.Activity; +import android.content.Intent; +import android.os.Bundle; +import android.view.View; +import android.widget.ExpandableListView; +import android.widget.ExpandableListView.OnChildClickListener; +import android.widget.LinearLayout; +import android.widget.SimpleExpandableListAdapter; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Show a filter selection using an {@code ExpandableListView}. + */ +@OptionsMenu(R.menu.filter_options) +@EActivity +public class FilterActivity extends AbstractActionBarActivity { + + public static final String EXTRA_FILTER_RESULT = null; + public static final int REQUEST_SELECT_FILTER = 1234; + + private static final String KEY_FILTER_NAME = "filterName"; + private static final String KEY_FILTER_GROUP_NAME = "filterGroupName"; + + @InjectView(R.id.filterList) protected ExpandableListView filterList; + @InjectView(R.id.filters) protected LinearLayout filtersContainer; + + @Override + public void onCreate(final Bundle savedInstanceState) { + super.onCreate(savedInstanceState, R.layout.filter_activity); + ButterKnife.inject(this); + + createListAdapter(); + } + + private void createListAdapter() { + final SimpleExpandableListAdapter adapter = + new SimpleExpandableListAdapter( + this, + // top level entries in the next 4 lines + createFilterTopLevelGroups(), + android.R.layout.simple_expandable_list_item_1, + new String[] { KEY_FILTER_GROUP_NAME }, + new int[] { android.R.id.text1 }, + + // child level entries in the next 4 lines + createFilterChildren(), + android.R.layout.simple_expandable_list_item_2, + new String[] { KEY_FILTER_NAME, "CHILD_NAME" }, + new int[] { android.R.id.text1 } + ); + filterList.setAdapter(adapter); + filterList.setOnChildClickListener(new OnChildClickListener() { + + @Override + public boolean onChildClick(final ExpandableListView parent, final View v, final int groupPosition, final int childPosition, final long id) { + setFilterResult(groupPosition, childPosition); + return true; + } + + }); + } + + public static @Nullable IFilter getFilterFromPosition(final int groupPosition, final int childPosition) { + if (groupPosition < 0 || childPosition < 0) { + return null; + } + final FactoryEntry factoryEntry = FilterRegistry.getInstance().getFactories().get(groupPosition); + return createFilterFactory(factoryEntry.getFactory()).getFilters().get(childPosition); + } + + /** + * Creates the group list with the mapped properties. + */ + private static List<Map<String, String>> createFilterTopLevelGroups() { + final ArrayList<Map<String, String>> groups = new ArrayList<>(); + for (final FactoryEntry factoryEntry : FilterRegistry.getInstance().getFactories()) { + final Map<String, String> map = new HashMap<>(); + map.put(KEY_FILTER_GROUP_NAME, factoryEntry.getName()); + groups.add(map); + } + return groups; + } + + private static List<List<Map<String, String>>> createFilterChildren() { + final List<List<Map<String, String>>> listOfChildGroups = new ArrayList<>(); + + for (final FactoryEntry factoryEntry : FilterRegistry.getInstance().getFactories()) { + final IFilterFactory factory = createFilterFactory(factoryEntry.getFactory()); + final List<? extends IFilter> filters = factory.getFilters(); + + final List<Map<String, String>> childGroups = new ArrayList<>(filters.size()); + + for (final IFilter filter : filters) { + final HashMap<String, String> hashMap = new HashMap<>(1); + hashMap.put(KEY_FILTER_NAME, filter.getName()); + hashMap.put("CHILD_NAME", filter.getName()); + childGroups.add(hashMap); + } + listOfChildGroups.add(childGroups); + } + return listOfChildGroups; + } + + private static IFilterFactory createFilterFactory(final Class<? extends IFilterFactory> class1) { + try { + return class1.newInstance(); + } catch (final InstantiationException e) { + Log.e("createFilterFactory", e); + } catch (final IllegalAccessException e) { + Log.e("createFilterFactory", e); + } + return null; + } + + /** + * After calling this method, the calling activity must implement onActivityResult, and check the + * {@link #EXTRA_FILTER_RESULT}. + */ + public static void selectFilter(@NonNull final Activity context) { + context.startActivityForResult(new Intent(context, FilterActivity_.class), REQUEST_SELECT_FILTER); + } + + @OptionsItem(R.id.menu_reset_filter) + void resetFilter() { + setFilterResult(-1, -1); + } + + private void setFilterResult(final int groupPosition, final int childPosition) { + final Intent resultIntent = new Intent(); + resultIntent.putExtra(EXTRA_FILTER_RESULT, new int[] { groupPosition, childPosition }); + setResult(Activity.RESULT_OK, resultIntent); + finish(); + } +} diff --git a/main/src/cgeo/geocaching/filter/FilterRegistry.java b/main/src/cgeo/geocaching/filter/FilterRegistry.java new file mode 100644 index 0000000..d6d9db9 --- /dev/null +++ b/main/src/cgeo/geocaching/filter/FilterRegistry.java @@ -0,0 +1,86 @@ +package cgeo.geocaching.filter; + +import cgeo.geocaching.CgeoApplication; +import cgeo.geocaching.R; +import cgeo.geocaching.filter.SizeFilter.Factory; + +import org.apache.commons.lang3.StringUtils; +import org.eclipse.jdt.annotation.NonNull; + +import android.content.res.Resources; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * singleton registry of all available filter components + * + */ +public class FilterRegistry { + private final List<FactoryEntry> registry = new ArrayList<>(); + private static Resources res; + + static class FactoryEntry { + private final String name; + private final @NonNull Class<? extends IFilterFactory> filterFactory; + + public FactoryEntry(final String name, final @NonNull Class<? extends IFilterFactory> filterFactory) { + this.name = name; + this.filterFactory = filterFactory; + } + + @Override + public String toString() { + return name; + } + + public String getName() { + return name; + } + + public Class<? extends IFilterFactory> getFactory() { + return filterFactory; + } + } + + private static class SingletonHolder { + private static final FilterRegistry INSTANCE = new FilterRegistry(); + } + + public static FilterRegistry getInstance() { + return SingletonHolder.INSTANCE; + } + + private FilterRegistry() { + res = CgeoApplication.getInstance().getResources(); + register(R.string.caches_filter_type, TypeFilter.Factory.class); + register(R.string.caches_filter_size, SizeFilter.Factory.class); + register(R.string.cache_terrain, TerrainFilter.Factory.class); + register(R.string.cache_difficulty, DifficultyFilter.Factory.class); + register(R.string.cache_attributes, AttributeFilter.Factory.class); + register(R.string.cache_status, StateFilterFactory.class); + register(R.string.caches_filter_origin, OriginFilter.Factory.class); + register(R.string.caches_filter_distance, DistanceFilter.Factory.class); + register(R.string.caches_filter_popularity, PopularityFilter.Factory.class); + register(R.string.caches_filter_popularity_ratio, PopularityRatioFilter.Factory.class); + register(R.string.caches_filter_personal_data, PersonalDataFilterFactory.class); + } + + private void register(final int resourceId, final @NonNull Class<? extends IFilterFactory> factoryClass) { + registry.add(new FactoryEntry(res.getString(resourceId), factoryClass)); + } + + public String getFactoryName(final Class<Factory> factoryClass) { + for (final FactoryEntry entry : registry) { + if (entry.filterFactory == factoryClass) { + return entry.name; + } + } + return StringUtils.EMPTY; + } + + public List<FactoryEntry> getFactories() { + return Collections.unmodifiableList(registry); + } +} diff --git a/main/src/cgeo/geocaching/filter/FilterUserInterface.java b/main/src/cgeo/geocaching/filter/FilterUserInterface.java deleted file mode 100644 index 97e70ba..0000000 --- a/main/src/cgeo/geocaching/filter/FilterUserInterface.java +++ /dev/null @@ -1,137 +0,0 @@ -package cgeo.geocaching.filter; - -import cgeo.geocaching.CgeoApplication; -import cgeo.geocaching.R; -import cgeo.geocaching.enumerations.CacheType; -import cgeo.geocaching.settings.Settings; -import cgeo.geocaching.utils.Log; -import cgeo.geocaching.utils.TextUtils; - -import org.eclipse.jdt.annotation.NonNull; -import org.eclipse.jdt.annotation.Nullable; - -import rx.functions.Action1; - -import android.app.Activity; -import android.app.AlertDialog; -import android.content.DialogInterface; -import android.content.res.Resources; -import android.widget.ArrayAdapter; - -import java.text.Collator; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - -public final class FilterUserInterface { - - private static class FactoryEntry { - @NonNull private final String name; - @Nullable private final Class<? extends IFilterFactory> filterFactory; - - public FactoryEntry(@NonNull final String name, @Nullable final Class<? extends IFilterFactory> filterFactory) { - this.name = name; - this.filterFactory = filterFactory; - } - - @Override - public String toString() { - return name; - } - } - - private final Activity activity; - private final ArrayList<FactoryEntry> registry; - private final Resources res; - - public FilterUserInterface(final Activity activity) { - this.activity = activity; - this.res = CgeoApplication.getInstance().getResources(); - - registry = new ArrayList<>(); - if (Settings.getCacheType() == CacheType.ALL) { - register(R.string.caches_filter_type, TypeFilter.Factory.class); - } - register(R.string.caches_filter_size, SizeFilter.Factory.class); - register(R.string.cache_terrain, TerrainFilter.Factory.class); - register(R.string.cache_difficulty, DifficultyFilter.Factory.class); - register(R.string.cache_attributes, AttributeFilter.Factory.class); - register(R.string.cache_status, StateFilter.Factory.class); - register(R.string.caches_filter_track, TrackablesFilter.class); - register(R.string.caches_filter_origin, OriginFilter.Factory.class); - register(R.string.caches_filter_distance, DistanceFilter.Factory.class); - register(R.string.caches_filter_popularity, PopularityFilter.Factory.class); - register(R.string.caches_filter_popularity_ratio, PopularityRatioFilter.Factory.class); - register(R.string.caches_filter_personal_data, PersonalDataFilterFactory.class); - register(R.string.caches_filter_rating, RatingFilter.class); - - // sort by localized names - final Collator collator = TextUtils.getCollator(); - Collections.sort(registry, new Comparator<FactoryEntry>() { - - @Override - public int compare(final FactoryEntry lhs, final FactoryEntry rhs) { - return collator.compare(lhs.name, rhs.name); - } - }); - - // reset shall be last - register(R.string.caches_filter_clear, null); - } - - private void register(final int resourceId, @Nullable final Class<? extends IFilterFactory> factoryClass) { - registry.add(new FactoryEntry(res.getString(resourceId), factoryClass)); - } - - public void selectFilter(final Action1<IFilter> runAfterwards) { - final AlertDialog.Builder builder = new AlertDialog.Builder(activity); - builder.setTitle(R.string.caches_filter_title); - - final ArrayAdapter<FactoryEntry> adapter = new ArrayAdapter<>(activity, android.R.layout.select_dialog_item, registry); - - builder.setAdapter(adapter, new DialogInterface.OnClickListener() { - @Override - public void onClick(final DialogInterface dialog, final int itemIndex) { - final FactoryEntry entry = adapter.getItem(itemIndex); - // reset? - final Class<? extends IFilterFactory> filterFactory = entry.filterFactory; - if (filterFactory == null) { - runAfterwards.call(null); - } - else { - try { - final IFilterFactory factoryInstance = filterFactory.newInstance(); - selectFromFactory(factoryInstance, entry.name, runAfterwards); - } catch (final Exception e) { - Log.e("selectFilter", e); - } - } - } - }); - - builder.create().show(); - } - - private void selectFromFactory(@NonNull final IFilterFactory factory, final String menuTitle, final Action1<IFilter> runAfterwards) { - final List<IFilter> filters = Collections.unmodifiableList(factory.getFilters()); - if (filters.size() == 1) { - runAfterwards.call(filters.get(0)); - return; - } - - final AlertDialog.Builder builder = new AlertDialog.Builder(activity); - builder.setTitle(menuTitle); - - final ArrayAdapter<IFilter> adapter = new ArrayAdapter<>(activity, android.R.layout.select_dialog_item, filters); - builder.setAdapter(adapter, new DialogInterface.OnClickListener() { - @Override - public void onClick(final DialogInterface dialog, final int item) { - runAfterwards.call(filters.get(item)); - } - }); - - builder.create().show(); - } - -} diff --git a/main/src/cgeo/geocaching/filter/IFilter.java b/main/src/cgeo/geocaching/filter/IFilter.java index 2bc2004..f590f79 100644 --- a/main/src/cgeo/geocaching/filter/IFilter.java +++ b/main/src/cgeo/geocaching/filter/IFilter.java @@ -12,9 +12,11 @@ public interface IFilter { String getName(); /** - * @return true if the filter accepts the cache, false otherwise + * @return {@code true} if the filter accepts the cache, false otherwise */ boolean accepts(@NonNull final Geocache cache); void filter(@NonNull final List<Geocache> list); + + int getImageId(); }
\ No newline at end of file diff --git a/main/src/cgeo/geocaching/filter/RatingFilter.java b/main/src/cgeo/geocaching/filter/RatingFilter.java index 63e9c0b..16ce16c 100644 --- a/main/src/cgeo/geocaching/filter/RatingFilter.java +++ b/main/src/cgeo/geocaching/filter/RatingFilter.java @@ -6,15 +6,12 @@ import cgeo.geocaching.gcvote.GCVote; import org.eclipse.jdt.annotation.NonNull; -import java.util.Collections; -import java.util.List; - /** * Filter {@link Geocache}s if they have a locally stored {@link GCVote} rating. This filter will not do any network * request to find potentially missing local votes. * */ -public class RatingFilter extends AbstractFilter implements IFilterFactory { +public class RatingFilter extends AbstractFilter { protected RatingFilter() { super(R.string.caches_filter_rating); @@ -25,10 +22,4 @@ public class RatingFilter extends AbstractFilter implements IFilterFactory { return cache.getRating() > 0; } - @Override - @NonNull - public List<RatingFilter> getFilters() { - return Collections.singletonList(this); - } - } diff --git a/main/src/cgeo/geocaching/filter/SizeFilter.java b/main/src/cgeo/geocaching/filter/SizeFilter.java index a48da2c..a19d95b 100644 --- a/main/src/cgeo/geocaching/filter/SizeFilter.java +++ b/main/src/cgeo/geocaching/filter/SizeFilter.java @@ -41,6 +41,6 @@ class SizeFilter extends AbstractFilter { } return filters; } - } + } diff --git a/main/src/cgeo/geocaching/filter/StateFilter.java b/main/src/cgeo/geocaching/filter/StateFilterFactory.java index 289f656..42de764 100644 --- a/main/src/cgeo/geocaching/filter/StateFilter.java +++ b/main/src/cgeo/geocaching/filter/StateFilterFactory.java @@ -10,13 +10,36 @@ import java.util.Collections; import java.util.Comparator; import java.util.List; -abstract class StateFilter extends AbstractFilter { - - protected StateFilter(final int nameResourceId) { - super(nameResourceId); +class StateFilterFactory implements IFilterFactory { + + @Override + @NonNull + public List<? extends IFilter> getFilters() { + final List<AbstractFilter> filters = new ArrayList<>(6); + filters.add(new StateFoundFilter()); + filters.add(new StateNotFoundFilter()); + filters.add(new StateArchivedFilter()); + filters.add(new StateDisabledFilter()); + filters.add(new StatePremiumFilter()); + filters.add(new StateNonPremiumFilter()); + filters.add(new StateOfflineLogFilter()); + filters.add(new StateStoredFilter()); + filters.add(new StateNotStoredFilter()); + filters.add(new RatingFilter()); + filters.add(new TrackablesFilter()); + + Collections.sort(filters, new Comparator<AbstractFilter>() { + + @Override + public int compare(final AbstractFilter filter1, final AbstractFilter filter2) { + return String.CASE_INSENSITIVE_ORDER.compare(filter1.getName(), filter2.getName()); + } + }); + + return filters; } - static class StateFoundFilter extends StateFilter { + static class StateFoundFilter extends AbstractFilter { public StateFoundFilter() { super(R.string.cache_status_found); @@ -29,7 +52,7 @@ abstract class StateFilter extends AbstractFilter { } - static class StateNotFoundFilter extends StateFilter { + static class StateNotFoundFilter extends AbstractFilter { public StateNotFoundFilter() { super(R.string.cache_not_status_found); @@ -42,7 +65,7 @@ abstract class StateFilter extends AbstractFilter { } - static class StateArchivedFilter extends StateFilter { + static class StateArchivedFilter extends AbstractFilter { public StateArchivedFilter() { super(R.string.cache_status_archived); } @@ -53,7 +76,7 @@ abstract class StateFilter extends AbstractFilter { } } - static class StateDisabledFilter extends StateFilter { + static class StateDisabledFilter extends AbstractFilter { public StateDisabledFilter() { super(R.string.cache_status_disabled); } @@ -64,7 +87,7 @@ abstract class StateFilter extends AbstractFilter { } } - static class StatePremiumFilter extends StateFilter { + static class StatePremiumFilter extends AbstractFilter { public StatePremiumFilter() { super(R.string.cache_status_premium); } @@ -75,7 +98,7 @@ abstract class StateFilter extends AbstractFilter { } } - static class StateNonPremiumFilter extends StateFilter { + static class StateNonPremiumFilter extends AbstractFilter { public StateNonPremiumFilter() { super(R.string.cache_status_not_premium); } @@ -86,7 +109,7 @@ abstract class StateFilter extends AbstractFilter { } } - private static class StateOfflineLogFilter extends StateFilter { + private static class StateOfflineLogFilter extends AbstractFilter { public StateOfflineLogFilter() { super(R.string.cache_status_offline_log); } @@ -97,7 +120,7 @@ abstract class StateFilter extends AbstractFilter { } } - static class StateStoredFilter extends StateFilter { + static class StateStoredFilter extends AbstractFilter { public StateStoredFilter() { super(R.string.cache_status_stored); } @@ -108,7 +131,7 @@ abstract class StateFilter extends AbstractFilter { } } - static class StateNotStoredFilter extends StateFilter { + static class StateNotStoredFilter extends AbstractFilter { public StateNotStoredFilter() { super(R.string.cache_status_not_stored); } @@ -119,33 +142,4 @@ abstract class StateFilter extends AbstractFilter { } } - public static class Factory implements IFilterFactory { - - @Override - @NonNull - public List<StateFilter> getFilters() { - final List<StateFilter> filters = new ArrayList<>(6); - filters.add(new StateFoundFilter()); - filters.add(new StateNotFoundFilter()); - filters.add(new StateArchivedFilter()); - filters.add(new StateDisabledFilter()); - filters.add(new StatePremiumFilter()); - filters.add(new StateNonPremiumFilter()); - filters.add(new StateOfflineLogFilter()); - filters.add(new StateStoredFilter()); - filters.add(new StateNotStoredFilter()); - - Collections.sort(filters, new Comparator<StateFilter>() { - - @Override - public int compare(final StateFilter filter1, final StateFilter filter2) { - return String.CASE_INSENSITIVE_ORDER.compare(filter1.getName(), filter2.getName()); - } - }); - - return filters; - } - - } - } diff --git a/main/src/cgeo/geocaching/filter/TrackablesFilter.java b/main/src/cgeo/geocaching/filter/TrackablesFilter.java index bdf91f7..debe11f 100644 --- a/main/src/cgeo/geocaching/filter/TrackablesFilter.java +++ b/main/src/cgeo/geocaching/filter/TrackablesFilter.java @@ -5,10 +5,7 @@ import cgeo.geocaching.R; import org.eclipse.jdt.annotation.NonNull; -import java.util.Collections; -import java.util.List; - -class TrackablesFilter extends AbstractFilter implements IFilterFactory { +class TrackablesFilter extends AbstractFilter { public TrackablesFilter() { super(R.string.caches_filter_track); } @@ -18,10 +15,4 @@ class TrackablesFilter extends AbstractFilter implements IFilterFactory { return cache.hasTrackables(); } - @Override - @NonNull - public List<TrackablesFilter> getFilters() { - return Collections.singletonList(this); - } - } diff --git a/main/src/cgeo/geocaching/filter/TypeFilter.java b/main/src/cgeo/geocaching/filter/TypeFilter.java index 70a8068..b8c879f 100644 --- a/main/src/cgeo/geocaching/filter/TypeFilter.java +++ b/main/src/cgeo/geocaching/filter/TypeFilter.java @@ -43,4 +43,9 @@ class TypeFilter extends AbstractFilter { } } + + @Override + public int getImageId() { + return cacheType.markerId; + } } diff --git a/main/src/cgeo/geocaching/ui/AbstractImageAdapter.java b/main/src/cgeo/geocaching/ui/AbstractImageAdapter.java new file mode 100644 index 0000000..075bfe6 --- /dev/null +++ b/main/src/cgeo/geocaching/ui/AbstractImageAdapter.java @@ -0,0 +1,66 @@ +package cgeo.geocaching.ui; + +import cgeo.geocaching.R; + +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.GridView; +import android.widget.ImageView; + +import java.util.List; + +public abstract class AbstractImageAdapter extends BaseAdapter { + private final Context context; + // references to our images + private final List<Integer> imageIds; + private final int imageWidth; + + public AbstractImageAdapter(final Context context, final GridView gridView, final List<Integer> imageIds) { + this.context = context; + this.imageIds = imageIds; + + final Drawable drawable = context.getResources().getDrawable(imageIds.get(0)); + imageWidth = drawable.getIntrinsicWidth(); + + // fix the column width, now that we know the images + gridView.setColumnWidth(getImageWidth()); + } + + @Override + public int getCount() { + return imageIds.size(); + } + + @Override + public Object getItem(final int position) { + return null; + } + + @Override + public long getItemId(final int position) { + return 0; + } + + // create a new ImageView for each item referenced by the Adapter + @Override + public View getView(final int position, final View convertView, final ViewGroup parent) { + ImageView imageView; + if (convertView == null) { // if it's not recycled, initialize some attributes + imageView = (ImageView) LayoutInflater.from(context).inflate(R.layout.grid_image, null); + } else { + imageView = (ImageView) convertView; + } + + imageView.setImageResource(imageIds.get(position)); + return imageView; + } + + int getImageWidth() { + return imageWidth; + } + +}
\ No newline at end of file |