Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/adapters/ArrivalsStopAdapter.java
Show All 12 Lines | BusTO - UI components | ||||
GNU General Public License for more details. | GNU General Public License for more details. | ||||
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 android.content.SharedPreferences; | |||||
import android.location.Location; | import android.location.Location; | ||||
import androidx.annotation.NonNull; | import androidx.annotation.NonNull; | ||||
import androidx.annotation.Nullable; | import androidx.annotation.Nullable; | ||||
import androidx.core.util.Pair; | import androidx.core.util.Pair; | ||||
import androidx.preference.PreferenceManager; | |||||
import androidx.recyclerview.widget.RecyclerView; | import androidx.recyclerview.widget.RecyclerView; | ||||
import android.util.Log; | import android.util.Log; | ||||
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.TextView; | import android.widget.TextView; | ||||
import it.reyboz.bustorino.R; | import it.reyboz.bustorino.R; | ||||
import it.reyboz.bustorino.backend.*; | import it.reyboz.bustorino.backend.*; | ||||
import it.reyboz.bustorino.fragments.FragmentListenerMain; | import it.reyboz.bustorino.fragments.FragmentListenerMain; | ||||
import it.reyboz.bustorino.util.RoutePositionSorter; | import it.reyboz.bustorino.util.RoutePositionSorter; | ||||
import it.reyboz.bustorino.util.StopSorterByDistance; | import it.reyboz.bustorino.util.StopSorterByDistance; | ||||
import java.util.*; | import java.util.*; | ||||
public class ArrivalsStopAdapter extends RecyclerView.Adapter<ArrivalsStopAdapter.ViewHolder> { | public class ArrivalsStopAdapter extends RecyclerView.Adapter<ArrivalsStopAdapter.ViewHolder> implements SharedPreferences.OnSharedPreferenceChangeListener { | ||||
private final static int layoutRes = R.layout.arrivals_nearby_card; | private final static int layoutRes = R.layout.arrivals_nearby_card; | ||||
//private List<Stop> stops; | //private List<Stop> stops; | ||||
private @Nullable Location userPosition; | private @Nullable Location userPosition; | ||||
private FragmentListenerMain listener; | private FragmentListenerMain listener; | ||||
private List< Pair<Stop, Route> > routesPairList = new ArrayList<>(); | private List< Pair<Stop, Route> > routesPairList; | ||||
private final Context context; | private final Context context; | ||||
//Maximum number of stops to keep | //Maximum number of stops to keep | ||||
private final int MAX_STOPS = 20; //TODO: make it programmable | private final int MAX_STOPS = 20; //TODO: make it programmable | ||||
private String KEY_CAPITALIZE; | |||||
private NameCapitalize capit; | |||||
public ArrivalsStopAdapter(@Nullable List< Pair<Stop, Route> > routesPairList, FragmentListenerMain fragmentListener, Context con, @Nullable Location pos) { | public ArrivalsStopAdapter(@Nullable List< Pair<Stop, Route> > routesPairList, FragmentListenerMain fragmentListener, Context con, @Nullable Location pos) { | ||||
listener = fragmentListener; | listener = fragmentListener; | ||||
userPosition = pos; | userPosition = pos; | ||||
this.routesPairList = routesPairList; | this.routesPairList = routesPairList; | ||||
context = con.getApplicationContext(); | context = con.getApplicationContext(); | ||||
resetListAndPosition(); | resetListAndPosition(); | ||||
// if(paline!=null) | // if(paline!=null) | ||||
//resetRoutesPairList(paline); | //resetRoutesPairList(paline); | ||||
KEY_CAPITALIZE = context.getString(R.string.pref_arrival_times_capit); | |||||
SharedPreferences defSharPref = PreferenceManager.getDefaultSharedPreferences(context); | |||||
defSharPref.registerOnSharedPreferenceChangeListener(this); | |||||
String capitalizeKey = defSharPref.getString(KEY_CAPITALIZE, ""); | |||||
this.capit = NameCapitalize.getCapitalize(capitalizeKey); | |||||
} | } | ||||
@NonNull | |||||
@Override | @Override | ||||
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | ||||
final View view = LayoutInflater.from(parent.getContext()).inflate(layoutRes, parent, false); | final View view = LayoutInflater.from(parent.getContext()).inflate(layoutRes, parent, false); | ||||
return new ViewHolder(view); | return new ViewHolder(view); | ||||
} | } | ||||
@Override | @Override | ||||
Show All 10 Lines | public void onBindViewHolder(@NonNull ViewHolder holder, int position) { | ||||
} else { | } else { | ||||
holder.distancetextView.setVisibility(View.GONE); | holder.distancetextView.setVisibility(View.GONE); | ||||
} | } | ||||
final String stopText = String.format(context.getResources().getString(R.string.two_strings_format),stop.getStopDisplayName(),stop.ID); | final String stopText = String.format(context.getResources().getString(R.string.two_strings_format),stop.getStopDisplayName(),stop.ID); | ||||
holder.stopNameView.setText(stopText); | holder.stopNameView.setText(stopText); | ||||
//final String routeName = String.format(context.getResources().getString(R.string.two_strings_format),r.getNameForDisplay(),r.destinazione); | //final String routeName = String.format(context.getResources().getString(R.string.two_strings_format),r.getNameForDisplay(),r.destinazione); | ||||
if (r!=null) { | if (r!=null) { | ||||
holder.lineNameTextView.setText(r.getNameForDisplay()); | holder.lineNameTextView.setText(r.getNameForDisplay()); | ||||
holder.lineDirectionTextView.setText(r.destinazione); | holder.lineDirectionTextView.setText(NameCapitalize.capitalizePass(r.destinazione, capit)); | ||||
holder.arrivalsTextView.setText(r.getPassaggiToString(0,2,true)); | holder.arrivalsTextView.setText(r.getPassaggiToString(0,2,true)); | ||||
} else { | } else { | ||||
holder.lineNameTextView.setVisibility(View.INVISIBLE); | holder.lineNameTextView.setVisibility(View.INVISIBLE); | ||||
holder.lineDirectionTextView.setVisibility(View.INVISIBLE); | holder.lineDirectionTextView.setVisibility(View.INVISIBLE); | ||||
//holder.arrivalsTextView.setVisibility(View.INVISIBLE); | //holder.arrivalsTextView.setVisibility(View.INVISIBLE); | ||||
} | } | ||||
/* EXPERIMENTS | /* EXPERIMENTS | ||||
if(r.destinazione==null || r.destinazione.trim().isEmpty()){ | if(r.destinazione==null || r.destinazione.trim().isEmpty()){ | ||||
Show All 15 Lines | public void onBindViewHolder(@NonNull ViewHolder holder, int position) { | ||||
} | } | ||||
} | } | ||||
@Override | @Override | ||||
public int getItemCount() { | public int getItemCount() { | ||||
return routesPairList.size(); | return routesPairList.size(); | ||||
} | } | ||||
@Override | |||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { | |||||
if(key.equals(KEY_CAPITALIZE)){ | |||||
String k = sharedPreferences.getString(KEY_CAPITALIZE, ""); | |||||
capit = NameCapitalize.getCapitalize(k); | |||||
notifyDataSetChanged(); | |||||
} | |||||
} | |||||
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { | class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { | ||||
TextView lineNameTextView; | TextView lineNameTextView; | ||||
TextView lineDirectionTextView; | TextView lineDirectionTextView; | ||||
TextView stopNameView; | TextView stopNameView; | ||||
TextView arrivalsDescriptionTextView; | TextView arrivalsDescriptionTextView; | ||||
TextView arrivalsTextView; | TextView arrivalsTextView; | ||||
TextView distancetextView; | TextView distancetextView; | ||||
String stopID; | String stopID; | ||||
▲ Show 20 Lines • Show All 145 Lines • Show Last 20 Lines |
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