Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/fragments/FragmentHelper.java
Show All 21 Lines | |||||
import android.content.ContentValues; | import android.content.ContentValues; | ||||
import android.database.sqlite.SQLiteException; | 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 androidx.swiperefreshlayout.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.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.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 Stop lastSuccessfullySearchedBusStop; | private Stop lastSuccessfullySearchedBusStop; | ||||
//support for multiple frames | //support for multiple frames | ||||
private int primaryFrameLayout,secondaryFrameLayout, swipeRefID; | private final int secondaryFrameLayout; | ||||
private final int swipeRefID; | |||||
private final int primaryFrameLayout; | |||||
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); | ||||
Show All 34 Lines | public void createOrUpdateStopFragment(Palina p){ | ||||
if(act==null || shouldHaltAllActivities) { | if(act==null || shouldHaltAllActivities) { | ||||
//SOMETHING WENT VERY WRONG | //SOMETHING WENT VERY WRONG | ||||
return; | return; | ||||
} | } | ||||
FragmentManager fm = act.getSupportFragmentManager(); | FragmentManager fm = act.getSupportFragmentManager(); | ||||
if(fm.findFragmentById(R.id.resultFrame) instanceof ArrivalsFragment) { | if(fm.findFragmentById(primaryFrameLayout) instanceof ArrivalsFragment) { | ||||
arrivalsFragment = (ArrivalsFragment) fm.findFragmentById(R.id.resultFrame); | arrivalsFragment = (ArrivalsFragment) fm.findFragmentById(primaryFrameLayout); | ||||
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(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(); | act.hideKeyboard(); | ||||
toggleSpinner(false); | toggleSpinner(false); | ||||
} | } | ||||
Show All 11 Lines | public class FragmentHelper { | ||||
} | } | ||||
/** | /** | ||||
* 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 FragmentListenerMain) | ||||
((FragmentListener) act).toggleSpinner(on); | ((FragmentListenerMain) 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 | ||||
▲ Show 20 Lines • Show All 80 Lines • Show Last 20 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