From fd515deeb2477f43515d9ace0a9bb5928cd7d56a Mon Sep 17 00:00:00 2001 From: neslihanturan Date: Tue, 21 May 2019 13:36:38 +0300 Subject: [PATCH] AddnetworkBroadcatsReceiver on view side, call it from presenter --- .../NearbyParentFragmentContract.java | 1 + .../mvp/fragments/NearbyParentFragment.java | 59 +++++++++++++++++++ .../NearbyParentFragmentPresenter.java | 8 ++- 3 files changed, 65 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/mvp/contract/NearbyParentFragmentContract.java b/app/src/main/java/fr/free/nrw/commons/nearby/mvp/contract/NearbyParentFragmentContract.java index 7565915e1..98c390113 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/mvp/contract/NearbyParentFragmentContract.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/mvp/contract/NearbyParentFragmentContract.java @@ -14,6 +14,7 @@ public interface NearbyParentFragmentContract { void checkGps(LocationServiceManager locationServiceManager); void checkLocationPermission(LocationServiceManager locationServiceManager); boolean isNetworkConnectionEstablished(); + void addNetworkBroadcastReceiver(); } interface UserActions { diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/mvp/fragments/NearbyParentFragment.java b/app/src/main/java/fr/free/nrw/commons/nearby/mvp/fragments/NearbyParentFragment.java index e02405d3c..f79aa1ce7 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/mvp/fragments/NearbyParentFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/mvp/fragments/NearbyParentFragment.java @@ -1,6 +1,9 @@ package fr.free.nrw.commons.nearby.mvp.fragments; +import android.content.BroadcastReceiver; +import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.os.Build; import android.os.Bundle; import android.view.LayoutInflater; @@ -10,6 +13,7 @@ import android.widget.FrameLayout; import android.widget.LinearLayout; import android.widget.ProgressBar; +import com.google.android.material.snackbar.Snackbar; import com.google.gson.Gson; import javax.inject.Inject; @@ -29,6 +33,7 @@ import fr.free.nrw.commons.location.LocationUpdateListener; import fr.free.nrw.commons.nearby.NearbyController; import fr.free.nrw.commons.nearby.mvp.contract.NearbyParentFragmentContract; import fr.free.nrw.commons.nearby.mvp.presenter.NearbyParentFragmentPresenter; +import fr.free.nrw.commons.utils.FragmentUtils; import fr.free.nrw.commons.utils.NetworkUtils; import fr.free.nrw.commons.wikidata.WikidataEditListener; import timber.log.Timber; @@ -72,6 +77,14 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment private static final String TAG_RETAINED_LIST_FRAGMENT = NearbyListFragment.class.getSimpleName(); private NearbyParentFragmentPresenter nearbyParentFragmentPresenter; + // Variables for adding network broadcast receiver. + private Snackbar snackbar; + private final String NETWORK_INTENT_ACTION = "android.net.conn.CONNECTIVITY_CHANGE"; + private BroadcastReceiver broadcastReceiver; + private boolean isNetworkErrorOccurred = false; + public View view; + + @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -83,6 +96,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_nearby, container, false); ButterKnife.bind(this, view); + this.view = view; return view; } @@ -293,4 +307,49 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment public boolean isNetworkConnectionEstablished() { return NetworkUtils.isInternetConnectionEstablished(getActivity()); } + + /** + * Adds network broadcast receiver to recognize connection established + */ + @Override + public void addNetworkBroadcastReceiver() { + if (!FragmentUtils.isFragmentUIActive(this)) { + return; + } + + if (broadcastReceiver != null) { + return; + } + + IntentFilter intentFilter = new IntentFilter(NETWORK_INTENT_ACTION); + + broadcastReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + if (getActivity() != null) { + if (NetworkUtils.isInternetConnectionEstablished(getActivity())) { + if (isNetworkErrorOccurred) { + nearbyParentFragmentPresenter.updateMapAndList(LOCATION_SIGNIFICANTLY_CHANGED); + isNetworkErrorOccurred = false; + } + + if (snackbar != null) { + snackbar.dismiss(); + snackbar = null; + } + } else { + if (snackbar == null) { + snackbar = Snackbar.make(view, R.string.no_internet, Snackbar.LENGTH_INDEFINITE); + // TODO make search this area button invisible + } + + isNetworkErrorOccurred = true; + snackbar.show(); + } + } + } + }; + + getActivity().registerReceiver(broadcastReceiver, intentFilter); + } } diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/mvp/presenter/NearbyParentFragmentPresenter.java b/app/src/main/java/fr/free/nrw/commons/nearby/mvp/presenter/NearbyParentFragmentPresenter.java index 94f7ade3f..aba35a44b 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/mvp/presenter/NearbyParentFragmentPresenter.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/mvp/presenter/NearbyParentFragmentPresenter.java @@ -12,7 +12,6 @@ import timber.log.Timber; import static fr.free.nrw.commons.location.LocationServiceManager.LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED; import static fr.free.nrw.commons.location.LocationServiceManager.LocationChangeType.MAP_UPDATED; -import static fr.free.nrw.commons.location.LocationServiceManager.LocationChangeType.PERMISSION_JUST_GRANTED; public class NearbyParentFragmentPresenter implements NearbyParentFragmentContract.UserActions, @@ -72,8 +71,11 @@ public class NearbyParentFragmentPresenter */ @Override public void initializeNearbyOperations() { - locationManager.addLocationListener(this); - nearbyParentFragmentView.registerLocationUpdates(locationManager); + locationManager.addLocationListener(this); + nearbyParentFragmentView.registerLocationUpdates(locationManager); + lockNearby(false); + nearbyParentFragmentView.checkGps(locationManager); + nearbyParentFragmentView.addNetworkBroadcastReceiver(); } /**