Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/fragments/FragmentHelper.java
/* | /* | ||||
BusTO (fragments) | BusTO (fragments) | ||||
Copyright (C) 2018 Fabio Mazza | Copyright (C) 2018 Fabio Mazza | ||||
This program is free software: you can redistribute it and/or modify | This program is free software: you can redistribute it and/or modify | ||||
it under the terms of the GNU General Public License as published by | it under the terms of the GNU General Public License as published by | ||||
the Free Software Foundation, either version 3 of the License, or | the Free Software Foundation, either version 3 of the License, or | ||||
(at your option) any later version. | (at your option) any later version. | ||||
This program is distributed in the hope that it will be useful, | This program is distributed in the hope that it will be useful, | ||||
but WITHOUT ANY WARRANTY; without even the implied warranty of | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
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.ContentResolver; | ||||
import android.content.ContentValues; | import android.content.ContentValues; | ||||
import android.database.sqlite.SQLiteException; | import android.database.sqlite.SQLiteException; | ||||
import android.support.v4.app.Fragment; | import android.support.v4.app.Fragment; | ||||
import android.support.v4.app.FragmentManager; | import android.support.v4.app.FragmentManager; | ||||
import android.support.v4.app.FragmentTransaction; | import android.support.v4.app.FragmentTransaction; | ||||
import android.support.v4.widget.SwipeRefreshLayout; | import android.support.v4.widget.SwipeRefreshLayout; | ||||
import android.util.Log; | import android.util.Log; | ||||
import it.reyboz.bustorino.R; | import it.reyboz.bustorino.R; | ||||
import it.reyboz.bustorino.adapters.PalinaAdapter; | import it.reyboz.bustorino.adapters.PalinaAdapter; | ||||
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.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 Stop lastSuccessfullySearchedBusStop; | private Stop lastSuccessfullySearchedBusStop; | ||||
//support for multiple frames | //support for multiple frames | ||||
private int primaryFrameLayout,secondaryFrameLayout, swipeRefID; | private int primaryFrameLayout,secondaryFrameLayout, swipeRefID; | ||||
public static final int NO_FRAME = -3; | public static final int NO_FRAME = -3; | ||||
private WeakReference<AsyncDataDownload> lastTaskRef; | private WeakReference<AsyncDataDownload> lastTaskRef; | ||||
private NextGenDB newDBHelper; | private NextGenDB newDBHelper; | ||||
private boolean shouldHaltAllActivities=false; | private boolean shouldHaltAllActivities=false; | ||||
public FragmentHelper(GeneralActivity act, int swipeRefID, int mainFrame) { | public FragmentHelper(GeneralActivity act, int swipeRefID, int mainFrame) { | ||||
this(act,swipeRefID,mainFrame,NO_FRAME); | this(act,swipeRefID,mainFrame,NO_FRAME); | ||||
} | } | ||||
public FragmentHelper(GeneralActivity act, int swipeRefID, int primaryFrameLayout, int secondaryFrameLayout) { | public FragmentHelper(GeneralActivity act, int swipeRefID, int primaryFrameLayout, int secondaryFrameLayout) { | ||||
this.act = act; | this.act = act; | ||||
this.swipeRefID = swipeRefID; | this.swipeRefID = swipeRefID; | ||||
this.primaryFrameLayout = primaryFrameLayout; | this.primaryFrameLayout = primaryFrameLayout; | ||||
this.secondaryFrameLayout = secondaryFrameLayout; | this.secondaryFrameLayout = secondaryFrameLayout; | ||||
newDBHelper = NextGenDB.getInstance(act.getApplicationContext()); | newDBHelper = NextGenDB.getInstance(act.getApplicationContext()); | ||||
} | } | ||||
/** | /** | ||||
* Get the last successfully searched bus stop or NULL | * Get the last successfully searched bus stop or NULL | ||||
* | * | ||||
* @return | * @return | ||||
*/ | */ | ||||
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(WeakReference<AsyncDataDownload> lastTaskRef) { | ||||
this.lastTaskRef = lastTaskRef; | this.lastTaskRef = lastTaskRef; | ||||
} | } | ||||
/** | /** | ||||
* 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(act==null || shouldHaltAllActivities) { | ||||
//SOMETHING WENT VERY WRONG | //SOMETHING WENT VERY WRONG | ||||
return; | return; | ||||
} | } | ||||
SwipeRefreshLayout srl = (SwipeRefreshLayout) act.findViewById(swipeRefID); | SwipeRefreshLayout srl = (SwipeRefreshLayout) act.findViewById(swipeRefID); | ||||
FragmentManager fm = act.getSupportFragmentManager(); | FragmentManager fm = act.getSupportFragmentManager(); | ||||
if(fm.findFragmentById(R.id.resultFrame) instanceof ArrivalsFragment) { | if(fm.findFragmentById(R.id.resultFrame) instanceof ArrivalsFragment) { | ||||
arrivalsFragment = (ArrivalsFragment) fm.findFragmentById(R.id.resultFrame); | arrivalsFragment = (ArrivalsFragment) fm.findFragmentById(R.id.resultFrame); | ||||
sameFragment = arrivalsFragment.isFragmentForTheSameStop(p); | sameFragment = arrivalsFragment.isFragmentForTheSameStop(p); | ||||
} else | } else | ||||
sameFragment = false; | sameFragment = false; | ||||
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(R.id.resultFrame); | arrivalsFragment = (ArrivalsFragment) fm.findFragmentById(R.id.resultFrame); | ||||
} | } | ||||
arrivalsFragment.setListAdapter(new PalinaAdapter(act.getApplicationContext(),p)); | arrivalsFragment.setListAdapter(new PalinaAdapter(act.getApplicationContext(),p)); | ||||
act.hideKeyboard(); | act.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(); | act.hideKeyboard(); | ||||
StopListFragment listfragment = StopListFragment.newInstance(query); | StopListFragment listfragment = StopListFragment.newInstance(query); | ||||
attachFragmentToContainer(act.getSupportFragmentManager(),listfragment,false,"search_"+query); | attachFragmentToContainer(act.getSupportFragmentManager(),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 FragmentListener) | if (act instanceof FragmentListener) | ||||
((FragmentListener) act).toggleSpinner(on); | ((FragmentListener) act).toggleSpinner(on); | ||||
else { | else { | ||||
SwipeRefreshLayout srl = (SwipeRefreshLayout) act.findViewById(swipeRefID); | SwipeRefreshLayout srl = (SwipeRefreshLayout) act.findViewById(swipeRefID); | ||||
srl.setRefreshing(false); | 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){ | synchronized public int insertBatchDataInNextGenDB(ContentValues[] valuesArr,String tableName){ | ||||
if(newDBHelper !=null) | if(newDBHelper !=null) | ||||
try { | try { | ||||
return newDBHelper.insertBatchContent(valuesArr, tableName); | return newDBHelper.insertBatchContent(valuesArr, tableName); | ||||
} catch (SQLiteException exc){ | } catch (SQLiteException exc){ | ||||
Log.w("DB Batch inserting: ","ERROR Inserting the data batch: ",exc.fillInStackTrace()); | Log.w("DB Batch inserting: ","ERROR Inserting the data batch: ",exc.fillInStackTrace()); | ||||
return -2; | return -2; | ||||
} | } | ||||
else return -1; | else return -1; | ||||
} | } | ||||
synchronized public ContentResolver getContentResolver(){ | synchronized public ContentResolver getContentResolver(){ | ||||
return act.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.showMessage(R.string.network_error); | act.showMessage(R.string.network_error); | ||||
break; | break; | ||||
case SERVER_ERROR: | case SERVER_ERROR: | ||||
if (act.isConnected()) { | if (act.isConnected()) { | ||||
act.showMessage(R.string.parsing_error); | act.showMessage(R.string.parsing_error); | ||||
} else { | } else { | ||||
act.showMessage(R.string.network_error); | act.showMessage(R.string.network_error); | ||||
} | } | ||||
case PARSER_ERROR: | case PARSER_ERROR: | ||||
default: | default: | ||||
act.showMessage(R.string.internal_error); | act.showMessage(R.string.internal_error); | ||||
break; | break; | ||||
case QUERY_TOO_SHORT: | case QUERY_TOO_SHORT: | ||||
act.showMessage(R.string.query_too_short); | act.showMessage(R.string.query_too_short); | ||||
break; | break; | ||||
case EMPTY_RESULT_SET: | case EMPTY_RESULT_SET: | ||||
act.showMessage(R.string.no_bus_stop_have_this_name); | act.showMessage(R.string.no_bus_stop_have_this_name); | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
} | } |
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