Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/data/FavoritesLiveData.java
package it.reyboz.bustorino.data; | package it.reyboz.bustorino.data; | ||||
import android.annotation.SuppressLint; | |||||
import android.content.AsyncQueryHandler; | |||||
import android.content.ContentResolver; | import android.content.ContentResolver; | ||||
import android.content.Context; | import android.content.Context; | ||||
import android.database.ContentObserver; | import android.database.ContentObserver; | ||||
import android.database.Cursor; | import android.database.Cursor; | ||||
import android.net.Uri; | import android.net.Uri; | ||||
import android.os.AsyncTask; | |||||
import android.os.Handler; | import android.os.Handler; | ||||
import android.util.Log; | import android.util.Log; | ||||
import androidx.annotation.NonNull; | import androidx.annotation.NonNull; | ||||
import androidx.annotation.Nullable; | import androidx.annotation.Nullable; | ||||
import androidx.core.content.ContentResolverCompat; | |||||
import androidx.core.os.CancellationSignal; | |||||
import androidx.core.os.OperationCanceledException; | |||||
import androidx.lifecycle.LiveData; | import androidx.lifecycle.LiveData; | ||||
import java.lang.ref.WeakReference; | |||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.Arrays; | import java.util.Arrays; | ||||
import java.util.Collection; | |||||
import java.util.Collections; | import java.util.Collections; | ||||
import java.util.List; | import java.util.List; | ||||
import it.reyboz.bustorino.backend.Route; | import it.reyboz.bustorino.BuildConfig; | ||||
import it.reyboz.bustorino.backend.Stop; | import it.reyboz.bustorino.backend.Stop; | ||||
import it.reyboz.bustorino.data.NextGenDB.Contract.*; | |||||
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 = "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; | ||||
private final CustomAsyncQueryHandler queryHandler; | private final CustomAsyncQueryHandler queryHandler; | ||||
private final Uri FAVORITES_URI = AppDataProvider.getUriBuilderToComplete().appendPath( | private final Uri FAVORITES_URI = AppDataProvider.getUriBuilderToComplete().appendPath( | ||||
AppDataProvider.FAVORITES).build(); | AppDataProvider.FAVORITES).build(); | ||||
private final int FAV_TOKEN = 23, STOPS_TOKEN_BASE=90; | private final int FAV_TOKEN = 23, STOPS_TOKEN_BASE=220; | ||||
@Nullable | @Nullable | ||||
private List<Stop> stopsFromFavorites, stopsDone; | private List<Stop> stopsFromFavorites, stopsDone; | ||||
private boolean isQueryRunning = false; | private boolean isQueryRunning = false; | ||||
private int stopNeededCount = 0; | private int stopNeededCount = 0; | ||||
Show All 31 Lines | private void loadData(boolean forceQuery) { | ||||
isQueryRunning = true; | isQueryRunning = true; | ||||
queryHandler.startQuery(FAV_TOKEN,null, FAVORITES_URI, UserDB.getFavoritesColumnNamesAsArray, null, null, null); | queryHandler.startQuery(FAV_TOKEN,null, FAVORITES_URI, UserDB.getFavoritesColumnNamesAsArray, null, null, null); | ||||
} | } | ||||
@Override | @Override | ||||
protected void onActive() { | protected void onActive() { | ||||
Log.d(TAG, "onActive()"); | //Log.d(TAG, "onActive()"); | ||||
loadData(); | loadData(); | ||||
} | } | ||||
@Override | |||||
protected void onInactive() { | |||||
Log.d(TAG, "onInactive()"); | |||||
} | |||||
/** | /** | ||||
* Clear the data for the cursor | * Clear the data for the cursor | ||||
*/ | */ | ||||
public void onClear(){ | public void onClear(){ | ||||
ContentResolver resolver = mContext.getContentResolver(); | ContentResolver resolver = mContext.getContentResolver(); | ||||
resolver.unregisterContentObserver(mObserver); | resolver.unregisterContentObserver(mObserver); | ||||
} | } | ||||
@Override | @Override | ||||
protected void setValue(List<Stop> stops) { | protected void setValue(List<Stop> stops) { | ||||
//Log.d("BusTO-FavoritesLiveData","Setting the new values for the stops, have "+ | |||||
// stops.size()+" 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 (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 | |||||
stopNeededCount = stopsFromFavorites.size(); | |||||
stopsDone = new ArrayList<>(); | |||||
if(stopsFromFavorites.size() == 0){ | |||||
//we don't need to call the other query | |||||
setValue(stopsDone); | |||||
} else | |||||
for (int i = 0; i < stopsFromFavorites.size(); i++) { | for (int i = 0; i < stopsFromFavorites.size(); i++) { | ||||
Stop s = stopsFromFavorites.get(i); | Stop s = stopsFromFavorites.get(i); | ||||
queryHandler.startQuery(STOPS_TOKEN_BASE + i, null, getStopsBuilder().appendPath(s.ID).build(), | queryHandler.startQuery(STOPS_TOKEN_BASE + i, null, | ||||
getStopsBuilder().appendPath(s.ID).build(), | |||||
NextGenDB.QUERY_COLUMN_stops_all, null, null, null); | NextGenDB.QUERY_COLUMN_stops_all, null, null, null); | ||||
} | } | ||||
stopNeededCount = stopsFromFavorites.size(); | |||||
stopsDone = new ArrayList<>(); | |||||
} 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 = Arrays.asList(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); | ||||
assert (finalStop.ID.equals(stopUpdate.ID)); | if (BuildConfig.DEBUG && !(finalStop.ID.equals(stopUpdate.ID))) { | ||||
throw new AssertionError("Assertion failed"); | |||||
} | |||||
finalStop.setStopUserName(stopUpdate.getStopUserName()); | finalStop.setStopUserName(stopUpdate.getStopUserName()); | ||||
} | } | ||||
if (stopsDone!=null) | if (stopsDone!=null) | ||||
stopsDone.add(finalStop); | stopsDone.add(finalStop); | ||||
stopNeededCount--; | stopNeededCount--; | ||||
if (stopNeededCount == 0) { | if (stopNeededCount == 0) { | ||||
// we have finished the queries | // we have finished the queries | ||||
Show All 37 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