1
1
package com .eventyay .organizer .core .event .create ;
2
2
3
- import androidx .lifecycle .ViewModelProvider ;
4
- import androidx .lifecycle .ViewModelProviders ;
5
- import android .content .Intent ;
6
3
import android .content .pm .ApplicationInfo ;
7
4
import android .content .pm .PackageManager ;
8
- import androidx . databinding . DataBindingUtil ;
5
+ import android . graphics . Color ;
9
6
import android .os .Bundle ;
10
- import androidx .annotation .NonNull ;
11
- import androidx .annotation .Nullable ;
12
7
import android .view .LayoutInflater ;
13
8
import android .view .View ;
14
9
import android .view .ViewGroup ;
15
10
import android .widget .AdapterView ;
16
11
import android .widget .ArrayAdapter ;
17
12
13
+ import androidx .annotation .NonNull ;
14
+ import androidx .annotation .Nullable ;
15
+ import androidx .databinding .DataBindingUtil ;
16
+ import androidx .lifecycle .ViewModelProvider ;
17
+ import androidx .lifecycle .ViewModelProviders ;
18
+
18
19
import com .eventyay .organizer .R ;
19
20
import com .eventyay .organizer .common .mvp .view .BaseBottomSheetFragment ;
20
21
import com .eventyay .organizer .data .event .Event ;
21
22
import com .eventyay .organizer .databinding .EventDetailsStepOneBinding ;
23
+ import com .eventyay .organizer .ui .ViewUtils ;
24
+ import com .mapbox .api .geocoding .v5 .models .CarmenFeature ;
25
+ import com .mapbox .mapboxsdk .plugins .places .autocomplete .model .PlaceOptions ;
26
+ import com .mapbox .mapboxsdk .plugins .places .autocomplete .ui .PlaceAutocompleteFragment ;
27
+ import com .mapbox .mapboxsdk .plugins .places .autocomplete .ui .PlaceSelectionListener ;
22
28
23
29
import java .util .Arrays ;
24
30
import java .util .List ;
27
33
28
34
import timber .log .Timber ;
29
35
30
- import static android .app .Activity .RESULT_OK ;
31
-
32
36
public class EventDetailsStepOne extends BaseBottomSheetFragment implements EventDetailsStepOneView {
33
37
34
38
@ Inject
35
39
ViewModelProvider .Factory viewModelFactory ;
36
40
37
41
private CreateEventViewModel createEventViewModel ;
38
42
private EventDetailsStepOneBinding binding ;
39
- private static final int PLACE_PICKER_REQUEST = 1 ;
40
- private final LocationPicker locationPicker = new LocationPicker ();
41
43
42
44
public static EventDetailsStepOne newInstance () {
43
45
return new EventDetailsStepOne ();
@@ -58,7 +60,7 @@ public void onStart() {
58
60
int timezoneIndex = createEventViewModel .setTimeZoneList (getTimeZoneList ());
59
61
setupSpinner ();
60
62
setDefaultTimeZone (timezoneIndex );
61
- setupPlacePicker ();
63
+ setupPlacesAutocomplete ();
62
64
}
63
65
64
66
private void setupSpinner () {
@@ -82,50 +84,52 @@ public void onNothingSelected(AdapterView<?> parent) {
82
84
});
83
85
}
84
86
85
- private void setupPlacePicker () {
86
- //check if there's a google places API key
87
+ private void setupPlacesAutocomplete () {
88
+
89
+ ApplicationInfo applicationInfo = null ;
87
90
try {
88
- ApplicationInfo ai = getContext ().getPackageManager ().getApplicationInfo (getContext ().getPackageName (), PackageManager .GET_META_DATA );
89
- Bundle bundle = ai .metaData ;
90
- String placesApiKey = bundle .getString ("com.google.android.geo.API_KEY" );
91
- if ("YOUR_API_KEY" .equals (placesApiKey )) {
92
- Timber .d ("Add Google Places API key in AndroidManifest.xml file to use Place Picker." );
93
- binding .buttonPlacePicker .setVisibility (View .GONE );
94
- showLocationLayouts ();
95
- }
91
+ applicationInfo = getContext ().getPackageManager ().getApplicationInfo (getContext ().getPackageName (), PackageManager .GET_META_DATA );
96
92
} catch (PackageManager .NameNotFoundException e ) {
97
- Timber .e (e , "Package name not found" );
93
+ Timber .e (e );
98
94
}
95
+ Bundle bundle = applicationInfo .metaData ;
99
96
100
- binding .buttonPlacePicker .setOnClickListener (view -> {
101
- boolean success = locationPicker .launchPicker (getActivity ());
102
- if (locationPicker .shouldShowLocationLayout () || !success )
103
- showLocationLayouts ();
104
- });
105
- }
97
+ String mapboxAccessToken = bundle .getString (getString (R .string .mapbox_access_token ));
106
98
107
- @ Override
108
- public void onActivityResult (int requestCode , int resultCode , Intent data ) {
109
- super .onActivityResult (requestCode , resultCode , data );
110
- if (requestCode == PLACE_PICKER_REQUEST && resultCode == RESULT_OK ) {
111
- //once place is picked from map, make location fields visible for confirmation by user
112
- showLocationLayouts ();
113
- //set event attributes
114
- Location location = locationPicker .getPlace (getActivity (), data );
115
- Event event = binding .getEvent ();
116
- event .latitude = location .getLatitude ();
117
- event .longitude = location .getLongitude ();
118
-
119
- //auto-complete location fields for confirmation by user
120
- binding .locationName .setText (location .getAddress ());
121
- binding .searchableLocationName .setText (
122
- createEventViewModel .getSearchableLocationName (location .getAddress ().toString ()));
123
- }
124
- }
99
+ binding .selectLocationButton .setOnClickListener (view -> {
125
100
126
- private void showLocationLayouts () {
127
- binding .layoutSearchableLocation .setVisibility (View .VISIBLE );
128
- binding .layoutLocationName .setVisibility (View .VISIBLE );
101
+ if (mapboxAccessToken .equals ("YOUR_ACCESS_TOKEN" )) {
102
+ ViewUtils .showSnackbar (binding .getRoot (), R .string .access_token_required );
103
+ return ;
104
+ }
105
+
106
+ PlaceAutocompleteFragment autocompleteFragment = PlaceAutocompleteFragment .newInstance (
107
+ mapboxAccessToken , PlaceOptions .builder ().backgroundColor (Color .WHITE ).build ());
108
+
109
+ getFragmentManager ().beginTransaction ()
110
+ .replace (R .id .fragment , autocompleteFragment )
111
+ .addToBackStack (null )
112
+ .commit ();
113
+
114
+ autocompleteFragment .setOnPlaceSelectedListener (new PlaceSelectionListener () {
115
+ @ Override
116
+ public void onPlaceSelected (CarmenFeature carmenFeature ) {
117
+ Event event = binding .getEvent ();
118
+ event .setLatitude (carmenFeature .center ().latitude ());
119
+ event .setLongitude (carmenFeature .center ().longitude ());
120
+ event .setLocationName (carmenFeature .placeName ());
121
+ event .setSearchableLocationName (carmenFeature .text ());
122
+ binding .layoutLocationName .setVisibility (View .VISIBLE );
123
+ binding .locationName .setText (event .getLocationName ());
124
+ getFragmentManager ().popBackStack ();
125
+ }
126
+
127
+ @ Override
128
+ public void onCancel () {
129
+ getFragmentManager ().popBackStack ();
130
+ }
131
+ });
132
+ });
129
133
}
130
134
131
135
@ Override
0 commit comments