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)); () -> DefinitionParametersKt.parametersOf(account));
} }
public Single<Boolean> login(Account account, boolean insert) { public Completable login(Account account, boolean insert) {
return repository.login(account, insert); return repository.login(account, insert);
} }

View File

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

View File

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

View File

@ -42,7 +42,7 @@ public class FreshRSSRepository extends ARepository {
} }
@Override @Override
public Single<Boolean> login(Account account, boolean insert) { public Completable login(Account account, boolean insert) {
setCredentials(account); setCredentials(account);
return dataSource.login(account.getLogin(), account.getPassword()) return dataSource.login(account.getLogin(), account.getPassword())
@ -57,19 +57,19 @@ public class FreshRSSRepository extends ARepository {
return dataSource.getUserInfo(); return dataSource.getUserInfo();
}) })
.flatMap(userInfo -> { .flatMapCompletable(userInfo -> {
account.setDisplayedName(userInfo.getUserName()); account.setDisplayedName(userInfo.getUserName());
if (insert) { if (insert) {
return database.accountDao().insert(account) return database.accountDao().insert(account)
.flatMap(id -> { .flatMapCompletable(id -> {
account.setId(id.intValue()); 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.Collections;
import java.util.List; import java.util.List;
import io.reactivex.Completable;
import io.reactivex.Observable; import io.reactivex.Observable;
import io.reactivex.Single; import io.reactivex.Single;
import kotlin.Pair; import kotlin.Pair;
@ -48,7 +49,7 @@ public class LocalFeedRepository extends ARepository {
} }
@Override @Override
public Single<Boolean> login(Account account, boolean insert) { public Completable login(Account account, boolean insert) {
return null; return null;
} }

View File

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