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

View File

@ -19,9 +19,14 @@ import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import com.annimon.stream.Collector;
import com.annimon.stream.Stream;
import com.jakewharton.retrofit2.adapter.rxjava2.HttpException;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.uam.wmi.findmytutor.R;
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.service.BackgroundLocalizationService;
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.SharingLevel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
@ -39,6 +45,7 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
@ -62,6 +69,7 @@ public class SharingFragment extends PreferenceFragment {
protected RightButtonPreference manualLocationButton;
protected Preference manualStatus;
protected ListPreference statusList;
protected List<PredefinedCoordViewModel> predefinedCoordsList= new ArrayList<>();
void getStatuses(CompositeDisposable disposable) {
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")
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.pref_sharing);
locationSharing = findPreference("key_sharing_enabled");
locationMode = findPreference("key_location_level");
preferenceCategory = (PreferenceCategory) findPreference("category_sharing");
@ -102,7 +138,10 @@ public class SharingFragment extends PreferenceFragment {
statusesService = ApiClient.getClient(getApplicationContext()).create(PredefinedStatusesService.class);
disposable = new CompositeDisposable();
getStatuses(disposable);
if (PrefUtils.getLocationLevel(getApplicationContext()).equals("manual") ) {
getLocations(disposable);
}
locationLevelMapping = new HashMap<Integer, String>();
locationLevelMapping.put(0, SharingLevel.PRESENCE.toString());
locationLevelMapping.put(1, SharingLevel.APPROXIMATED.toString());
@ -117,7 +156,6 @@ public class SharingFragment extends PreferenceFragment {
/** Main sharing switch**/
locationSharing.setOnPreferenceChangeListener((buttonView, newValue) -> {
PrefUtils.storeEnableSharingLocalization(getApplicationContext(), (Boolean) newValue);
((MapActivity) getActivity()).handleBackgroundTaskLifeCycle();
return true;
});
@ -127,7 +165,8 @@ public class SharingFragment extends PreferenceFragment {
PrefUtils.storeLocationMode(getApplicationContext(), locationLevelMapping.get(Integer.parseInt((String) newValue)));
if (PrefUtils.getLocationLevel(getApplicationContext()).equals("manual") ) {
preferenceCategory.addPreference(manualLocationList);
preferenceCategory.addPreference(manualLocationButton);
getLocations(disposable);
preferenceCategory.addPreference(manualLocationButton);
} else {
preferenceCategory.removePreference(manualLocationList);
@ -144,7 +183,13 @@ public class SharingFragment extends PreferenceFragment {
/** Custom manual location list change listener **/
manualLocationList.setOnPreferenceChangeListener((preference, newValue) -> {
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;
});

View File

@ -2,6 +2,7 @@ package com.uam.wmi.findmytutor.model;
import java.util.UUID;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.mapbox.mapboxsdk.geometry.LatLng;
public class PredefinedCoordViewModel {
@ -46,9 +47,9 @@ public class PredefinedCoordViewModel {
@SerializedName("displayMode")
@Expose
private String displayMode = "predefined";
@SerializedName("label")
@SerializedName("name")
@Expose
private String label;
private String name;
/**
* No args constructor for use in serialization
@ -62,22 +63,21 @@ public class PredefinedCoordViewModel {
* @param altitude
* @param userId
* @param displayMode
* @param label
* @param name
* @param longitude
* @param latitude
* @param approximatedLocation
* @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();
this.predefinedCoordinateId = predefinedCoordinateId;
this.latitude = latitude;
this.longitude = longitude;
this.altitude = altitude;
this.userId = userId;
this.approximatedLocation = approximatedLocation;
this.displayMode = displayMode;
this.label = label;
this.name = name;
}
public UUID getPredefinedCoordinateId() {
@ -176,12 +176,29 @@ public class PredefinedCoordViewModel {
this.displayMode = displayMode;
}
public String getLabel() {
return label;
public String getName() {
return name;
}
public void setLabel(String label) {
this.label = label;
public void setName(String name) {
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();
modalUserInput.requestFocus();
} else {
PrefUtils.putManualLocation(activityContext, latLng);
sendLocation(body,latLng);
alertDialog.dismiss();
}
@ -77,13 +78,12 @@ public class ManualLocationUtils {
private void sendLocation(String body, LatLng latLng) {
PredefinedCoordViewModel droppedMarkercoordinate = new PredefinedCoordViewModel(
null,
latLng.getLatitude(),
latLng.getLongitude(),
latLng.getAltitude(),
PrefUtils.getUserId(activityContext),
"approx",
"nie wiem",
"string",
"predefined",
body
);
PredefinedStatusesService predefinedStatusesService = ApiClient.getClient(activityContext).create(PredefinedStatusesService.class);
@ -104,11 +104,13 @@ public class ManualLocationUtils {
ResponseBody responseBody = ((HttpException) error).response().errorBody();
Toast.makeText(activityContext,
RestApiHelper.getErrorMessage(responseBody), Toast.LENGTH_SHORT).show();
Log.d("POSTCOORDINATE", error.getMessage());
} else {
Toast.makeText(activityContext,
"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.putLong("longitude_manual_location", Double.doubleToRawLongBits(latLng.getLongitude()));
editor.putLong("latitude_manual_location", Double.doubleToRawLongBits(latLng.getLatitude()));
Log.d("SAVE_COORD","zapisane");
editor.apply();
}