From 2d64f4ba5e1bc3570cf49840d21b1487950cfd75 Mon Sep 17 00:00:00 2001 From: neslihanturan Date: Tue, 1 Oct 2019 13:07:23 +0300 Subject: [PATCH] Use a singleton to access the presenter, instead of passing it to a variable inside fragment --- .../commons/contributions/MainActivity.java | 5 ++-- .../mvp/fragments/NearbyParentFragment.java | 4 ++-- .../NearbyParentFragmentPresenter.java | 24 +++++++++++++++++-- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/contributions/MainActivity.java b/app/src/main/java/fr/free/nrw/commons/contributions/MainActivity.java index a698a2d83..4ee5a005c 100644 --- a/app/src/main/java/fr/free/nrw/commons/contributions/MainActivity.java +++ b/app/src/main/java/fr/free/nrw/commons/contributions/MainActivity.java @@ -33,6 +33,7 @@ import fr.free.nrw.commons.auth.SessionManager; import fr.free.nrw.commons.location.LocationServiceManager; import fr.free.nrw.commons.nearby.NearbyNotificationCardView; import fr.free.nrw.commons.nearby.mvp.fragments.NearbyParentFragment; +import fr.free.nrw.commons.nearby.mvp.presenter.NearbyParentFragmentPresenter; import fr.free.nrw.commons.notification.Notification; import fr.free.nrw.commons.notification.NotificationActivity; import fr.free.nrw.commons.notification.NotificationController; @@ -172,7 +173,7 @@ public class MainActivity extends AuthenticatedActivity implements FragmentManag isContributionsFragmentVisible = false; updateMenuItem(); // Do all permission and GPS related tasks on tab selected, not on create - ((NearbyParentFragment)contributionsActivityPagerAdapter.getItem(1)).nearbyParentFragmentPresenter.onTabSelected(); + NearbyParentFragmentPresenter.getInstance().onTabSelected(); break; default: tabLayout.getTabAt(CONTRIBUTIONS_TAB_POSITION).select(); @@ -254,7 +255,7 @@ public class MainActivity extends AuthenticatedActivity implements FragmentManag } else if (getSupportFragmentManager().findFragmentByTag(nearbyFragmentTag) != null && !isContributionsFragmentVisible) { // Means that nearby fragment is visible (not contributions fragment) NearbyParentFragment nearbyFragment = (NearbyParentFragment) contributionsActivityPagerAdapter.getItem(1); - nearbyFragment.nearbyParentFragmentPresenter.backButtonClicked(); + NearbyParentFragmentPresenter.getInstance().backButtonClicked(); } else { super.onBackPressed(); } 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 27472174d..7a4d743b0 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 @@ -126,7 +126,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment private boolean isNetworkErrorOccurred = false; private Snackbar snackbar; private View view; - public NearbyParentFragmentPresenter nearbyParentFragmentPresenter; + private NearbyParentFragmentPresenter nearbyParentFragmentPresenter; private boolean isDarkTheme; private boolean isFABsExpanded; private Marker selectedMarker; @@ -355,7 +355,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment */ private void childMapFragmentAttached() { Timber.d("Child map fragment attached"); - nearbyParentFragmentPresenter = new NearbyParentFragmentPresenter + nearbyParentFragmentPresenter = NearbyParentFragmentPresenter.getInstance (nearbyListFragment,this, nearbyMapFragment, locationManager); nearbyParentFragmentPresenter.nearbyFragmentsAreReady(); initViews(); 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 5d84f49cf..9dae44af1 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 @@ -45,10 +45,11 @@ public class NearbyParentFragmentPresenter private boolean isPortraitMode; private boolean placesLoadedOnce; - private LocationServiceManager locationServiceManager; - public NearbyParentFragmentPresenter(NearbyParentFragmentContract.NearbyListView nearbyListFragmentView, + public static NearbyParentFragmentPresenter presenterInstance; + + private NearbyParentFragmentPresenter(NearbyParentFragmentContract.NearbyListView nearbyListFragmentView, NearbyParentFragmentContract.View nearbyParentFragmentView, NearbyMapContract.View nearbyMapFragmentView, LocationServiceManager locationServiceManager) { @@ -59,6 +60,25 @@ public class NearbyParentFragmentPresenter this.locationServiceManager = locationServiceManager; } + // static method to create instance of Singleton class + public static NearbyParentFragmentPresenter getInstance(NearbyParentFragmentContract.NearbyListView nearbyListFragmentView, + NearbyParentFragmentContract.View nearbyParentFragmentView, + NearbyMapContract.View nearbyMapFragmentView, + LocationServiceManager locationServiceManager) { + if (presenterInstance == null) { + presenterInstance = new NearbyParentFragmentPresenter(nearbyListFragmentView, + nearbyParentFragmentView, + nearbyMapFragmentView, + locationServiceManager); + } + return presenterInstance; + } + + // We call this method when we are sure presenterInstance is not null + public static NearbyParentFragmentPresenter getInstance() { + return presenterInstance; + } + /** * Note: To initialize nearby operations both views should be ready and tab is selected. * Initializes nearby operations if nearby views are ready