Skip to content

Commit 70b399c

Browse files
ecgrebsarahsnow1
authored andcommitted
Handle place picker dialog on configuration change (#334)
* Handle place picker dialog on configuration change * Javadoc * Update support repo version number for caching on Circle CI * Rename `ARG_NAME_TITLE` to `ARG_NAME_MESSAGE` for clarity * Change `title` to `message` in local var
1 parent b774a9b commit 70b399c

File tree

8 files changed

+166
-24
lines changed

8 files changed

+166
-24
lines changed

circle.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ dependencies:
2121
pre:
2222
- if [ ! -e /usr/local/android-sdk-linux/build-tools/25.0.2 ]; then echo y | android update sdk --all --no-ui --filter "build-tools-25.0.2"; fi;
2323
- if [ ! -e /usr/local/android-sdk-linux/platforms/android-25 ]; then echo y | android update sdk --all --no-ui --filter "android-25"; fi;
24-
- if ! $(grep -q "Revision=43.0.0" /usr/local/android-sdk-linux/extras/android/m2repository/source.properties); then echo y | android update sdk --all --no-ui --filter "extra-android-m2repository"; fi;
24+
- if ! $(grep -q "Revision=45.0.0" /usr/local/android-sdk-linux/extras/android/m2repository/source.properties); then echo y | android update sdk --all --no-ui --filter "extra-android-m2repository"; fi;
2525
cache_directories:
2626
- /usr/local/android-sdk-linux/build-tools/25.0.2
2727
- /usr/local/android-sdk-linux/platforms/android-25
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.mapzen.places.api.internal;
2+
3+
import com.mapzen.places.api.R;
4+
5+
import android.app.AlertDialog;
6+
import android.app.Dialog;
7+
import android.app.DialogFragment;
8+
import android.content.DialogInterface;
9+
import android.os.Bundle;
10+
11+
/**
12+
* Confirmation dialog shown to the user when a POI has been selected.
13+
*/
14+
public class PlaceDialogFragment extends DialogFragment implements DialogInterface.OnClickListener {
15+
public static final String TAG = PlaceDialogFragment.class.getSimpleName();
16+
17+
private static final String ARG_NAME_MESSAGE = "message";
18+
19+
private AlertDialog dialog;
20+
private PlaceDialogListener listener;
21+
22+
/**
23+
* Create new instance of the dialog fragment.
24+
*
25+
* @param message text to be displayed in the dialog.
26+
* @return a new instance of the dialog fragment.
27+
*/
28+
public static PlaceDialogFragment newInstance(String message) {
29+
final PlaceDialogFragment fragment = new PlaceDialogFragment();
30+
Bundle args = new Bundle();
31+
args.putString(ARG_NAME_MESSAGE, message);
32+
fragment.setArguments(args);
33+
return fragment;
34+
}
35+
36+
@Override public Dialog onCreateDialog(Bundle savedInstanceState) {
37+
final String message = getArguments().getString(ARG_NAME_MESSAGE);
38+
dialog = new AlertDialog.Builder(getActivity())
39+
.setTitle(R.string.use_this_place)
40+
.setMessage(message)
41+
.setNegativeButton(R.string.change_location, this)
42+
.setPositiveButton(R.string.select, this)
43+
.create();
44+
return dialog;
45+
}
46+
47+
/**
48+
* Update text displayed in the dialog.
49+
*
50+
* @param detail new text to display.
51+
*/
52+
public void setMessage(String detail) {
53+
dialog.setMessage(detail);
54+
getArguments().putString(ARG_NAME_MESSAGE, detail);
55+
}
56+
57+
@Override public void onClick(DialogInterface dialog, int which) {
58+
if (listener == null) {
59+
return;
60+
}
61+
62+
if (which == DialogInterface.BUTTON_POSITIVE) {
63+
listener.onPlaceConfirmed();
64+
} else {
65+
listener.onPlaceDismissed();
66+
}
67+
}
68+
69+
/**
70+
* Set place dialog listener.
71+
*
72+
* @param listener the callbacks to invoke when the dialog is confirmed or dismissed.
73+
*/
74+
public void setListener(PlaceDialogListener listener) {
75+
this.listener = listener;
76+
}
77+
78+
/**
79+
* Dialog listener interface.
80+
*/
81+
public interface PlaceDialogListener {
82+
/**
83+
* Positive button click listener.
84+
*/
85+
void onPlaceConfirmed();
86+
87+
/**
88+
* Negative button click listener.
89+
*/
90+
void onPlaceDismissed();
91+
}
92+
}

mapzen-places-api/src/main/java/com/mapzen/places/api/internal/PlacePickerActivity.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
import com.mapzen.tangram.LngLat;
1515

1616
import android.app.Activity;
17-
import android.app.AlertDialog;
18-
import android.content.DialogInterface;
1917
import android.content.Intent;
2018
import android.graphics.Point;
2119
import android.graphics.PointF;
@@ -30,13 +28,13 @@
3028
*/
3129
public class PlacePickerActivity extends Activity implements
3230
PlacePickerViewController, OnMapReadyCallback, LabelPickListener,
33-
DialogInterface.OnClickListener {
31+
PlaceDialogFragment.PlaceDialogListener {
3432

3533
PlacePickerPresenter presenter;
3634
PlaceAutocompleteView autocompleteView;
3735
MapView mapView;
3836
MapzenMap map;
39-
AlertDialog dialog;
37+
PlaceDialogFragment dialog;
4038
String dialogPlaceId;
4139

4240
private boolean mapInitComplete = false;
@@ -61,6 +59,12 @@ public class PlacePickerActivity extends Activity implements
6159

6260
mapView = (MapView) findViewById(R.id.mz_map_view);
6361
mapView.getMapAsync(this);
62+
63+
final PlaceDialogFragment fragment =
64+
(PlaceDialogFragment) getFragmentManager().findFragmentByTag(PlaceDialogFragment.TAG);
65+
if (fragment != null) {
66+
fragment.setListener(this);
67+
}
6468
}
6569

6670
@Override protected void onPause() {
@@ -101,14 +105,10 @@ public class PlacePickerActivity extends Activity implements
101105
}
102106

103107
@Override public void showDialog(String id, String title) {
104-
dialog = new AlertDialog.Builder(this)
105-
.setTitle(R.string.use_this_place)
106-
.setMessage(title)
107-
.setNegativeButton(R.string.change_location, this)
108-
.setPositiveButton(R.string.select, this)
109-
.create();
110-
dialog.show();
108+
dialog = PlaceDialogFragment.newInstance(title);
109+
dialog.show(getFragmentManager(), PlaceDialogFragment.TAG);
111110
dialogPlaceId = id;
111+
dialog.setListener(this);
112112
}
113113

114114
@Override public void updateDialog(String id, String detail) {
@@ -117,10 +117,12 @@ public class PlacePickerActivity extends Activity implements
117117
}
118118
}
119119

120-
@Override public void onClick(DialogInterface dialogInterface, int i) {
121-
if (i == DialogInterface.BUTTON_POSITIVE) {
122-
presenter.onPlaceConfirmed();
123-
}
120+
@Override public void onPlaceConfirmed() {
121+
presenter.onPlaceConfirmed();
122+
dialogPlaceId = null;
123+
}
124+
125+
@Override public void onPlaceDismissed() {
124126
dialogPlaceId = null;
125127
}
126128

mapzen-places-api/src/main/java/com/mapzen/places/api/internal/PlacePickerPresenterImpl.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ class PlacePickerPresenterImpl implements PlacePickerPresenter {
1515
private static final String PROPERTY_ID = "id";
1616
private static final String PROPERTY_NAME = "name";
1717

18-
PlacePickerViewController controller;
19-
PlaceDetailFetcher detailFetcher;
20-
Place place;
18+
private PlacePickerViewController controller;
19+
private PlaceDetailFetcher detailFetcher;
2120

2221
/**
2322
* Construct a new object.
@@ -36,7 +35,7 @@ class PlacePickerPresenterImpl implements PlacePickerPresenter {
3635

3736
detailFetcher.fetchDetails(properties, new OnPlaceDetailsFetchedListener() {
3837
@Override public void onFetchSuccess(Place place, String details) {
39-
PlacePickerPresenterImpl.this.place = place;
38+
PlaceStore.instance().setCurrentSelectedPlace(place);
4039
controller.updateDialog(id, details);
4140
}
4241

@@ -49,11 +48,11 @@ class PlacePickerPresenterImpl implements PlacePickerPresenter {
4948
}
5049

5150
@Override public void onPlaceConfirmed() {
52-
controller.finishWithPlace(place);
51+
controller.finishWithPlace(PlaceStore.instance().getCurrentSelectedPlace());
5352
}
5453

5554
@Override public void onAutocompletePlacePicked(Place place, String details) {
56-
this.place = place;
55+
PlaceStore.instance().setCurrentSelectedPlace(place);
5756
controller.showDialog(place.getId(), details);
5857
//TODO:dialog change location should bring back autocomplete ui
5958
//TODO:hide map ui and just have dialog visible
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.mapzen.places.api.internal;
2+
3+
import com.mapzen.places.api.Place;
4+
5+
/**
6+
* In-memory store used to persist data for currently selected place.
7+
*/
8+
public class PlaceStore {
9+
private static PlaceStore instance = new PlaceStore();
10+
11+
/**
12+
* Return place store singleton instance.
13+
*
14+
* @return the place store instance.
15+
*/
16+
public static PlaceStore instance() {
17+
return instance;
18+
}
19+
20+
private PlaceStore() {
21+
}
22+
23+
private Place currentSelectedPlace;
24+
25+
public Place getCurrentSelectedPlace() {
26+
return currentSelectedPlace;
27+
}
28+
29+
public void setCurrentSelectedPlace(Place currentSelectedPlace) {
30+
this.currentSelectedPlace = currentSelectedPlace;
31+
}
32+
}

mapzen-places-api/src/test/java/com/mapzen/places/api/internal/PlacePickerPresenterTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ public class PlacePickerPresenterTest {
3939
assertThat(controller.dialogShown).isTrue();
4040
}
4141

42+
@Test public void onAutocompletePlacePicked_shouldPersistPlaceOnRotation() throws Exception {
43+
Place place = new PlaceImpl.Builder().build();
44+
presenter.onAutocompletePlacePicked(place, "details");
45+
presenter = new PlacePickerPresenterImpl(new TestPlaceDetailFetcher());
46+
presenter.setController(controller);
47+
presenter.onPlaceConfirmed();
48+
assertThat(controller.place).isEqualTo(place);
49+
}
50+
4251
@Test public void onHideView_shouldDisableLocationServices() throws Exception {
4352
controller.myLocationEnabled = true;
4453
presenter.onHideView();

mapzen-places-api/src/test/java/com/mapzen/places/api/internal/TestPlacePickerController.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class TestPlacePickerController implements PlacePickerViewController {
88
String dialogTitle = null;
99
boolean finished = false;
1010
boolean myLocationEnabled = false;
11+
Place place = null;
1112

1213
@Override public void showDialog(String id, String title) {
1314
dialogShown = true;
@@ -19,6 +20,7 @@ class TestPlacePickerController implements PlacePickerViewController {
1920
}
2021

2122
@Override public void finishWithPlace(Place place) {
23+
this.place = place;
2224
finished = true;
2325
}
2426

samples/mapzen-places-api-sample/src/main/java/com/mapzen/places/api/sample/PlacePickerDemoActivity.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ public class PlacePickerDemoActivity extends AppCompatActivity {
2323

2424
private static final int PERMISSIONS_REQUEST_CODE = 1;
2525
private static final int NUMBER_OF_PERMISSIONS = 1;
26-
2726
private static final int PLACE_PICKER_REQUEST = 1;
2827

28+
private static boolean isPickingPlace = false;
29+
2930
TextView placeName;
3031
TextView placeAddress;
3132
TextView placeAttribution;
@@ -35,7 +36,10 @@ public class PlacePickerDemoActivity extends AppCompatActivity {
3536
super.onCreate(savedInstanceState);
3637
setContentView(R.layout.place_picker_demo);
3738
setupTextViews();
38-
safeLaunchPicker();
39+
40+
if (!isPickingPlace) {
41+
safeLaunchPicker();
42+
}
3943
}
4044

4145
@Override public void onRequestPermissionsResult(int requestCode, String[] permissions,
@@ -57,6 +61,7 @@ public class PlacePickerDemoActivity extends AppCompatActivity {
5761

5862
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
5963
super.onActivityResult(requestCode, resultCode, data);
64+
isPickingPlace = false;
6065

6166
if (requestCode == PLACE_PICKER_REQUEST && resultCode == RESULT_OK) {
6267
Place place = PlacePicker.getPlace(this, data);
@@ -111,6 +116,7 @@ private void launchPlacePicker() {
111116
Intent intent = new PlacePicker.IntentBuilder()
112117
.setLatLngBounds(new LatLngBounds(southwest, northeast))
113118
.build(this);
119+
isPickingPlace = true;
114120
startActivityForResult(intent, PLACE_PICKER_REQUEST);
115121
}
116122

0 commit comments

Comments
 (0)