Skip to content

Commit

Permalink
Refactored the Home and Favorites business logic. This includes a fix…
Browse files Browse the repository at this point in the history
… for the timing and order of calls for fetching the latest arrival times from the cache.

Also, removed the CachedDataMap class and just added simple hasCachedData methods to each repo class.
  • Loading branch information
andrewvora committed May 13, 2017
1 parent 6d422fd commit bb85cdf
Show file tree
Hide file tree
Showing 30 changed files with 178 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.andrewvora.apps.rideatlanta.buses.BusRoutesContract;
import com.andrewvora.apps.rideatlanta.buses.BusRoutesFragment;
import com.andrewvora.apps.rideatlanta.buses.BusRoutesPresenter;
import com.andrewvora.apps.rideatlanta.data.CachedDataMap;
import com.andrewvora.apps.rideatlanta.data.remote.buses.GetBusesIntentService;
import com.andrewvora.apps.rideatlanta.data.remote.trains.GetTrainsIntentService;
import com.andrewvora.apps.rideatlanta.data.repos.BusesRepo;
Expand Down Expand Up @@ -202,7 +201,12 @@ private void onHomeTabSelected() {
fragment = HomeFragment.newInstance();
}

HomeContract.Presenter presenter = new HomePresenter(fragment);
HomeContract.Presenter presenter = new HomePresenter(
fragment,
FavoriteRoutesRepo.getInstance(this),
NotificationsRepo.getInstance(this),
BusesRepo.getInstance(this),
TrainsRepo.getInstance(this));
fragment.setPresenter(presenter);

startFragment(R.id.fragment_container, fragment, HomeFragment.TAG, false);
Expand Down Expand Up @@ -261,8 +265,7 @@ private void onNotificationsTabSelected() {

NotificationsContract.Presenter presenter = new NotificationsPresenter(
fragment,
NotificationsRepo.getInstance(this),
CachedDataMap.getInstance());
NotificationsRepo.getInstance(this));
fragment.setPresenter(presenter);

