mirror of https://github.com/readrops/Readrops.git
Put account logic in the view model
This commit is contained in:
parent
4df3087691
commit
00f331a18f
|
@ -48,7 +48,6 @@ import com.readrops.app.viewmodels.MainViewModel;
|
|||
import com.readrops.app.views.MainItemListAdapter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -94,9 +93,6 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
|
|||
|
||||
private ActionMode actionMode;
|
||||
|
||||
private Account currentAccount;
|
||||
private List<Account> accounts;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
@ -144,6 +140,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
|
|||
});
|
||||
|
||||
drawerManager.setHeaderListener((view, profile, current) -> {
|
||||
if (!current) {
|
||||
int id = (int) profile.getIdentifier();
|
||||
|
||||
switch (id) {
|
||||
|
@ -153,34 +150,36 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
|
|||
break;
|
||||
case DrawerManager.ACCOUNT_SETTINGS_ID:
|
||||
break;
|
||||
default:
|
||||
viewModel.setCurrentAccount(id);
|
||||
updateDrawerFeeds();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
currentAccount = getIntent().getParcelableExtra(ACCOUNT_KEY);
|
||||
Account currentAccount = getIntent().getParcelableExtra(ACCOUNT_KEY);
|
||||
|
||||
if (currentAccount != null) { // first account created
|
||||
drawer = drawerManager.buildDrawer(Collections.singletonList(currentAccount));
|
||||
viewModel.setRepository(currentAccount.getAccountType(), getApplication());
|
||||
List<Account> accounts = new ArrayList<>();
|
||||
accounts.add(currentAccount);
|
||||
|
||||
viewModel.setAccounts(accounts);
|
||||
|
||||
drawer = drawerManager.buildDrawer(accounts);
|
||||
|
||||
if (!viewModel.isAccountLocal()) {
|
||||
refreshLayout.setRefreshing(true);
|
||||
onRefresh();
|
||||
}
|
||||
|
||||
} else { // last current account
|
||||
viewModel.getAllAccounts().observe(this, accounts -> {
|
||||
this.accounts = accounts;
|
||||
viewModel.setAccounts(accounts);
|
||||
|
||||
for (Account account1 : accounts) {
|
||||
if (account1.isCurrentAccount()) {
|
||||
currentAccount = account1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
viewModel.setRepository(currentAccount.getAccountType(), getApplication());
|
||||
|
||||
drawerManager.buildDrawer(accounts);
|
||||
drawer = drawerManager.buildDrawer(accounts);
|
||||
updateDrawerFeeds();
|
||||
});
|
||||
}
|
||||
|
@ -494,13 +493,15 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
|
|||
Account newAccount = data.getParcelableExtra(ACCOUNT_KEY);
|
||||
|
||||
if (newAccount != null) {
|
||||
currentAccount = newAccount;
|
||||
viewModel.addAccount(newAccount);
|
||||
|
||||
viewModel.setRepository(currentAccount.getAccountType(), getApplication());
|
||||
if (!viewModel.isAccountLocal()) {
|
||||
refreshLayout.setRefreshing(true);
|
||||
onRefresh();
|
||||
}
|
||||
|
||||
//drawerManager.
|
||||
drawerManager.resetItems();
|
||||
drawerManager.addAccount(newAccount);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -510,19 +511,20 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
|
|||
}
|
||||
|
||||
private void sync(List<Feed> feeds) {
|
||||
if (currentAccount.getLogin() == null)
|
||||
currentAccount.setLogin(SharedPreferencesManager.readString(this, currentAccount.getLoginKey()));
|
||||
Account account = viewModel.getCurrentAccount();
|
||||
if (account.getLogin() == null)
|
||||
account.setLogin(SharedPreferencesManager.readString(this, account.getLoginKey()));
|
||||
|
||||
if (currentAccount.getPassword() == null)
|
||||
currentAccount.setPassword(SharedPreferencesManager.readString(this, currentAccount.getPasswordKey()));
|
||||
if (viewModel.getCurrentAccount().getPassword() == null)
|
||||
account.setPassword(SharedPreferencesManager.readString(this, account.getPasswordKey()));
|
||||
|
||||
viewModel.sync(feeds, currentAccount)
|
||||
viewModel.sync(feeds, account)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Observer<Feed>() {
|
||||
@Override
|
||||
public void onSubscribe(Disposable d) {
|
||||
if (isAccountLocal() && feedNb > 0) {
|
||||
if (viewModel.isAccountLocal() && feedNb > 0) {
|
||||
syncProgressLayout.setVisibility(View.VISIBLE);
|
||||
syncProgressBar.setProgress(0);
|
||||
}
|
||||
|
@ -530,7 +532,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
|
|||
|
||||
@Override
|
||||
public void onNext(Feed feed) {
|
||||
if (isAccountLocal() && feedNb > 0) {
|
||||
if (viewModel.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);
|
||||
|
@ -551,7 +553,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
|
|||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
if (isAccountLocal() && feedNb > 0) {
|
||||
if (viewModel.isAccountLocal() && feedNb > 0) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
|
||||
syncProgressBar.setProgress(100, true);
|
||||
else
|
||||
|
@ -570,9 +572,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
|
|||
});
|
||||
}
|
||||
|
||||
private boolean isAccountLocal() {
|
||||
return currentAccount.getAccountType() == Account.AccountType.LOCAL;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
|
|
|
@ -28,7 +28,10 @@ public interface AccountDao {
|
|||
void updateLastModified(int accountId, long lastModified);
|
||||
|
||||
@Query("Update Account set current_account = 0 Where id Not In (:accountId)")
|
||||
void setCurrentAccountsToFalse(int accountId);
|
||||
void deselectOldCurrentAccount(int accountId);
|
||||
|
||||
@Query("Update Account set current_account = 1 Where id = :accountId")
|
||||
void setCurrentAccount(int accountId);
|
||||
|
||||
@Query("Select count(*) From Account Where account_type = :accounType")
|
||||
Integer getAccountCountByType(int accounType);
|
||||
|
|
|
@ -29,10 +29,10 @@ import static com.readrops.app.utils.Utils.drawableWithColor;
|
|||
|
||||
public class DrawerManager {
|
||||
|
||||
public static final int ARTICLES_ITEM_ID = 1;
|
||||
public static final int READ_LATER_ID = 2;
|
||||
public static final int ACCOUNT_SETTINGS_ID = 3;
|
||||
public static final int ADD_ACCOUNT_ID = 4;
|
||||
public static final int ARTICLES_ITEM_ID = -1;
|
||||
public static final int READ_LATER_ID = -2;
|
||||
public static final int ACCOUNT_SETTINGS_ID = -3;
|
||||
public static final int ADD_ACCOUNT_ID = -4;
|
||||
|
||||
private Activity activity;
|
||||
private Toolbar toolbar;
|
||||
|
@ -125,15 +125,15 @@ public class DrawerManager {
|
|||
|
||||
private void createAccountHeader(List<Account> accounts) {
|
||||
ProfileDrawerItem[] profileItems = new ProfileDrawerItem[accounts.size()];
|
||||
int currentAccountId = 1;
|
||||
|
||||
for (int i = 0; i < accounts.size(); i++) {
|
||||
Account account = accounts.get(i);
|
||||
|
||||
ProfileDrawerItem profileItem = new ProfileDrawerItem()
|
||||
.withIcon(Account.getLogoFromAccountType(account.getAccountType()))
|
||||
.withName(account.getDisplayedName())
|
||||
.withEmail(account.getAccountName());
|
||||
if (account.isCurrentAccount())
|
||||
currentAccountId = account.getId();
|
||||
|
||||
ProfileDrawerItem profileItem = createProfileItem(account);
|
||||
profileItems[i] = profileItem;
|
||||
}
|
||||
|
||||
|
@ -159,6 +159,16 @@ public class DrawerManager {
|
|||
.withHeaderBackgroundScaleType(ImageView.ScaleType.CENTER_CROP)
|
||||
.withOnAccountHeaderListener(headerListener)
|
||||
.build();
|
||||
|
||||
header.setActiveProfile(currentAccountId);
|
||||
}
|
||||
|
||||
private ProfileDrawerItem createProfileItem(Account account) {
|
||||
return new ProfileDrawerItem()
|
||||
.withIcon(Account.getLogoFromAccountType(account.getAccountType()))
|
||||
.withName(account.getDisplayedName())
|
||||
.withEmail(account.getAccountName())
|
||||
.withIdentifier(account.getId());
|
||||
}
|
||||
|
||||
private void addDefaultPlaces() {
|
||||
|
@ -178,4 +188,16 @@ public class DrawerManager {
|
|||
drawer.addItem(toReadLater);
|
||||
drawer.addItem(new DividerDrawerItem());
|
||||
}
|
||||
|
||||
public void addAccount(Account account) {
|
||||
ProfileDrawerItem profileItem = createProfileItem(account);
|
||||
|
||||
header.addProfiles(profileItem);
|
||||
header.setActiveProfile(profileItem.getIdentifier());
|
||||
}
|
||||
|
||||
public void resetItems() {
|
||||
drawer.removeAllItems();
|
||||
addDefaultPlaces();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ import com.readrops.app.repositories.LocalFeedRepository;
|
|||
import com.readrops.app.repositories.NextNewsRepository;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
@ -28,6 +29,8 @@ import java.util.TreeMap;
|
|||
import io.reactivex.Completable;
|
||||
import io.reactivex.Observable;
|
||||
import io.reactivex.Single;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
|
||||
public class MainViewModel extends AndroidViewModel {
|
||||
|
||||
|
@ -38,6 +41,9 @@ public class MainViewModel extends AndroidViewModel {
|
|||
|
||||
private ItemsListQueryBuilder queryBuilder;
|
||||
|
||||
private Account currentAccount;
|
||||
private List<Account> accounts;
|
||||
|
||||
public MainViewModel(@NonNull Application application) {
|
||||
super(application);
|
||||
|
||||
|
@ -52,13 +58,15 @@ public class MainViewModel extends AndroidViewModel {
|
|||
itemsWithFeed = new MediatorLiveData<>();
|
||||
}
|
||||
|
||||
public void setRepository(Account.AccountType accountType, Application application) {
|
||||
//region main query
|
||||
|
||||
private void setRepository(Account.AccountType accountType) {
|
||||
switch (accountType) {
|
||||
case LOCAL:
|
||||
repository = new LocalFeedRepository(application);
|
||||
repository = new LocalFeedRepository(getApplication());
|
||||
break;
|
||||
case NEXTCLOUD_NEWS:
|
||||
repository = new NextNewsRepository(application);
|
||||
repository = new NextNewsRepository(getApplication());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -142,21 +150,81 @@ public class MainViewModel extends AndroidViewModel {
|
|||
});
|
||||
}
|
||||
|
||||
public LiveData<Account> getCurrentAccount() {
|
||||
return db.accountDao().selectCurrentAccount();
|
||||
}
|
||||
//endregion
|
||||
|
||||
//region Account
|
||||
|
||||
public LiveData<List<Account>> getAllAccounts() {
|
||||
return db.accountDao().selectAll();
|
||||
}
|
||||
|
||||
public Completable setCurrentAccountsToFalse(int accountId) {
|
||||
private Completable deselectOldCurrentAccount(int accountId) {
|
||||
return Completable.create(emitter -> {
|
||||
db.accountDao().setCurrentAccountsToFalse(accountId);
|
||||
db.accountDao().deselectOldCurrentAccount(accountId);
|
||||
emitter.onComplete();
|
||||
});
|
||||
}
|
||||
|
||||
private Account getAccount(int id) {
|
||||
for (Account account : accounts) {
|
||||
if (account.getId() == id)
|
||||
return account;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void addAccount(Account account) {
|
||||
accounts.add(account);
|
||||
setCurrentAccount(account);
|
||||
}
|
||||
|
||||
public Account getCurrentAccount() {
|
||||
return currentAccount;
|
||||
}
|
||||
|
||||
public void setCurrentAccount(Account currentAccount) {
|
||||
this.currentAccount = currentAccount;
|
||||
setRepository(currentAccount.getAccountType());
|
||||
|
||||
// set the new account as the current one
|
||||
Completable setCurrentAccount = Completable.create(emitter -> {
|
||||
db.accountDao().setCurrentAccount(currentAccount.getId());
|
||||
emitter.onComplete();
|
||||
});
|
||||
|
||||
Completable.concat(Arrays.asList(setCurrentAccount, deselectOldCurrentAccount(currentAccount.getId())))
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
public void setCurrentAccount(int id) {
|
||||
setCurrentAccount(getAccount(id));
|
||||
}
|
||||
|
||||
|
||||
public void setAccounts(List<Account> accounts) {
|
||||
this.accounts = accounts;
|
||||
|
||||
for (Account account1 : accounts) {
|
||||
if (account1.isCurrentAccount()) {
|
||||
currentAccount = account1;
|
||||
setRepository(currentAccount.getAccountType());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isAccountLocal() {
|
||||
return currentAccount.getAccountType() == Account.AccountType.LOCAL;
|
||||
}
|
||||
|
||||
//endregion
|
||||
|
||||
|
||||
//region Item read state
|
||||
|
||||
public Completable setItemReadState(int itemId, boolean read, boolean readChanged) {
|
||||
return Completable.create(emitter -> {
|
||||
db.itemDao().setReadState(itemId, read ? 1 : 0, readChanged ? 1 : 0);
|
||||
|
@ -197,4 +265,6 @@ public class MainViewModel extends AndroidViewModel {
|
|||
READ_IT_LATER_FILTER,
|
||||
NO_FILTER
|
||||
}
|
||||
|
||||
//endregion
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue