Skip to content

Commit 70bbf1c

Browse files
andy-ifenicolas-raoul
authored andcommitted
Feat: Make it smoother to switch between nearby and explore maps (commons-app#6164)
* Nearby: Add 'Show in Explore' 3-dots menu item * MainActivity: Add methods to pass extras between Nearby and Explore * MainActivity: Extend loadFragment() to support passing fragment arguments * Nearby: Add ability to navigate to Explore fragment on 'Show in Explore' click * Explore: Read fragment arguments for Nearby map data and update Explore map if present * Explore: Add 'Show in Nearby' 3-dots menu item. Only visible when Map tab is selected * Explore: On 'Show in Nearby' click, navigate to Nearby fragment, passing map data as fragment args * Nearby: Read fragment arguments for Explore map data and update Nearby map if present * MainActivity: Fix memory leaks when navigating between bottom nav destinations * Explore: Fix crashes caused by unattached map fragment * Refactor code to pass unit tests * Explore: Format javadocs --------- Co-authored-by: Nicolas Raoul <nicolas.raoul@gmail.com>
1 parent 8eb3152 commit 70bbf1c

File tree

9 files changed

+445
-151
lines changed

9 files changed

+445
-151
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,5 @@ captures/*
4646

4747
# Test and other output
4848
app/jacoco.exec
49-
app/CommonsContributions
49+
app/CommonsContributions
50+
app/.*

app/src/main/java/fr/free/nrw/commons/explore/ExploreFragment.java

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package fr.free.nrw.commons.explore;
22

3+
import static androidx.viewpager.widget.ViewPager.SCROLL_STATE_IDLE;
4+
35
import android.os.Bundle;
46
import android.view.LayoutInflater;
57
import android.view.Menu;
@@ -42,9 +44,13 @@ public class ExploreFragment extends CommonsDaggerSupportFragment {
4244
@Named("default_preferences")
4345
public JsonKvStore applicationKvStore;
4446

45-
public void setScroll(boolean canScroll){
46-
if (binding != null)
47-
{
47+
// Nearby map state (for if we came from Nearby fragment)
48+
private double prevZoom;
49+
private double prevLatitude;
50+
private double prevLongitude;
51+
52+
public void setScroll(boolean canScroll) {
53+
if (binding != null) {
4854
binding.viewPager.setCanScroll(canScroll);
4955
}
5056
}
@@ -60,6 +66,7 @@ public static ExploreFragment newInstance() {
6066
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
6167
@Nullable Bundle savedInstanceState) {
6268
super.onCreate(savedInstanceState);
69+
loadNearbyMapData();
6370
binding = FragmentExploreBinding.inflate(inflater, container, false);
6471

6572
viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager());
@@ -89,6 +96,11 @@ public void onPageScrollStateChanged(int state) {
8996
});
9097
setTabs();
9198
setHasOptionsMenu(true);
99+
100+
// if we came from 'Show in Explore' in Nearby, jump to Map tab
101+
if (isCameFromNearbyMap()) {
102+
binding.viewPager.setCurrentItem(2);
103+
}
92104
return binding.getRoot();
93105
}
94106

@@ -108,6 +120,13 @@ public void setTabs() {
108120
Bundle mapArguments = new Bundle();
109121
mapArguments.putString("categoryName", EXPLORE_MAP);
110122

123+
// if we came from 'Show in Explore' in Nearby, pass on zoom and center to Explore map root
124+
if (isCameFromNearbyMap()) {
125+
mapArguments.putDouble("prev_zoom", prevZoom);
126+
mapArguments.putDouble("prev_latitude", prevLatitude);
127+
mapArguments.putDouble("prev_longitude", prevLongitude);
128+
}
129+
111130
featuredRootFragment = new ExploreListRootFragment(featuredArguments);
112131
mobileRootFragment = new ExploreListRootFragment(mobileArguments);
113132
mapRootFragment = new ExploreMapRootFragment(mapArguments);
@@ -120,13 +139,35 @@ public void setTabs() {
120139
fragmentList.add(mapRootFragment);
121140
titleList.add(getString(R.string.explore_tab_title_map).toUpperCase(Locale.ROOT));
122141

123-
((MainActivity)getActivity()).showTabs();
142+
((MainActivity) getActivity()).showTabs();
124143
((BaseActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);
125144

126145
viewPagerAdapter.setTabData(fragmentList, titleList);
127146
viewPagerAdapter.notifyDataSetChanged();
128147
}
129148

149+
/**
150+
* Fetch Nearby map camera data from fragment arguments if any.
151+
*/
152+
public void loadNearbyMapData() {
153+
// get fragment arguments
154+
if (getArguments() != null) {
155+
prevZoom = getArguments().getDouble("prev_zoom");
156+
prevLatitude = getArguments().getDouble("prev_latitude");
157+
prevLongitude = getArguments().getDouble("prev_longitude");
158+
}
159+
}
160+
161+
/**
162+
* Checks if fragment arguments contain data from Nearby map. if present, then the user
163+
* navigated from Nearby using 'Show in Explore'.
164+
*
165+
* @return true if user navigated from Nearby map
166+
**/
167+
public boolean isCameFromNearbyMap() {
168+
return prevZoom != 0.0 || prevLatitude != 0.0 || prevLongitude != 0.0;
169+
}
170+
130171
public boolean onBackPressed() {
131172
if (binding.tabLayout.getSelectedTabPosition() == 0) {
132173
if (featuredRootFragment.backPressed()) {
@@ -155,7 +196,38 @@ public boolean onBackPressed() {
155196
*/
156197
@Override
157198
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
158-
inflater.inflate(R.menu.menu_search, menu);
199+
// if logged in 'Show in Nearby' menu item is visible
200+
if (applicationKvStore.getBoolean("login_skipped") == false) {
201+
inflater.inflate(R.menu.explore_fragment_menu, menu);
202+
203+
MenuItem others = menu.findItem(R.id.list_item_show_in_nearby);
204+
205+
if (binding.viewPager.getCurrentItem() == 2) {
206+
others.setVisible(true);
207+
}
208+
209+
// if on Map tab, show all menu options, else only show search
210+
binding.viewPager.addOnPageChangeListener(new OnPageChangeListener() {
211+
@Override
212+
public void onPageScrolled(int position, float positionOffset,
213+
int positionOffsetPixels) {
214+
}
215+
216+
@Override
217+
public void onPageSelected(int position) {
218+
others.setVisible((position == 2));
219+
}
220+
221+
@Override
222+
public void onPageScrollStateChanged(int state) {
223+
if (state == SCROLL_STATE_IDLE && binding.viewPager.getCurrentItem() == 2) {
224+
onPageSelected(2);
225+
}
226+
}
227+
});
228+
} else {
229+
inflater.inflate(R.menu.menu_search, menu);
230+
}
159231
super.onCreateOptionsMenu(menu, inflater);
160232
}
161233

@@ -171,6 +243,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
171243
case R.id.action_search:
172244
ActivityUtils.startActivityWithFlags(getActivity(), SearchActivity.class);
173245
return true;
246+
case R.id.list_item_show_in_nearby:
247+
mapRootFragment.loadNearbyMapFromExplore();
248+
return true;
174249
default:
175250
return super.onOptionsItemSelected(item);
176251
}

app/src/main/java/fr/free/nrw/commons/explore/ExploreMapRootFragment.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,22 @@ public static ExploreMapRootFragment newInstance() {
3939
}
4040

4141
public ExploreMapRootFragment(Bundle bundle) {
42+
// get fragment arguments
4243
String title = bundle.getString("categoryName");
44+
double zoom = bundle.getDouble("prev_zoom");
45+
double latitude = bundle.getDouble("prev_latitude");
46+
double longitude = bundle.getDouble("prev_longitude");
47+
4348
mapFragment = new ExploreMapFragment();
4449
Bundle featuredArguments = new Bundle();
4550
featuredArguments.putString("categoryName", title);
51+
52+
// if we came from 'Show in Explore' in Nearby, pass on zoom and center
53+
if (zoom != 0.0 || latitude != 0.0 || longitude != 0.0) {
54+
featuredArguments.putDouble("prev_zoom", zoom);
55+
featuredArguments.putDouble("prev_latitude", latitude);
56+
featuredArguments.putDouble("prev_longitude", longitude);
57+
}
4658
mapFragment.setArguments(featuredArguments);
4759
}
4860

@@ -198,7 +210,8 @@ public boolean backPressed() {
198210
((MainActivity) getActivity()).showTabs();
199211
return true;
200212

201-
} if (mapFragment != null && mapFragment.isVisible()) {
213+
}
214+
if (mapFragment != null && mapFragment.isVisible()) {
202215
if (mapFragment.backButtonClicked()) {
203216
// Explore map fragment handled the event no further action required.
204217
return true;
@@ -213,6 +226,10 @@ public boolean backPressed() {
213226
return false;
214227
}
215228

229+
public void loadNearbyMapFromExplore() {
230+
mapFragment.loadNearbyMapFromExplore();
231+
}
232+
216233
@Override
217234
public void onDestroy() {
218235
super.onDestroy();

0 commit comments

Comments
 (0)