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