Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/fragments/FragmentHelper.java
Show All 13 Lines | BusTO (fragments) | ||||
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.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.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; | ||||
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 | ||||
*/ | */ | ||||
Show All 40 Lines | public class FragmentHelper { | ||||
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 addToBackStack){ | ||||
boolean sameFragment; | boolean sameFragment; | ||||
ArrivalsFragment arrivalsFragment; | ArrivalsFragment arrivalsFragment; | ||||
if(managerWeakRef.get()==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"); | Log.e(DEBUG_TAG, "We are asked for a new stop but we can't show anything"); | ||||
return; | return; | ||||
} | } | ||||
FragmentManager fm = managerWeakRef.get(); | 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?"); | //Log.d(DEBUG_TAG, "Arrivals are for fragment with same stop?"); | ||||
assert arrivalsFragment != null; | |||||
sameFragment = arrivalsFragment.isFragmentForTheSameStop(p); | sameFragment = arrivalsFragment.isFragmentForTheSameStop(p); | ||||
} else { | } else { | ||||
sameFragment = false; | sameFragment = false; | ||||
Log.d(DEBUG_TAG, "We aren't showing an ArrivalsFragment"); | 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)); | String probableTag = ResultListFragment.getFragmentTag(p); | ||||
attachFragmentToContainer(fm,arrivalsFragment,new AttachParameters(probableTag, true, addToBackStack)); | |||||
} 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); | ||||
// enable fragment auto refresh | |||||
arrivalsFragment.setReloadOnResume(true); | |||||
listenerMain.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 createStopListFragment(List<Stop> resultList, String query, boolean addToBackStack){ | ||||
listenerMain.hideKeyboard(); | listenerMain.hideKeyboard(); | ||||
StopListFragment listfragment = StopListFragment.newInstance(query); | StopListFragment listfragment = StopListFragment.newInstance(query); | ||||
if(managerWeakRef.get()==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"); | Log.e(DEBUG_TAG, "We are asked for a new stop but we can't show anything"); | ||||
return; | return; | ||||
} | } | ||||
attachFragmentToContainer(managerWeakRef.get(),listfragment,false,"search_"+query); | attachFragmentToContainer(managerWeakRef.get(),listfragment, | ||||
new AttachParameters("search_"+query, false,addToBackStack)); | |||||
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){ | ||||
listenerMain.toggleSpinner(on); | listenerMain.toggleSpinner(on); | ||||
} | } | ||||
/** | /** | ||||
* 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 parameters attach parameters | ||||
* @param tag tag for the fragment | |||||
*/ | */ | ||||
public void attachFragmentToContainer(FragmentManager fm,Fragment fragment, boolean sendToSecondaryFrame, String tag){ | protected void attachFragmentToContainer(FragmentManager fm,Fragment fragment, AttachParameters parameters){ | ||||
FragmentTransaction ft = fm.beginTransaction(); | FragmentTransaction ft = fm.beginTransaction(); | ||||
if(sendToSecondaryFrame && secondaryFrameLayout!=NO_FRAME) | int frameID; | ||||
ft.replace(secondaryFrameLayout,fragment,tag); | if(parameters.attachToSecondaryFrame && secondaryFrameLayout!=NO_FRAME) | ||||
else ft.replace(primaryFrameLayout,fragment,tag); | // ft.replace(secondaryFrameLayout,fragment,tag); | ||||
ft.addToBackStack("state_"+tag); | frameID = secondaryFrameLayout; | ||||
else frameID = primaryFrameLayout; | |||||
switch (parameters.transaction){ | |||||
case REPLACE: | |||||
ft.replace(frameID,fragment,parameters.tag); | |||||
} | |||||
if (parameters.addToBackStack) | |||||
ft.addToBackStack("state_"+parameters.tag); | |||||
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE); | ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE); | ||||
ft.commit(); | ft.commit(); | ||||
//fm.executePendingTransactions(); | //fm.executePendingTransactions(); | ||||
} | } | ||||
public void setBlockAllActivities(boolean shouldI) { | public void setBlockAllActivities(boolean shouldI) { | ||||
this.shouldHaltAllActivities = shouldI; | this.shouldHaltAllActivities = shouldI; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | 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) | ||||
Toast.makeText(context, messageID, length).show(); | Toast.makeText(context, messageID, length).show(); | ||||
} | } | ||||
private void showShortToast(int messageID){ | private void showShortToast(int messageID){ | ||||
showToastMessage(messageID, true); | showToastMessage(messageID, true); | ||||
} | } | ||||
enum Transaction{ | |||||
REPLACE, | |||||
} | |||||
static final class AttachParameters { | |||||
String tag; | |||||
boolean attachToSecondaryFrame; | |||||
Transaction transaction; | |||||
boolean addToBackStack; | |||||
public AttachParameters(String tag, boolean attachToSecondaryFrame, Transaction transaction, boolean addToBackStack) { | |||||
this.tag = tag; | |||||
this.attachToSecondaryFrame = attachToSecondaryFrame; | |||||
this.transaction = transaction; | |||||
this.addToBackStack = addToBackStack; | |||||
} | |||||
public AttachParameters(String tag, boolean attachToSecondaryFrame, boolean addToBackStack) { | |||||
this.tag = tag; | |||||
this.attachToSecondaryFrame = attachToSecondaryFrame; | |||||
this.addToBackStack = addToBackStack; | |||||
this.transaction = Transaction.REPLACE; | |||||
} | |||||
} | |||||
} | } |
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