added handling of choosing manual location from list in sharing tab

This commit is contained in:
marcin.jedynski 2018-11-27 03:10:33 +01:00
parent 493395d36a
commit bbc7803177
5 changed files with 98 additions and 35 deletions

View File

@ -262,17 +262,16 @@ public class MapActivity extends BaseActivity
// Toast instructing user to tap on the mapboxMap // Toast instructing user to tap on the mapboxMap
// TODO PUT MANUAL CORD // TODO PUT MANUAL CORD
try { try {
droppedMarkercoordinate = new Coordinate( // droppedMarkercoordinate = new Coordinate(
latLng.getLatitude(), // latLng.getLatitude(),
latLng.getLongitude(), // latLng.getLongitude(),
latLng.getAltitude(), // latLng.getAltitude(),
"approx", // "approx",
PrefUtils.getUserFirstName(getApplicationContext()) + " " + PrefUtils.getUserLastName(getApplicationContext()), // PrefUtils.getUserFirstName(getApplicationContext()) + " " + PrefUtils.getUserLastName(getApplicationContext()),
PrefUtils.getUserId(getApplicationContext()), // PrefUtils.getUserId(getApplicationContext()),
PrefUtils.getLocationLevel(getApplicationContext()) // PrefUtils.getLocationLevel(getApplicationContext())
); // );
manualLocationUtils.showLocationDialog("Name the location",latLng);
PrefUtils.putManualLocation(this, latLng);
handleBackgroundTaskLifeCycle(); handleBackgroundTaskLifeCycle();
@ -318,17 +317,16 @@ public class MapActivity extends BaseActivity
Timber.e(String.valueOf(e)); Timber.e(String.valueOf(e));
} }
Toast.makeText( // Toast.makeText(
MapActivity.this, // MapActivity.this,
"Manual Locations selected!" + latLng, // "Manual Locations selected!" + latLng,
Toast.LENGTH_LONG // Toast.LENGTH_LONG
).show(); // ).show();
// LayoutInflater layoutInflaterAndroid = LayoutInflater.from(MapActivity.this); // LayoutInflater layoutInflaterAndroid = LayoutInflater.from(MapActivity.this);
// View dialogView = layoutInflaterAndroid.inflate(R.layout.feedback_modal, null); // View dialogView = layoutInflaterAndroid.inflate(R.layout.feedback_modal, null);
// AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(MapActivity.this ); // AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(MapActivity.this );
// alertDialogBuilderUserInput.setView(dialogView).setPositiveButton("dupa", null); // alertDialogBuilderUserInput.setView(dialogView).setPositiveButton("dupa", null);
// final AlertDialog alertDialog = alertDialogBuilderUserInput.create(); // final AlertDialog alertDialog = alertDialogBuilderUserInput.create();
manualLocationUtils.showLocationDialog("Name the location",latLng);
// alertDialog.show(); // alertDialog.show();
selectLocationButton.setVisibility(View.GONE); selectLocationButton.setVisibility(View.GONE);
mapboxMap.removeMarker(tmpLocalMarker); mapboxMap.removeMarker(tmpLocalMarker);

View File

