Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/fragments/FragmentHelper.java
Show All 15 Lines | BusTO (fragments) | ||||
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 androidx.fragment.app.Fragment; | ||||
import android.support.v4.app.FragmentManager; | import androidx.fragment.app.FragmentManager; | ||||
import android.support.v4.app.FragmentTransaction; | import androidx.fragment.app.FragmentTransaction; | ||||
import android.support.v4.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.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.*; | ||||
Show All 24 Lines | public FragmentHelper(GeneralActivity act, int swipeRefID, int primaryFrameLayout, int secondaryFrameLayout) { | ||||
this.primaryFrameLayout = primaryFrameLayout; | this.primaryFrameLayout = primaryFrameLayout; | ||||
this.secondaryFrameLayout = secondaryFrameLayout; | this.secondaryFrameLayout = secondaryFrameLayout; | ||||
newDBHelper = new NextGenDB(act.getApplicationContext()); | newDBHelper = new NextGenDB(act.getApplicationContext()); | ||||
} | } | ||||
/** | /** | ||||
* Get the last successfully searched bus stop or NULL | * Get the last successfully searched bus stop or NULL | ||||
* | * | ||||
* @return | * @return the stop | ||||
*/ | */ | ||||
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; | ||||
} | } | ||||
Show All 10 Lines | 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); | |||||
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; | ||||
Show All 9 Lines | public void createOrUpdateStopFragment(Palina p){ | ||||
} 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); | ||||
} | } | ||||
// DO NOT CALL `setListAdapter` ever on arrivals fragment | |||||
arrivalsFragment.updateFragmentData(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 | ||||
▲ Show 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | public class FragmentHelper { | ||||
* @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.showToastMessage(R.string.network_error, true); | ||||
break; | break; | ||||
case SERVER_ERROR: | case SERVER_ERROR: | ||||
if (act.isConnected()) { | if (act.isConnected()) { | ||||
act.showMessage(R.string.parsing_error); | act.showToastMessage(R.string.parsing_error, true); | ||||
} else { | } else { | ||||
act.showMessage(R.string.network_error); | act.showToastMessage(R.string.network_error, true); | ||||
} | } | ||||
case PARSER_ERROR: | case PARSER_ERROR: | ||||
default: | default: | ||||
act.showMessage(R.string.internal_error); | showShortToast(R.string.internal_error); | ||||
break; | break; | ||||
case QUERY_TOO_SHORT: | case QUERY_TOO_SHORT: | ||||
act.showMessage(R.string.query_too_short); | showShortToast(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); | showShortToast(R.string.no_bus_stop_have_this_name); | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
public void showShortToast(int message){ | |||||
if (act!=null) | |||||
act.showToastMessage(message,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