From b7e895e8a91dcc4b964b1778da6229943f5ad6ce Mon Sep 17 00:00:00 2001 From: Ialokim Date: Sat, 9 Dec 2017 15:55:28 -0600 Subject: [PATCH] order regions by continents --- .../grobox/transportr/networks/Continent.java | 56 +++++++++ .../transportr/networks/ContinentItem.java | 111 ++++++++++++++++++ .../networks/ContinentViewHolder.java | 50 ++++++++ .../PickTransportNetworkActivity.java | 79 +++++++++---- .../de/grobox/transportr/networks/Region.java | 60 +++++----- .../networks/TransportNetworks.java | 2 +- .../layout/list_item_transport_continent.xml | 52 ++++++++ app/src/main/res/values-de/strings.xml | 6 + app/src/main/res/values/strings.xml | 11 +- 9 files changed, 372 insertions(+), 55 deletions(-) create mode 100644 app/src/main/java/de/grobox/transportr/networks/Continent.java create mode 100644 app/src/main/java/de/grobox/transportr/networks/ContinentItem.java create mode 100644 app/src/main/java/de/grobox/transportr/networks/ContinentViewHolder.java create mode 100644 app/src/main/res/layout/list_item_transport_continent.xml diff --git a/app/src/main/java/de/grobox/transportr/networks/Continent.java b/app/src/main/java/de/grobox/transportr/networks/Continent.java new file mode 100644 index 000000000..e6e949eb4 --- /dev/null +++ b/app/src/main/java/de/grobox/transportr/networks/Continent.java @@ -0,0 +1,56 @@ +/* + * Transportr + * + * Copyright (c) 2013 - 2017 Torsten Grote + * + * 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 . + */ + +package de.grobox.transportr.networks; + +import android.content.Context; +import android.support.annotation.Nullable; +import android.support.annotation.StringRes; + +import javax.annotation.ParametersAreNonnullByDefault; + +import de.grobox.transportr.R; + +@ParametersAreNonnullByDefault +enum Continent { + + EUROPE(R.string.np_continent_europe), + AFRICA(R.string.np_continent_africa), + NORTH_AMERICA(R.string.np_continent_north_america), + SOUTH_AMERICA(R.string.np_continent_south_america), + ASIA(R.string.np_continent_asia), + AUSTRALIA(R.string.np_continent_australia); + + + private final @StringRes int name; + + Continent(@StringRes int name) { + this.name = name; + } + + @StringRes + public int getName() { + return name; + } + + public String getName(Context context) { + return context.getString(name); + } + +} diff --git a/app/src/main/java/de/grobox/transportr/networks/ContinentItem.java b/app/src/main/java/de/grobox/transportr/networks/ContinentItem.java new file mode 100644 index 000000000..15b0171fb --- /dev/null +++ b/app/src/main/java/de/grobox/transportr/networks/ContinentItem.java @@ -0,0 +1,111 @@ +/* + * Transportr + * + * Copyright (c) 2013 - 2017 Torsten Grote + * + * 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 . + */ + +package de.grobox.transportr.networks; + +import android.content.Context; +import android.support.annotation.IdRes; +import android.support.annotation.LayoutRes; +import android.support.v4.view.ViewCompat; +import android.view.View; + +import com.mikepenz.fastadapter.IItem; +import com.mikepenz.fastadapter.expandable.items.AbstractExpandableItem; +import com.mikepenz.fastadapter.listeners.OnClickListener; + +import java.util.Comparator; +import java.util.List; + +import javax.annotation.ParametersAreNonnullByDefault; + +import de.grobox.transportr.R; + +@ParametersAreNonnullByDefault +class ContinentItem extends AbstractExpandableItem { + + private final Continent continent; + + ContinentItem(Continent continent) { + super(); + this.continent = continent; + } + + @IdRes + @Override + public int getType() { + return R.id.list_item_transport_continent; + } + + @Override + @LayoutRes + public int getLayoutRes() { + return R.layout.list_item_transport_continent; + } + + @Override + public void bindView(ContinentViewHolder ui, List payloads) { + super.bindView(ui, payloads); + ui.bind(continent, isExpanded()); + } + + @Override + public ContinentViewHolder getViewHolder(View view) { + return new ContinentViewHolder(view); + } + + @Override + public long getIdentifier() { + return continent.getName(); + } + + @Override + public OnClickListener getOnItemClickListener() { + return (v, adapter, item, position) -> { + if (item.getSubItems() != null) { + if (!item.isExpanded()) { + ViewCompat.animate(v.findViewById(R.id.chevron)).rotation(180).start(); + } else { + ViewCompat.animate(v.findViewById(R.id.chevron)).rotation(0).start(); + } + return true; + } + return false; + }; + } + + static class ContinentComparator implements Comparator { + + private final Context context; + + ContinentComparator(Context context) { + super(); + this.context = context; + } + + @Override + public int compare(IItem i1, IItem i2) { + if (i1 instanceof ContinentItem && i2 instanceof ContinentItem) { + // sort continents alphabetically + return ((ContinentItem) i1).continent.getName(context).compareTo(((ContinentItem) i2).continent.getName(context)); + } + return 0; + } + } + +} diff --git a/app/src/main/java/de/grobox/transportr/networks/ContinentViewHolder.java b/app/src/main/java/de/grobox/transportr/networks/ContinentViewHolder.java new file mode 100644 index 000000000..7d8504a2d --- /dev/null +++ b/app/src/main/java/de/grobox/transportr/networks/ContinentViewHolder.java @@ -0,0 +1,50 @@ +/* + * Transportr + * + * Copyright (c) 2013 - 2017 Torsten Grote + * + * 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 . + */ + +package de.grobox.transportr.networks; + +import android.os.Build; +import android.support.v7.widget.RecyclerView; +import android.view.View; +import android.widget.ImageView; +import android.widget.TextView; + +import de.grobox.transportr.R; + +import static android.view.View.GONE; +import static android.view.View.VISIBLE; + +class ContinentViewHolder extends RecyclerView.ViewHolder { + + private final TextView name; + private final ImageView chevron; + + ContinentViewHolder(View v) { + super(v); + name = v.findViewById(R.id.name); + chevron = v.findViewById(R.id.chevron); + } + + void bind(Continent continent, boolean expanded) { + name.setText(continent.getName()); + if (expanded) chevron.setRotation(0); + else chevron.setRotation(180); + } + +} diff --git a/app/src/main/java/de/grobox/transportr/networks/PickTransportNetworkActivity.java b/app/src/main/java/de/grobox/transportr/networks/PickTransportNetworkActivity.java index 22db9c453..e8648098a 100644 --- a/app/src/main/java/de/grobox/transportr/networks/PickTransportNetworkActivity.java +++ b/app/src/main/java/de/grobox/transportr/networks/PickTransportNetworkActivity.java @@ -23,16 +23,19 @@ import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.ActionBar; +import android.support.v7.util.SortedList; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.MenuItem; +import com.google.common.collect.RowSortedTable; import com.mikepenz.fastadapter.IItem; import com.mikepenz.fastadapter.ISelectionListener; import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter; import com.mikepenz.fastadapter.expandable.ExpandableExtension; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -82,7 +85,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { adapter = new FastItemAdapter<>(); adapter.withSelectable(true); - adapter.getItemAdapter().withComparator(new RegionItem.RegionComparator(this)); + adapter.getItemAdapter().withComparator(new ContinentItem.ContinentComparator(this)); adapter.withSelectionListener(this); expandableExtension = new ExpandableExtension<>(); adapter.addExtension(expandableExtension); @@ -90,14 +93,22 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { list.setLayoutManager(new LinearLayoutManager(this)); list.setAdapter(adapter); - Map> networksByRegion = getTransportNetworksByRegion(); - for (Region region : networksByRegion.keySet()) { - RegionItem regionItem = new RegionItem(region); - List networks = networksByRegion.get(region); - List networkItems = new ArrayList<>(networks.size()); - for (TransportNetwork n : networks) networkItems.add(new TransportNetworkItem(n)); - regionItem.withSubItems(networkItems); - adapter.add(regionItem); + Map>> continents = getRegionsAndNetworksByContinent(); + for (Continent continent : continents.keySet()) { + ContinentItem continentItem = new ContinentItem(continent); + Map> regions = continents.get(continent); + List regionItems = new ArrayList(regions.size()); + for (Region region : regions.keySet()) { + RegionItem regionItem = new RegionItem(region); + List networks = regions.get(region); + List networkItems = new ArrayList<>(networks.size()); + for (TransportNetwork n : networks) networkItems.add(new TransportNetworkItem(n)); + regionItem.withSubItems(networkItems); + regionItems.add(regionItem); + } + Collections.sort(regionItems, new RegionItem.RegionComparator(this)); + continentItem.withSubItems(regionItems); + adapter.add(continentItem); } if (savedInstanceState != null) adapter.withSavedInstanceState(savedInstanceState); @@ -134,31 +145,49 @@ private void selectItem() { selectAllowed = true; return; } - int pos = adapter.getPosition(new RegionItem(network.getRegion())); + int pos = adapter.getPosition(new ContinentItem(network.getRegion().getContinent())); if (pos != -1) { expandableExtension.expand(pos); - pos = adapter.getPosition(new TransportNetworkItem(network)); + pos = adapter.getPosition(new RegionItem(network.getRegion())); if (pos != -1) { - adapter.select(pos, false); - list.scrollToPosition(pos); - list.smoothScrollBy(0, -75); - selectAllowed = true; + expandableExtension.expand(pos); + pos = adapter.getPosition(new TransportNetworkItem(network)); + if (pos != -1) { + adapter.select(pos, false); + list.scrollToPosition(pos); + list.smoothScrollBy(0, -75); + selectAllowed = true; + } } } } - - private HashMap> getTransportNetworksByRegion() { - HashMap> networks = new HashMap<>(); + + private HashMap>> getRegionsAndNetworksByContinent() { + + HashMap>> continents = new HashMap<>(); for (TransportNetwork n : TransportNetworks.networks) { - if (networks.containsKey(n.getRegion())) { - networks.get(n.getRegion()).add(n); - } else { - List list = new ArrayList<>(); - list.add(n); - networks.put(n.getRegion(), list); + Region region = n.getRegion(); + Continent continent = region.getContinent(); + HashMap> regions = new HashMap(); + List networks = new ArrayList(); + + if (continents.containsKey(continent)) { + regions = continents.get(continent); + } + if (regions.containsKey(region)) { + networks = regions.get(region); + } + + networks.add(n); + + if (!regions.containsKey(region)) { + regions.put(region, networks); + } + if (!continents.containsKey(continent)) { + continents.put(continent, regions); } } - return networks; + return continents; } } diff --git a/app/src/main/java/de/grobox/transportr/networks/Region.java b/app/src/main/java/de/grobox/transportr/networks/Region.java index 5c6f25dda..e8020e691 100644 --- a/app/src/main/java/de/grobox/transportr/networks/Region.java +++ b/app/src/main/java/de/grobox/transportr/networks/Region.java @@ -30,39 +30,41 @@ @ParametersAreNonnullByDefault enum Region { - EUROPE(R.string.np_region_europe, "๐Ÿ‡ช๐Ÿ‡บ"), - GERMANY(R.string.np_region_germany, "๐Ÿ‡ฉ๐Ÿ‡ช"), - AUSTRIA(R.string.np_region_austria, "๐Ÿ‡ฆ๐Ÿ‡น"), - LIECHTENSTEIN(R.string.np_region_liechtenstein, "๐Ÿ‡ฑ๐Ÿ‡ฎ"), - SWITZERLAND(R.string.np_region_switzerland, "๐Ÿ‡จ๐Ÿ‡ญ"), - BELGIUM(R.string.np_region_belgium, "๐Ÿ‡ง๐Ÿ‡ช"), - LUXEMBOURG(R.string.np_region_luxembourg, "๐Ÿ‡ฑ๐Ÿ‡บ"), - NETHERLANDS(R.string.np_region_netherlands, "๐Ÿ‡ณ๐Ÿ‡ฑ"), - DENMARK(R.string.np_region_denmark, "๐Ÿ‡ฉ๐Ÿ‡ฐ"), - SWEDEN(R.string.np_region_sweden, "๐Ÿ‡ธ๐Ÿ‡ช"), - NORWAY(R.string.np_region_norway, "๐Ÿ‡ณ๐Ÿ‡ด"), - FINLAND(R.string.np_region_finland, "๐Ÿ‡ซ๐Ÿ‡ฎ"), - GREAT_BRITAIN(R.string.np_region_gb, "๐Ÿ‡ฌ๐Ÿ‡ง"), - IRELAND(R.string.np_region_ireland, "๐Ÿ‡ฎ๐Ÿ‡ช"), - ITALY(R.string.np_region_italy, "๐Ÿ‡ฎ๐Ÿ‡น"), - POLAND(R.string.np_region_poland, "๐Ÿ‡ต๐Ÿ‡ฑ"), - UAE(R.string.np_region_uae, "๐Ÿ‡ฆ๐Ÿ‡ช"), - USA(R.string.np_region_usa, "๐Ÿ‡บ๐Ÿ‡ธ"), - AUSTRALIA(R.string.np_region_australia, "๐Ÿ‡ฆ๐Ÿ‡บ"), - FRANCE(R.string.np_region_france, "๐Ÿ‡ซ๐Ÿ‡ท"), - NEW_ZEALAND(R.string.np_region_nz, "๐Ÿ‡ณ๐Ÿ‡ฟ"), - SPAIN(R.string.np_region_spain, "๐Ÿ‡ช๐Ÿ‡ธ"), - BRAZIL(R.string.np_region_br, "๐Ÿ‡ง๐Ÿ‡ท"), - CANADA(R.string.np_region_canada, "๐Ÿ‡จ๐Ÿ‡ฆ"), - COSTA_RICA(R.string.np_region_costa_rica, "๐Ÿ‡จ๐Ÿ‡ท"), - AFRICA(R.string.np_region_africa, "๐ŸŒ"); + EUROPE(R.string.np_region_europe, "๐Ÿ‡ช๐Ÿ‡บ", Continent.EUROPE), + GERMANY(R.string.np_region_germany, "๐Ÿ‡ฉ๐Ÿ‡ช", Continent.EUROPE), + AUSTRIA(R.string.np_region_austria, "๐Ÿ‡ฆ๐Ÿ‡น", Continent.EUROPE), + LIECHTENSTEIN(R.string.np_region_liechtenstein, "๐Ÿ‡ฑ๐Ÿ‡ฎ", Continent.EUROPE), + SWITZERLAND(R.string.np_region_switzerland, "๐Ÿ‡จ๐Ÿ‡ญ", Continent.EUROPE), + BELGIUM(R.string.np_region_belgium, "๐Ÿ‡ง๐Ÿ‡ช", Continent.EUROPE), + LUXEMBOURG(R.string.np_region_luxembourg, "๐Ÿ‡ฑ๐Ÿ‡บ", Continent.EUROPE), + NETHERLANDS(R.string.np_region_netherlands, "๐Ÿ‡ณ๐Ÿ‡ฑ", Continent.EUROPE), + DENMARK(R.string.np_region_denmark, "๐Ÿ‡ฉ๐Ÿ‡ฐ", Continent.EUROPE), + SWEDEN(R.string.np_region_sweden, "๐Ÿ‡ธ๐Ÿ‡ช", Continent.EUROPE), + NORWAY(R.string.np_region_norway, "๐Ÿ‡ณ๐Ÿ‡ด", Continent.EUROPE), + FINLAND(R.string.np_region_finland, "๐Ÿ‡ซ๐Ÿ‡ฎ", Continent.EUROPE), + GREAT_BRITAIN(R.string.np_region_gb, "๐Ÿ‡ฌ๐Ÿ‡ง", Continent.EUROPE), + IRELAND(R.string.np_region_ireland, "๐Ÿ‡ฎ๐Ÿ‡ช", Continent.EUROPE), + ITALY(R.string.np_region_italy, "๐Ÿ‡ฎ๐Ÿ‡น", Continent.EUROPE), + POLAND(R.string.np_region_poland, "๐Ÿ‡ต๐Ÿ‡ฑ", Continent.EUROPE), + UAE(R.string.np_region_uae, "๐Ÿ‡ฆ๐Ÿ‡ช", Continent.ASIA), + USA(R.string.np_region_usa, "๐Ÿ‡บ๐Ÿ‡ธ", Continent.NORTH_AMERICA), + AUSTRALIA(R.string.np_region_australia, "๐Ÿ‡ฆ๐Ÿ‡บ", Continent.AUSTRALIA), + FRANCE(R.string.np_region_france, "๐Ÿ‡ซ๐Ÿ‡ท", Continent.EUROPE), + NEW_ZEALAND(R.string.np_region_nz, "๐Ÿ‡ณ๐Ÿ‡ฟ", Continent.AUSTRALIA), + SPAIN(R.string.np_region_spain, "๐Ÿ‡ช๐Ÿ‡ธ", Continent.EUROPE), + BRAZIL(R.string.np_region_br, "๐Ÿ‡ง๐Ÿ‡ท", Continent.SOUTH_AMERICA), + CANADA(R.string.np_region_canada, "๐Ÿ‡จ๐Ÿ‡ฆ", Continent.NORTH_AMERICA), + COSTA_RICA(R.string.np_region_costa_rica, "๐Ÿ‡จ๐Ÿ‡ท", Continent.NORTH_AMERICA), + GHANA(R.string.np_region_ghana, "๐Ÿ‡ฌ๐Ÿ‡ญ", Continent.AFRICA); private final @StringRes int name; private final @Nullable String flag; + private final Continent continent; - Region(@StringRes int name, @Nullable String flag) { + Region(@StringRes int name, @Nullable String flag, Continent continent) { this.name = name; this.flag = flag; + this.continent = continent; } @StringRes @@ -78,5 +80,9 @@ public String getName(Context context) { public String getFlag() { return flag; } + + public Continent getContinent() { + return continent; + } } diff --git a/app/src/main/java/de/grobox/transportr/networks/TransportNetworks.java b/app/src/main/java/de/grobox/transportr/networks/TransportNetworks.java index 79ff027d5..2ef9b4734 100644 --- a/app/src/main/java/de/grobox/transportr/networks/TransportNetworks.java +++ b/app/src/main/java/de/grobox/transportr/networks/TransportNetworks.java @@ -638,7 +638,7 @@ public interface TransportNetworks { .setId(NetworkId.GHANA) .setName(R.string.np_name_ghana) .setDescription(R.string.np_desc_ghana) - .setRegion(Region.AFRICA) + .setRegion(Region.GHANA) .setStatus(ALPHA) .setGoodLineNames(true) .build(), diff --git a/app/src/main/res/layout/list_item_transport_continent.xml b/app/src/main/res/layout/list_item_transport_continent.xml new file mode 100644 index 000000000..fe0b86133 --- /dev/null +++ b/app/src/main/res/layout/list_item_transport_continent.xml @@ -0,0 +1,52 @@ + + + + + + + + + + diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 9fa818665..db1aafb1e 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -156,6 +156,12 @@ es welche gibt) anzeigen lassen. ร„nderungsprotokoll Was gibt\'s Neues Mehrโ€ฆ + Europa + Afrika + Nordamerika + Sรผdamerika + Asien + Australien Australien Melbourne, Sydney, Brisbane, Perth, Adelaide, Hobart, Launceston, Burnie Australien, Neusรผdwales, Sydney diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 586436e68..bc94dae69 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -186,6 +186,13 @@ there are any). Changelog What\'s New Moreโ€ฆ + + Europe + Africa + North America + South America + Asia + Australia Australia Australia @@ -230,7 +237,7 @@ there are any). Denmark Denmark, Copenhagen - Europe + Europe (cross-border) Europe Long-distance trains only Railteam alliance @@ -373,7 +380,7 @@ there are any). Tren Urbano INCOFER - Africa + Ghana Ghana Accra