Use a singleton to access the presenter, instead of passing it to a variable inside fragment

This commit is contained in:
neslihanturan 2019-10-01 13:07:23 +03:00
parent d0310b35b7
commit 2d64f4ba5e
3 changed files with 27 additions and 6 deletions

View file

@ -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();
}

View file

@ -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();

View file

@ -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