Merge branch 'saveManualLocation' of s416084/find-my-tutor-android into develop

This commit is contained in:
Marcin Jedyński 2018-11-27 05:33:39 +00:00 committed by Gogs
commit 6684274488
9 changed files with 335 additions and 31 deletions

View File

@ -38,10 +38,13 @@ import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.uam.wmi.findmytutor.R;
import com.uam.wmi.findmytutor.model.Coordinate;
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.CoordinateService;
import com.uam.wmi.findmytutor.service.UserService;
import com.uam.wmi.findmytutor.utils.FeedbackUtils;
import com.uam.wmi.findmytutor.utils.ManualLocationUtils;
import com.uam.wmi.findmytutor.utils.MapMarker;
import com.uam.wmi.findmytutor.utils.PrefUtils;
import com.uam.wmi.findmytutor.utils.RestApiHelper;
@ -86,7 +89,7 @@ public class MapActivity extends BaseActivity
private HashMap<String, MapMarker> markerHash = new HashMap<>();
private HashMap<Long, String> markerUserHash = new HashMap<>();
private Set<String> previousCoordsIds = new HashSet<>();
private ManualLocationUtils manualLocationUtils;
// Camera Animation params
private int zoomParam = 17;
private int bearingParam = 180;
@ -111,6 +114,7 @@ public class MapActivity extends BaseActivity
} finally {
mHandler.postDelayed(mStatusChecker, mInterval);
}
manualLocationUtils = new ManualLocationUtils(MapActivity.this);
};
selectLocationButton = findViewById(R.id.select_location_button);
@ -267,28 +271,33 @@ 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();
} catch (IllegalArgumentException e) {
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();
// alertDialog.show();
selectLocationButton.setVisibility(View.GONE);
mapboxMap.removeMarker(tmpLocalMarker);
tmpLocalMarker = null;

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;
});
@ -129,7 +167,8 @@ public class SharingFragment extends PreferenceFragment {
if (PrefUtils.getLocationLevel(getApplicationContext()).equals("manual") ) {
preferenceCategory.addPreference(manualLocationList);
preferenceCategory.addPreference(manualLocationButton);
getLocations(disposable);
preferenceCategory.addPreference(manualLocationButton);
} else {
preferenceCategory.removePreference(manualLocationList);
@ -146,7 +185,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

@ -0,0 +1,31 @@
package com.uam.wmi.findmytutor.activity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import com.uam.wmi.findmytutor.R;
public class TutorTab extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tutor_tab);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}

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

@ -0,0 +1,116 @@
package com.uam.wmi.findmytutor.utils;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.jakewharton.retrofit2.adapter.rxjava2.HttpException;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.uam.wmi.findmytutor.R;
import com.uam.wmi.findmytutor.activity.BaseActivity;
import com.uam.wmi.findmytutor.model.Coordinate;
import com.uam.wmi.findmytutor.model.Feedback;
import com.uam.wmi.findmytutor.model.PredefinedCoordViewModel;
import com.uam.wmi.findmytutor.network.ApiClient;
import com.uam.wmi.findmytutor.service.FeedbackService;
import com.uam.wmi.findmytutor.service.PredefinedStatusesService;
import java.util.List;
import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;
import retrofit2.Response;
public class ManualLocationUtils {
private Context activityContext;
public ManualLocationUtils(Context context) {
activityContext = context;
}
public void showLocationDialog(String subject, LatLng latLng) {
LayoutInflater layoutInflaterAndroid = LayoutInflater.from(activityContext);
View view = layoutInflaterAndroid.inflate(R.layout.location_modal, null);
AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(activityContext);
alertDialogBuilderUserInput.setView(view).setPositiveButton(activityContext.getString(R.string.modal_feedback_send), null);
final AlertDialog alertDialog = alertDialogBuilderUserInput.create();
EditText modalUserInput = view.findViewById(R.id.feedback_input);
TextView modalTitle = view.findViewById(R.id.feedback_modal_title);
modalTitle.setText(subject);
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
Button sendButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String body = modalUserInput.getText().toString();
if (TextUtils.isEmpty(body)) {
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();
}
}
});
}
});
alertDialog.show();
}
private void sendLocation(String body, LatLng latLng) {
PredefinedCoordViewModel droppedMarkercoordinate = new PredefinedCoordViewModel(
latLng.getLatitude(),
latLng.getLongitude(),
latLng.getAltitude(),
PrefUtils.getUserId(activityContext),
"string",
"predefined",
body
);
PredefinedStatusesService predefinedStatusesService = ApiClient.getClient(activityContext).create(PredefinedStatusesService.class);
CompositeDisposable disposable = new CompositeDisposable();
disposable.add(predefinedStatusesService.postUserPredefinedCoord(PrefUtils.getUserId(activityContext),droppedMarkercoordinate)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::handleResponse, this::handleError));
}
private void handleResponse(List<PredefinedCoordViewModel> resp) {
Toast.makeText(activityContext, "Location saved", Toast.LENGTH_SHORT).show();
}
private void handleError(Throwable error) {
if (error instanceof HttpException) {
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("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();
}

View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.TutorTab">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_tutor_tab" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".activity.TutorTab"
tools:showIn="@layout/activity_tutor_tab">
</android.support.constraint.ConstraintLayout>

View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<TextView
android:id="@+id/feedback_modal_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/feedback_input"
android:layout_marginBottom="@dimen/dimen_10"
android:fontFamily="sans-serif-medium"
android:lineSpacingExtra="8sp"
android:text="placeholder"
android:textColor="@color/colorAccent"
android:textSize="@dimen/lbl_new_note_title"
android:textStyle="normal" />
<EditText
android:id="@+id/feedback_input"
android:layout_width="315dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:ems="10"
android:hint="@string/modal_feedback_hint"
android:maxLength="30"
android:maxLines="1"
android:requiresFadingEdge="vertical"
android:scrollbars="vertical" />
</RelativeLayout>