startFragment(R.id.fragment_container, fragment, NotificationsFragment.TAG, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.os.Bundle;
import android.support.annotation.NonNull;

import com.andrewvora.apps.rideatlanta.data.CachedDataMap;
import com.andrewvora.apps.rideatlanta.data.contracts.BusesDataSource;
import com.andrewvora.apps.rideatlanta.data.contracts.FavoriteRoutesDataSource;
import com.andrewvora.apps.rideatlanta.data.models.Bus;
Expand Down Expand Up @@ -104,7 +103,6 @@ private BusesDataSource.GetBusesCallback createGetBusesCallbackInstance() {
@Override
public void onFinished(List<Bus> buses) {
updateView(buses);
makeCachedDataAvailable();
}

@Override
Expand All @@ -119,11 +117,7 @@ private void updateView(List<Bus> buses) {
}

private boolean hasNoCachedData() {
return !CachedDataMap.getInstance().hasCachedData(TAG);
}

private void makeCachedDataAvailable() {
CachedDataMap.getInstance().put(TAG, true);
return !mBusesRepo.hasCachedData();
}

private void useCachedDataIfAvailable() {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ interface DeleteBusesCallback {
void deleteAllBus(@Nullable DeleteBusesCallback callback);
void saveBus(@NonNull Bus route);
void reloadBuses();

boolean hasCachedData();
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ interface GetFavoriteRouteCallback {
void deleteRoute(@NonNull FavoriteRouteDataObject route);
void deleteAllRoutes();
void reloadRoutes();

boolean hasCachedData();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* Created by faytx on 10/22/2016.
* @author Andrew Vorakrajangthiti
*/

public interface NotificationsDataSource {

interface GetNotificationsCallback {
Expand All @@ -22,4 +21,6 @@ interface GetNotificationsCallback {
void deleteAllNotifications();
void saveNotification(@NonNull Notification notification);
void reloadNotifications();

boolean hasCachedData();
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ interface DeleteTrainRoutesCallback {
void deleteAllTrains(@Nullable DeleteTrainRoutesCallback callback);
void saveTrain(@NonNull Train route);
void reloadTrains();

boolean hasCachedData();
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ public void saveBus(@NonNull Bus route) {
}
}

@Override
public boolean hasCachedData() {
return false;
}

@Override
public void reloadBuses() {
// reload handled in the BusesRepo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public static NotificationsLocalSource getInstance(@NonNull Context context) {
return mInstance;
}

@Override
public boolean hasCachedData() {
return false;
}

@Override
public void getNotifications(@NonNull GetNotificationsCallback callback) {
SQLiteDatabase db = mDbHelper.getReadableDatabase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ public void deleteRoute(@NonNull FavoriteRouteDataObject route) {
db.delete(FavoriteRoutesTable.TABLE_NAME, whereClause, whereArgs);
}

@Override
public boolean hasCachedData() {
return false;
}

@Override
public void reloadRoutes() {
// reloading is handled in the FavoriteRoutesRepo.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ public void saveTrain(@NonNull Train route) {
}
}

@Override
public boolean hasCachedData() {
return false;
}

@Override
public void reloadTrains() {
// refreshing handled in the TrainRepo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ public void reloadBuses() {

}

@Override
public boolean hasCachedData() {
return false;
}

private RequestQueue getRequestQueue() {
if(mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(mContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public void failure(TwitterException exception) {
});
}

@Override
public boolean hasCachedData() {
return false;
}

@Override
public void deleteAllNotifications() {
// remote notifications are read-only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public static FavoriteRoutesRemoteSource getInstance(@NonNull Context context) {
return mInstance;
}

@Override
public boolean hasCachedData() {
return false;
}

@Override
public void getFavoriteRoutes(@NonNull GetFavoriteRoutesCallback callback) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ private <T> void addToRequestQueue(Request<T> request) {
requestQueue.add(request);
}

@Override
public boolean hasCachedData() {
return false;
}

private static class ParseTrainsJsonTask extends AsyncTask<JSONArray, Void, List<Train>> {
@NonNull
private GetTrainRoutesCallback callbackRef;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ public class BusesRepo implements BusesDataSource {

private static BusesRepo mInstance;

@NonNull
private Map<String, Bus> mCachedBuses;
private BusesDataSource mRemoteSource;
private BusesDataSource mLocalSource;
@NonNull private Map<String, Bus> mCachedBuses;
@NonNull private BusesDataSource mRemoteSource;
@NonNull private BusesDataSource mLocalSource;

private boolean mCacheIsDirty;

Expand Down Expand Up @@ -136,6 +135,11 @@ public void saveBus(@NonNull Bus route) {
}
}

@Override
public boolean hasCachedData() {
return !mCachedBuses.isEmpty();
}

@Override
public void reloadBuses() {
mCacheIsDirty = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ public class FavoriteRoutesRepo implements FavoriteRoutesDataSource {

private static FavoriteRoutesRepo mInstance;

private FavoriteRoutesDataSource mRemoteSource;
private FavoriteRoutesDataSource mLocalSource;
@NonNull private FavoriteRoutesDataSource mRemoteSource;
@NonNull private FavoriteRoutesDataSource mLocalSource;

@NonNull
private Map<String, FavoriteRoute> mCachedRoutes;
@NonNull private Map<String, FavoriteRoute> mCachedRoutes;

private boolean mCacheIsDirty;

Expand Down Expand Up @@ -119,6 +118,11 @@ public void deleteRoute(@NonNull FavoriteRouteDataObject route) {
mCachedRoutes.remove(getMapKeyFor(route));
}

@Override
public boolean hasCachedData() {
return !mCachedRoutes.isEmpty();
}

@Override
public void reloadRoutes() {
mCacheIsDirty = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.andrewvora.apps.rideatlanta.data.models.Notification;
import com.andrewvora.apps.rideatlanta.data.contracts.NotificationsDataSource;
import com.andrewvora.apps.rideatlanta.data.local.notifications.NotificationsLocalSource;
import com.andrewvora.apps.rideatlanta.data.models.Notification;
import com.andrewvora.apps.rideatlanta.data.remote.notifications.NotificationsRemoteSource;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* Created by faytx on 10/22/2016.
Expand All @@ -23,16 +22,19 @@ public class NotificationsRepo implements NotificationsDataSource {

private static NotificationsRepo mInstance;

private Map<String, Notification> mCachedNotifications;
private NotificationsDataSource mLocalSource;
private NotificationsDataSource mRemoteSource;
@NonNull private Map<String, Notification> mCachedNotifications;
@NonNull private NotificationsDataSource mLocalSource;
@NonNull private NotificationsDataSource mRemoteSource;

private boolean mCacheIsDirty;

private NotificationsRepo(@NonNull NotificationsDataSource localSource,
@NonNull NotificationsDataSource remoteSource)
{
mLocalSource = localSource;
mRemoteSource = remoteSource;

mCachedNotifications = new ConcurrentHashMap<>();
}

public static NotificationsRepo getInstance(@NonNull Context context) {
Expand All @@ -46,13 +48,14 @@ public static NotificationsRepo getInstance(@NonNull Context context) {
return mInstance;
}

public static void destroyInstance() {
mInstance = null;
@Override
public boolean hasCachedData() {
return !mCachedNotifications.isEmpty();
}

@Override
public void getNotifications(@NonNull final GetNotificationsCallback callback) {
if(mCachedNotifications != null && !mCacheIsDirty) {
if(!mCachedNotifications.isEmpty() && !mCacheIsDirty) {
callback.onFinished(new ArrayList<>(mCachedNotifications.values()));
}
else if(mCacheIsDirty) {
Expand All @@ -79,7 +82,6 @@ public void deleteAllNotifications() {
mLocalSource.deleteAllNotifications();
mRemoteSource.deleteAllNotifications();

mCachedNotifications = checkNotNull(mCachedNotifications);
mCachedNotifications.clear();
}

Expand Down Expand Up @@ -116,7 +118,6 @@ public void onError(Object error) {

private void reloadCachedNotifications(@NonNull List<Notification> notifications) {

mCachedNotifications = checkNotNull(mCachedNotifications);
mCachedNotifications.clear();

for(Notification notification : notifications) {
Expand All @@ -136,16 +137,6 @@ private void reloadLocalNotifications(@NonNull List<Notification> notifications)
}

private void cacheNotification(Notification notification) {
mCachedNotifications = checkNotNull(mCachedNotifications);
mCachedNotifications.put(notification.getNotificationId(), notification);
}

private Map<String, Notification> checkNotNull(@Nullable Map<String, Notification> map)
{
if(map == null) {
map = new LinkedHashMap<>();
}

return map;
}
}
Loading

0 comments on commit bb85cdf

Please sign in to comment.