When a new account is inserted, it becomes the current account

This commit is contained in:
Shinokuni 2019-05-21 13:29:18 +02:00
parent feba9b321e
commit bfc7f934bf
2 changed files with 33 additions and 5 deletions

View File

@ -142,14 +142,23 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
initRecyclerView();
account = getIntent().getParcelableExtra(ACCOUNT_KEY);
if (account != null) {
if (account != null) { // new inserted account
buildDrawer();
refreshLayout.setRefreshing(true);
onRefresh();
} else {
viewModel.setCurrentAccountsToFalse(account.getId())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe();
viewModel.setRepository(account.getAccountType(), getApplication());
} else { // last current account
viewModel.getCurrentAccount().observe(this, account1 -> {
account = account1;
viewModel.setRepository(account.getAccountType(), getApplication());
buildDrawer();
});
}
@ -517,7 +526,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
.subscribe(new Observer<Feed>() {
@Override
public void onSubscribe(Disposable d) {
if (isAccountLocal()) {
if (isAccountLocal() && feedNb > 0) {
syncProgressLayout.setVisibility(View.VISIBLE);
syncProgressBar.setProgress(0);
}
@ -525,7 +534,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
@Override
public void onNext(Feed feed) {
if (isAccountLocal()) {
if (isAccountLocal() && feedNb > 0) {
syncProgress.setText(getString(R.string.updating_feed, feed.getName()));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
syncProgressBar.setProgress((feedCount * 100) / feedNb, true);
@ -546,7 +555,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
@Override
public void onComplete() {
if (isAccountLocal()) {
if (isAccountLocal() && feedNb > 0) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
syncProgressBar.setProgress(100, true);
else

View File

@ -17,6 +17,7 @@ import com.readrops.app.database.entities.Feed;
import com.readrops.app.database.entities.Folder;
import com.readrops.app.database.pojo.ItemWithFeed;
import com.readrops.app.repositories.ARepository;
import com.readrops.app.repositories.LocalFeedRepository;
import com.readrops.app.repositories.NextNewsRepository;
import java.util.ArrayList;
@ -51,6 +52,17 @@ public class MainViewModel extends AndroidViewModel {
itemsWithFeed = new MediatorLiveData<>();
}
public void setRepository(Account.AccountType accountType, Application application) {
switch (accountType) {
case LOCAL:
repository = new LocalFeedRepository(application);
break;
case NEXTCLOUD_NEWS:
repository = new NextNewsRepository(application);
break;
}
}
private void buildPagedList() {
if (lastFetch != null)
itemsWithFeed.removeSource(lastFetch);
@ -134,6 +146,13 @@ public class MainViewModel extends AndroidViewModel {
return db.accountDao().selectCurrentAccount();
}
public Completable setCurrentAccountsToFalse(int accountId) {
return Completable.create(emitter -> {
db.accountDao().setCurrentAccountsToFalse(accountId);
emitter.onComplete();
});
}
public Completable setItemReadState(int itemId, boolean read, boolean readChanged) {
return Completable.create(emitter -> {
db.itemDao().setReadState(itemId, read ? 1 : 0, readChanged ? 1 : 0);