Make larger methods smaller

This commit is contained in:
neslihanturan 2019-10-01 14:23:17 +03:00
parent 7e40d94dd0
commit 25a88f32f9

View file

@ -161,16 +161,27 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
private void initViews() { private void initViews() {
Timber.d("init views called"); Timber.d("init views called");
initBottomSheets();
loadAnimations();
setBottomSheetCallbacks();
decideButtonVisibilities();
addActionToTitle();
}
/**
* Creates bottom sheet behaviours from bottom sheets, sets initial states and visibility
*/
private void initBottomSheets() {
bottomSheetListBehavior = BottomSheetBehavior.from(bottomSheetList); bottomSheetListBehavior = BottomSheetBehavior.from(bottomSheetList);
bottomSheetDetailsBehavior = BottomSheetBehavior.from(bottomSheetDetails); bottomSheetDetailsBehavior = BottomSheetBehavior.from(bottomSheetDetails);
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
bottomSheetDetails.setVisibility(View.VISIBLE); bottomSheetDetails.setVisibility(View.VISIBLE);
}
fab_open = AnimationUtils.loadAnimation(getActivity(), R.anim.fab_open); /**
fab_close = AnimationUtils.loadAnimation(getActivity(), R.anim.fab_close); * Defines how bottom sheets will act on click
rotate_forward = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_forward); */
rotate_backward = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_backward); private void setBottomSheetCallbacks() {
bottomSheetDetailsBehavior.setBottomSheetCallback(new BottomSheetBehavior bottomSheetDetailsBehavior.setBottomSheetCallback(new BottomSheetBehavior
.BottomSheetCallback() { .BottomSheetCallback() {
@Override @Override
@ -210,7 +221,22 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
} }
}); });
}
/**
* Loads animations will be used for FABs
*/
private void loadAnimations() {
fab_open = AnimationUtils.loadAnimation(getActivity(), R.anim.fab_open);
fab_close = AnimationUtils.loadAnimation(getActivity(), R.anim.fab_close);
rotate_forward = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_forward);
rotate_backward = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_backward);
}
/**
* Fits buttons according to our layout
*/
private void decideButtonVisibilities() {
// Remove button text if they exceed 1 line or if internal layout has not been built // Remove button text if they exceed 1 line or if internal layout has not been built
// Only need to check for directions button because it is the longest // Only need to check for directions button because it is the longest
if (directionsButtonText.getLineCount() > 1 || directionsButtonText.getLineCount() == 0) { if (directionsButtonText.getLineCount() > 1 || directionsButtonText.getLineCount() == 0) {
@ -219,15 +245,15 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
commonsButtonText.setVisibility(View.GONE); commonsButtonText.setVisibility(View.GONE);
directionsButtonText.setVisibility(View.GONE); directionsButtonText.setVisibility(View.GONE);
} }
}
title.setOnLongClickListener(view -> { /**
Utils.copy("place", title.getText().toString(), getContext()); *
Toast.makeText(getContext(), "Text copied to clipboard", Toast.LENGTH_SHORT).show(); */
return true; private void addActionToTitle() {
}
);
title.setOnClickListener(view -> { title.setOnClickListener(view -> {
Utils.copy("place", title.getText().toString(), getContext());
Toast.makeText(getContext(), "Text copied to clipboard", Toast.LENGTH_SHORT).show();
if (bottomSheetDetailsBehavior.getState() == BottomSheetBehavior.STATE_COLLAPSED) { if (bottomSheetDetailsBehavior.getState() == BottomSheetBehavior.STATE_COLLAPSED) {
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
} else { } else {
@ -435,41 +461,47 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
} }
} }
@Override @Override
public void populatePlaces(fr.free.nrw.commons.location.LatLng curlatLng, fr.free.nrw.commons.location.LatLng searchLatLng) { public void populatePlaces(fr.free.nrw.commons.location.LatLng curlatLng, fr.free.nrw.commons.location.LatLng searchLatLng) {
boolean checkingAroundCurrentLocation;
if (curlatLng.equals(searchLatLng)) { // Means we are checking around current location if (curlatLng.equals(searchLatLng)) { // Means we are checking around current location
checkingAroundCurrentLocation = true; populatePlacesForCurrentLocation(curlatLng, searchLatLng);
compositeDisposable.add(Observable.fromCallable(() -> nearbyController
.loadAttractionsFromLocation(curlatLng, searchLatLng, false, checkingAroundCurrentLocation))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::updateMapMarkers,
throwable -> {
Timber.d(throwable);
// TODO: find out why NPE occurs here
// showErrorMessage(getString(R.string.error_fetching_nearby_places));
setProgressBarVisibility(false);
nearbyParentFragmentPresenter.lockUnlockNearby(false);
}));
} else { } else {
checkingAroundCurrentLocation = false; populatePlacesForAnotherLocation(curlatLng, searchLatLng);
compositeDisposable.add(Observable.fromCallable(() -> nearbyController
.loadAttractionsFromLocation(curlatLng, searchLatLng, false, checkingAroundCurrentLocation))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::updateMapMarkersForCustomLocation,
throwable -> {
Timber.d(throwable);
// TODO: find out why NPE occurs here
// showErrorMessage(getString(R.string.error_fetching_nearby_places));
setProgressBarVisibility(false);
nearbyParentFragmentPresenter.lockUnlockNearby(false);
}));
} }
} }
private void populatePlacesForCurrentLocation(fr.free.nrw.commons.location.LatLng curlatLng,
fr.free.nrw.commons.location.LatLng searchLatLng) {
compositeDisposable.add(Observable.fromCallable(() -> nearbyController
.loadAttractionsFromLocation(curlatLng, searchLatLng, false, true))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::updateMapMarkers,
throwable -> {
Timber.d(throwable);
// TODO: find out why NPE occurs here
// showErrorMessage(getString(R.string.error_fetching_nearby_places));
setProgressBarVisibility(false);
nearbyParentFragmentPresenter.lockUnlockNearby(false);
}));
}
private void populatePlacesForAnotherLocation(fr.free.nrw.commons.location.LatLng curlatLng,
fr.free.nrw.commons.location.LatLng searchLatLng) {
compositeDisposable.add(Observable.fromCallable(() -> nearbyController
.loadAttractionsFromLocation(curlatLng, searchLatLng, false, false))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::updateMapMarkersForCustomLocation,
throwable -> {
Timber.d(throwable);
// TODO: find out why NPE occurs here
// showErrorMessage(getString(R.string.error_fetching_nearby_places));
setProgressBarVisibility(false);
nearbyParentFragmentPresenter.lockUnlockNearby(false);
}));
}
/** /**
* Populates places for your location, should be used for finding nearby places around a * Populates places for your location, should be used for finding nearby places around a
* location where you are. * location where you are.