1
1
package fr .free .nrw .commons .nearby ;
2
2
3
- import android .content .Context ;
4
3
import android .content .Intent ;
5
4
import android .content .SharedPreferences ;
6
5
import android .content .pm .PackageManager ;
9
8
import android .os .Bundle ;
10
9
import android .preference .PreferenceManager ;
11
10
import android .support .annotation .NonNull ;
11
+ import android .support .design .widget .BottomSheetBehavior ;
12
+ import android .support .design .widget .FloatingActionButton ;
12
13
import android .support .v4 .app .Fragment ;
13
14
import android .support .v4 .app .FragmentTransaction ;
14
15
import android .support .v7 .app .AlertDialog ;
16
+ import android .util .Log ;
15
17
import android .view .Menu ;
16
18
import android .view .MenuInflater ;
17
19
import android .view .MenuItem ;
18
20
import android .view .View ;
21
+ import android .widget .LinearLayout ;
19
22
import android .widget .ProgressBar ;
20
23
import android .widget .Toast ;
21
24
41
44
import io .reactivex .schedulers .Schedulers ;
42
45
import timber .log .Timber ;
43
46
44
- import static fr .free .nrw .commons .location .LocationServiceManager .LOCATION_REQUEST ;
45
-
46
47
47
48
public class NearbyActivity extends NavigationBaseActivity implements LocationUpdateListener {
48
49
49
50
private static final int LOCATION_REQUEST = 1 ;
50
- private static final String MAP_LAST_USED_PREFERENCE = "mapLastUsed" ;
51
51
52
52
@ BindView (R .id .progressBar )
53
53
ProgressBar progressBar ;
54
54
55
+ @ BindView (R .id .bottom_sheet )
56
+ LinearLayout bottomSheet ;
57
+ @ BindView (R .id .bottom_sheet_details )
58
+ LinearLayout bottomSheetDetails ;
59
+ @ BindView (R .id .transparentView )
60
+ View transparentView ;
61
+
55
62
@ Inject
56
63
LocationServiceManager locationManager ;
57
64
@ Inject
58
65
NearbyController nearbyController ;
59
66
60
67
private LatLng curLatLang ;
61
68
private Bundle bundle ;
62
- private SharedPreferences sharedPreferences ;
63
69
private NearbyActivityMode viewMode ;
64
70
private Disposable placesDisposable ;
65
71
private boolean lockNearbyView ; //Determines if the nearby places needs to be refreshed
72
+ private BottomSheetBehavior bottomSheetBehavior ; // Behavior for list bottom sheet
73
+ private BottomSheetBehavior bottomSheetBehaviorForDetails ; // Behavior for details bottom sheet
74
+ private NearbyMapFragment nearbyMapFragment ;
75
+ private static final String TAG_RETAINED_FRAGMENT = "RetainedFragment" ;
66
76
67
77
@ Override
68
78
protected void onCreate (Bundle savedInstanceState ) {
69
79
super .onCreate (savedInstanceState );
70
- sharedPreferences = PreferenceManager .getDefaultSharedPreferences (getApplicationContext ());
71
80
setContentView (R .layout .activity_nearby );
72
81
ButterKnife .bind (this );
82
+ resumeFragment ();
73
83
bundle = new Bundle ();
84
+
85
+ initBottomSheetBehaviour ();
74
86
initDrawer ();
75
- initViewState ();
76
87
}
77
88
78
- private void initViewState () {
79
- if (sharedPreferences .getBoolean (MAP_LAST_USED_PREFERENCE , false )) {
80
- viewMode = NearbyActivityMode .MAP ;
81
- } else {
82
- viewMode = NearbyActivityMode .LIST ;
89
+ private void resumeFragment () {
90
+ // find the retained fragment on activity restarts
91
+ android .support .v4 .app .FragmentManager fm = getSupportFragmentManager ();
92
+ nearbyMapFragment = (NearbyMapFragment ) fm .findFragmentByTag (TAG_RETAINED_FRAGMENT );
93
+
94
+ // create the fragment and data the first time
95
+ if (nearbyMapFragment == null ) {
96
+ // add the fragment
97
+ nearbyMapFragment = new NearbyMapFragment ();
98
+ fm .beginTransaction ().add (nearbyMapFragment , TAG_RETAINED_FRAGMENT ).commit ();
99
+ // load data from a data source or perform any calculation
83
100
}
101
+
102
+ }
103
+
104
+ private void initBottomSheetBehaviour () {
105
+ transparentView .setAlpha (0 );
106
+
107
+ bottomSheet .getLayoutParams ().height = getWindowManager ()
108
+ .getDefaultDisplay ().getHeight () / 16 * 9 ;
109
+ bottomSheetBehavior = BottomSheetBehavior .from (bottomSheet );
110
+ // TODO initProperBottomSheetBehavior();
111
+ bottomSheetBehavior .setBottomSheetCallback (new BottomSheetBehavior .BottomSheetCallback () {
112
+
113
+ @ Override
114
+ public void onStateChanged (View bottomSheet , int newState ) {
115
+ prepareViewsForSheetPosition (newState );
116
+ }
117
+
118
+ @ Override
119
+ public void onSlide (View bottomSheet , float slideOffset ) {
120
+
121
+ }
122
+ });
123
+
124
+ bottomSheetBehavior .setState (BottomSheetBehavior .STATE_HIDDEN );
125
+ bottomSheetBehaviorForDetails = BottomSheetBehavior .from (bottomSheetDetails );
126
+ bottomSheetBehaviorForDetails .setState (BottomSheetBehavior .STATE_HIDDEN );
84
127
}
85
128
86
129
@ Override
87
130
public boolean onCreateOptionsMenu (Menu menu ) {
88
131
MenuInflater inflater = getMenuInflater ();
89
132
inflater .inflate (R .menu .menu_nearby , menu );
90
133
91
- if (viewMode .isMap ()) {
92
- MenuItem item = menu .findItem (R .id .action_toggle_view );
93
- item .setIcon (viewMode .getIcon ());
94
- }
95
-
96
134
return super .onCreateOptionsMenu (menu );
97
135
}
98
136
@@ -104,10 +142,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
104
142
lockNearbyView (false );
105
143
refreshView (true );
106
144
return true ;
107
- case R .id .action_toggle_view :
108
- viewMode = viewMode .toggle ();
109
- item .setIcon (viewMode .getIcon ());
110
- toggleView ();
145
+ case R .id .action_display_list :
146
+ bottomSheetBehaviorForDetails .setState (BottomSheetBehavior .STATE_HIDDEN );
147
+ bottomSheetBehavior .setState (BottomSheetBehavior .STATE_EXPANDED );
111
148
return true ;
112
149
default :
113
150
return super .onOptionsItemSelected (item );
@@ -219,15 +256,6 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
219
256
}
220
257
}
221
258
222
- private void toggleView () {
223
- if (viewMode .isMap ()) {
224
- setMapFragment ();
225
- } else {
226
- setListFragment ();
227
- }
228
- sharedPreferences .edit ().putBoolean (MAP_LAST_USED_PREFERENCE , viewMode .isMap ()).apply ();
229
- }
230
-
231
259
@ Override
232
260
protected void onStart () {
233
261
super .onStart ();
@@ -256,6 +284,21 @@ protected void onResume() {
256
284
checkGps ();
257
285
}
258
286
287
+ @ Override
288
+ public void onPause () {
289
+ super .onPause ();
290
+ // this means that this activity will not be recreated now, user is leaving it
291
+ // or the activity is otherwise finishing
292
+ if (isFinishing ()) {
293
+ android .support .v4 .app .FragmentManager fm = getSupportFragmentManager ();
294
+ // we will not need this fragment anymore, this may also be a good place to signal
295
+ // to the retained fragment object to perform its own cleanup.
296
+ fm .beginTransaction ().remove (nearbyMapFragment ).commit ();
297
+ }
298
+ }
299
+
300
+
301
+
259
302
/**
260
303
* This method should be the single point to load/refresh nearby places
261
304
*
@@ -306,12 +349,8 @@ private void populatePlaces(List<Place> placeList) {
306
349
bundle .putString ("CurLatLng" , gsonCurLatLng );
307
350
308
351
lockNearbyView (true );
309
- // Begin the transaction
310
- if (viewMode .isMap ()) {
311
- setMapFragment ();
312
- } else {
313
- setListFragment ();
314
- }
352
+ setMapFragment ();
353
+ setListFragment ();
315
354
316
355
hideProgressBar ();
317
356
}
@@ -352,12 +391,17 @@ private void setListFragment() {
352
391
FragmentTransaction fragmentTransaction = getSupportFragmentManager ().beginTransaction ();
353
392
Fragment fragment = new NearbyListFragment ();
354
393
fragment .setArguments (bundle );
355
- fragmentTransaction .replace (R .id .container , fragment , fragment .getClass ().getSimpleName ());
394
+ fragmentTransaction .replace (R .id .container_sheet , fragment );
395
+ bottomSheetBehavior .setState (BottomSheetBehavior .STATE_HIDDEN );
356
396
fragmentTransaction .commitAllowingStateLoss ();
357
397
}
358
398
359
399
@ Override
360
400
public void onLocationChanged (LatLng latLng ) {
361
401
refreshView (false );
362
402
}
403
+
404
+ public void prepareViewsForSheetPosition (int bottomSheetState ) {
405
+ // TODO
406
+ }
363
407
}
0 commit comments