1
1
package fr .free .nrw .commons .explore ;
2
2
3
+ import static androidx .viewpager .widget .ViewPager .SCROLL_STATE_IDLE ;
4
+
3
5
import android .os .Bundle ;
4
6
import android .view .LayoutInflater ;
5
7
import android .view .Menu ;
@@ -42,9 +44,13 @@ public class ExploreFragment extends CommonsDaggerSupportFragment {
42
44
@ Named ("default_preferences" )
43
45
public JsonKvStore applicationKvStore ;
44
46
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 ) {
48
54
binding .viewPager .setCanScroll (canScroll );
49
55
}
50
56
}
@@ -60,6 +66,7 @@ public static ExploreFragment newInstance() {
60
66
public View onCreateView (LayoutInflater inflater , @ Nullable ViewGroup container ,
61
67
@ Nullable Bundle savedInstanceState ) {
62
68
super .onCreate (savedInstanceState );
69
+ loadNearbyMapData ();
63
70
binding = FragmentExploreBinding .inflate (inflater , container , false );
64
71
65
72
viewPagerAdapter = new ViewPagerAdapter (getChildFragmentManager ());
@@ -89,6 +96,11 @@ public void onPageScrollStateChanged(int state) {
89
96
});
90
97
setTabs ();
91
98
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
+ }
92
104
return binding .getRoot ();
93
105
}
94
106
@@ -108,6 +120,13 @@ public void setTabs() {
108
120
Bundle mapArguments = new Bundle ();
109
121
mapArguments .putString ("categoryName" , EXPLORE_MAP );
110
122
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
+
111
130
featuredRootFragment = new ExploreListRootFragment (featuredArguments );
112
131
mobileRootFragment = new ExploreListRootFragment (mobileArguments );
113
132
mapRootFragment = new ExploreMapRootFragment (mapArguments );
@@ -120,13 +139,35 @@ public void setTabs() {
120
139
fragmentList .add (mapRootFragment );
121
140
titleList .add (getString (R .string .explore_tab_title_map ).toUpperCase (Locale .ROOT ));
122
141
123
- ((MainActivity )getActivity ()).showTabs ();
142
+ ((MainActivity ) getActivity ()).showTabs ();
124
143
((BaseActivity ) getActivity ()).getSupportActionBar ().setDisplayHomeAsUpEnabled (false );
125
144
126
145
viewPagerAdapter .setTabData (fragmentList , titleList );
127
146
viewPagerAdapter .notifyDataSetChanged ();
128
147
}
129
148
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
+
130
171
public boolean onBackPressed () {
131
172
if (binding .tabLayout .getSelectedTabPosition () == 0 ) {
132
173
if (featuredRootFragment .backPressed ()) {
@@ -155,7 +196,38 @@ public boolean onBackPressed() {
155
196
*/
156
197
@ Override
157
198
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
+ }
159
231
super .onCreateOptionsMenu (menu , inflater );
160
232
}
161
233
@@ -171,6 +243,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
171
243
case R .id .action_search :
172
244
ActivityUtils .startActivityWithFlags (getActivity (), SearchActivity .class );
173
245
return true ;
246
+ case R .id .list_item_show_in_nearby :
247
+ mapRootFragment .loadNearbyMapFromExplore ();
248
+ return true ;
174
249
default :
175
250
return super .onOptionsItemSelected (item );
176
251
}
0 commit comments