deleting works, needs some polishing like refreshing summaries and hiding buttons depending on the level of sharing

This commit is contained in:
marcin.jedynski 2019-01-03 02:12:17 +01:00
parent 1eeaee7da1
commit 94368b05d1
8 changed files with 185 additions and 24 deletions

View File

@ -44,6 +44,7 @@ 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.PredefinedCoordinatesService;
import com.uam.wmi.findmytutor.service.PredefinedStatusesService;
import com.uam.wmi.findmytutor.service.UserService;
import com.uam.wmi.findmytutor.utils.ApproximatedLocalization;
@ -365,7 +366,7 @@ public class MapActivity extends BaseActivity
private void sendLocation(String body, LatLng latLng) {
PredefinedStatusesService predefinedStatusesService = ApiClient.getClient(getApplicationContext()).create(PredefinedStatusesService.class);
PredefinedCoordinatesService predefinedCoordinatesService = ApiClient.getClient(getApplicationContext()).create(PredefinedCoordinatesService.class);
PredefinedCoordViewModel droppedMarkercoordinate = new PredefinedCoordViewModel(
latLng.getLatitude(),
@ -379,7 +380,7 @@ public class MapActivity extends BaseActivity
CompositeDisposable disposable = new CompositeDisposable();
disposable.add(
predefinedStatusesService.postUserPredefinedCoord(PrefUtils.getUserId(getApplicationContext()), droppedMarkercoordinate)
predefinedCoordinatesService.postUserPredefinedCoord(PrefUtils.getUserId(getApplicationContext()), droppedMarkercoordinate)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::SaveCurrentManualLocation, this::handleError)

View File

@ -1,7 +1,9 @@
package com.uam.wmi.findmytutor.activity;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.ListPreference;
@ -21,15 +23,21 @@ import com.jakewharton.retrofit2.adapter.rxjava2.HttpException;
import com.uam.wmi.findmytutor.R;
import com.uam.wmi.findmytutor.model.PredefinedCoordViewModel;
import com.uam.wmi.findmytutor.network.ApiClient;
import com.uam.wmi.findmytutor.service.PredefinedCoordinatesService;
import com.uam.wmi.findmytutor.service.PredefinedDataService;
import com.uam.wmi.findmytutor.service.PredefinedStatusesService;
import com.uam.wmi.findmytutor.utils.Const;
import com.uam.wmi.findmytutor.utils.CustomListPreference2;
import com.uam.wmi.findmytutor.utils.EnableSharingDialog;
import com.uam.wmi.findmytutor.utils.PrefUtils;
import com.uam.wmi.findmytutor.utils.RestApiHelper;
import com.uam.wmi.findmytutor.utils.RightButtonPreference;
import com.uam.wmi.findmytutor.utils.SharingLevel;
import org.apache.commons.collections4.BidiMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
@ -49,13 +57,19 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
protected ListPreference manualLocationList;
protected PreferenceCategory preferenceCategory;
protected RightButtonPreference manualLocationButton;
protected RightButtonPreference removeManualLocation;
protected RightButtonPreference removeManualStatus;
protected Preference manualStatus;
protected ListPreference statusList;
private HashMap<String,String> locationMap;
private ArrayList <String> locationUUIDs;
protected List<PredefinedCoordViewModel> predefinedCoordsList = new ArrayList<>();
private HashMap<Integer, String> locationLevelMapping;
private HashMap<Integer, String> statusMapping;
private PredefinedStatusesService statusesService;
private PredefinedCoordinatesService locationService;
private CompositeDisposable disposable;
private AlertDialog.Builder builder;
public static SharingFragment newInstance() {
return new SharingFragment();
@ -82,7 +96,7 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
}
void getLocations(CompositeDisposable disposable) {
disposable.add(statusesService.getUserPredefinedCoords(PrefUtils.getUserId(getApplicationContext()))
disposable.add(locationService.getUserPredefinedCoords(PrefUtils.getUserId(getApplicationContext()))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableSingleObserver<List<PredefinedCoordViewModel>>() {
@ -91,7 +105,12 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
public void onSuccess(List<PredefinedCoordViewModel> coords) {
String currentCoordId = PrefUtils.getCurrentManualLocation(getApplicationContext());
locationMap = new HashMap<String,String>();
locationUUIDs = new ArrayList<String>();
for (PredefinedCoordViewModel i : coords) locationMap.put(i.getPredefinedCoordinateId(),i.getName());
for (PredefinedCoordViewModel i : coords) locationUUIDs.add(i.getPredefinedCoordinateId());
List<String> predefinedLocationsNames = Stream.of(coords).map(PredefinedCoordViewModel::getName).toList();
List<String> predefinedLocationsUUIDs = Stream.of(coords).map(PredefinedCoordViewModel::getPredefinedCoordinateId).toList();
predefinedCoordsList.addAll(coords);
String[] stringnames = predefinedLocationsNames.toArray(new String[0]);
@ -124,9 +143,13 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
preferenceCategory = (PreferenceCategory) findPreference("category_sharing");
manualLocationList = (ListPreference) findPreference("key_manual_location_value");
manualLocationButton = (RightButtonPreference) findPreference("manual_location_button");
removeManualLocation = (RightButtonPreference) findPreference("remove_manual_location");
builder = new AlertDialog.Builder(getActivity());
removeManualStatus = (RightButtonPreference) findPreference("remove_manual_status");
manualStatus = findPreference("key_manual_status");
statusList = (ListPreference) findPreference("key_status_value");
statusesService = ApiClient.getClient(getApplicationContext()).create(PredefinedStatusesService.class);
locationService = ApiClient.getClient(getApplicationContext()).create(PredefinedCoordinatesService.class);
disposable = new CompositeDisposable();
getStatuses(disposable);
getLocations(disposable);
@ -155,7 +178,6 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
locationMode.setOnPreferenceChangeListener((preference, newValue) -> {
PrefUtils.storeLocationMode(getApplicationContext(), locationLevelMapping.get(Integer.parseInt((String) newValue)));
if (PrefUtils.getLocationLevel(getApplicationContext()).equals(SharingLevel.MANUAL.toString())) {
if (!predefinedCoordsList.isEmpty()) {
preferenceCategory.addPreference(manualLocationList);
@ -226,6 +248,39 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
return true;
});
removeManualStatus.setOnPreferenceChangeListener((preference, newValue) -> {
showRemoveDialog(statusList.getEntries(),"status");
return true;
});
removeManualLocation.setOnPreferenceChangeListener(((preference, newValue) -> {
showRemoveDialog(manualLocationList.getEntries(),"location");
return true;
}));
}
public void showRemoveDialog(CharSequence[] entries, String service){
boolean [] checked = new boolean[entries.length];
ArrayList<String> tobeDeleted = new ArrayList<String>();
Log.d("sharingDialog", "no to siup");
builder.setPositiveButton("DELETE", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (int i=0; i< entries.length; i++){
if(checked[i] == true) {
tobeDeleted.add((String) entries[i]);
}
}
removeEntries(service,tobeDeleted);
Log.d("MANAGE-PREF",tobeDeleted.toString());
}
});
builder.setMultiChoiceItems(entries, checked, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
}
});
builder.create().show();
}
@Override
@ -236,6 +291,35 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
return view;
}
protected void removeEntries(String service, ArrayList<String> toBeDeleted){
Log.d("MANAGE-PREF", toBeDeleted.toString());
if(service.equals("status")){
for (String uuid:toBeDeleted) {
disposable.add(statusesService.deleteUserPredefinedStatus(PrefUtils.getUserId(getApplicationContext()), uuid)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::handleDeleteStatuses, this::handleError));
}
}else {
ArrayList<String> uuidsToBeDeleted = new ArrayList<String>();
for (String name:toBeDeleted) {
for (String uuid:locationUUIDs) {
if(locationMap.get(uuid).equals(name)){
uuidsToBeDeleted.add(uuid);
}
}
}
for (String uuid : uuidsToBeDeleted) {
disposable.add(locationService.deleteUserPredefinedCoord(PrefUtils.getUserId(getApplicationContext()), uuid)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::handleDeleteLocations, this::handleError));
}
}
}
protected void setListPreferenceData(ListPreference lp, String[] entries, Integer activeId) {
try {
@ -280,6 +364,13 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
Toast.makeText(getApplicationContext(),
"Network error " + error.getMessage(), Toast.LENGTH_SHORT).show();
}
}
private void handleDeleteStatuses(List<String> resp){
String[] statusesArray = resp.toArray(new String[resp.size()]);
setListPreferenceData(statusList, statusesArray, null);
}
private void handleDeleteLocations(List<PredefinedCoordViewModel> resp){
getLocations(disposable);
}
@Override

View File

@ -11,12 +11,10 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import com.annimon.stream.Collectors;
import com.annimon.stream.Stream;
import com.uam.wmi.findmytutor.R;
import com.uam.wmi.findmytutor.model.DutyHourViewModel;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
@ -114,16 +112,16 @@ public class DutyHoursAdapter extends RecyclerView.Adapter<DutyHoursAdapter.MyV
@Override
public void onClick(View v) {
try{
Log.d("DutyIndex bf rm size",Integer.toString(getItemCount()));
Log.d("DutyIndex bf rm pos",Integer.toString(holder.getLayoutPosition()));
// Log.d("DutyIndex bf rm size",Integer.toString(getItemCount()));
// Log.d("DutyIndex bf rm pos",Integer.toString(holder.getLayoutPosition()));
removeDuty(holder.getLayoutPosition());
Log.d("DutyIndex af rm pos",Integer.toString(holder.getLayoutPosition()));
// Log.d("DutyIndex af rm pos",Integer.toString(holder.getLayoutPosition()));
}catch(Error e){
Log.e("DutyIndex error",e.getMessage());
Log.e("DutyIndex size",Integer.toString(getItemCount()));
Log.e("DutyIndex pos",Integer.toString(holder.getAdapterPosition()));
// Log.e("DutyIndex error",e.getMessage());
// Log.e("DutyIndex size",Integer.toString(getItemCount()));
// Log.e("DutyIndex pos",Integer.toString(holder.getAdapterPosition()));
}

View File

@ -0,0 +1,24 @@
package com.uam.wmi.findmytutor.service;
import com.uam.wmi.findmytutor.model.PredefinedCoordViewModel;
import java.util.List;
import io.reactivex.Single;
import retrofit2.http.Body;
import retrofit2.http.DELETE;
import retrofit2.http.GET;
import retrofit2.http.HTTP;
import retrofit2.http.POST;
import retrofit2.http.Path;
public interface PredefinedCoordinatesService {
@GET("api/users/predefined/coordinate/{tutorId}")
Single<List<PredefinedCoordViewModel>> getUserPredefinedCoords(@Path("tutorId") String tutorId);
@POST("api/users/predefined/coordinate/{tutorId}")
Single <PredefinedCoordViewModel> postUserPredefinedCoord(@Path("tutorId") String tutorId, @Body PredefinedCoordViewModel coord);
@HTTP(method = "DELETE", path = "api/users/predefined/coordinate/{tutorId}", hasBody = true)
Single<List<PredefinedCoordViewModel>> deleteUserPredefinedCoord(@Path("tutorId") String tutorId, @Body String uuid);
}

View File

@ -5,6 +5,7 @@ import io.reactivex.Single;
import retrofit2.http.Body;
import retrofit2.http.DELETE;
import retrofit2.http.GET;
import retrofit2.http.HTTP;
import retrofit2.http.POST;
import retrofit2.http.Path;
@ -15,15 +16,16 @@ public interface PredefinedStatusesService {
@POST("api/users/predefined/status/{tutorId}")
Single<List<String>> postUserPredefinedStatus(@Path("tutorId") String tutorId, @Body String status);
@DELETE("api/users/predefined/status/{tutorId}")
// @DELETE("api/users/predefined/status/{tutorId}")
@HTTP(method = "DELETE", path = "api/users/predefined/status/{tutorId}", hasBody = true)
Single<List<String>> deleteUserPredefinedStatus(@Path("tutorId") String tutorId, @Body String status);
@GET("api/users/predefined/coordinate/{tutorId}")
/* @GET("api/users/predefined/coordinate/{tutorId}")
Single<List<PredefinedCoordViewModel>> getUserPredefinedCoords(@Path("tutorId") String tutorId);
@POST("api/users/predefined/coordinate/{tutorId}")
Single <PredefinedCoordViewModel> postUserPredefinedCoord(@Path("tutorId") String tutorId, @Body PredefinedCoordViewModel coord);
@DELETE("api/users/predefined/coordinate/{tutorId}")
Single<List<PredefinedCoordViewModel>> deleteUserPredefinedCoord(@Path("tutorId") String tutorId, @Body PredefinedCoordViewModel coord);
Single<List<PredefinedCoordViewModel>> deleteUserPredefinedCoord(@Path("tutorId") String tutorId, @Body PredefinedCoordViewModel coord);*/
}

View File

@ -2,6 +2,8 @@ package com.uam.wmi.findmytutor.utils;
import android.content.Context;
import android.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
@ -9,25 +11,41 @@ import com.uam.wmi.findmytutor.R;
public class RightButtonPreference extends Preference {
private Button prefButton;
private String buttonText;
public RightButtonPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setWidgetLayoutResource(R.layout.preference_button_widget);
for (int i=0;i<attrs.getAttributeCount();i++) {
String attr = attrs.getAttributeName(i);
String val = attrs.getAttributeValue(i);
if (attr.equalsIgnoreCase("text")) {
buttonText = val;
Log.d("RightButtonPreference", "step = " + val);
}
}
}
public void setText(String text){
prefButton.setText(text);
}
@Override
protected View onCreateView(ViewGroup parent) {
View view = super.onCreateView(parent);
// LayoutInflater li = (LayoutInflater)getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
// View temp =li.inflate( R.layout.preference_button_widget, parent, false);
return view;
}
@Override
protected void onBindView(View view)
{
super.onBindView(view);
Button button = (Button)view.findViewById(R.id.button_choose_from_map);
if(button != null)
prefButton = (Button) view.findViewById(R.id.button_choose_from_map);
prefButton.setText(buttonText);
if(prefButton != null)
{
button.setOnClickListener(new View.OnClickListener() {
prefButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
callChangeListener(null);

View File

@ -30,6 +30,12 @@
android:title="@string/title_list_manual_location" />
<com.uam.wmi.findmytutor.utils.RightButtonPreference
android:key="manual_location_button"
android:text="CHOOSE FROM MAP"
/>
<com.uam.wmi.findmytutor.utils.RightButtonPreference
android:key="remove_manual_location"
android:text="Manage Saved"
/>
</PreferenceCategory>
@ -51,6 +57,11 @@
android:singleLine="true"
android:title="@string/title_manual_status"
/>
<com.uam.wmi.findmytutor.utils.RightButtonPreference
android:key="remove_manual_status"
android:text="Manage Saved"
/>
</PreferenceCategory>

View File

@ -74,6 +74,7 @@
<item>2</item>
</string-array>
<string-array name="language_entries">
<item>@string/lang_eng</item>
<item>@string/lang_pol</item>
</string-array>
@ -81,4 +82,19 @@
<item>0</item>
<item>1</item>
</string-array>
<string-array name="yourArray">
<item>Item 1</item>
<item>Item 2</item>
<item>Item 3</item>
<item>Item 4</item>
</string-array>
<string-array name="yourValues">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
</resources>