Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/fragments/FragmentHelper.java
Show All 17 Lines | |||||
package it.reyboz.bustorino.fragments; | package it.reyboz.bustorino.fragments; | ||||
import android.content.Context; | import android.content.Context; | ||||
import androidx.fragment.app.Fragment; | import androidx.fragment.app.Fragment; | ||||
import androidx.fragment.app.FragmentManager; | import androidx.fragment.app.FragmentManager; | ||||
import androidx.fragment.app.FragmentTransaction; | import androidx.fragment.app.FragmentTransaction; | ||||
import android.os.AsyncTask; | |||||
import android.util.Log; | import android.util.Log; | ||||
import android.widget.Toast; | import android.widget.Toast; | ||||
import it.reyboz.bustorino.R; | import it.reyboz.bustorino.R; | ||||
import it.reyboz.bustorino.backend.Fetcher; | import it.reyboz.bustorino.backend.Fetcher; | ||||
import it.reyboz.bustorino.backend.Palina; | import it.reyboz.bustorino.backend.Palina; | ||||
import it.reyboz.bustorino.backend.Stop; | import it.reyboz.bustorino.backend.Stop; | ||||
import it.reyboz.bustorino.backend.utils; | import it.reyboz.bustorino.backend.utils; | ||||
Show All 11 Lines | public class FragmentHelper { | ||||
private final WeakReference<FragmentManager> managerWeakRef; | private final WeakReference<FragmentManager> managerWeakRef; | ||||
private Stop lastSuccessfullySearchedBusStop; | private Stop lastSuccessfullySearchedBusStop; | ||||
//support for multiple frames | //support for multiple frames | ||||
private final int secondaryFrameLayout; | private final int secondaryFrameLayout; | ||||
private final int primaryFrameLayout; | private final int primaryFrameLayout; | ||||
private final Context context; | private final Context context; | ||||
public static final int NO_FRAME = -3; | public static final int NO_FRAME = -3; | ||||
private static final String DEBUG_TAG = "BusTO FragmHelper"; | private static final String DEBUG_TAG = "BusTO FragmHelper"; | ||||
private WeakReference<AsyncDataDownload> lastTaskRef; | private WeakReference<AsyncTask> lastTaskRef; | ||||
private SearchRequestType lastTaskType; | |||||
private boolean shouldHaltAllActivities=false; | private boolean shouldHaltAllActivities=false; | ||||
public FragmentHelper(FragmentListenerMain listener, FragmentManager framan, Context context, int mainFrame) { | public FragmentHelper(FragmentListenerMain listener, FragmentManager framan, Context context, int mainFrame) { | ||||
this(listener,framan, context,mainFrame,NO_FRAME); | this(listener,framan, context,mainFrame,NO_FRAME); | ||||
} | } | ||||
public FragmentHelper(FragmentListenerMain listener, FragmentManager fraMan, Context context, int primaryFrameLayout, int secondaryFrameLayout) { | public FragmentHelper(FragmentListenerMain listener, FragmentManager fraMan, Context context, int primaryFrameLayout, int secondaryFrameLayout) { | ||||
Show All 12 Lines | public class FragmentHelper { | ||||
public Stop getLastSuccessfullySearchedBusStop() { | public Stop getLastSuccessfullySearchedBusStop() { | ||||
return lastSuccessfullySearchedBusStop; | return lastSuccessfullySearchedBusStop; | ||||
} | } | ||||
public void setLastSuccessfullySearchedBusStop(Stop stop) { | public void setLastSuccessfullySearchedBusStop(Stop stop) { | ||||
this.lastSuccessfullySearchedBusStop = stop; | this.lastSuccessfullySearchedBusStop = stop; | ||||
} | } | ||||
public void setLastTaskRef(WeakReference<AsyncDataDownload> lastTaskRef) { | public void setLastTaskRef(AsyncTask task) { | ||||
this.lastTaskRef = lastTaskRef; | this.lastTaskRef = new WeakReference<>(task); | ||||
} | } | ||||
/** | /** | ||||
* Called when you need to create a fragment for a specified Palina | * Called when you need to create a fragment for a specified Palina | ||||
* @param p the Stop that needs to be displayed | * @param p the Stop that needs to be displayed | ||||
*/ | */ | ||||
public void createOrUpdateStopFragment(Palina p, boolean addToBackStack){ | public void createOrUpdateStopFragment(Palina p, boolean addToBackStack){ | ||||
boolean sameFragment; | boolean sameFragment; | ||||
▲ Show 20 Lines • Show All 102 Lines • ▼ Show 20 Lines | public class FragmentHelper { | ||||
} | } | ||||
public synchronized void setBlockAllActivities(boolean shouldI) { | public synchronized void setBlockAllActivities(boolean shouldI) { | ||||
this.shouldHaltAllActivities = shouldI; | this.shouldHaltAllActivities = shouldI; | ||||
} | } | ||||
public void stopLastRequestIfNeeded(boolean interruptIfRunning){ | public void stopLastRequestIfNeeded(boolean interruptIfRunning){ | ||||
if(lastTaskRef == null) return; | if(lastTaskRef == null) return; | ||||
AsyncDataDownload task = lastTaskRef.get(); | AsyncTask task = lastTaskRef.get(); | ||||
if(task!=null){ | if(task!=null){ | ||||
task.cancel(interruptIfRunning); | task.cancel(interruptIfRunning); | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Wrapper to show the errors/status that happened | * Wrapper to show the errors/status that happened | ||||
* @param res result from Fetcher | * @param res result from Fetcher | ||||
*/ | */ | ||||
public void showErrorMessage(Fetcher.Result res){ | public void showErrorMessage(Fetcher.Result res, SearchRequestType type){ | ||||
//TODO: implement a common set of errors for all fragments | //TODO: implement a common set of errors for all fragments | ||||
Log.d(DEBUG_TAG, "Showing result for "+res); | |||||
switch (res){ | switch (res){ | ||||
case OK: | case OK: | ||||
break; | break; | ||||
case CLIENT_OFFLINE: | case CLIENT_OFFLINE: | ||||
showToastMessage(R.string.network_error, true); | showToastMessage(R.string.network_error, true); | ||||
break; | break; | ||||
case SERVER_ERROR: | case SERVER_ERROR: | ||||
if (utils.isConnected(context)) { | if (utils.isConnected(context)) { | ||||
showToastMessage(R.string.parsing_error, true); | showToastMessage(R.string.parsing_error, true); | ||||
} else { | } else { | ||||
showToastMessage(R.string.network_error, true); | showToastMessage(R.string.network_error, true); | ||||
} | } | ||||
case PARSER_ERROR: | case PARSER_ERROR: | ||||
default: | default: | ||||
showShortToast(R.string.internal_error); | showShortToast(R.string.internal_error); | ||||
break; | break; | ||||
case QUERY_TOO_SHORT: | case QUERY_TOO_SHORT: | ||||
showShortToast(R.string.query_too_short); | showShortToast(R.string.query_too_short); | ||||
break; | break; | ||||
case EMPTY_RESULT_SET: | case EMPTY_RESULT_SET: | ||||
if (type == SearchRequestType.STOPS) | |||||
showShortToast(R.string.no_bus_stop_have_this_name); | |||||
else if(type == SearchRequestType.ARRIVALS){ | |||||
showShortToast(R.string.no_arrivals_stop); | |||||
} | |||||
break; | |||||
case NOT_FOUND: | |||||
showShortToast(R.string.no_bus_stop_have_this_name); | showShortToast(R.string.no_bus_stop_have_this_name); | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
public void showToastMessage(int messageID, boolean short_lenght) { | public void showToastMessage(int messageID, boolean short_lenght) { | ||||
final int length = short_lenght ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG; | final int length = short_lenght ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG; | ||||
if (context != null) | if (context != null) | ||||
Show All 30 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