@ -19,9 +19,14 @@ import android.view.ViewGroup;
import android.widget.Button; import android.widget.Button;
import android.widget.Toast; import android.widget.Toast;
import com.annimon.stream.Collector;
import com.annimon.stream.Stream;
import com.jakewharton.retrofit2.adapter.rxjava2.HttpException; import com.jakewharton.retrofit2.adapter.rxjava2.HttpException;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.uam.wmi.findmytutor.R; import com.uam.wmi.findmytutor.R;
import com.uam.wmi.findmytutor.model.Feedback; import com.uam.wmi.findmytutor.model.Feedback;
import com.uam.wmi.findmytutor.model.PredefinedCoordViewModel;
import com.uam.wmi.findmytutor.model.User;
import com.uam.wmi.findmytutor.network.ApiClient; import com.uam.wmi.findmytutor.network.ApiClient;
import com.uam.wmi.findmytutor.service.BackgroundLocalizationService; import com.uam.wmi.findmytutor.service.BackgroundLocalizationService;
import com.uam.wmi.findmytutor.service.FeedbackService; import com.uam.wmi.findmytutor.service.FeedbackService;
@ -32,6 +37,7 @@ import com.uam.wmi.findmytutor.utils.RestApiHelper;
import com.uam.wmi.findmytutor.utils.RightButtonPreference; import com.uam.wmi.findmytutor.utils.RightButtonPreference;
import com.uam.wmi.findmytutor.utils.SharingLevel; import com.uam.wmi.findmytutor.utils.SharingLevel;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
@ -39,6 +45,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable; import io.reactivex.disposables.CompositeDisposable;
@ -62,6 +69,7 @@ public class SharingFragment extends PreferenceFragment {
protected RightButtonPreference manualLocationButton; protected RightButtonPreference manualLocationButton;
protected Preference manualStatus; protected Preference manualStatus;
protected ListPreference statusList; protected ListPreference statusList;
protected List<PredefinedCoordViewModel> predefinedCoordsList= new ArrayList<>();
void getStatuses(CompositeDisposable disposable) { void getStatuses(CompositeDisposable disposable) {
disposable.add(statusesService.getUserPredefinedStatuses(PrefUtils.getUserId(getApplicationContext())) disposable.add(statusesService.getUserPredefinedStatuses(PrefUtils.getUserId(getApplicationContext()))
@ -85,12 +93,40 @@ public class SharingFragment extends PreferenceFragment {
})); }));
} }
void getLocations(CompositeDisposable disposable) {
disposable.add(statusesService.getUserPredefinedCoords(PrefUtils.getUserId(getApplicationContext()))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableSingleObserver<List<PredefinedCoordViewModel>>() {
@Override
public void onSuccess(List<PredefinedCoordViewModel> coords) {
PredefinedCoordViewModel [] statusesArray = (PredefinedCoordViewModel []) coords.toArray(new PredefinedCoordViewModel[0]);
// setListPreferenceData(statusList.getKey(), statusesArray);
List<String> names = Stream.of(coords).map( p -> p.getName()).collect(com.annimon.stream.Collectors.toList());
predefinedCoordsList.addAll(coords);
String [] stringnames = names.toArray(new String[0]);
Log.d("GETLOCATIONS", predefinedCoordsList.toString());
setListPreferenceData(manualLocationList.getKey(),stringnames);
Log.d("GETLOCATIONS", Arrays.toString(stringnames));
}
@Override
public void onError(Throwable e) {
Toast.makeText(getApplicationContext(), "Error handling status fetch", Toast.LENGTH_SHORT).show();
}
}));
}
@SuppressLint("ResourceType") @SuppressLint("ResourceType")
@Override @Override
public void onCreate(final Bundle savedInstanceState) { public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.pref_sharing); addPreferencesFromResource(R.layout.pref_sharing);
locationSharing = findPreference("key_sharing_enabled"); locationSharing = findPreference("key_sharing_enabled");
locationMode = findPreference("key_location_level"); locationMode = findPreference("key_location_level");
preferenceCategory = (PreferenceCategory) findPreference("category_sharing"); preferenceCategory = (PreferenceCategory) findPreference("category_sharing");
@ -102,7 +138,10 @@ public class SharingFragment extends PreferenceFragment {
statusesService = ApiClient.getClient(getApplicationContext()).create(PredefinedStatusesService.class); statusesService = ApiClient.getClient(getApplicationContext()).create(PredefinedStatusesService.class);
disposable = new CompositeDisposable(); disposable = new CompositeDisposable();
getStatuses(disposable); getStatuses(disposable);
if (PrefUtils.getLocationLevel(getApplicationContext()).equals("manual") ) {
getLocations(disposable);
}
locationLevelMapping = new HashMap<Integer, String>(); locationLevelMapping = new HashMap<Integer, String>();
locationLevelMapping.put(0, SharingLevel.PRESENCE.toString()); locationLevelMapping.put(0, SharingLevel.PRESENCE.toString());
locationLevelMapping.put(1, SharingLevel.APPROXIMATED.toString()); locationLevelMapping.put(1, SharingLevel.APPROXIMATED.toString());
@ -117,7 +156,6 @@ public class SharingFragment extends PreferenceFragment {
/** Main sharing switch**/ /** Main sharing switch**/
locationSharing.setOnPreferenceChangeListener((buttonView, newValue) -> { locationSharing.setOnPreferenceChangeListener((buttonView, newValue) -> {
PrefUtils.storeEnableSharingLocalization(getApplicationContext(), (Boolean) newValue); PrefUtils.storeEnableSharingLocalization(getApplicationContext(), (Boolean) newValue);
((MapActivity) getActivity()).handleBackgroundTaskLifeCycle();
return true; return true;
}); });
@ -127,6 +165,7 @@ public class SharingFragment extends PreferenceFragment {
PrefUtils.storeLocationMode(getApplicationContext(), locationLevelMapping.get(Integer.parseInt((String) newValue))); PrefUtils.storeLocationMode(getApplicationContext(), locationLevelMapping.get(Integer.parseInt((String) newValue)));
if (PrefUtils.getLocationLevel(getApplicationContext()).equals("manual") ) { if (PrefUtils.getLocationLevel(getApplicationContext()).equals("manual") ) {
preferenceCategory.addPreference(manualLocationList); preferenceCategory.addPreference(manualLocationList);
getLocations(disposable);
preferenceCategory.addPreference(manualLocationButton); preferenceCategory.addPreference(manualLocationButton);
} else { } else {
@ -144,7 +183,13 @@ public class SharingFragment extends PreferenceFragment {
/** Custom manual location list change listener **/ /** Custom manual location list change listener **/
manualLocationList.setOnPreferenceChangeListener((preference, newValue) -> { manualLocationList.setOnPreferenceChangeListener((preference, newValue) -> {
ListPreference lp = (ListPreference) preference; ListPreference lp = (ListPreference) preference;
//ToDo handle manual location change Log.d("SELECTEDLOCATION",(lp.getEntries()[Integer.parseInt((String) newValue)].toString()));
PredefinedCoordViewModel temp = Stream.of(predefinedCoordsList).filter( p -> p.getName().equals(lp.getEntries()[Integer.parseInt((String) newValue)].toString())).single();
Log.d("SELECTEDLOCATION",temp.toString());
// LatLng latLng = new LatLng(latitude, longitude, altitude);
PrefUtils.putManualLocation(getApplicationContext(),temp.getGeoData());
// ((MapActivity) getActivity()).handleBackgroundTaskLifeCycle();
return true; return true;
}); });

View File

@ -2,6 +2,7 @@ package com.uam.wmi.findmytutor.model;
import java.util.UUID; import java.util.UUID;
import com.google.gson.annotations.Expose; import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import com.mapbox.mapboxsdk.geometry.LatLng;
public class PredefinedCoordViewModel { public class PredefinedCoordViewModel {
@ -46,9 +47,9 @@ public class PredefinedCoordViewModel {
@SerializedName("displayMode") @SerializedName("displayMode")
@Expose @Expose
private String displayMode = "predefined"; private String displayMode = "predefined";
@SerializedName("label") @SerializedName("name")
@Expose @Expose
private String label; private String name;
/** /**
* No args constructor for use in serialization * No args constructor for use in serialization
@ -62,22 +63,21 @@ public class PredefinedCoordViewModel {
* @param altitude * @param altitude
* @param userId * @param userId
* @param displayMode * @param displayMode
* @param label * @param name
* @param longitude * @param longitude
* @param latitude * @param latitude
* @param approximatedLocation * @param approximatedLocation
* @param predefinedCoordinateId * @param predefinedCoordinateId
*/ */
public PredefinedCoordViewModel(UUID predefinedCoordinateId, Double latitude, Double longitude, Double altitude, String userId, String approximatedLocation, String displayMode, String label) { public PredefinedCoordViewModel(Double latitude, Double longitude, Double altitude, String userId, String approximatedLocation, String displayMode, String name) {
super(); super();
this.predefinedCoordinateId = predefinedCoordinateId;
this.latitude = latitude; this.latitude = latitude;
this.longitude = longitude; this.longitude = longitude;
this.altitude = altitude; this.altitude = altitude;
this.userId = userId; this.userId = userId;
this.approximatedLocation = approximatedLocation; this.approximatedLocation = approximatedLocation;
this.displayMode = displayMode; this.displayMode = displayMode;
this.label = label; this.name = name;
} }
public UUID getPredefinedCoordinateId() { public UUID getPredefinedCoordinateId() {
@ -176,12 +176,29 @@ public class PredefinedCoordViewModel {
this.displayMode = displayMode; this.displayMode = displayMode;
} }
public String getLabel() { public String getName() {
return label; return name;
} }
public void setLabel(String label) { public void setName(String name) {
this.label = label; this.name = name;
} }
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class PredefinedCoordinate {\n");
sb.append(" lat: ").append(latitude).append("\n");
sb.append(" long: ").append(longitude).append("\n");
sb.append(" att: ").append(altitude).append("\n");
sb.append(" userID: ").append(userId).append("\n");
sb.append(" approx: ").append(approximatedLocation).append("\n");
sb.append(" dispMode: ").append(displayMode).append("\n");
sb.append(" name: ").append(name).append("\n");
sb.append("}");
return sb.toString();
}
public LatLng getGeoData(){
return new LatLng(this.latitude,this.longitude,this.altitude);
}
} }

View File

@ -65,6 +65,7 @@ public class ManualLocationUtils {
Toast.makeText(activityContext, activityContext.getString(R.string.modal_feedback_hint), Toast.LENGTH_SHORT).show(); Toast.makeText(activityContext, activityContext.getString(R.string.modal_feedback_hint), Toast.LENGTH_SHORT).show();
modalUserInput.requestFocus(); modalUserInput.requestFocus();
} else { } else {
PrefUtils.putManualLocation(activityContext, latLng);
sendLocation(body,latLng); sendLocation(body,latLng);
alertDialog.dismiss(); alertDialog.dismiss();
} }
@ -77,13 +78,12 @@ public class ManualLocationUtils {
private void sendLocation(String body, LatLng latLng) { private void sendLocation(String body, LatLng latLng) {
PredefinedCoordViewModel droppedMarkercoordinate = new PredefinedCoordViewModel( PredefinedCoordViewModel droppedMarkercoordinate = new PredefinedCoordViewModel(
null,
latLng.getLatitude(), latLng.getLatitude(),
latLng.getLongitude(), latLng.getLongitude(),
latLng.getAltitude(), latLng.getAltitude(),
PrefUtils.getUserId(activityContext), PrefUtils.getUserId(activityContext),
"approx", "string",
"nie wiem", "predefined",
body body
); );
PredefinedStatusesService predefinedStatusesService = ApiClient.getClient(activityContext).create(PredefinedStatusesService.class); PredefinedStatusesService predefinedStatusesService = ApiClient.getClient(activityContext).create(PredefinedStatusesService.class);
@ -104,11 +104,13 @@ public class ManualLocationUtils {
ResponseBody responseBody = ((HttpException) error).response().errorBody(); ResponseBody responseBody = ((HttpException) error).response().errorBody();
Toast.makeText(activityContext, Toast.makeText(activityContext,
RestApiHelper.getErrorMessage(responseBody), Toast.LENGTH_SHORT).show(); RestApiHelper.getErrorMessage(responseBody), Toast.LENGTH_SHORT).show();
Log.d("POSTCOORDINATE", error.getMessage());
} else { } else {
Toast.makeText(activityContext, Toast.makeText(activityContext,
"Network error " + error.getMessage(), Toast.LENGTH_SHORT).show(); "Network error " + error.getMessage(), Toast.LENGTH_SHORT).show();
Log.d("FEEDBACK", error.getMessage()); Log.d("POSTCOORDINATE", error.getMessage());
} }
} }
} }

View File

@ -164,6 +164,7 @@ public class PrefUtils {
editor.putBoolean("key_sharing_enabled", true); editor.putBoolean("key_sharing_enabled", true);
editor.putLong("longitude_manual_location", Double.doubleToRawLongBits(latLng.getLongitude())); editor.putLong("longitude_manual_location", Double.doubleToRawLongBits(latLng.getLongitude()));
editor.putLong("latitude_manual_location", Double.doubleToRawLongBits(latLng.getLatitude())); editor.putLong("latitude_manual_location", Double.doubleToRawLongBits(latLng.getLatitude()));
Log.d("SAVE_COORD","zapisane");
editor.apply(); editor.apply();
} }