Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/fragments/ArrivalsFragment.java
/* | /* | ||||
BusTO - Fragments components | BusTO - Fragments components | ||||
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.database.Cursor; | import android.database.Cursor; | ||||
import android.net.Uri; | import android.net.Uri; | ||||
import android.os.Bundle; | import android.os.Bundle; | ||||
import android.support.annotation.Nullable; | import android.support.annotation.Nullable; | ||||
import android.support.v4.app.LoaderManager; | import android.support.v4.app.LoaderManager; | ||||
import android.support.v4.content.CursorLoader; | import android.support.v4.content.CursorLoader; | ||||
import android.support.v4.content.Loader; | import android.support.v4.content.Loader; | ||||
import android.util.Log; | import android.util.Log; | ||||
import android.widget.TextView; | import android.widget.ImageButton; | ||||
import android.widget.TextView; | |||||
import it.reyboz.bustorino.R; | |||||
import it.reyboz.bustorino.backend.DBStatusManager; | import it.reyboz.bustorino.R; | ||||
import it.reyboz.bustorino.middleware.AppDataProvider; | import it.reyboz.bustorino.backend.DBStatusManager; | ||||
import it.reyboz.bustorino.middleware.NextGenDB; | import it.reyboz.bustorino.middleware.AppDataProvider; | ||||
import it.reyboz.bustorino.middleware.UserDB; | import it.reyboz.bustorino.middleware.NextGenDB; | ||||
import it.reyboz.bustorino.middleware.UserDB; | |||||
public class ArrivalsFragment extends ResultListFragment implements LoaderManager.LoaderCallbacks<Cursor> { | |||||
public class ArrivalsFragment extends ResultListFragment implements LoaderManager.LoaderCallbacks<Cursor> { | |||||
private final static String KEY_STOP_ID = "stopid"; | |||||
private final static String KEY_STOP_NAME = "stopname"; | private final static String KEY_STOP_ID = "stopid"; | ||||
private final static String DEBUG_TAG = "BUSTOArrivalsFragment"; | private final static String KEY_STOP_NAME = "stopname"; | ||||
private final static int loaderFavId = 2; | private final static String DEBUG_TAG = "BUSTOArrivalsFragment"; | ||||
private final static int loaderStopId = 1; | private final static int loaderFavId = 2; | ||||
private @Nullable String stopID,stopName; | private final static int loaderStopId = 1; | ||||
private TextView messageTextView; | private @Nullable String stopID,stopName; | ||||
private DBStatusManager prefs; | private TextView messageTextView; | ||||
private DBStatusManager.OnDBUpdateStatusChangeListener listener; | private DBStatusManager prefs; | ||||
private boolean justCreated = false; | private DBStatusManager.OnDBUpdateStatusChangeListener listener; | ||||
private boolean justCreated = false; | |||||
public static ArrivalsFragment newInstance(String stopID){ | private ImageButton addToFavorites; | ||||
Bundle args = new Bundle(); | |||||
args.putString(KEY_STOP_ID,stopID); | |||||
ArrivalsFragment fragment = new ArrivalsFragment(); | public static ArrivalsFragment newInstance(String stopID){ | ||||
//parameter for ResultListFragment | Bundle args = new Bundle(); | ||||
args.putSerializable(LIST_TYPE,FragmentKind.ARRIVALS); | args.putString(KEY_STOP_ID,stopID); | ||||
fragment.setArguments(args); | ArrivalsFragment fragment = new ArrivalsFragment(); | ||||
return fragment; | //parameter for ResultListFragment | ||||
} | args.putSerializable(LIST_TYPE,FragmentKind.ARRIVALS); | ||||
public static ArrivalsFragment newInstance(String stopID,String stopName){ | fragment.setArguments(args); | ||||
ArrivalsFragment fragment = newInstance(stopID); | return fragment; | ||||
Bundle args = fragment.getArguments(); | } | ||||
args.putString(KEY_STOP_NAME,stopName); | public static ArrivalsFragment newInstance(String stopID,String stopName){ | ||||
fragment.setArguments(args); | ArrivalsFragment fragment = newInstance(stopID); | ||||
return fragment; | Bundle args = fragment.getArguments(); | ||||
} | args.putString(KEY_STOP_NAME,stopName); | ||||
fragment.setArguments(args); | |||||
@Override | return fragment; | ||||
public void onCreate(Bundle savedInstanceState) { | } | ||||
super.onCreate(savedInstanceState); | |||||
stopID = getArguments().getString(KEY_STOP_ID); | @Override | ||||
//this might really be null | public void onCreate(Bundle savedInstanceState) { | ||||
stopName = getArguments().getString(KEY_STOP_NAME); | super.onCreate(savedInstanceState); | ||||
final ArrivalsFragment f = this; | stopID = getArguments().getString(KEY_STOP_ID); | ||||
listener = new DBStatusManager.OnDBUpdateStatusChangeListener() { | //this might really be null | ||||
@Override | stopName = getArguments().getString(KEY_STOP_NAME); | ||||
public void onDBStatusChanged(boolean updating) { | final ArrivalsFragment f = this; | ||||
if(!updating){ | listener = new DBStatusManager.OnDBUpdateStatusChangeListener() { | ||||
getLoaderManager().restartLoader(loaderFavId,getArguments(),f); | @Override | ||||
} else { | public void onDBStatusChanged(boolean updating) { | ||||
final LoaderManager lm = getLoaderManager(); | if(!updating){ | ||||
lm.destroyLoader(loaderFavId); | getLoaderManager().restartLoader(loaderFavId,getArguments(),f); | ||||
lm.destroyLoader(loaderStopId); | } else { | ||||
} | final LoaderManager lm = getLoaderManager(); | ||||
} | lm.destroyLoader(loaderFavId); | ||||
lm.destroyLoader(loaderStopId); | |||||
@Override | } | ||||
public boolean defaultStatusValue() { | } | ||||
return true; | |||||
} | @Override | ||||
}; | public boolean defaultStatusValue() { | ||||
prefs = new DBStatusManager(getContext().getApplicationContext(),listener); | return true; | ||||
justCreated = true; | } | ||||
}; | |||||
} | prefs = new DBStatusManager(getContext().getApplicationContext(),listener); | ||||
justCreated = true; | |||||
@Override | |||||
public void onResume() { | } | ||||
super.onResume(); | |||||
LoaderManager loaderManager = getLoaderManager(); | @Override | ||||
public void onResume() { | |||||
if(stopID!=null){ | super.onResume(); | ||||
//refresh the arrivals | LoaderManager loaderManager = getLoaderManager(); | ||||
if(!justCreated) | |||||
mListener.createFragmentForStop(stopID); | if(stopID!=null){ | ||||
else justCreated = false; | //refresh the arrivals | ||||
//start the loader | if(!justCreated) | ||||
if(prefs.isDBUpdating(true)){ | mListener.createFragmentForStop(stopID); | ||||
prefs.registerListener(); | else justCreated = false; | ||||
} else { | //start the loader | ||||
loaderManager.restartLoader(loaderFavId, getArguments(), this); | if(prefs.isDBUpdating(true)){ | ||||
} | prefs.registerListener(); | ||||
updateMessage(); | } else { | ||||
} | loaderManager.restartLoader(loaderFavId, getArguments(), this); | ||||
} | } | ||||
updateMessage(); | |||||
@Nullable | } | ||||
public String getStopID() { | } | ||||
return stopID; | |||||
} | @Nullable | ||||
public String getStopID() { | |||||
/** | return stopID; | ||||
* Update the message in the fragment | } | ||||
* | |||||
* It may eventually change the "Add to Favorite" icon | /** | ||||
*/ | * Update the message in the fragment | ||||
private void updateMessage(){ | * | ||||
String message = null; | * It may eventually change the "Add to Favorite" icon | ||||
if (stopName != null && stopID != null && stopName.length() > 0) { | */ | ||||
message = (stopID.concat(" - ").concat(stopName)); | private void updateMessage(){ | ||||
} else if(stopID!=null) { | String message = null; | ||||
message = stopID; | if (stopName != null && stopID != null && stopName.length() > 0) { | ||||
} else { | message = (stopID.concat(" - ").concat(stopName)); | ||||
Log.e("ArrivalsFragm"+getTag(),"NO ID FOR THIS FRAGMENT - something went horribly wrong"); | } else if(stopID!=null) { | ||||
} | message = stopID; | ||||
if(message!=null) { | } else { | ||||
setTextViewMessage(getString(R.string.passages,message)); | Log.e("ArrivalsFragm"+getTag(),"NO ID FOR THIS FRAGMENT - something went horribly wrong"); | ||||
} | } | ||||
if(message!=null) { | |||||
// whatever is the case, update the star icon | setTextViewMessage(getString(R.string.passages,message)); | ||||
mListener.updateStarIconFromLastBusStop(); | } | ||||
} | |||||
// whatever is the case, update the star icon | |||||
@Override | mListener.updateStarIconFromLastBusStop(); | ||||
public Loader<Cursor> onCreateLoader(int id, Bundle args) { | } | ||||
if(args.getString(KEY_STOP_ID)==null) return null; | |||||
final String stopID = args.getString(KEY_STOP_ID); | @Override | ||||
final Uri.Builder builder = AppDataProvider.getUriBuilderToComplete(); | public Loader<Cursor> onCreateLoader(int id, Bundle args) { | ||||
CursorLoader cl; | if(args.getString(KEY_STOP_ID)==null) return null; | ||||
switch (id){ | final String stopID = args.getString(KEY_STOP_ID); | ||||
case loaderFavId: | final Uri.Builder builder = AppDataProvider.getUriBuilderToComplete(); | ||||
builder.appendPath("favorites").appendPath(stopID); | CursorLoader cl; | ||||
cl = new CursorLoader(getContext(),builder.build(),UserDB.getFavoritesColumnNamesAsArray,null,null,null); | switch (id){ | ||||
case loaderFavId: | |||||
break; | builder.appendPath("favorites").appendPath(stopID); | ||||
case loaderStopId: | cl = new CursorLoader(getContext(),builder.build(),UserDB.getFavoritesColumnNamesAsArray,null,null,null); | ||||
builder.appendPath("stop").appendPath(stopID); | |||||
cl = new CursorLoader(getContext(),builder.build(),new String[]{NextGenDB.Contract.StopsTable.COL_NAME}, | break; | ||||
null,null,null); | case loaderStopId: | ||||
break; | builder.appendPath("stop").appendPath(stopID); | ||||
default: | cl = new CursorLoader(getContext(),builder.build(),new String[]{NextGenDB.Contract.StopsTable.COL_NAME}, | ||||
return null; | null,null,null); | ||||
} | break; | ||||
cl.setUpdateThrottle(500); | default: | ||||
return cl; | return null; | ||||
} | } | ||||
cl.setUpdateThrottle(500); | |||||
@Override | return cl; | ||||
public void onLoadFinished(Loader<Cursor> loader, Cursor data) { | } | ||||
switch (loader.getId()){ | @Override | ||||
case loaderFavId: | public void onLoadFinished(Loader<Cursor> loader, Cursor data) { | ||||
final int colUserName = data.getColumnIndex(UserDB.getFavoritesColumnNamesAsArray[1]); | |||||
if(data.getCount()>0){ | switch (loader.getId()){ | ||||
data.moveToFirst(); | case loaderFavId: | ||||
final String probableName = data.getString(colUserName); | final int colUserName = data.getColumnIndex(UserDB.getFavoritesColumnNamesAsArray[1]); | ||||
if(probableName!=null && !probableName.isEmpty()){ | if(data.getCount()>0){ | ||||
stopName = probableName; | data.moveToFirst(); | ||||
updateMessage(); | final String probableName = data.getString(colUserName); | ||||
} | if(probableName!=null && !probableName.isEmpty()){ | ||||
} | stopName = probableName; | ||||
if(stopName == null){ | updateMessage(); | ||||
//stop is not inside the favorites and wasn't provided | } | ||||
Log.d("ArrivalsFragment"+getTag(),"Stop wasn't in the favorites and has no name, looking in the DB"); | } | ||||
getLoaderManager().restartLoader(loaderStopId,getArguments(),this); | if(stopName == null){ | ||||
} | //stop is not inside the favorites and wasn't provided | ||||
break; | Log.d("ArrivalsFragment"+getTag(),"Stop wasn't in the favorites and has no name, looking in the DB"); | ||||
case loaderStopId: | getLoaderManager().restartLoader(loaderStopId,getArguments(),this); | ||||
if(data.getCount()>0){ | } | ||||
data.moveToFirst(); | break; | ||||
stopName = data.getString(data.getColumnIndex( | case loaderStopId: | ||||
NextGenDB.Contract.StopsTable.COL_NAME | if(data.getCount()>0){ | ||||
)); | data.moveToFirst(); | ||||
updateMessage(); | stopName = data.getString(data.getColumnIndex( | ||||
} else { | NextGenDB.Contract.StopsTable.COL_NAME | ||||
Log.w("ArrivalsFragment"+getTag(),"Stop is not inside the database... CLOISTER BELL"); | )); | ||||
} | updateMessage(); | ||||
} | } else { | ||||
Log.w("ArrivalsFragment"+getTag(),"Stop is not inside the database... CLOISTER BELL"); | |||||
} | } | ||||
} | |||||
@Override | |||||
public void onPause() { | } | ||||
if(listener!=null) | |||||
prefs.unregisterListener(); | @Override | ||||
super.onPause(); | public void onPause() { | ||||
} | if(listener!=null) | ||||
prefs.unregisterListener(); | |||||
@Override | super.onPause(); | ||||
public void onLoaderReset(Loader<Cursor> loader) { | } | ||||
//NOTHING TO DO | |||||
} | @Override | ||||
} | public void onLoaderReset(Loader<Cursor> loader) { | ||||
//NOTHING TO DO | |||||
} | |||||
} |
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