Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/fragments/FragmentHelper.java
Show All 12 Lines | BusTO (fragments) | ||||
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.fragments; | package it.reyboz.bustorino.fragments; | ||||
import android.content.ContentResolver; | import android.content.Context; | ||||
import android.content.ContentValues; | |||||
import android.database.sqlite.SQLiteException; | |||||
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 androidx.swiperefreshlayout.widget.SwipeRefreshLayout; | |||||
import android.util.Log; | import android.util.Log; | ||||
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.data.NextGenDB; | import it.reyboz.bustorino.data.NextGenDB; | ||||
import it.reyboz.bustorino.middleware.*; | import it.reyboz.bustorino.middleware.*; | ||||
import java.lang.ref.WeakReference; | import java.lang.ref.WeakReference; | ||||
import java.util.List; | import java.util.List; | ||||
/** | /** | ||||
* Helper class to manage the fragments and their needs | * Helper class to manage the fragments and their needs | ||||
*/ | */ | ||||
public class FragmentHelper { | public class FragmentHelper { | ||||
GeneralActivity act; | //GeneralActivity act; | ||||
private final FragmentListenerMain listenerMain; | |||||
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 swipeRefID; | |||||
private final int primaryFrameLayout; | private final int primaryFrameLayout; | ||||
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 WeakReference<AsyncDataDownload> lastTaskRef; | private WeakReference<AsyncDataDownload> lastTaskRef; | ||||
private NextGenDB newDBHelper; | |||||
private boolean shouldHaltAllActivities=false; | private boolean shouldHaltAllActivities=false; | ||||
public FragmentHelper(GeneralActivity act, int swipeRefID, int mainFrame) { | public FragmentHelper(FragmentListenerMain listener, FragmentManager framan, Context context, int mainFrame) { | ||||
this(act,swipeRefID,mainFrame,NO_FRAME); | this(listener,framan, context,mainFrame,NO_FRAME); | ||||
} | } | ||||
public FragmentHelper(GeneralActivity act, int swipeRefID, int primaryFrameLayout, int secondaryFrameLayout) { | public FragmentHelper(FragmentListenerMain listener, FragmentManager fraMan, Context context, int primaryFrameLayout, int secondaryFrameLayout) { | ||||
this.act = act; | this.listenerMain = listener; | ||||
this.swipeRefID = swipeRefID; | this.managerWeakRef = new WeakReference<>(fraMan); | ||||
this.primaryFrameLayout = primaryFrameLayout; | this.primaryFrameLayout = primaryFrameLayout; | ||||
this.secondaryFrameLayout = secondaryFrameLayout; | this.secondaryFrameLayout = secondaryFrameLayout; | ||||
newDBHelper = new NextGenDB(act.getApplicationContext()); | this.context = context.getApplicationContext(); | ||||
} | } | ||||
/** | /** | ||||
* Get the last successfully searched bus stop or NULL | * Get the last successfully searched bus stop or NULL | ||||
* | * | ||||
* @return the stop | * @return the stop | ||||
*/ | */ | ||||
public Stop getLastSuccessfullySearchedBusStop() { | public Stop getLastSuccessfullySearchedBusStop() { | ||||
Show All 11 Lines | public class FragmentHelper { | ||||
/** | /** | ||||
* 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){ | public void createOrUpdateStopFragment(Palina p){ | ||||
boolean sameFragment; | boolean sameFragment; | ||||
ArrivalsFragment arrivalsFragment; | ArrivalsFragment arrivalsFragment; | ||||
if(act==null || shouldHaltAllActivities) { | if(managerWeakRef.get()==null || shouldHaltAllActivities) { | ||||
//SOMETHING WENT VERY WRONG | //SOMETHING WENT VERY WRONG | ||||
Log.e(DEBUG_TAG, "We are asked for a new stop but we can't show anything"); | |||||
return; | return; | ||||
} | } | ||||
FragmentManager fm = act.getSupportFragmentManager(); | FragmentManager fm = managerWeakRef.get(); | ||||
if(fm.findFragmentById(primaryFrameLayout) instanceof ArrivalsFragment) { | if(fm.findFragmentById(primaryFrameLayout) instanceof ArrivalsFragment) { | ||||
arrivalsFragment = (ArrivalsFragment) fm.findFragmentById(primaryFrameLayout); | arrivalsFragment = (ArrivalsFragment) fm.findFragmentById(primaryFrameLayout); | ||||
//Log.d(DEBUG_TAG, "Arrivals are for fragment with same stop?"); | |||||
sameFragment = arrivalsFragment.isFragmentForTheSameStop(p); | sameFragment = arrivalsFragment.isFragmentForTheSameStop(p); | ||||
} else | } else { | ||||
sameFragment = false; | sameFragment = false; | ||||
Log.d(DEBUG_TAG, "We aren't showing an ArrivalsFragment"); | |||||
} | |||||
setLastSuccessfullySearchedBusStop(p); | setLastSuccessfullySearchedBusStop(p); | ||||
if(!sameFragment) { | if(!sameFragment) { | ||||
//set the String to be displayed on the fragment | //set the String to be displayed on the fragment | ||||
String displayName = p.getStopDisplayName(); | String displayName = p.getStopDisplayName(); | ||||
String displayStuff; | String displayStuff; | ||||
if (displayName != null && displayName.length() > 0) { | if (displayName != null && displayName.length() > 0) { | ||||
arrivalsFragment = ArrivalsFragment.newInstance(p.ID,displayName); | arrivalsFragment = ArrivalsFragment.newInstance(p.ID,displayName); | ||||
} else { | } else { | ||||
arrivalsFragment = ArrivalsFragment.newInstance(p.ID); | arrivalsFragment = ArrivalsFragment.newInstance(p.ID); | ||||
} | } | ||||
attachFragmentToContainer(fm,arrivalsFragment,true,ResultListFragment.getFragmentTag(p)); | attachFragmentToContainer(fm,arrivalsFragment,true,ResultListFragment.getFragmentTag(p)); | ||||
} else { | } else { | ||||
Log.d("BusTO", "Same bus stop, accessing existing fragment"); | Log.d("BusTO", "Same bus stop, accessing existing fragment"); | ||||
arrivalsFragment = (ArrivalsFragment) fm.findFragmentById(primaryFrameLayout); | arrivalsFragment = (ArrivalsFragment) fm.findFragmentById(primaryFrameLayout); | ||||
} | } | ||||
// DO NOT CALL `setListAdapter` ever on arrivals fragment | // DO NOT CALL `setListAdapter` ever on arrivals fragment | ||||
arrivalsFragment.updateFragmentData(p); | arrivalsFragment.updateFragmentData(p); | ||||
act.hideKeyboard(); | listenerMain.hideKeyboard(); | ||||
toggleSpinner(false); | toggleSpinner(false); | ||||
} | } | ||||
/** | /** | ||||
* Called when you need to display the results of a search of stops | * Called when you need to display the results of a search of stops | ||||
* @param resultList the List of stops found | * @param resultList the List of stops found | ||||
* @param query String queried | * @param query String queried | ||||
*/ | */ | ||||
public void createFragmentFor(List<Stop> resultList,String query){ | public void createFragmentFor(List<Stop> resultList,String query){ | ||||
act.hideKeyboard(); | listenerMain.hideKeyboard(); | ||||
StopListFragment listfragment = StopListFragment.newInstance(query); | StopListFragment listfragment = StopListFragment.newInstance(query); | ||||
attachFragmentToContainer(act.getSupportFragmentManager(),listfragment,false,"search_"+query); | if(managerWeakRef.get()==null || shouldHaltAllActivities) { | ||||
//SOMETHING WENT VERY WRONG | |||||
valerio.bozzolan: Something very very asd
asd | |||||
Log.e(DEBUG_TAG, "We are asked for a new stop but we can't show anything"); | |||||
return; | |||||
} | |||||
attachFragmentToContainer(managerWeakRef.get(),listfragment,false,"search_"+query); | |||||
listfragment.setStopList(resultList); | listfragment.setStopList(resultList); | ||||
toggleSpinner(false); | toggleSpinner(false); | ||||
} | } | ||||
/** | /** | ||||
* Wrapper for toggleSpinner in Activity | * Wrapper for toggleSpinner in Activity | ||||
* @param on new status of spinner system | * @param on new status of spinner system | ||||
*/ | */ | ||||
public void toggleSpinner(boolean on){ | public void toggleSpinner(boolean on){ | ||||
if (act instanceof FragmentListenerMain) | listenerMain.toggleSpinner(on); | ||||
((FragmentListenerMain) act).toggleSpinner(on); | |||||
else { | |||||
SwipeRefreshLayout srl = (SwipeRefreshLayout) act.findViewById(swipeRefID); | |||||
srl.setRefreshing(false); | |||||
} | |||||
} | } | ||||
/** | /** | ||||
* Attach a new fragment to a cointainer | * Attach a new fragment to a cointainer | ||||
* @param fm the FragmentManager | * @param fm the FragmentManager | ||||
* @param fragment the Fragment | * @param fragment the Fragment | ||||
* @param sendToSecondaryFrame needs to be displayed in secondary frame or not | * @param sendToSecondaryFrame needs to be displayed in secondary frame or not | ||||
* @param tag tag for the fragment | * @param tag tag for the fragment | ||||
*/ | */ | ||||
public void attachFragmentToContainer(FragmentManager fm,Fragment fragment, boolean sendToSecondaryFrame, String tag){ | public void attachFragmentToContainer(FragmentManager fm,Fragment fragment, boolean sendToSecondaryFrame, String tag){ | ||||
FragmentTransaction ft = fm.beginTransaction(); | FragmentTransaction ft = fm.beginTransaction(); | ||||
if(sendToSecondaryFrame && secondaryFrameLayout!=NO_FRAME) | if(sendToSecondaryFrame && secondaryFrameLayout!=NO_FRAME) | ||||
ft.replace(secondaryFrameLayout,fragment,tag); | ft.replace(secondaryFrameLayout,fragment,tag); | ||||
else ft.replace(primaryFrameLayout,fragment,tag); | else ft.replace(primaryFrameLayout,fragment,tag); | ||||
ft.addToBackStack("state_"+tag); | ft.addToBackStack("state_"+tag); | ||||
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE); | ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE); | ||||
ft.commit(); | ft.commit(); | ||||
//fm.executePendingTransactions(); | //fm.executePendingTransactions(); | ||||
} | } | ||||
synchronized public int insertBatchDataInNextGenDB(ContentValues[] valuesArr,String tableName){ | |||||
if(newDBHelper !=null) | |||||
try { | |||||
return newDBHelper.insertBatchContent(valuesArr, tableName); | |||||
} catch (SQLiteException exc){ | |||||
Log.w("DB Batch inserting: ","ERROR Inserting the data batch: ",exc.fillInStackTrace()); | |||||
return -2; | |||||
} | |||||
else return -1; | |||||
} | |||||
synchronized public ContentResolver getContentResolver(){ | |||||
return act.getContentResolver(); | |||||
} | |||||
public void setBlockAllActivities(boolean shouldI) { | public void setBlockAllActivities(boolean shouldI) { | ||||
this.shouldHaltAllActivities = shouldI; | this.shouldHaltAllActivities = shouldI; | ||||
} | } | ||||
public void stopLastRequestIfNeeded(){ | public void stopLastRequestIfNeeded(){ | ||||
if(lastTaskRef == null) return; | if(lastTaskRef == null) return; | ||||
AsyncDataDownload task = lastTaskRef.get(); | AsyncDataDownload task = lastTaskRef.get(); | ||||
if(task!=null){ | if(task!=null){ | ||||
task.cancel(true); | task.cancel(true); | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* 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){ | ||||
//TODO: implement a common set of errors for all fragments | //TODO: implement a common set of errors for all fragments | ||||
switch (res){ | switch (res){ | ||||
case OK: | case OK: | ||||
break; | break; | ||||
case CLIENT_OFFLINE: | case CLIENT_OFFLINE: | ||||
act.showToastMessage(R.string.network_error, true); | showToastMessage(R.string.network_error, true); | ||||
break; | break; | ||||
case SERVER_ERROR: | case SERVER_ERROR: | ||||
if (act.isConnected()) { | if (utils.isConnected(context)) { | ||||
act.showToastMessage(R.string.parsing_error, true); | showToastMessage(R.string.parsing_error, true); | ||||
} else { | } else { | ||||
act.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: | ||||
showShortToast(R.string.no_bus_stop_have_this_name); | showShortToast(R.string.no_bus_stop_have_this_name); | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
public void showShortToast(int message){ | public void showToastMessage(int messageID, boolean short_lenght) { | ||||
if (act!=null) | final int length = short_lenght ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG; | ||||
act.showToastMessage(message,true); | if (context != null) | ||||
Toast.makeText(context, messageID, length).show(); | |||||
} | |||||
private void showShortToast(int messageID){ | |||||
showToastMessage(messageID, true); | |||||
} | } | ||||
} | } |
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
Something very very asd