Replace Single<Boolean> by Completable for login call

This commit is contained in:
Shinokuni 2020-10-24 19:24:19 +02:00
parent ecdc714316
commit e37853a619
6 changed files with 32 additions and 51 deletions

View File

@ -42,7 +42,7 @@ public class AccountViewModel extends ViewModel {
() -> DefinitionParametersKt.parametersOf(account));
}
public Single<Boolean> login(Account account, boolean insert) {
public Completable login(Account account, boolean insert) {
return repository.login(account, insert);
}

View File

@ -2,6 +2,7 @@ package com.readrops.app.account;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.util.Patterns;
import android.view.KeyEvent;
import android.view.MenuItem;
@ -19,9 +20,7 @@ import com.readrops.db.entities.account.AccountType;
import org.koin.androidx.viewmodel.compat.ViewModelCompat;
import io.reactivex.Completable;
import io.reactivex.CompletableObserver;
import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
@ -32,6 +31,8 @@ import static com.readrops.app.utils.ReadropsKeys.EDIT_ACCOUNT;
public class AddAccountActivity extends AppCompatActivity {
private static final String TAG = AddAccountActivity.class.getSimpleName();
private ActivityAddAccountBinding binding;
private AccountViewModel viewModel;
@ -101,7 +102,7 @@ public class AddAccountActivity extends AppCompatActivity {
viewModel.login(account, true)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new SingleObserver<Boolean>() {
.subscribe(new CompletableObserver() {
@Override
public void onSubscribe(Disposable d) {
@ -110,33 +111,25 @@ public class AddAccountActivity extends AppCompatActivity {
}
@Override
public void onSuccess(Boolean success) {
binding.addAccountLoading.setVisibility(View.GONE);
public void onComplete() {
saveLoginPassword(account);
if (success) {
saveLoginPassword(account);
if (forwardResult) {
Intent intent = new Intent();
intent.putExtra(ACCOUNT, account);
setResult(RESULT_OK, intent);
finish();
} else {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra(ACCOUNT, account);
startActivity(intent);
}
finish();
if (forwardResult) {
Intent intent = new Intent();
intent.putExtra(ACCOUNT, account);
setResult(RESULT_OK, intent);
} else {
binding.addAccountValidate.setEnabled(true);
Utils.showSnackbar(binding.addAccountRoot, getString(R.string.login_failed));
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra(ACCOUNT, account);
startActivity(intent);
}
finish();
}
@Override
public void onError(Throwable e) {
Log.d(TAG, e.getMessage());
binding.addAccountLoading.setVisibility(View.GONE);
binding.addAccountValidate.setEnabled(true);
@ -198,20 +191,8 @@ public class AddAccountActivity extends AppCompatActivity {
private void updateAccount() {
viewModel.login(accountToEdit, false)
.doOnError(throwable -> Utils.showSnackbar(binding.addAccountRoot, throwable.getMessage()))
.flatMapCompletable(b -> {
if (b) {
saveLoginPassword(accountToEdit);
return viewModel.update(accountToEdit);
} else {
runOnUiThread(() -> {
binding.addAccountLoading.setVisibility(View.GONE);
binding.addAccountValidate.setEnabled(true);
Utils.showSnackbar(binding.addAccountRoot, getString(R.string.login_failed));
});
return Completable.never();
}
})
.doAfterTerminate(() -> saveLoginPassword(accountToEdit))
.andThen(viewModel.update(accountToEdit))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new CompletableObserver() {

View File

@ -54,8 +54,7 @@ public abstract class ARepository {
.setCredentials(account != null && !account.isLocal() ? Credentials.toCredentials(account) : null);
}
// TODO : replace Single by Completable
public abstract Single<Boolean> login(Account account, boolean insert);
public abstract Completable login(Account account, boolean insert);
public abstract Observable<Feed> sync(List<Feed> feeds);

View File

@ -42,7 +42,7 @@ public class FreshRSSRepository extends ARepository {
}
@Override
public Single<Boolean> login(Account account, boolean insert) {
public Completable login(Account account, boolean insert) {
setCredentials(account);
return dataSource.login(account.getLogin(), account.getPassword())
@ -57,19 +57,19 @@ public class FreshRSSRepository extends ARepository {
return dataSource.getUserInfo();
})
.flatMap(userInfo -> {
.flatMapCompletable(userInfo -> {
account.setDisplayedName(userInfo.getUserName());
if (insert) {
return database.accountDao().insert(account)
.flatMap(id -> {
.flatMapCompletable(id -> {
account.setId(id.intValue());
return Single.just(true);
return Completable.complete();
});
}
return Single.just(true);
return Completable.complete();
});
}

View File

@ -29,6 +29,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.List;
import io.reactivex.Completable;
import io.reactivex.Observable;
import io.reactivex.Single;
import kotlin.Pair;
@ -48,7 +49,7 @@ public class LocalFeedRepository extends ARepository {
}
@Override
public Single<Boolean> login(Account account, boolean insert) {
public Completable login(Account account, boolean insert) {
return null;
}

View File

@ -46,7 +46,7 @@ public class NextNewsRepository extends ARepository {
}
@Override
public Single<Boolean> login(Account account, boolean insert) {
public Completable login(Account account, boolean insert) {
setCredentials(account);
return Single.<NextNewsUser>create(emitter -> {
NextNewsUser user = dataSource.login();
@ -56,19 +56,19 @@ public class NextNewsRepository extends ARepository {
} else {
emitter.onError(new Exception("Login failed. Please check your credentials and your Nextcloud News setup."));
}
}).flatMap(user -> {
}).flatMapCompletable(user -> {
account.setDisplayedName(user.getDisplayName());
account.setCurrentAccount(true);
if (insert) {
return database.accountDao().insert(account)
.flatMap(id -> {
.flatMapCompletable(id -> {
account.setId(id.intValue());
return Single.just(true);
return Completable.complete();
});
}
return Single.just(true);
return Completable.complete();
});
}