From 01c75bf96d67770336d365d794ae964f5c54f348 Mon Sep 17 00:00:00 2001 From: Domagalski Date: Mon, 14 Jan 2019 13:59:00 +0100 Subject: [PATCH] niedetrymisnistyczne zachowanie statusy --- .../findmytutor/activity/SharingFragment.java | 88 +++++++++++++++++-- 1 file changed, 80 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/uam/wmi/findmytutor/activity/SharingFragment.java b/app/src/main/java/com/uam/wmi/findmytutor/activity/SharingFragment.java index 5ce1844..137c5a0 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/activity/SharingFragment.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/activity/SharingFragment.java @@ -12,6 +12,7 @@ import android.preference.Preference; import android.preference.PreferenceCategory; import android.preference.PreferenceFragment; import android.preference.SwitchPreference; +import android.support.design.widget.Snackbar; import android.util.Log; import android.view.LayoutInflater; import android.view.View; @@ -23,6 +24,7 @@ import com.annimon.stream.Stream; 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.model.User; import com.uam.wmi.findmytutor.network.ApiClient; import com.uam.wmi.findmytutor.service.PredefinedCoordinatesService; 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.SharingLevel; +import org.reactivestreams.Subscriber; + import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Objects; +import io.reactivex.Observable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.observers.DisposableObserver; import io.reactivex.observers.DisposableSingleObserver; import io.reactivex.schedulers.Schedulers; import okhttp3.ResponseBody; @@ -71,6 +78,8 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere private String[] statusesArray; private boolean statusSwitchFlag; private ArrayList predefinedLocationsList; + private List afterDeleted = new ArrayList<>(); + public static SharingFragment newInstance() { return new SharingFragment(); @@ -329,16 +338,62 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere return view; } - protected void removeEntries(String service, ArrayList toBeDeleted) { + private Observable> 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 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)); - } + Observable toDelete = Observable.fromIterable(toBeDeleted); + + disposable.add( + toDelete.subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .flatMap(this::deleteStatus) + .subscribeWith(new DisposableObserver>(){ + @Override + public void onNext(List 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 { ArrayList uuidsToBeDeleted = new ArrayList(); for (String name : toBeDeleted) { @@ -411,12 +466,13 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere } private void handleDeleteStatuses(List resp) { +// getStatuses(disposable); + String currentEntry = PrefUtils.getUserStatus(getApplicationContext()); if (resp.isEmpty()) { disableStatusPreferences(); } else { String[] statusesArray = resp.toArray(new String[resp.size()]); setListPreferenceData(statusList, statusesArray, null); - String currentEntry = PrefUtils.getUserStatus(getApplicationContext()); if (resp.contains(currentEntry)) { statusList.setValueIndex(resp.indexOf(currentEntry)); } else { @@ -507,4 +563,20 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere 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(); + } + }