niedetrymisnistyczne zachowanie statusy

This commit is contained in:
Domagalski 2019-01-14 13:59:00 +01:00
parent fae7a5f0fe
commit 01c75bf96d

View File

@ -12,6 +12,7 @@ import android.preference.Preference;
import android.preference.PreferenceCategory; import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment; import android.preference.PreferenceFragment;
import android.preference.SwitchPreference; import android.preference.SwitchPreference;
import android.support.design.widget.Snackbar;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -23,6 +24,7 @@ import com.annimon.stream.Stream;
import com.jakewharton.retrofit2.adapter.rxjava2.HttpException; import com.jakewharton.retrofit2.adapter.rxjava2.HttpException;
import com.uam.wmi.findmytutor.R; import com.uam.wmi.findmytutor.R;
import com.uam.wmi.findmytutor.model.PredefinedCoordViewModel; 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.PredefinedCoordinatesService; import com.uam.wmi.findmytutor.service.PredefinedCoordinatesService;
import com.uam.wmi.findmytutor.service.PredefinedStatusesService; import com.uam.wmi.findmytutor.service.PredefinedStatusesService;
@ -33,14 +35,19 @@ 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 org.reactivestreams.Subscriber;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable; import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.observers.DisposableSingleObserver; import io.reactivex.observers.DisposableSingleObserver;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody; import okhttp3.ResponseBody;
@ -71,6 +78,8 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
private String[] statusesArray; private String[] statusesArray;
private boolean statusSwitchFlag; private boolean statusSwitchFlag;
private ArrayList<String> predefinedLocationsList; private ArrayList<String> predefinedLocationsList;
private List<String> afterDeleted = new ArrayList<>();
public static SharingFragment newInstance() { public static SharingFragment newInstance() {
return new SharingFragment(); return new SharingFragment();
@ -329,16 +338,62 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
return view; return view;
} }
protected void removeEntries(String service, ArrayList<String> toBeDeleted) { private Observable<List<String>> deleteStatus(String statusId) {
String myID = PrefUtils.getUserId(getApplicationContext());
return statusesService
.deleteUserPredefinedStatus(myID, statusId)
.toObservable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
protected void removeEntries(String service, ArrayList<String> toBeDeleted) {
// Log.d("MANAGE-PREF", toBeDeleted.toString()); // Log.d("MANAGE-PREF", toBeDeleted.toString());
if (service.equals("status")) { if (service.equals("status")) {
for (String uuid : toBeDeleted) { Observable<String> toDelete = Observable.fromIterable(toBeDeleted);
disposable.add(statusesService.deleteUserPredefinedStatus(PrefUtils.getUserId(getApplicationContext()), uuid)
.subscribeOn(Schedulers.io()) disposable.add(
.observeOn(AndroidSchedulers.mainThread()) toDelete.subscribeOn(Schedulers.io())
.subscribe(this::handleDeleteStatuses, this::handleError)); .observeOn(AndroidSchedulers.mainThread())
} .flatMap(this::deleteStatus)
.subscribeWith(new DisposableObserver<List<String>>(){
@Override
public void onNext(List<String> oo) {
Log.e("onNext", oo.toString());
afterDeleted = oo;
}
@Override
public void onError(Throwable e) {
showError(e);
}
@Override
public void onComplete() {
Log.e("onComplete", afterDeleted.toString());
if (toBeDeleted.size() == statusesArray.length){
Log.e("onComplete taaak", afterDeleted.toString());
afterDeleted.clear();
}
handleDeleteStatuses(afterDeleted);
}
})
);
//
// for (String uuid : toBeDeleted) {
// disposable.add(statusesService.deleteUserPredefinedStatus(PrefUtils.getUserId(getApplicationContext()), uuid)
// .subscribeOn(Schedulers.io())
// .observeOn(AndroidSchedulers.mainThread())
// .subscribe(this::handleDeleteStatuses, this::handleError));
// }
} else { } else {
ArrayList<String> uuidsToBeDeleted = new ArrayList<String>(); ArrayList<String> uuidsToBeDeleted = new ArrayList<String>();
for (String name : toBeDeleted) { for (String name : toBeDeleted) {
@ -411,12 +466,13 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
} }
private void handleDeleteStatuses(List<String> resp) { private void handleDeleteStatuses(List<String> resp) {
// getStatuses(disposable);
String currentEntry = PrefUtils.getUserStatus(getApplicationContext());
if (resp.isEmpty()) { if (resp.isEmpty()) {
disableStatusPreferences(); disableStatusPreferences();
} else { } else {
String[] statusesArray = resp.toArray(new String[resp.size()]); String[] statusesArray = resp.toArray(new String[resp.size()]);
setListPreferenceData(statusList, statusesArray, null); setListPreferenceData(statusList, statusesArray, null);
String currentEntry = PrefUtils.getUserStatus(getApplicationContext());
if (resp.contains(currentEntry)) { if (resp.contains(currentEntry)) {
statusList.setValueIndex(resp.indexOf(currentEntry)); statusList.setValueIndex(resp.indexOf(currentEntry));
} else { } else {
@ -507,4 +563,20 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
disposable.dispose(); disposable.dispose();
} }
private void showError(Throwable e) {
String message;
if (e instanceof HttpException) {
ResponseBody responseBody = ((HttpException) e).response().errorBody();
message = RestApiHelper.getErrorMessage(responseBody);
if (((HttpException) e).response().code() == 404) {
message = getString(R.string.no_such_a_user);
}
} else {
message = "Network Error !";
}
Log.e("ERR", message);
Toast.makeText(getApplicationContext(), message, Snackbar.LENGTH_LONG).show();
}
} }