Convert multiple api calls to flatMap with rxJava
This commit is contained in:
parent
35ef25b2b7
commit
cb1e7bcdef
@ -57,8 +57,12 @@ import java.util.Set;
|
|||||||
|
|
||||||
import butterknife.BindView;
|
import butterknife.BindView;
|
||||||
import butterknife.ButterKnife;
|
import butterknife.ButterKnife;
|
||||||
|
import io.reactivex.Observable;
|
||||||
|
import io.reactivex.ObservableSource;
|
||||||
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.functions.Function;
|
||||||
|
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;
|
||||||
@ -158,29 +162,44 @@ public class BlackList extends AppCompatActivity {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Observable<List<String>> getListOfBlacklistedUsers(String userId) {
|
||||||
|
return userService.getTutorBlacklistedByID(userId)
|
||||||
|
.toObservable()
|
||||||
|
.subscribeOn(Schedulers.io())
|
||||||
|
.observeOn(AndroidSchedulers.mainThread());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Observable<User> getUserObservable(String userId) {
|
||||||
|
return userService
|
||||||
|
.getUserById(userId)
|
||||||
|
.toObservable()
|
||||||
|
.subscribeOn(Schedulers.io())
|
||||||
|
.observeOn(AndroidSchedulers.mainThread());
|
||||||
|
}
|
||||||
|
|
||||||
private void fetchBlackListedUsers() {
|
private void fetchBlackListedUsers() {
|
||||||
for (String GUID : blacklistedUsersIDs){
|
disposable.add(getListOfBlacklistedUsers(PrefUtils.getUserId(getApplicationContext()))
|
||||||
disposable.add(
|
|
||||||
userService.getUserById(GUID)
|
|
||||||
.subscribeOn(Schedulers.io())
|
.subscribeOn(Schedulers.io())
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
.subscribeWith(new DisposableSingleObserver<User>() {
|
.flatMap((Function<List<String>, Observable<String>>) Observable::fromIterable)
|
||||||
|
.flatMap((Function<String, ObservableSource<User>>) this::getUserObservable)
|
||||||
|
.subscribeWith(new DisposableObserver<User>() {
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess(User user) {
|
public void onNext(User user) {
|
||||||
blacklistedUsers.add(user);
|
blacklistedUsers.add(user);
|
||||||
toggleEmptyNotes();
|
|
||||||
if (blacklistedUsers.size() == blacklistedUsersIDs.size()) {
|
|
||||||
mAdapter.notifyDataSetChanged();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onError(Throwable e) {
|
public void onError(Throwable e) {
|
||||||
showError(e);
|
showError(e);
|
||||||
}
|
}
|
||||||
})
|
|
||||||
);
|
@Override
|
||||||
|
public void onComplete() {
|
||||||
|
toggleEmptyNotes();
|
||||||
|
mAdapter.notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -235,6 +254,8 @@ public class BlackList extends AppCompatActivity {
|
|||||||
|
|
||||||
|
|
||||||
private void handleAddUser(User user) {
|
private void handleAddUser(User user) {
|
||||||
|
Toast.makeText(this, R.string.add_user_to_list, Snackbar.LENGTH_LONG).show();
|
||||||
|
|
||||||
blacklistedUsersIDs.clear();
|
blacklistedUsersIDs.clear();
|
||||||
blacklistedUsers.clear();
|
blacklistedUsers.clear();
|
||||||
blacklistedUsersIDs.addAll(user.getBlacklist());
|
blacklistedUsersIDs.addAll(user.getBlacklist());
|
||||||
|
@ -148,10 +148,10 @@ public class LoginActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void loginProcess(String email, String password) {
|
private void loginProcess(String email, String password) {
|
||||||
ValidateUser user = new ValidateUser(email, password);
|
//ValidateUser user = new ValidateUser(email, password);
|
||||||
//LdapUser fakeUser = new LdapUser(email, password,"wmi","tutor",email,"Fałszywy",email);
|
LdapUser fakeUser = new LdapUser(email, password,"wmi","tutor",email,"Fałszywy",email);
|
||||||
disposable.add(ldapService.validate(user)
|
//disposable.add(ldapService.validate(user)
|
||||||
//disposable.add(ldapService.fakeValidate(fakeUser)
|
disposable.add(ldapService.fakeValidate(fakeUser)
|
||||||
.subscribeOn(Schedulers.io())
|
.subscribeOn(Schedulers.io())
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
.subscribe(this::handleResponse, this::handleError));
|
.subscribe(this::handleResponse, this::handleError));
|
||||||
|
@ -46,8 +46,12 @@ import java.util.List;
|
|||||||
|
|
||||||
import butterknife.BindView;
|
import butterknife.BindView;
|
||||||
import butterknife.ButterKnife;
|
import butterknife.ButterKnife;
|
||||||
|
import io.reactivex.Observable;
|
||||||
|
import io.reactivex.ObservableSource;
|
||||||
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.functions.Function;
|
||||||
|
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;
|
||||||
@ -146,32 +150,45 @@ public class WhiteList extends AppCompatActivity {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Observable<List<String>> getListOfWhitelistedUsers(String userId) {
|
||||||
|
return userService.getTutorWhitelistedByID(userId)
|
||||||
|
.toObservable()
|
||||||
|
.subscribeOn(Schedulers.io())
|
||||||
|
.observeOn(AndroidSchedulers.mainThread());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Observable<User> getUserObservable(String userId) {
|
||||||
|
return userService
|
||||||
|
.getUserById(userId)
|
||||||
|
.toObservable()
|
||||||
|
.subscribeOn(Schedulers.io())
|
||||||
|
.observeOn(AndroidSchedulers.mainThread());
|
||||||
|
}
|
||||||
|
|
||||||
private void fetchWhiteListedUsers() {
|
private void fetchWhiteListedUsers() {
|
||||||
for (String GUID : whitelistedUsersIDs){
|
disposable.add(getListOfWhitelistedUsers(PrefUtils.getUserId(getApplicationContext()))
|
||||||
disposable.add(
|
|
||||||
userService.getUserById(GUID)
|
|
||||||
.subscribeOn(Schedulers.io())
|
.subscribeOn(Schedulers.io())
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
.subscribeWith(new DisposableSingleObserver<User>() {
|
.flatMap((Function<List<String>, Observable<String>>) Observable::fromIterable)
|
||||||
|
.flatMap((Function<String, ObservableSource<User>>) this::getUserObservable)
|
||||||
|
.subscribeWith(new DisposableObserver<User>() {
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess(User user) {
|
public void onNext(User user) {
|
||||||
whitelistedUsers.add(user);
|
whitelistedUsers.add(user);
|
||||||
toggleEmptyNotes();
|
|
||||||
|
|
||||||
if (whitelistedUsers.size() == whitelistedUsersIDs.size()) {
|
|
||||||
mAdapter.notifyDataSetChanged();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onError(Throwable e) {
|
public void onError(Throwable e) {
|
||||||
showError(e);
|
showError(e);
|
||||||
}
|
}
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onComplete() {
|
||||||
|
toggleEmptyNotes();
|
||||||
|
mAdapter.notifyDataSetChanged();
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
private void showFabDialog(View v){
|
private void showFabDialog(View v){
|
||||||
LayoutInflater layoutInflaterAndroid = LayoutInflater.from(getApplicationContext());
|
LayoutInflater layoutInflaterAndroid = LayoutInflater.from(getApplicationContext());
|
||||||
@ -223,6 +240,8 @@ public class WhiteList extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void handleAddUser(User user) {
|
private void handleAddUser(User user) {
|
||||||
|
Toast.makeText(this, R.string.add_user_to_list, Snackbar.LENGTH_LONG).show();
|
||||||
|
|
||||||
whitelistedUsersIDs.clear();
|
whitelistedUsersIDs.clear();
|
||||||
whitelistedUsers.clear();
|
whitelistedUsers.clear();
|
||||||
whitelistedUsersIDs.addAll(user.getWhitelist());
|
whitelistedUsersIDs.addAll(user.getWhitelist());
|
||||||
@ -231,6 +250,7 @@ public class WhiteList extends AppCompatActivity {
|
|||||||
|
|
||||||
private void showError(Throwable e) {
|
private void showError(Throwable e) {
|
||||||
String message;
|
String message;
|
||||||
|
Log.e("ERR", String.valueOf(e));
|
||||||
|
|
||||||
if (e instanceof HttpException) {
|
if (e instanceof HttpException) {
|
||||||
ResponseBody responseBody = ((HttpException) e).response().errorBody();
|
ResponseBody responseBody = ((HttpException) e).response().errorBody();
|
||||||
@ -241,6 +261,7 @@ public class WhiteList extends AppCompatActivity {
|
|||||||
} else {
|
} else {
|
||||||
message = "Network Error !";
|
message = "Network Error !";
|
||||||
}
|
}
|
||||||
|
Log.e("ERR",message);
|
||||||
Toast.makeText(this, message, Snackbar.LENGTH_LONG).show();
|
Toast.makeText(this, message, Snackbar.LENGTH_LONG).show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@ import java.util.List;
|
|||||||
|
|
||||||
import io.reactivex.Completable;
|
import io.reactivex.Completable;
|
||||||
import io.reactivex.Observable;
|
import io.reactivex.Observable;
|
||||||
|
import io.reactivex.ObservableEmitter;
|
||||||
import io.reactivex.Single;
|
import io.reactivex.Single;
|
||||||
import retrofit2.Response;
|
import retrofit2.Response;
|
||||||
import retrofit2.http.Body;
|
import retrofit2.http.Body;
|
||||||
@ -75,7 +76,7 @@ public interface UserService {
|
|||||||
Completable setUserInActive(@Path("userID") String userID);
|
Completable setUserInActive(@Path("userID") String userID);
|
||||||
|
|
||||||
@GET("api/users/blacklist/{tutorID}")
|
@GET("api/users/blacklist/{tutorID}")
|
||||||
Single<List<String>> getTutorBlacklistedByID(@Path("tutorID") String tutorID);
|
Single <List<String>> getTutorBlacklistedByID(@Path("tutorID") String tutorID);
|
||||||
|
|
||||||
@PUT("api/users/blacklist/{tutorID}")
|
@PUT("api/users/blacklist/{tutorID}")
|
||||||
Completable setTutorBlacklist(@Path("tutorID") String tutorID, @Body IsUsingListBool isUsing);
|
Completable setTutorBlacklist(@Path("tutorID") String tutorID, @Body IsUsingListBool isUsing);
|
||||||
|
@ -6,7 +6,7 @@ import java.util.Arrays;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Const {
|
public class Const {
|
||||||
public final static String BASE_URL = "https://s416084.projektstudencki.pl/master/";
|
public final static String BASE_URL = "https://s416084.projektstudencki.pl/develop/";
|
||||||
public final static Integer mapRefreshInterval = 5000;
|
public final static Integer mapRefreshInterval = 5000;
|
||||||
public final static Integer onlineBackgroundLocationInterval = 7000;
|
public final static Integer onlineBackgroundLocationInterval = 7000;
|
||||||
public final static Integer offlineBackgroundLocationInterval = 360000;
|
public final static Integer offlineBackgroundLocationInterval = 360000;
|
||||||
|
@ -266,5 +266,8 @@
|
|||||||
<string name="search_null">Brak wyników!</string>
|
<string name="search_null">Brak wyników!</string>
|
||||||
<string name="modal_manual_hint">Nadaj nazwę tej lokalizacji.</string>
|
<string name="modal_manual_hint">Nadaj nazwę tej lokalizacji.</string>
|
||||||
|
|
||||||
|
<!--(ENG) Blacked/Whited users -->
|
||||||
|
<string name="add_user_to_list">Użytkownik został dodany!</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
|
||||||
|
@ -437,4 +437,7 @@
|
|||||||
<string name="search_null">Search response is empty!</string>
|
<string name="search_null">Search response is empty!</string>
|
||||||
<string name="modal_manual_hint">Insert a name for this localization.</string>
|
<string name="modal_manual_hint">Insert a name for this localization.</string>
|
||||||
|
|
||||||
|
<!--(ENG) Blacked/Whited users -->
|
||||||
|
<string name="add_user_to_list">User has been added!</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
Loading…
Reference in New Issue
Block a user