Replace standard navigation drawer by mike penz material drawer

This commit is contained in:
Shinokuni 2019-03-29 14:26:25 +01:00
parent 75a65730f7
commit 252c661858
7 changed files with 116 additions and 67 deletions

View File

@ -60,4 +60,5 @@ dependencies {
// fastadapter requires androidx after 3.2.9
implementation 'com.mikepenz:fastadapter:3.2.9'
implementation 'com.mikepenz:fastadapter-commons:3.2.9'
implementation "com.mikepenz:materialdrawer:6.0.9"
}

View File

@ -30,7 +30,14 @@ 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.Drawer;
import com.mikepenz.materialdrawer.DrawerBuilder;
import com.mikepenz.materialdrawer.model.ExpandableBadgeDrawerItem;
import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
import com.mikepenz.materialdrawer.model.SecondaryDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;
import com.readrops.app.database.entities.Feed;
import com.readrops.app.database.entities.Folder;
import com.readrops.app.views.MainItemListAdapter;
import com.readrops.app.viewmodels.MainViewModel;
import com.readrops.app.R;
@ -43,6 +50,7 @@ import com.readrops.app.utils.ParsingResult;
import org.joda.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.TreeMap;
@ -50,19 +58,20 @@ import io.reactivex.Observer;
import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableSingleObserver;
import io.reactivex.schedulers.Schedulers;
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
public static final String TAG = MainActivity.class.getSimpleName();
public static final int ADD_FEED_REQUEST = 1;
public static final int MANAGE_FEEDS_REQUEST = 2;
private RecyclerView recyclerView;
private MainItemListAdapter adapter;
private SwipeRefreshLayout refreshLayout;
private NavigationView navigationView;
private DrawerLayout drawerLayout;
private Drawer drawer;
private FloatingActionMenu actionMenu;
private List<ItemWithFeed> newItems;
@ -85,28 +94,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
Toolbar toolbar = findViewById(R.id.toolbar_main);
setSupportActionBar(toolbar);
drawerLayout = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open_nav_drawer, R.string.close_nav_drawer);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
actionMenu = findViewById(R.id.fab_menu);
navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener((menuItem) -> {
menuItem.setChecked(true);
drawerLayout.closeDrawers();
switch (menuItem.getItemId()) {
case R.id.to_read:
break;
case R.id.non_read_articles:
break;
}
return true;
});
viewModel = ViewModelProviders.of(this).get(MainViewModel.class);
itemsMap = new TreeMap<>(LocalDateTime::compareTo);
@ -127,25 +115,80 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
syncProgressBar = findViewById(R.id.sync_progress_bar);
feedCount = 0;
initRecyclerView();
drawer = new DrawerBuilder()
.withActivity(this)
.withToolbar(toolbar)
.withShowDrawerOnFirstLaunch(true)
.build();
updateDrawerFeeds();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
drawerLayout.openDrawer(GravityCompat.START);
return true;
private void updateDrawerFeeds() {
viewModel.getFoldersWithFeeds()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new DisposableSingleObserver<HashMap<Folder, List<Feed>>>() {
@Override
public void onSuccess(HashMap<Folder, List<Feed>> folderListHashMap) {
populateDrawer(folderListHashMap);
}
@Override
public void onError(Throwable e) {
}
});
}
private void populateDrawer(HashMap<Folder, List<Feed>> folderListHashMap) {
drawer.removeAllItems();
List<SecondaryDrawerItem> feedsWithoutFolder = new ArrayList<>();
for (Folder folder : folderListHashMap.keySet()) {
if (folder.getId() != 1) {
ExpandableBadgeDrawerItem badgeDrawerItem = new ExpandableBadgeDrawerItem()
.withName(folder.getName())
.withIdentifier(folder.getId())
.withIcon(getDrawable(R.drawable.ic_folder_grey));
List<IDrawerItem> secondaryDrawerItems = new ArrayList<>();
for (Feed feed : folderListHashMap.get(folder)) {
SecondaryDrawerItem secondaryDrawerItem = new SecondaryDrawerItem()
.withName(feed.getName())
.withIdentifier(feed.getId());
secondaryDrawerItems.add(secondaryDrawerItem);
}
if (secondaryDrawerItems.size() > 0) {
badgeDrawerItem.withSubItems(secondaryDrawerItems);
drawer.addItem(badgeDrawerItem);
}
} else { // no folder case
for (Feed feed : folderListHashMap.get(folder)) {
SecondaryDrawerItem primaryDrawerItem = new SecondaryDrawerItem()
.withName(feed.getName())
.withIdentifier(feed.getId());
feedsWithoutFolder.add(primaryDrawerItem);
}
}
}
return super.onOptionsItemSelected(item);
// work-around as MaterialDrawer doesn't accept an item list
for (SecondaryDrawerItem primaryDrawerItem : feedsWithoutFolder) {
drawer.addItem(primaryDrawerItem);
}
}
@Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START))
drawerLayout.closeDrawers();
if (drawer.isDrawerOpen())
drawer.closeDrawer();
else
super.onBackPressed();
}
@ -242,7 +285,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
});
}
public void displayAddFeedDialog(View view) {
public void openAddFeedActivity(View view) {
actionMenu.close(true);
Intent intent = new Intent(this, AddFeedActivity.class);
@ -253,12 +296,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
actionMenu.close(true);
Intent intent = new Intent(this, ManageFeedsActivity.class);
startActivity(intent);
}
public void insertNewFeed(ParsingResult result) {
refreshLayout.setRefreshing(true);
viewModel.addFeed(result);
startActivityForResult(intent, MANAGE_FEEDS_REQUEST);
}
@Override
@ -271,6 +309,8 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
feedNb = feeds.size();
sync(feeds);
}
} else if (requestCode == MANAGE_FEEDS_REQUEST) {
updateDrawerFeeds();
}
super.onActivityResult(requestCode, resultCode, data);

