Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/adapters/PalinaAdapter.java
Show All 13 Lines | BusTO (backend components) | ||||
You should have received a copy of the GNU General Public License | You should have received a copy of the GNU General Public License | ||||
along with this program. If not, see <http://www.gnu.org/licenses/>. | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
package it.reyboz.bustorino.adapters; | package it.reyboz.bustorino.adapters; | ||||
import android.content.Context; | import android.content.Context; | ||||
import androidx.annotation.NonNull; | import androidx.annotation.NonNull; | ||||
import androidx.preference.PreferenceManager; | |||||
import android.content.SharedPreferences; | |||||
import android.os.Build; | |||||
import android.util.TypedValue; | import android.util.TypedValue; | ||||
import android.view.LayoutInflater; | import android.view.LayoutInflater; | ||||
import android.view.View; | import android.view.View; | ||||
import android.view.ViewGroup; | import android.view.ViewGroup; | ||||
import android.widget.ArrayAdapter; | import android.widget.ArrayAdapter; | ||||
import android.widget.TextView; | import android.widget.TextView; | ||||
import java.util.List; | import java.util.List; | ||||
import java.util.Locale; | |||||
import it.reyboz.bustorino.BuildConfig; | |||||
import it.reyboz.bustorino.R; | import it.reyboz.bustorino.R; | ||||
import it.reyboz.bustorino.backend.Palina; | import it.reyboz.bustorino.backend.Palina; | ||||
import it.reyboz.bustorino.backend.Passaggio; | import it.reyboz.bustorino.backend.Passaggio; | ||||
import it.reyboz.bustorino.backend.Route; | import it.reyboz.bustorino.backend.Route; | ||||
import it.reyboz.bustorino.backend.utils; | import it.reyboz.bustorino.backend.utils; | ||||
/** | /** | ||||
* This once was a ListView Adapter for BusLine[]. | * This once was a ListView Adapter for BusLine[]. | ||||
* | * | ||||
* Thanks to Framentos developers for the guide: | * Thanks to Framentos developers for the guide: | ||||
* http://www.framentos.com/en/android-tutorial/2012/07/16/listview-in-android-using-custom-listadapter-and-viewcache/# | * http://www.framentos.com/en/android-tutorial/2012/07/16/listview-in-android-using-custom-listadapter-and-viewcache/# | ||||
* | * | ||||
* @author Valerio Bozzolan | * @author Valerio Bozzolan | ||||
* @author Ludovico Pavesi | * @author Ludovico Pavesi | ||||
*/ | */ | ||||
public class PalinaAdapter extends ArrayAdapter<Route> { | public class PalinaAdapter extends ArrayAdapter<Route> implements SharedPreferences.OnSharedPreferenceChangeListener { | ||||
private LayoutInflater li; | private LayoutInflater li; | ||||
private static int row_layout = R.layout.entry_bus_line_passage; | private static int row_layout = R.layout.entry_bus_line_passage; | ||||
private static final int metroBg = R.drawable.route_background_metro; | private static final int metroBg = R.drawable.route_background_metro; | ||||
private static final int busBg = R.drawable.route_background_bus; | private static final int busBg = R.drawable.route_background_bus; | ||||
private static final int extraurbanoBg = R.drawable.route_background_bus_long_distance; | private static final int extraurbanoBg = R.drawable.route_background_bus_long_distance; | ||||
private static final int busIcon = R.drawable.bus; | private static final int busIcon = R.drawable.bus; | ||||
private static final int trainIcon = R.drawable.subway; | private static final int trainIcon = R.drawable.subway; | ||||
private static final int tramIcon = R.drawable.tram; | private static final int tramIcon = R.drawable.tram; | ||||
private final String KEY_CAPITALIZE; | |||||
private Capitalize capit = Capitalize.DO_NOTHING; | |||||
//private static final int cityIcon = R.drawable.city; | //private static final int cityIcon = R.drawable.city; | ||||
// hey look, a pattern! | // hey look, a pattern! | ||||
private static class ViewHolder { | private static class ViewHolder { | ||||
TextView rowStopIcon; | TextView rowStopIcon; | ||||
TextView rowRouteDestination; | TextView rowRouteDestination; | ||||
TextView rowRouteTimetable; | TextView rowRouteTimetable; | ||||
} | } | ||||
private static Capitalize getCapitalize(SharedPreferences shPr, String key){ | |||||
String capitalize = shPr.getString(key, ""); | |||||
switch (capitalize.trim()){ | |||||
case "KEEP": | |||||
return Capitalize.DO_NOTHING; | |||||
case "CAPITALIZE_ALL": | |||||
return Capitalize.ALL; | |||||
case "CAPITALIZE_FIRST": | |||||
return Capitalize.FIRST; | |||||
} | |||||
return Capitalize.DO_NOTHING; | |||||
} | |||||
public PalinaAdapter(Context context, Palina p) { | public PalinaAdapter(Context context, Palina p) { | ||||
super(context, row_layout, p.queryAllRoutes()); | super(context, row_layout, p.queryAllRoutes()); | ||||
li = LayoutInflater.from(context); | li = LayoutInflater.from(context); | ||||
sort(new RouteSorterByArrivalTime()); | |||||
KEY_CAPITALIZE = context.getString(R.string.pref_arrival_times_capit); | |||||
SharedPreferences defSharPref = PreferenceManager.getDefaultSharedPreferences(context); | |||||
defSharPref.registerOnSharedPreferenceChangeListener(this); | |||||
this.capit = getCapitalize(defSharPref, KEY_CAPITALIZE); | |||||
} | } | ||||
/** | /** | ||||
* Some parts taken from the AdapterBusLines class.<br> | * Some parts taken from the AdapterBusLines class.<br> | ||||
* Some parts inspired by these enlightening tutorials:<br> | * Some parts inspired by these enlightening tutorials:<br> | ||||
* http://www.simplesoft.it/android/guida-agli-adapter-e-le-listview-in-android.html<br> | * http://www.simplesoft.it/android/guida-agli-adapter-e-le-listview-in-android.html<br> | ||||
* https://www.codeofaninja.com/2013/09/android-viewholder-pattern-example.html<br> | * https://www.codeofaninja.com/2013/09/android-viewholder-pattern-example.html<br> | ||||
* And some other bits and bobs TIRATI FUORI DAL NULLA CON L'INTUIZIONE INTELLETTUALE PERCHÉ | * And some other bits and bobs TIRATI FUORI DAL NULLA CON L'INTUIZIONE INTELLETTUALE PERCHÉ | ||||
Show All 21 Lines | public View getView(int position, View convertView, @NonNull ViewGroup parent) { | ||||
// RECOVER THIS STUFF! | // RECOVER THIS STUFF! | ||||
vh = (ViewHolder) convertView.getTag(); | vh = (ViewHolder) convertView.getTag(); | ||||
} | } | ||||
Route route = getItem(position); | Route route = getItem(position); | ||||
vh.rowStopIcon.setText(route.getNameForDisplay()); | vh.rowStopIcon.setText(route.getNameForDisplay()); | ||||
if(route.destinazione==null || route.destinazione.length() == 0) { | if(route.destinazione==null || route.destinazione.length() == 0) { | ||||
vh.rowRouteDestination.setVisibility(View.GONE); | vh.rowRouteDestination.setVisibility(View.GONE); | ||||
// move around the route timetable | |||||
final ViewGroup.MarginLayoutParams pars = (ViewGroup.MarginLayoutParams) vh.rowRouteTimetable.getLayoutParams(); | |||||
if (pars!=null){ | |||||
pars.topMargin = 16; | |||||
if(Build.VERSION.SDK_INT >= 17) | |||||
pars.setMarginStart(20); | |||||
pars.leftMargin = 20; | |||||
} | |||||
} else { | } else { | ||||
// View Holder Pattern(R) renders each element from a previous one: if the other one had an invisible rowRouteDestination, we need to make it visible. | // View Holder Pattern(R) renders each element from a previous one: if the other one had an invisible rowRouteDestination, we need to make it visible. | ||||
vh.rowRouteDestination.setVisibility(View.VISIBLE); | vh.rowRouteDestination.setVisibility(View.VISIBLE); | ||||
vh.rowRouteDestination.setText(route.destinazione); | String dest = route.destinazione; | ||||
switch (capit){ | |||||
case ALL: | |||||
dest = route.destinazione.toUpperCase(Locale.ROOT); | |||||
break; | |||||
case FIRST: | |||||
dest = utils.toTitleCase(route.destinazione, true); | |||||
break; | |||||
case DO_NOTHING: | |||||
default: | |||||
} | |||||
vh.rowRouteDestination.setText(dest); | |||||
} | } | ||||
switch (route.type) { | switch (route.type) { | ||||
//UNKNOWN = BUS for the moment | //UNKNOWN = BUS for the moment | ||||
case UNKNOWN: | case UNKNOWN: | ||||
case BUS: | case BUS: | ||||
default: | default: | ||||
// convertView could contain another background, reset it | // convertView could contain another background, reset it | ||||
Show All 17 Lines | public View getView(int position, View convertView, @NonNull ViewGroup parent) { | ||||
vh.rowStopIcon.setBackgroundResource(busBg); | vh.rowStopIcon.setBackgroundResource(busBg); | ||||
vh.rowRouteDestination.setCompoundDrawablesWithIntrinsicBounds(tramIcon, 0, 0, 0); | vh.rowRouteDestination.setCompoundDrawablesWithIntrinsicBounds(tramIcon, 0, 0, 0); | ||||
break; | break; | ||||
} | } | ||||
List<Passaggio> passaggi = route.passaggi; | List<Passaggio> passaggi = route.passaggi; | ||||
if(passaggi.size() == 0) { | if(passaggi.size() == 0) { | ||||
vh.rowRouteTimetable.setText(R.string.no_passages); | vh.rowRouteTimetable.setText(R.string.no_passages); | ||||
} else { | } else { | ||||
vh.rowRouteTimetable.setText(route.getPassaggiToString()); | vh.rowRouteTimetable.setText(route.getPassaggiToString()); | ||||
} | } | ||||
return convertView; | return convertView; | ||||
} | } | ||||
@Override | |||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { | |||||
if(key.equals(KEY_CAPITALIZE)){ | |||||
capit = getCapitalize(sharedPreferences, KEY_CAPITALIZE); | |||||
notifyDataSetChanged(); | |||||
} | |||||
} | |||||
enum Capitalize{ | |||||
DO_NOTHING, ALL, FIRST | |||||
} | |||||
} | } |
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register