Put drawer build in DrawerManager

This commit is contained in:
Shinokuni 2019-05-21 22:10:57 +02:00
parent 642617b1ce
commit 86a6daaf5b
3 changed files with 167 additions and 101 deletions

View File

@ -9,7 +9,6 @@ import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
@ -33,13 +32,8 @@ import com.bumptech.glide.Glide;
import com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader;
import com.bumptech.glide.util.ViewPreloadSizeProvider;
import com.github.clans.fab.FloatingActionMenu;
import com.mikepenz.materialdrawer.AccountHeader;
import com.mikepenz.materialdrawer.AccountHeaderBuilder;
import com.mikepenz.materialdrawer.Drawer;
import com.mikepenz.materialdrawer.DrawerBuilder;
import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
import com.mikepenz.materialdrawer.model.ProfileDrawerItem;
import com.mikepenz.materialdrawer.model.ProfileSettingDrawerItem;
import com.mikepenz.materialdrawer.model.SecondaryDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;
import com.readrops.app.R;
@ -54,6 +48,7 @@ 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;
@ -99,7 +94,8 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
private ActionMode actionMode;
private Account account;
private Account currentAccount;
private List<Account> accounts;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -141,75 +137,55 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
feedCount = 0;
initRecyclerView();
account = getIntent().getParcelableExtra(ACCOUNT_KEY);
if (account != null) { // new inserted account
buildDrawer();
drawerManager = new DrawerManager(this, toolbar, (view, position, drawerItem) -> {
handleDrawerClick(drawerItem);
return true;
});
drawerManager.setHeaderListener((view, profile, current) -> {
int id = (int) profile.getIdentifier();
switch (id) {
case DrawerManager.ADD_ACCOUNT_ID:
Intent intent = new Intent(this, AccountTypeListActivity.class);
startActivityForResult(intent, ADD_ACCOUNT_REQUEST);
break;
case DrawerManager.ACCOUNT_SETTINGS_ID:
break;
}
return true;
});
currentAccount = getIntent().getParcelableExtra(ACCOUNT_KEY);
if (currentAccount != null) { // first account created
drawer = drawerManager.buildDrawer(Collections.singletonList(currentAccount));
viewModel.setRepository(currentAccount.getAccountType(), getApplication());
refreshLayout.setRefreshing(true);
onRefresh();
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());
viewModel.getAllAccounts().observe(this, accounts -> {
this.accounts = accounts;
buildDrawer();
for (Account account1 : accounts) {
if (account1.isCurrentAccount()) {
currentAccount = account1;
break;
}
}
viewModel.setRepository(currentAccount.getAccountType(), getApplication());
drawerManager.buildDrawer(accounts);
updateDrawerFeeds();
});
}
}
private void buildDrawer() {
ProfileDrawerItem profileItem = new ProfileDrawerItem()
.withIcon(Account.getLogoFromAccountType(account.getAccountType()))
.withName(account.getDisplayedName())
.withEmail(account.getAccountName());
ProfileSettingDrawerItem profileSettingsItem = new ProfileSettingDrawerItem()
.withName(getString(R.string.account_settings))
.withIcon(R.drawable.ic_account);
ProfileSettingDrawerItem addAccountSettingsItem = new ProfileSettingDrawerItem()
.withName(getString(R.string.add_account))
.withIcon(R.drawable.ic_add_account_grey)
.withOnDrawerItemClickListener((view, position, drawerItem) -> {
Intent intent = new Intent(this, AccountTypeListActivity.class);
startActivityForResult(intent, ADD_ACCOUNT_REQUEST);
return true;
});
AccountHeader header = new AccountHeaderBuilder()
.withActivity(this)
.addProfiles(profileItem, profileSettingsItem, addAccountSettingsItem)
.withDividerBelowHeader(false)
.withAlternativeProfileHeaderSwitching(true)
.withCurrentProfileHiddenInList(true)
.withTextColorRes(R.color.colorBackground)
.withHeaderBackground(R.drawable.header_background)
.withHeaderBackgroundScaleType(ImageView.ScaleType.CENTER_CROP)
.build();
drawer = new DrawerBuilder()
.withActivity(this)
.withToolbar(toolbar)
.withAccountHeader(header)
.withSelectedItem(DrawerManager.ARTICLES_ITEM_ID)
.withOnDrawerItemClickListener((view, position, drawerItem) -> {
handleDrawerClick(drawerItem);
return true;
})
.build();
drawerManager = new DrawerManager(drawer);
updateDrawerFeeds();
}
private void handleDrawerClick(IDrawerItem drawerItem) {
if (drawerItem instanceof PrimaryDrawerItem) {
drawer.closeDrawer();
@ -243,7 +219,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
.subscribe(new DisposableSingleObserver<Map<Folder, List<Feed>>>() {
@Override
public void onSuccess(Map<Folder, List<Feed>> folderListHashMap) {
drawerManager.updateDrawer(getApplicationContext(), folderListHashMap);
drawerManager.updateDrawer(folderListHashMap);
}
@Override
@ -367,10 +343,10 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
GlideApp.with(this).clear(vh.getItemImage());
});
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
DividerItemDecoration decoration = new DividerItemDecoration(this, ((LinearLayoutManager) layoutManager).getOrientation());
DividerItemDecoration decoration = new DividerItemDecoration(this, layoutManager.getOrientation());
recyclerView.addItemDecoration(decoration);
recyclerView.setAdapter(adapter);
@ -499,39 +475,48 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode == ADD_FEED_REQUEST && resultCode == RESULT_OK) {
ArrayList<Feed> feeds = data.getParcelableArrayListExtra("feedIds");
if (data != null) {
ArrayList<Feed> feeds = data.getParcelableArrayListExtra("feedIds");
if (feeds != null && feeds.size() > 0) {
refreshLayout.setRefreshing(true);
feedNb = feeds.size();
sync(feeds);
if (feeds != null && feeds.size() > 0) {
refreshLayout.setRefreshing(true);
feedNb = feeds.size();
sync(feeds);
}
}
} else if (requestCode == MANAGE_FEEDS_REQUEST) {
updateDrawerFeeds();
} else if (requestCode == ADD_ACCOUNT_REQUEST) {
Account newAccount = data.getParcelableExtra(ACCOUNT_KEY);
if (newAccount != null) {
account = newAccount;
if (data != null) {
Account newAccount = data.getParcelableExtra(ACCOUNT_KEY);
viewModel.setRepository(account.getAccountType(), getApplication());
refreshLayout.setRefreshing(true);
onRefresh();
buildDrawer();
if (newAccount != null) {
currentAccount = newAccount;
viewModel.setRepository(currentAccount.getAccountType(), getApplication());
refreshLayout.setRefreshing(true);
onRefresh();
//drawerManager.
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
private void sync(List<Feed> feeds) {
if (account.getLogin() == null)
account.setLogin(SharedPreferencesManager.readString(this, account.getLoginKey()));
if (currentAccount.getLogin() == null)
currentAccount.setLogin(SharedPreferencesManager.readString(this, currentAccount.getLoginKey()));
if (account.getPassword() == null)
account.setPassword(SharedPreferencesManager.readString(this, account.getPasswordKey()));
if (currentAccount.getPassword() == null)
currentAccount.setPassword(SharedPreferencesManager.readString(this, currentAccount.getPasswordKey()));
viewModel.sync(feeds, account)
viewModel.sync(feeds, currentAccount)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Feed>() {
@ -586,7 +571,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
}
private boolean isAccountLocal() {
return account.getAccountType() == Account.AccountType.LOCAL;
return currentAccount.getAccountType() == Account.AccountType.LOCAL;
}
@Override

View File

@ -1,14 +1,23 @@
package com.readrops.app.utils;
import android.content.Context;
import android.app.Activity;
import android.widget.ImageView;
import androidx.appcompat.widget.Toolbar;
import com.mikepenz.materialdrawer.AccountHeader;
import com.mikepenz.materialdrawer.AccountHeaderBuilder;
import com.mikepenz.materialdrawer.Drawer;
import com.mikepenz.materialdrawer.DrawerBuilder;
import com.mikepenz.materialdrawer.model.DividerDrawerItem;
import com.mikepenz.materialdrawer.model.ExpandableBadgeDrawerItem;
import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
import com.mikepenz.materialdrawer.model.ProfileDrawerItem;
import com.mikepenz.materialdrawer.model.ProfileSettingDrawerItem;
import com.mikepenz.materialdrawer.model.SecondaryDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;
import com.readrops.app.R;
import com.readrops.app.database.entities.Account;
import com.readrops.app.database.entities.Feed;
import com.readrops.app.database.entities.Folder;
@ -22,16 +31,46 @@ 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;
private Activity activity;
private Toolbar toolbar;
private Drawer drawer;
public DrawerManager(Drawer drawer) {
this.drawer = drawer;
private AccountHeader header;
private Drawer.OnDrawerItemClickListener listener;
private AccountHeader.OnAccountHeaderListener headerListener;
public DrawerManager(Activity activity, Toolbar toolbar, Drawer.OnDrawerItemClickListener listener) {
this.activity = activity;
this.listener = listener;
this.toolbar = toolbar;
}
public void updateDrawer(Context context, Map<Folder, List<Feed>> folderListMap) {
public void setHeaderListener(AccountHeader.OnAccountHeaderListener headerListener) {
this.headerListener = headerListener;
}
public Drawer buildDrawer(List<Account> accounts) {
createAccountHeader(accounts);
drawer = new DrawerBuilder()
.withActivity(activity)
.withToolbar(toolbar)
.withAccountHeader(header)
.withSelectedItem(DrawerManager.ARTICLES_ITEM_ID)
.withOnDrawerItemClickListener(listener)
.build();
addDefaultPlaces();
return drawer;
}
public void updateDrawer(Map<Folder, List<Feed>> folderListMap) {
drawer.removeAllItems();
addDefaultPlaces(context);
addDefaultPlaces();
List<SecondaryDrawerItem> feedsWithoutFolder = new ArrayList<>();
@ -40,7 +79,7 @@ public class DrawerManager {
ExpandableBadgeDrawerItem badgeDrawerItem = new ExpandableBadgeDrawerItem()
.withName(folder.getName())
.withIdentifier(folder.getId())
.withIcon(context.getDrawable(R.drawable.ic_folder_grey));
.withIcon(activity.getDrawable(R.drawable.ic_folder_grey));
List<IDrawerItem> secondaryDrawerItems = new ArrayList<>();
int expandableUnreadCount = 0;
@ -52,7 +91,7 @@ public class DrawerManager {
SecondaryDrawerItem secondaryDrawerItem = new SecondaryDrawerItem()
.withName(feed.getName())
.withBadge(String.valueOf(feed.getUnreadCount()))
.withIcon(color != 0 ? drawableWithColor(color) : drawableWithColor(context.getResources().getColor(R.color.colorPrimary)))
.withIcon(color != 0 ? drawableWithColor(color) : drawableWithColor(activity.getResources().getColor(R.color.colorPrimary)))
.withIdentifier(feed.getId());
secondaryDrawerItems.add(secondaryDrawerItem);
@ -70,7 +109,7 @@ public class DrawerManager {
SecondaryDrawerItem primaryDrawerItem = new SecondaryDrawerItem()
.withName(feed.getName())
.withBadge(String.valueOf(feed.getUnreadCount()))
.withIcon(color != 0 ? drawableWithColor(color) : drawableWithColor(context.getResources().getColor(R.color.colorPrimary)))
.withIcon(color != 0 ? drawableWithColor(color) : drawableWithColor(activity.getResources().getColor(R.color.colorPrimary)))
.withIdentifier(feed.getId());
feedsWithoutFolder.add(primaryDrawerItem);
@ -84,16 +123,54 @@ public class DrawerManager {
}
}
private void addDefaultPlaces(Context context) {
private void createAccountHeader(List<Account> accounts) {
ProfileDrawerItem[] profileItems = new ProfileDrawerItem[accounts.size()];
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());
profileItems[i] = profileItem;
}
ProfileSettingDrawerItem profileSettingsItem = new ProfileSettingDrawerItem()
.withName(activity.getString(R.string.account_settings))
.withIcon(R.drawable.ic_account)
.withIdentifier(ACCOUNT_SETTINGS_ID);
ProfileSettingDrawerItem addAccountSettingsItem = new ProfileSettingDrawerItem()
.withName(activity.getString(R.string.add_account))
.withIcon(R.drawable.ic_add_account_grey)
.withIdentifier(ADD_ACCOUNT_ID);
header = new AccountHeaderBuilder()
.withActivity(activity)
.addProfiles(profileItems)
.addProfiles(profileSettingsItem, addAccountSettingsItem)
.withDividerBelowHeader(false)
.withAlternativeProfileHeaderSwitching(true)
.withCurrentProfileHiddenInList(true)
.withTextColorRes(R.color.colorBackground)
.withHeaderBackground(R.drawable.header_background)
.withHeaderBackgroundScaleType(ImageView.ScaleType.CENTER_CROP)
.withOnAccountHeaderListener(headerListener)
.build();
}
private void addDefaultPlaces() {
PrimaryDrawerItem articles = new PrimaryDrawerItem()
.withName(context.getString(R.string.articles))
.withIcon(context.getDrawable(R.drawable.ic_rss_feed_grey))
.withName(activity.getString(R.string.articles))
.withIcon(activity.getDrawable(R.drawable.ic_rss_feed_grey))
.withSelectable(true)
.withIdentifier(ARTICLES_ITEM_ID);
PrimaryDrawerItem toReadLater = new PrimaryDrawerItem()
.withName(context.getString(R.string.read_later))
.withIcon(context.getDrawable(R.drawable.ic_read_later_grey))
.withName(activity.getString(R.string.read_later))
.withIcon(activity.getDrawable(R.drawable.ic_read_later_grey))
.withSelectable(true)
.withIdentifier(READ_LATER_ID);

View File

@ -146,6 +146,10 @@ public class MainViewModel extends AndroidViewModel {
return db.accountDao().selectCurrentAccount();
}
public LiveData<List<Account>> getAllAccounts() {
return db.accountDao().selectAll();
}
public Completable setCurrentAccountsToFalse(int accountId) {
return Completable.create(emitter -> {
db.accountDao().setCurrentAccountsToFalse(accountId);