|
@@ -0,0 +1,245 @@
|
|
1
|
+package com.rthoni.stssaguenay.ui.fragments;
|
|
2
|
+
|
|
3
|
+import android.Manifest;
|
|
4
|
+import android.app.ProgressDialog;
|
|
5
|
+import android.content.Context;
|
|
6
|
+import android.content.pm.PackageManager;
|
|
7
|
+import android.os.Bundle;
|
|
8
|
+import android.support.annotation.NonNull;
|
|
9
|
+import android.support.annotation.Nullable;
|
|
10
|
+import android.support.v4.app.ActivityCompat;
|
|
11
|
+import android.support.v4.app.Fragment;
|
|
12
|
+import android.view.LayoutInflater;
|
|
13
|
+import android.view.View;
|
|
14
|
+import android.view.ViewGroup;
|
|
15
|
+import android.widget.Toast;
|
|
16
|
+
|
|
17
|
+import com.google.android.gms.maps.CameraUpdateFactory;
|
|
18
|
+import com.google.android.gms.maps.GoogleMap;
|
|
19
|
+import com.google.android.gms.maps.MapView;
|
|
20
|
+import com.google.android.gms.maps.OnMapReadyCallback;
|
|
21
|
+import com.google.android.gms.maps.model.LatLng;
|
|
22
|
+import com.google.android.gms.maps.model.Marker;
|
|
23
|
+import com.google.android.gms.maps.model.MarkerOptions;
|
|
24
|
+import com.google.maps.android.clustering.ClusterItem;
|
|
25
|
+import com.google.maps.android.clustering.ClusterManager;
|
|
26
|
+import com.google.maps.android.clustering.view.DefaultClusterRenderer;
|
|
27
|
+import com.luticate.utils.business.LuPromise;
|
|
28
|
+import com.rthoni.stssaguenay.R;
|
|
29
|
+import com.rthoni.stssaguenay.business.STSBusiness;
|
|
30
|
+import com.rthoni.stssaguenay.business.StopsBusiness;
|
|
31
|
+import com.rthoni.stssaguenay.dbo.StopsDbo;
|
|
32
|
+
|
|
33
|
+import java.util.HashMap;
|
|
34
|
+import java.util.List;
|
|
35
|
+
|
|
36
|
+import butterknife.BindView;
|
|
37
|
+import butterknife.ButterKnife;
|
|
38
|
+
|
|
39
|
+/**
|
|
40
|
+ * Created by robin on 9/29/16.
|
|
41
|
+ */
|
|
42
|
+public class StopMapPickerFragment extends Fragment {
|
|
43
|
+
|
|
44
|
+ public static class StopDboMapItem implements ClusterItem {
|
|
45
|
+ private LatLng _latLng;
|
|
46
|
+
|
|
47
|
+ private StopsDbo _stopsDbo;
|
|
48
|
+
|
|
49
|
+ StopDboMapItem(StopsDbo stopsDbo) {
|
|
50
|
+ _stopsDbo = stopsDbo;
|
|
51
|
+ _latLng = new LatLng(_stopsDbo.getPosY(), _stopsDbo.getPosX());
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ @Override
|
|
55
|
+ public LatLng getPosition() {
|
|
56
|
+ return _latLng;
|
|
57
|
+ }
|
|
58
|
+
|
|
59
|
+ public StopsDbo getStopsDbo() {
|
|
60
|
+ return _stopsDbo;
|
|
61
|
+ }
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ public static class StopDboMapRenderer extends DefaultClusterRenderer<StopDboMapItem> {
|
|
65
|
+
|
|
66
|
+ private HashMap<String, StopDboMapItem> _items = new HashMap<>();
|
|
67
|
+
|
|
68
|
+ public StopDboMapRenderer(Context context, GoogleMap map, ClusterManager<StopDboMapItem> clusterManager) {
|
|
69
|
+ super(context, map, clusterManager);
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ @Override
|
|
73
|
+ protected void onBeforeClusterItemRendered(StopDboMapItem item, MarkerOptions markerOptions) {
|
|
74
|
+ markerOptions.title(item.getStopsDbo().getFullName());
|
|
75
|
+ super.onBeforeClusterItemRendered(item, markerOptions);
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ @Override
|
|
79
|
+ protected void onClusterItemRendered(StopDboMapItem clusterItem, Marker marker) {
|
|
80
|
+ _items.put(marker.getId(), clusterItem);
|
|
81
|
+ super.onClusterItemRendered(clusterItem, marker);
|
|
82
|
+ }
|
|
83
|
+
|
|
84
|
+ public StopDboMapItem getStopItem(String id)
|
|
85
|
+ {
|
|
86
|
+ return _items.get(id);
|
|
87
|
+ }
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ @BindView(R.id.mapView)
|
|
91
|
+ MapView _mapView;
|
|
92
|
+
|
|
93
|
+ private LuPromise.LuConsumer<StopsDbo> _onStopSelectedConsumer;
|
|
94
|
+
|
|
95
|
+ private GoogleMap _map;
|
|
96
|
+
|
|
97
|
+ private List<StopsDbo> _stopsDbos;
|
|
98
|
+
|
|
99
|
+ private StopDboMapRenderer _renderer;
|
|
100
|
+
|
|
101
|
+ @Override
|
|
102
|
+ public void onStart() {
|
|
103
|
+ _mapView.onStart();
|
|
104
|
+ super.onStart();
|
|
105
|
+ }
|
|
106
|
+
|
|
107
|
+ @Override
|
|
108
|
+ public void onResume() {
|
|
109
|
+ _mapView.onResume();
|
|
110
|
+ super.onResume();
|
|
111
|
+ }
|
|
112
|
+
|
|
113
|
+ @Override
|
|
114
|
+ public void onPause() {
|
|
115
|
+ _mapView.onPause();
|
|
116
|
+ super.onPause();
|
|
117
|
+ }
|
|
118
|
+
|
|
119
|
+ @Override
|
|
120
|
+ public void onStop() {
|
|
121
|
+ _mapView.onStop();
|
|
122
|
+ super.onStop();
|
|
123
|
+ }
|
|
124
|
+
|
|
125
|
+ @Override
|
|
126
|
+ public void onDestroy() {
|
|
127
|
+ _mapView.onDestroy();
|
|
128
|
+ super.onDestroy();
|
|
129
|
+ }
|
|
130
|
+
|
|
131
|
+ @Override
|
|
132
|
+ public void onSaveInstanceState(Bundle outState) {
|
|
133
|
+ _mapView.onSaveInstanceState(outState);
|
|
134
|
+ super.onSaveInstanceState(outState);
|
|
135
|
+ }
|
|
136
|
+
|
|
137
|
+ @Override
|
|
138
|
+ public void onLowMemory() {
|
|
139
|
+ _mapView.onLowMemory();
|
|
140
|
+ super.onLowMemory();
|
|
141
|
+ }
|
|
142
|
+
|
|
143
|
+ @Nullable
|
|
144
|
+ @Override
|
|
145
|
+ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
|
146
|
+ View v = inflater.inflate(R.layout.fragment_stop_picker_map, container, false);
|
|
147
|
+ ButterKnife.bind(this, v);
|
|
148
|
+
|
|
149
|
+ _mapView.onCreate(savedInstanceState);
|
|
150
|
+ _mapView.getMapAsync(new OnMapReadyCallback() {
|
|
151
|
+ @Override
|
|
152
|
+ public void onMapReady(GoogleMap googleMap) {
|
|
153
|
+ _map = googleMap;
|
|
154
|
+ setupMap();
|
|
155
|
+ }
|
|
156
|
+ });
|
|
157
|
+
|
|
158
|
+ loadStops();
|
|
159
|
+
|
|
160
|
+ return v;
|
|
161
|
+ }
|
|
162
|
+
|
|
163
|
+ @Override
|
|
164
|
+ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
165
|
+ if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ||
|
|
166
|
+ ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
|
167
|
+ _map.setMyLocationEnabled(true);
|
|
168
|
+ }
|
|
169
|
+ super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
170
|
+ }
|
|
171
|
+
|
|
172
|
+ public void setupMap()
|
|
173
|
+ {
|
|
174
|
+ _map.getUiSettings().setMyLocationButtonEnabled(true);
|
|
175
|
+ _map.getUiSettings().setCompassEnabled(true);
|
|
176
|
+ _map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(48.3786565, -71.3354953), 9));
|
|
177
|
+ _map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
|
|
178
|
+ @Override
|
|
179
|
+ public void onInfoWindowClick(Marker marker) {
|
|
180
|
+ onStopSelected(_renderer.getStopItem(marker.getId()).getStopsDbo());
|
|
181
|
+ }
|
|
182
|
+ });
|
|
183
|
+ if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ||
|
|
184
|
+ ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
|
185
|
+ _map.setMyLocationEnabled(true);
|
|
186
|
+ }
|
|
187
|
+ else {
|
|
188
|
+ requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 0);
|
|
189
|
+ }
|
|
190
|
+ if (_stopsDbos != null) {
|
|
191
|
+ addStops();
|
|
192
|
+ }
|
|
193
|
+ }
|
|
194
|
+
|
|
195
|
+ public void loadStops()
|
|
196
|
+ {
|
|
197
|
+ final ProgressDialog progressDialog = new ProgressDialog(getContext());
|
|
198
|
+ progressDialog.setIndeterminate(true);
|
|
199
|
+ progressDialog.setCancelable(false);
|
|
200
|
+ progressDialog.show();
|
|
201
|
+ progressDialog.setTitle(R.string.loading);
|
|
202
|
+ progressDialog.setMessage(getString(R.string.loading_stops));
|
|
203
|
+
|
|
204
|
+ StopsBusiness.getAll(STSBusiness.getConfig(getContext()))
|
|
205
|
+ .then(new LuPromise.LuConsumer<List<StopsDbo>>() {
|
|
206
|
+ @Override
|
|
207
|
+ public void execute(List<StopsDbo> data) {
|
|
208
|
+ progressDialog.dismiss();
|
|
209
|
+ _stopsDbos = data;
|
|
210
|
+ if (_map != null) {
|
|
211
|
+ addStops();
|
|
212
|
+ }
|
|
213
|
+ }
|
|
214
|
+ }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
|
|
215
|
+ @Override
|
|
216
|
+ public void execute(LuPromise.LuPromiseError data) {
|
|
217
|
+ progressDialog.dismiss();
|
|
218
|
+ _stopsDbos = null;
|
|
219
|
+ Toast.makeText(getContext(), data.getError(), Toast.LENGTH_LONG).show();
|
|
220
|
+ }
|
|
221
|
+ });
|
|
222
|
+ }
|
|
223
|
+
|
|
224
|
+ public void addStops()
|
|
225
|
+ {
|
|
226
|
+ ClusterManager<StopDboMapItem> clusterManager = new ClusterManager<>(getContext(), _map);
|
|
227
|
+ _renderer = new StopDboMapRenderer(getContext(), _map, clusterManager);
|
|
228
|
+ clusterManager.setRenderer(_renderer);
|
|
229
|
+ for (StopsDbo stopsDbo : _stopsDbos) {
|
|
230
|
+ clusterManager.addItem(new StopDboMapItem(stopsDbo));
|
|
231
|
+ }
|
|
232
|
+ _map.setOnCameraIdleListener(clusterManager);
|
|
233
|
+ }
|
|
234
|
+
|
|
235
|
+ public void onStopSelected(StopsDbo stopsDbo)
|
|
236
|
+ {
|
|
237
|
+ if (_onStopSelectedConsumer != null) {
|
|
238
|
+ _onStopSelectedConsumer.execute(stopsDbo);
|
|
239
|
+ }
|
|
240
|
+ }
|
|
241
|
+
|
|
242
|
+ public void setOnStopSelectedConsumer(LuPromise.LuConsumer<StopsDbo> onStopSelectedConsumer) {
|
|
243
|
+ _onStopSelectedConsumer = onStopSelectedConsumer;
|
|
244
|
+ }
|
|
245
|
+}
|