Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/fragments/ArrivalsFragment.java
Show First 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | |||||
import it.reyboz.bustorino.backend.Fetcher; | import it.reyboz.bustorino.backend.Fetcher; | ||||
import it.reyboz.bustorino.backend.FiveTAPIFetcher; | import it.reyboz.bustorino.backend.FiveTAPIFetcher; | ||||
import it.reyboz.bustorino.backend.FiveTNormalizer; | import it.reyboz.bustorino.backend.FiveTNormalizer; | ||||
import it.reyboz.bustorino.backend.FiveTScraperFetcher; | import it.reyboz.bustorino.backend.FiveTScraperFetcher; | ||||
import it.reyboz.bustorino.backend.GTTJSONFetcher; | import it.reyboz.bustorino.backend.GTTJSONFetcher; | ||||
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.middleware.AppDataProvider; | import it.reyboz.bustorino.backend.Stop; | ||||
import it.reyboz.bustorino.middleware.NextGenDB; | import it.reyboz.bustorino.data.AppDataProvider; | ||||
import it.reyboz.bustorino.middleware.UserDB; | import it.reyboz.bustorino.data.NextGenDB; | ||||
import it.reyboz.bustorino.data.UserDB; | |||||
import it.reyboz.bustorino.middleware.AsyncStopFavoriteAction; | |||||
public class ArrivalsFragment extends ResultListFragment implements LoaderManager.LoaderCallbacks<Cursor> { | public class ArrivalsFragment extends ResultListFragment implements LoaderManager.LoaderCallbacks<Cursor> { | ||||
private final static String KEY_STOP_ID = "stopid"; | private final static String KEY_STOP_ID = "stopid"; | ||||
private final static String KEY_STOP_NAME = "stopname"; | private final static String KEY_STOP_NAME = "stopname"; | ||||
private final static String DEBUG_TAG = "BUSTOArrivalsFragment"; | private final static String DEBUG_TAG = "BUSTOArrivalsFragment"; | ||||
private final static int loaderFavId = 2; | private final static int loaderFavId = 2; | ||||
private final static int loaderStopId = 1; | private final static int loaderStopId = 1; | ||||
private final static ArrivalsFetcher[] defaultFetchers = new ArrivalsFetcher[]{new FiveTAPIFetcher(), new GTTJSONFetcher(), new FiveTScraperFetcher()}; | private final static ArrivalsFetcher[] defaultFetchers = new ArrivalsFetcher[]{new FiveTAPIFetcher(), new GTTJSONFetcher(), new FiveTScraperFetcher()}; | ||||
static final String STOP_TITLE = "messageExtra"; | static final String STOP_TITLE = "messageExtra"; | ||||
private @Nullable String stopID,stopName; | private @Nullable String stopID,stopName; | ||||
private DBStatusManager prefs; | private DBStatusManager prefs; | ||||
private DBStatusManager.OnDBUpdateStatusChangeListener listener; | private DBStatusManager.OnDBUpdateStatusChangeListener listener; | ||||
private boolean justCreated = false; | private boolean justCreated = false; | ||||
private Palina lastUpdatedPalina = null; | private Palina lastUpdatedPalina = null; | ||||
private boolean needUpdateOnAttach = false; | private boolean needUpdateOnAttach = false; | ||||
private boolean requestedNewArrivalTimes = false; | private boolean fetchersChangeRequestPending = false; | ||||
private boolean stopIsInFavorites = false; | |||||
//Views | //Views | ||||
protected ImageButton addToFavorites; | protected ImageButton addToFavorites; | ||||
protected TextView timesSourceTextView; | protected TextView timesSourceTextView; | ||||
private List<ArrivalsFetcher> fetchers = new ArrayList<>(Arrays.asList(defaultFetchers)); | private List<ArrivalsFetcher> fetchers = new ArrayList<>(Arrays.asList(defaultFetchers)); | ||||
▲ Show 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | public class ArrivalsFragment extends ResultListFragment implements LoaderManager.LoaderCallbacks<Cursor> { | ||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, | public View onCreateView(LayoutInflater inflater, ViewGroup container, | ||||
Bundle savedInstanceState) { | Bundle savedInstanceState) { | ||||
View root = inflater.inflate(R.layout.fragment_arrivals, container, false); | View root = inflater.inflate(R.layout.fragment_arrivals, container, false); | ||||
messageTextView = (TextView) root.findViewById(R.id.messageTextView); | messageTextView = (TextView) root.findViewById(R.id.messageTextView); | ||||
addToFavorites = (ImageButton) root.findViewById(R.id.addToFavorites); | addToFavorites = (ImageButton) root.findViewById(R.id.addToFavorites); | ||||
resultsListView = (ListView) root.findViewById(R.id.resultsListView); | resultsListView = (ListView) root.findViewById(R.id.resultsListView); | ||||
timesSourceTextView = (TextView) root.findViewById(R.id.timesSourceTextView); | timesSourceTextView = (TextView) root.findViewById(R.id.timesSourceTextView); | ||||
timesSourceTextView.setOnLongClickListener(view -> { | timesSourceTextView.setOnLongClickListener(view -> { | ||||
if(!requestedNewArrivalTimes){ | if(!fetchersChangeRequestPending){ | ||||
rotateFetchers(); | rotateFetchers(); | ||||
//Show we are changing provider | |||||
timesSourceTextView.setText(R.string.arrival_source_changing); | timesSourceTextView.setText(R.string.arrival_source_changing); | ||||
mListener.createFragmentForStop(stopID); | |||||
requestedNewArrivalTimes = true; | mListener.requestArrivalsForStopID(stopID); | ||||
fetchersChangeRequestPending = true; | |||||
return true; | return true; | ||||
} | } | ||||
return false; | return false; | ||||
}); | }); | ||||
timesSourceTextView.setOnClickListener(view -> { | timesSourceTextView.setOnClickListener(view -> { | ||||
Toast.makeText(getContext(), R.string.change_arrivals_source_message, Toast.LENGTH_SHORT) | Toast.makeText(getContext(), R.string.change_arrivals_source_message, Toast.LENGTH_SHORT) | ||||
.show(); | .show(); | ||||
}); | }); | ||||
//Button | //Button | ||||
addToFavorites.setClickable(true); | addToFavorites.setClickable(true); | ||||
addToFavorites.setOnClickListener(v -> { | addToFavorites.setOnClickListener(v -> { | ||||
// add/remove the stop in the favorites | // add/remove the stop in the favorites | ||||
mListener.toggleLastStopToFavorites(); | toggleLastStopToFavorites(); | ||||
}); | }); | ||||
resultsListView.setOnItemClickListener((parent, view, position, id) -> { | resultsListView.setOnItemClickListener((parent, view, position, id) -> { | ||||
String routeName; | String routeName; | ||||
Route r = (Route) parent.getItemAtPosition(position); | Route r = (Route) parent.getItemAtPosition(position); | ||||
routeName = FiveTNormalizer.routeInternalToDisplay(r.getNameForDisplay()); | routeName = FiveTNormalizer.routeInternalToDisplay(r.getNameForDisplay()); | ||||
if (routeName == null) { | if (routeName == null) { | ||||
Show All 24 Lines | public class ArrivalsFragment extends ResultListFragment implements LoaderManager.LoaderCallbacks<Cursor> { | ||||
@Override | @Override | ||||
public void onResume() { | public void onResume() { | ||||
super.onResume(); | super.onResume(); | ||||
LoaderManager loaderManager = getLoaderManager(); | LoaderManager loaderManager = getLoaderManager(); | ||||
if(stopID!=null){ | if(stopID!=null){ | ||||
//refresh the arrivals | //refresh the arrivals | ||||
if(!justCreated) | if(!justCreated) | ||||
mListener.createFragmentForStop(stopID); | mListener.requestArrivalsForStopID(stopID); | ||||
else justCreated = false; | else justCreated = false; | ||||
//start the loader | //start the loader | ||||
if(prefs.isDBUpdating(true)){ | if(prefs.isDBUpdating(true)){ | ||||
prefs.registerListener(); | prefs.registerListener(); | ||||
} else { | } else { | ||||
loaderManager.restartLoader(loaderFavId, getArguments(), this); | loaderManager.restartLoader(loaderFavId, getArguments(), this); | ||||
} | } | ||||
updateMessage(); | updateMessage(); | ||||
▲ Show 20 Lines • Show All 76 Lines • ▼ Show 20 Lines | protected void showArrivalsSources(Palina p){ | ||||
break; | break; | ||||
case UNDETERMINED: | case UNDETERMINED: | ||||
//Don't show the view | //Don't show the view | ||||
timesSourceTextView.setVisibility(View.GONE); | timesSourceTextView.setVisibility(View.GONE); | ||||
return; | return; | ||||
default: | default: | ||||
throw new IllegalStateException("Unexpected value: " + source); | throw new IllegalStateException("Unexpected value: " + source); | ||||
} | } | ||||
int count = 0; | |||||
while (source != fetchers.get(0).getSourceForFetcher() && count < 100){ | |||||
//we need to update the fetcher that is requested | |||||
rotateFetchers(); | |||||
count++; | |||||
} | |||||
if (count>10) | |||||
Log.w(DEBUG_TAG, "Tried to update the source fetcher but it didn't work"); | |||||
final String base_message = getString(R.string.times_source_fmt, source_txt); | final String base_message = getString(R.string.times_source_fmt, source_txt); | ||||
timesSourceTextView.setVisibility(View.VISIBLE); | timesSourceTextView.setVisibility(View.VISIBLE); | ||||
timesSourceTextView.setText(base_message); | timesSourceTextView.setText(base_message); | ||||
requestedNewArrivalTimes = false; | fetchersChangeRequestPending = false; | ||||
} | } | ||||
@Override | @Override | ||||
public void setNewListAdapter(ListAdapter adapter) { | public void setNewListAdapter(ListAdapter adapter) { | ||||
throw new UnsupportedOperationException(); | throw new UnsupportedOperationException(); | ||||
} | } | ||||
/** | /** | ||||
Show All 10 Lines | private void updateMessage(){ | ||||
} else { | } else { | ||||
Log.e("ArrivalsFragm"+getTag(),"NO ID FOR THIS FRAGMENT - something went horribly wrong"); | Log.e("ArrivalsFragm"+getTag(),"NO ID FOR THIS FRAGMENT - something went horribly wrong"); | ||||
} | } | ||||
if(message!=null) { | if(message!=null) { | ||||
setTextViewMessage(getString(R.string.passages,message)); | setTextViewMessage(getString(R.string.passages,message)); | ||||
} | } | ||||
// whatever is the case, update the star icon | // whatever is the case, update the star icon | ||||
mListener.updateStarIconFromLastBusStop(); | //updateStarIconFromLastBusStop(); | ||||
} | } | ||||
@NonNull | |||||
@Override | @Override | ||||
public Loader<Cursor> onCreateLoader(int id, Bundle args) { | public Loader<Cursor> onCreateLoader(int id, Bundle args) { | ||||
if(args.getString(KEY_STOP_ID)==null) return null; | if(args.getString(KEY_STOP_ID)==null) return null; | ||||
final String stopID = args.getString(KEY_STOP_ID); | final String stopID = args.getString(KEY_STOP_ID); | ||||
final Uri.Builder builder = AppDataProvider.getUriBuilderToComplete(); | final Uri.Builder builder = AppDataProvider.getUriBuilderToComplete(); | ||||
CursorLoader cl; | CursorLoader cl; | ||||
switch (id){ | switch (id){ | ||||
case loaderFavId: | case loaderFavId: | ||||
Show All 15 Lines | public class ArrivalsFragment extends ResultListFragment implements LoaderManager.LoaderCallbacks<Cursor> { | ||||
@Override | @Override | ||||
public void onLoadFinished(Loader<Cursor> loader, Cursor data) { | public void onLoadFinished(Loader<Cursor> loader, Cursor data) { | ||||
switch (loader.getId()){ | switch (loader.getId()){ | ||||
case loaderFavId: | case loaderFavId: | ||||
final int colUserName = data.getColumnIndex(UserDB.getFavoritesColumnNamesAsArray[1]); | final int colUserName = data.getColumnIndex(UserDB.getFavoritesColumnNamesAsArray[1]); | ||||
if(data.getCount()>0){ | if(data.getCount()>0){ | ||||
// IT'S IN FAVORITES | |||||
data.moveToFirst(); | data.moveToFirst(); | ||||
final String probableName = data.getString(colUserName); | final String probableName = data.getString(colUserName); | ||||
stopIsInFavorites = true; | |||||
if(probableName!=null && !probableName.isEmpty()){ | if(probableName!=null && !probableName.isEmpty()){ | ||||
stopName = probableName; | stopName = probableName; | ||||
//update the message in the textview | |||||
updateMessage(); | updateMessage(); | ||||
} | } | ||||
} else { | |||||
stopIsInFavorites =false; | |||||
} | } | ||||
updateStarIcon(); | |||||
if(stopName == null){ | if(stopName == null){ | ||||
//stop is not inside the favorites and wasn't provided | //stop is not inside the favorites and wasn't provided | ||||
Log.d("ArrivalsFragment"+getTag(),"Stop wasn't in the favorites and has no name, looking in the DB"); | Log.d("ArrivalsFragment"+getTag(),"Stop wasn't in the favorites and has no name, looking in the DB"); | ||||
getLoaderManager().restartLoader(loaderStopId,getArguments(),this); | getLoaderManager().restartLoader(loaderStopId,getArguments(),this); | ||||
} | } | ||||
break; | break; | ||||
case loaderStopId: | case loaderStopId: | ||||
if(data.getCount()>0){ | if(data.getCount()>0){ | ||||
Show All 15 Lines | public void onPause() { | ||||
prefs.unregisterListener(); | prefs.unregisterListener(); | ||||
super.onPause(); | super.onPause(); | ||||
} | } | ||||
@Override | @Override | ||||
public void onLoaderReset(Loader<Cursor> loader) { | public void onLoaderReset(Loader<Cursor> loader) { | ||||
//NOTHING TO DO | //NOTHING TO DO | ||||
} | } | ||||
public void toggleLastStopToFavorites() { | |||||
Stop stop = lastUpdatedPalina; | |||||
if (stop != null) { | |||||
// toggle the status in background | |||||
new AsyncStopFavoriteAction(getContext().getApplicationContext(), AsyncStopFavoriteAction.Action.TOGGLE, | |||||
v->updateStarIconFromLastBusStop(v)).execute(stop); | |||||
} else { | |||||
// this case have no sense, but just immediately update the favorite icon | |||||
updateStarIconFromLastBusStop(true); | |||||
} | |||||
} | |||||
/** | |||||
* Update the star "Add to favorite" icon | |||||
*/ | |||||
public void updateStarIconFromLastBusStop(Boolean toggleDone) { | |||||
if (stopIsInFavorites) | |||||
stopIsInFavorites = !toggleDone; | |||||
else stopIsInFavorites = toggleDone; | |||||
updateStarIcon(); | |||||
// check if there is a last Stop | |||||
/* | |||||
if (stopID == null) { | |||||
addToFavorites.setVisibility(View.INVISIBLE); | |||||
} else { | |||||
// filled or outline? | |||||
if (isStopInFavorites(stopID)) { | |||||
addToFavorites.setImageResource(R.drawable.ic_star_filled); | |||||
} else { | |||||
addToFavorites.setImageResource(R.drawable.ic_star_outline); | |||||
} | |||||
addToFavorites.setVisibility(View.VISIBLE); | |||||
} | |||||
*/ | |||||
} | |||||
/** | |||||
* Update the star icon according to `stopIsInFavorites` | |||||
*/ | |||||
public void updateStarIcon() { | |||||
// no favorites no party! | |||||
// check if there is a last Stop | |||||
if (stopID == null) { | |||||
addToFavorites.setVisibility(View.INVISIBLE); | |||||
} else { | |||||
// filled or outline? | |||||
if (stopIsInFavorites) { | |||||
addToFavorites.setImageResource(R.drawable.ic_star_filled); | |||||
} else { | |||||
addToFavorites.setImageResource(R.drawable.ic_star_outline); | |||||
} | |||||
addToFavorites.setVisibility(View.VISIBLE); | |||||
} | |||||
} | |||||
} | } |
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