View File

@ -36,6 +36,9 @@ public interface FeedDao {
@Query("Select * from Feed Where url = :feedUrl")
Feed getFeedByUrl(String feedUrl);
@Query("Select * from Feed Where folder_id = :folderId")
List<Feed> getFeedsByFolder(int folderId);
@Query("Update Feed set etag = :etag, last_modified = :lastModified Where id = :feedId")
void updateHeaders(String etag, String lastModified, int feedId);

View File

@ -16,6 +16,9 @@ public interface FolderDao {
@Query("Select * from Folder Order By name ASC")
LiveData<List<Folder>> getAllFolders();
@Query("Select * from Folder Order By name ASC")
List<Folder> getFolders();
@Insert
long insert(Folder folder);

View File

@ -3,6 +3,7 @@ package com.readrops.app.repositories;
import android.accounts.NetworkErrorException;
import android.app.Application;
import android.arch.lifecycle.LiveData;
import android.support.annotation.Nullable;
import com.readrops.app.database.entities.Folder;
import com.readrops.app.database.pojo.FeedWithFolder;
@ -53,7 +54,7 @@ public class LocalFeedRepository extends ARepository {
}
@Override
public Observable<Feed> sync(List<Feed> feeds) {
public Observable<Feed> sync(@Nullable List<Feed> feeds) {
return Observable.create(emitter -> {
List<Feed> feedList;

View File

@ -5,11 +5,14 @@ import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.LiveData;
import android.support.annotation.NonNull;
import com.readrops.app.database.Database;
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.LocalFeedRepository;
import com.readrops.app.utils.ParsingResult;
import java.util.HashMap;
import java.util.List;
import io.reactivex.Observable;
@ -19,13 +22,14 @@ public class MainViewModel extends AndroidViewModel {
private LiveData<List<ItemWithFeed>> itemsWithFeed;
private LocalFeedRepository repository;
private Database db;
public MainViewModel(@NonNull Application application) {
super(application);
repository = new LocalFeedRepository(application);
itemsWithFeed = repository.getItemsWithFeed();
db = Database.getInstance(application);
}
public LiveData<List<ItemWithFeed>> getItemsWithFeed() {
@ -43,4 +47,18 @@ public class MainViewModel extends AndroidViewModel {
public Single<Integer> getFeedCount() {
return repository.getFeedCount();
}
public Single<HashMap<Folder, List<Feed>>> getFoldersWithFeeds() {
return Single.create(emitter -> {
List<Folder> folders = db.folderDao().getFolders();
HashMap<Folder, List<Feed>> foldersWithFeeds = new HashMap<>();
for (Folder folder : folders) {
List<Feed> feeds = db.feedDao().getFeedsByFolder(folder.getId());
foldersWithFeeds.put(folder, feeds);
}
emitter.onSuccess(foldersWithFeeds);
});
}
}

View File

@ -1,8 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.MainActivity">
@ -13,19 +12,13 @@
android:animateLayoutChanges="true"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_main"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:id="@+id/sync_progress_layout"
@ -102,7 +95,7 @@
android:id="@+id/add_feed_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="displayAddFeedDialog"
android:onClick="openAddFeedActivity"
android:src="@drawable/ic_insert_link_white"
app:fab_colorNormal="@color/colorPrimary"
app:fab_label="@string/add_feed_item"
@ -122,14 +115,4 @@
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_view" />
</android.support.v4.widget.DrawerLayout>
</android.support.constraint.ConstraintLayout>