Skip to content
This repository has been archived by the owner on Oct 9, 2020. It is now read-only.

Commit

Permalink
fix: Use direct network checks instead of callbacks (#2451)
Browse files Browse the repository at this point in the history
  • Loading branch information
simarsingh24 authored and iamareebjamal committed May 13, 2018
1 parent 67de337 commit 2af02ba
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.fossasia.openevent.core.auth.LoginActivity;
import org.fossasia.openevent.data.DiscountCode;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -118,24 +117,17 @@ public void onDiscountCodeDownloadDone(boolean status) {
}

private void refresh() {
NetworkUtils.checkConnection(new WeakReference<>(getContext()), new NetworkUtils.NetworkStateReceiverListener() {

@Override
public void networkAvailable() {
if (AuthUtil.isUserLoggedIn()) {
downloadDiscountCodes();
} else {
redirectToLogin();
if (swipeRefreshLayout != null)
swipeRefreshLayout.setRefreshing(false);
}
}

@Override
public void networkUnavailable() {
onDiscountCodeDownloadDone(false);
if (NetworkUtils.haveNetworkConnection(getContext())) {
if (AuthUtil.isUserLoggedIn()) {
downloadDiscountCodes();
} else {
redirectToLogin();
if (swipeRefreshLayout != null)
swipeRefreshLayout.setRefreshing(false);
}
});
} else {
onDiscountCodeDownloadDone(false);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.fossasia.openevent.core.auth.LoginActivity;
import org.fossasia.openevent.data.FAQ;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

import butterknife.BindView;
Expand Down Expand Up @@ -99,20 +98,12 @@ private void onDownloadResponse(boolean faqDownloadResult) {
}

private void refresh() {
NetworkUtils.checkConnection(new WeakReference<>(getContext()), new NetworkUtils.NetworkStateReceiverListener() {

@Override
public void networkAvailable() {
downloadFAQS();
swipeRefreshLayout.setRefreshing(false);
}

@Override
public void networkUnavailable() {
onDownloadResponse(false);
}

});
if (NetworkUtils.haveNetworkConnection(getContext())) {
downloadFAQS();
swipeRefreshLayout.setRefreshing(false);
} else {
onDownloadResponse(false);
}
}

private void handleVisibility() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.fossasia.openevent.core.feed.facebook.api.Feed;
import org.fossasia.openevent.core.feed.facebook.api.FeedItem;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -96,34 +95,27 @@ private void downloadFeed() {
}

private void refresh() {
NetworkUtils.checkConnection(new WeakReference<>(getContext()), new NetworkUtils.NetworkStateReceiverListener() {

@Override
public void networkAvailable() {
if (SharedPreferencesUtil.getString(ConstantStrings.FACEBOOK_PAGE_ID, null) == null) {
facebookFeedFragmentViewModel.getFBPageID(getResources().getString(R.string.facebook_access_token))
.observe(FacebookFeedFragment.this, facebookPageIdResource -> {
if (facebookPageIdResource == null) return;
if (facebookPageIdResource.getStatus() == Resource.Status.SUCCESS) {
SharedPreferencesUtil.putString(ConstantStrings.FACEBOOK_PAGE_ID, facebookPageIdResource.getData().getId());
downloadFeed();
} else {
Timber.e(facebookPageIdResource.getMessage());
showRetrySnackbar(R.string.refresh_failed);
swipeRefreshLayout.setRefreshing(false);
}
});
} else {
downloadFeed();
}
if (NetworkUtils.haveNetworkConnection(getContext())) {
if (SharedPreferencesUtil.getString(ConstantStrings.FACEBOOK_PAGE_ID, null) == null) {
facebookFeedFragmentViewModel.getFBPageID(getResources().getString(R.string.facebook_access_token))
.observe(FacebookFeedFragment.this, facebookPageIdResource -> {
if (facebookPageIdResource == null) return;
if (facebookPageIdResource.getStatus() == Resource.Status.SUCCESS) {
SharedPreferencesUtil.putString(ConstantStrings.FACEBOOK_PAGE_ID, facebookPageIdResource.getData().getId());
downloadFeed();
} else {
Timber.e(facebookPageIdResource.getMessage());
showRetrySnackbar(R.string.refresh_failed);
swipeRefreshLayout.setRefreshing(false);
}
});
} else {
downloadFeed();
}

@Override
public void networkUnavailable() {
swipeRefreshLayout.setRefreshing(false);
showRetrySnackbar(R.string.no_internet_connection);
}
});
} else {
swipeRefreshLayout.setRefreshing(false);
showRetrySnackbar(R.string.no_internet_connection);
}
}

private void showRetrySnackbar(@StringRes int resId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.fossasia.openevent.config.StrategyRegistry;
import org.fossasia.openevent.data.Microlocation;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -203,18 +202,10 @@ public void onLocationsDownloadDone(MicrolocationDownloadEvent event) {
}

private void refresh() {
NetworkUtils.checkConnection(new WeakReference<>(context), new NetworkUtils.NetworkStateReceiverListener() {

@Override
public void networkAvailable() {
// Network is available
DataDownloadManager.getInstance().downloadMicrolocations();
}

@Override
public void networkUnavailable() {
StrategyRegistry.getInstance().getEventBusStrategy().getEventBus().post(new MicrolocationDownloadEvent(false));
}
});
if (NetworkUtils.haveNetworkConnection(getContext())) {
DataDownloadManager.getInstance().downloadMicrolocations();
} else {
StrategyRegistry.getInstance().getEventBusStrategy().getEventBus().post(new MicrolocationDownloadEvent(false));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.fossasia.openevent.core.auth.LoginActivity;
import org.fossasia.openevent.data.Notification;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -126,25 +125,17 @@ public void onNotificationsDownloadDone(boolean status) {
}

private void refresh() {
NetworkUtils.checkConnection(new WeakReference<>(getContext()), new NetworkUtils.NetworkStateReceiverListener() {

@Override
public void networkAvailable() {
// Network is available
if (AuthUtil.isUserLoggedIn()) {
downloadNotifications();
} else {
redirectToLogin();
if (swipeRefreshLayout != null)
swipeRefreshLayout.setRefreshing(false);
}
}

@Override
public void networkUnavailable() {
onNotificationsDownloadDone(false);
if (NetworkUtils.haveNetworkConnection(getContext())) {
if (AuthUtil.isUserLoggedIn()) {
downloadNotifications();
} else {
redirectToLogin();
if (swipeRefreshLayout != null)
swipeRefreshLayout.setRefreshing(false);
}
});
} else {
onNotificationsDownloadDone(false);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.fossasia.openevent.config.StrategyRegistry;
import org.fossasia.openevent.core.bookmark.OnBookmarkSelectedListener;
import org.fossasia.openevent.data.Session;
import java.lang.ref.WeakReference;

import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -209,19 +209,11 @@ public boolean onQueryTextSubmit(String query) {
}

private void refresh() {
NetworkUtils.checkConnection(new WeakReference<>(context), new NetworkUtils.NetworkStateReceiverListener() {

@Override
public void networkAvailable() {
// Network is available
DataDownloadManager.getInstance().downloadSession();
}

@Override
public void networkUnavailable() {
StrategyRegistry.getInstance().getEventBusStrategy().getEventBus().post(new SessionDownloadEvent(false));
}
});
if (NetworkUtils.haveNetworkConnection(getContext())) {
DataDownloadManager.getInstance().downloadSession();
} else {
StrategyRegistry.getInstance().getEventBusStrategy().getEventBus().post(new SessionDownloadEvent(false));
}
}

public void setOnBookmarkSelectedListener(OnBookmarkSelectedListener onBookmarkSelectedListener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.fossasia.openevent.config.StrategyRegistry;
import org.fossasia.openevent.data.Speaker;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -214,19 +213,11 @@ public void speakerDownloadDone(SpeakerDownloadEvent event) {
}

private void refresh() {
NetworkUtils.checkConnection(new WeakReference<>(getContext()), new NetworkUtils.NetworkStateReceiverListener() {

@Override
public void networkAvailable() {
// Network is available
DataDownloadManager.getInstance().downloadSpeakers();
}

@Override
public void networkUnavailable() {
StrategyRegistry.getInstance().getEventBusStrategy().getEventBus().post(new SpeakerDownloadEvent(false));
}
});
if (NetworkUtils.haveNetworkConnection(getContext())) {
DataDownloadManager.getInstance().downloadSpeakers();
} else {
StrategyRegistry.getInstance().getEventBusStrategy().getEventBus().post(new SpeakerDownloadEvent(false));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.fossasia.openevent.config.StrategyRegistry;
import org.fossasia.openevent.data.Sponsor;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -131,22 +130,11 @@ public void sponsorDownloadDone(SponsorDownloadEvent event) {
}

private void refresh() {
NetworkUtils.checkConnection(new WeakReference<>(getContext()), new NetworkUtils.NetworkStateReceiverListener() {

@Override
public void networkAvailable() {
// Network is available
DataDownloadManager.getInstance().downloadSponsors();
}

@Override
public void networkUnavailable() {
StrategyRegistry.getInstance()
.getEventBusStrategy()
.getEventBus()
.post(new SponsorDownloadEvent(true));
}
});
if (NetworkUtils.haveNetworkConnection(getContext())) {
DataDownloadManager.getInstance().downloadSponsors();
} else {
StrategyRegistry.getInstance().getEventBusStrategy().getEventBus().post(new SponsorDownloadEvent(false));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.fossasia.openevent.config.StrategyRegistry;
import org.fossasia.openevent.data.Track;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -196,19 +195,11 @@ public void onTrackDownloadDone(TracksDownloadEvent event) {
}

private void refresh() {
NetworkUtils.checkConnection(new WeakReference<>(context), new NetworkUtils.NetworkStateReceiverListener() {

@Override
public void networkAvailable() {
// Network is available
DataDownloadManager.getInstance().downloadTracks();
}

@Override
public void networkUnavailable() {
StrategyRegistry.getInstance().getEventBusStrategy().getEventBus().post(new TracksDownloadEvent(false));
}
});
if (NetworkUtils.haveNetworkConnection(getContext())) {
DataDownloadManager.getInstance().downloadTracks();
} else {
StrategyRegistry.getInstance().getEventBusStrategy().getEventBus().post(new TracksDownloadEvent(false));
}
}

}

0 comments on commit 2af02ba

Please sign in to comment.