Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/data/FavoritesLiveData.java
Show All 34 Lines | |||||
import java.util.Arrays; | import java.util.Arrays; | ||||
import java.util.Collections; | import java.util.Collections; | ||||
import java.util.List; | import java.util.List; | ||||
import it.reyboz.bustorino.BuildConfig; | import it.reyboz.bustorino.BuildConfig; | ||||
import it.reyboz.bustorino.backend.Stop; | import it.reyboz.bustorino.backend.Stop; | ||||
public class FavoritesLiveData extends LiveData<List<Stop>> implements CustomAsyncQueryHandler.AsyncQueryListener { | public class FavoritesLiveData extends LiveData<List<Stop>> implements CustomAsyncQueryHandler.AsyncQueryListener { | ||||
private static final String TAG = "FavoritesLiveData"; | private static final String TAG = "BusTO-FavoritesLiveData"; | ||||
private final boolean notifyChangesDescendants; | private final boolean notifyChangesDescendants; | ||||
@NonNull | @NonNull | ||||
private final Context mContext; | private final Context mContext; | ||||
@NonNull | @NonNull | ||||
private final FavoritesLiveData.ForceLoadContentObserver mObserver; | private final FavoritesLiveData.ForceLoadContentObserver mObserver; | ||||
▲ Show 20 Lines • Show All 75 Lines • ▼ Show 20 Lines | protected void setValue(List<Stop> stops) { | ||||
ContentResolver resolver = mContext.getContentResolver(); | ContentResolver resolver = mContext.getContentResolver(); | ||||
resolver.registerContentObserver(FAVORITES_URI, notifyChangesDescendants,mObserver); | resolver.registerContentObserver(FAVORITES_URI, notifyChangesDescendants,mObserver); | ||||
super.setValue(stops); | super.setValue(stops); | ||||
} | } | ||||
@Override | @Override | ||||
public void onQueryComplete(int token, Object cookie, Cursor cursor) { | public void onQueryComplete(int token, Object cookie, Cursor cursor) { | ||||
if (cursor == null){ | |||||
//Nothing to do | |||||
Log.e(TAG, "Null cursor for token "+token); | |||||
return; | |||||
} | |||||
if (token == FAV_TOKEN) { | if (token == FAV_TOKEN) { | ||||
stopsFromFavorites = UserDB.getFavoritesFromCursor(cursor, UserDB.getFavoritesColumnNamesAsArray); | stopsFromFavorites = UserDB.getFavoritesFromCursor(cursor, UserDB.getFavoritesColumnNamesAsArray); | ||||
cursor.close(); | cursor.close(); | ||||
//reset counters | //reset counters | ||||
stopNeededCount = stopsFromFavorites.size(); | stopNeededCount = stopsFromFavorites.size(); | ||||
stopsDone = new ArrayList<>(); | stopsDone = new ArrayList<>(); | ||||
if(stopsFromFavorites.size() == 0){ | if(stopsFromFavorites.size() == 0){ | ||||
//we don't need to call the other query | //we don't need to call the other query | ||||
Show All 9 Lines | public void onQueryComplete(int token, Object cookie, Cursor cursor) { | ||||
} else if(token >= STOPS_TOKEN_BASE){ | } else if(token >= STOPS_TOKEN_BASE){ | ||||
final int index = token - STOPS_TOKEN_BASE; | final int index = token - STOPS_TOKEN_BASE; | ||||
assert stopsFromFavorites != null; | assert stopsFromFavorites != null; | ||||
Stop stopUpdate = stopsFromFavorites.get(index); | Stop stopUpdate = stopsFromFavorites.get(index); | ||||
Stop finalStop; | Stop finalStop; | ||||
List<Stop> result = Arrays.asList(NextGenDB.getStopsFromCursorAllFields(cursor)); | List<Stop> result = NextGenDB.getStopsFromCursorAllFields(cursor); | ||||
cursor.close(); | cursor.close(); | ||||
if (result.size() < 1){ | if (result.size() < 1){ | ||||
// stop is not in the DB | // stop is not in the DB | ||||
finalStop = stopUpdate; | finalStop = stopUpdate; | ||||
} else { | } else { | ||||
finalStop = result.get(0); | finalStop = result.get(0); | ||||
if (BuildConfig.DEBUG && !(finalStop.ID.equals(stopUpdate.ID))) { | if (BuildConfig.DEBUG && !(finalStop.ID.equals(stopUpdate.ID))) { | ||||
throw new AssertionError("Assertion failed"); | throw new AssertionError("Assertion failed"); | ||||
▲ Show 20 Lines • Show All 46 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