Skip to content

Commit

Permalink
Add context menu for article lists
Browse files Browse the repository at this point in the history
  • Loading branch information
di72nn committed Mar 16, 2020
1 parent 5a4b030 commit f95fac0
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 4 deletions.
39 changes: 38 additions & 1 deletion app/src/main/java/fr/gaulupeau/apps/Poche/data/ListAdapter.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package fr.gaulupeau.apps.Poche.data;

import android.app.Activity;
import android.content.Context;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
Expand All @@ -14,6 +18,7 @@

import fr.gaulupeau.apps.InThePoche.R;
import fr.gaulupeau.apps.Poche.data.dao.entities.Article;
import fr.gaulupeau.apps.Poche.ui.ArticleActionsHelper;

import static fr.gaulupeau.apps.Poche.data.ListTypes.LIST_TYPE_ARCHIVED;
import static fr.gaulupeau.apps.Poche.data.ListTypes.LIST_TYPE_FAVORITES;
Expand All @@ -27,11 +32,14 @@ public interface OnItemClickListener {

private Context context;
private Settings settings;
private ArticleActionsHelper articleActionsHelper = new ArticleActionsHelper();

private List<Article> articles;
private OnItemClickListener listener;
private int listType;

private Article articleWithContextMenu;

public ListAdapter(Context context, Settings settings,
List<Article> articles, OnItemClickListener listener, int listType) {
this.context = context;
Expand All @@ -58,8 +66,18 @@ public int getItemCount() {
return articles.size();
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public boolean handleContextItemSelected(Activity activity, MenuItem item) {
return articleWithContextMenu != null && articleActionsHelper
.handleContextItemSelected(activity, articleWithContextMenu, item);
}

public class ViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener, View.OnCreateContextMenuListener {

OnItemClickListener listener;

Article article;

TextView title;
TextView url;
ImageView favourite;
Expand All @@ -69,15 +87,20 @@ public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickL
ViewHolder(View itemView, OnItemClickListener listener) {
super(itemView);
this.listener = listener;

title = itemView.findViewById(R.id.title);
url = itemView.findViewById(R.id.url);
favourite = itemView.findViewById(R.id.favourite);
read = itemView.findViewById(R.id.read);
readingTime = itemView.findViewById(R.id.estimatedReadingTime);

itemView.setOnClickListener(this);
itemView.setOnCreateContextMenuListener(this);
}

void bind(Article article) {
this.article = article;

title.setText(article.getTitle());
url.setText(article.getDomain());

Expand Down Expand Up @@ -108,6 +131,20 @@ void bind(Article article) {
public void onClick(View v) {
listener.onItemClick(getAdapterPosition());
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
articleWithContextMenu = article;

if (article == null) return;

new MenuInflater(context) // not sure about this
.inflate(R.menu.article_list_context_menu, menu);

articleActionsHelper.initMenu(menu, article);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
import static fr.gaulupeau.apps.Poche.data.ListTypes.LIST_TYPE_FAVORITES;
import static fr.gaulupeau.apps.Poche.data.ListTypes.LIST_TYPE_UNREAD;

public class ArticleListFragment extends RecyclerViewListFragment<Article> {
public class ArticleListFragment extends RecyclerViewListFragment<Article>
implements ContextMenuItemHandler {

public interface OnFragmentInteractionListener {
void onRecyclerViewListSwipeUpdate();
Expand Down Expand Up @@ -143,6 +144,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

@Override
public boolean handleContextItemSelected(Activity activity, MenuItem item) {
return ((ListAdapter) listAdapter).handleContextItemSelected(activity, item);
}

public void forceContentUpdate() {
forceContentUpdate = true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package fr.gaulupeau.apps.Poche.ui;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

Expand All @@ -21,7 +23,8 @@
import fr.gaulupeau.apps.Poche.events.ArticlesChangedEvent;
import fr.gaulupeau.apps.Poche.events.FeedsChangedEvent;

public class ArticleListsFragment extends Fragment implements Sortable, Searchable {
public class ArticleListsFragment extends Fragment
implements Sortable, Searchable, ContextMenuItemHandler {

private static final String TAG = ArticleListsFragment.class.getSimpleName();

Expand Down Expand Up @@ -144,6 +147,12 @@ public void setSearchQuery(String searchQuery) {
setSearchQueryOnFragment(getCurrentFragment(), searchQuery);
}

@Override
public boolean handleContextItemSelected(Activity activity, MenuItem item) {
ArticleListFragment fragment = getCurrentFragment();
return fragment != null && fragment.handleContextItemSelected(activity, item);
}

public void onFeedsChangedEvent(FeedsChangedEvent event) {
Log.d(TAG, "onFeedsChangedEvent()");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package fr.gaulupeau.apps.Poche.ui;

import android.app.Activity;
import android.view.MenuItem;

interface ContextMenuItemHandler {

boolean handleContextItemSelected(Activity activity, MenuItem item);

}
10 changes: 10 additions & 0 deletions app/src/main/java/fr/gaulupeau/apps/Poche/ui/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,11 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) {
return true;
}

@Override
public boolean onContextItemSelected(MenuItem item) {
return handleContextMenuItemInFragment(item) || super.onContextItemSelected(item);
}

@Subscribe(threadMode = ThreadMode.MAIN, priority = -1)
public void onOfflineQueueChangedEvent(OfflineQueueChangedEvent event) {
Log.d(TAG, "onOfflineQueueChangedEvent()");
Expand Down Expand Up @@ -666,6 +671,11 @@ private void setSearchQueryOnFragment(Fragment fragment, String searchQuery) {
}
}

private boolean handleContextMenuItemInFragment(MenuItem item) {
return currentFragment instanceof ContextMenuItemHandler
&& ((ContextMenuItemHandler) currentFragment).handleContextItemSelected(this, item);
}

private void setCurrentFragment(String type) {
setCurrentFragment(type, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public abstract class RecyclerViewListFragment<T> extends Fragment
protected RecyclerView.Adapter listAdapter;
protected EndlessRecyclerViewScrollListener scrollListener;

protected boolean active = false;
protected boolean active = false; // TODO: check: doesn't work as expected in PagerAdapter
protected boolean invalidList = true;

public RecyclerViewListFragment() {}
Expand Down
40 changes: 40 additions & 0 deletions app/src/main/res/menu/article_list_context_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/menuArticleMarkAsRead"
android:title="@string/btnMarkRead" />
<item
android:id="@+id/menuArticleMarkAsUnread"
android:title="@string/btnMarkUnread"
android:visible="false" />
<item
android:id="@+id/menuArticleFavorite"
android:title="@string/add_to_favorites" />
<item
android:id="@+id/menuArticleUnfavorite"
android:title="@string/remove_from_favorites"
android:visible="false" />
<item
android:id="@+id/menuShare"
android:title="@string/menuShare" />
<item
android:id="@+id/menuChangeTitle"
android:title="@string/menu_changeTitle" />
<item
android:id="@+id/menuManageTags"
android:title="@string/menu_manageTags" />
<item
android:id="@+id/menuDelete"
android:title="@string/menuDelete" />
<item
android:id="@+id/menuOpenOriginal"
android:title="@string/menuOpenOriginal" />
<item
android:id="@+id/menuCopyOriginalURL"
android:title="@string/menuCopyOriginalURL" />
<item
android:id="@+id/menuDownloadAsFile"
android:title="@string/menuDownloadAsFile" />

</menu>

0 comments on commit f95fac0

Please sign in to comment.