Dbrant has uploaded a new change for review. https://gerrit.wikimedia.org/r/295258
Change subject: [WIP] Add interactivity to "because you read" card. ...................................................................... [WIP] Add interactivity to "because you read" card. Please do not review yet. Change-Id: I0663b26dd247f65a28655213b73e08e07078426b --- M app/src/main/java/org/wikipedia/feed/FeedFragment.java A app/src/main/java/org/wikipedia/feed/FeedViewCallback.java M app/src/main/java/org/wikipedia/feed/becauseyouread/BecauseYouReadCardView.java M app/src/main/java/org/wikipedia/feed/becauseyouread/BecauseYouReadItemCard.java M app/src/main/java/org/wikipedia/feed/view/FeedRecyclerAdapter.java M app/src/main/java/org/wikipedia/feed/view/FeedView.java M app/src/main/java/org/wikipedia/feed/view/ListCardItemView.java A app/src/main/java/org/wikipedia/feed/view/PageTitleListCardItemView.java A app/src/main/java/org/wikipedia/feed/view/PageTitleListCardView.java M app/src/main/java/org/wikipedia/history/HistoryEntry.java M app/src/main/java/org/wikipedia/page/MwApiResultPage.java M app/src/main/java/org/wikipedia/page/NavDrawerHelper.java M app/src/main/java/org/wikipedia/page/PageActivity.java M app/src/main/java/org/wikipedia/readinglist/AddToReadingListDialog.java 14 files changed, 233 insertions(+), 40 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia refs/changes/58/295258/1 diff --git a/app/src/main/java/org/wikipedia/feed/FeedFragment.java b/app/src/main/java/org/wikipedia/feed/FeedFragment.java index 1d84d45..8e91cbf 100644 --- a/app/src/main/java/org/wikipedia/feed/FeedFragment.java +++ b/app/src/main/java/org/wikipedia/feed/FeedFragment.java @@ -1,6 +1,7 @@ package org.wikipedia.feed; import android.os.Bundle; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; @@ -13,6 +14,7 @@ import org.wikipedia.activity.FragmentUtil; import org.wikipedia.feed.model.Card; import org.wikipedia.feed.view.FeedView; +import org.wikipedia.page.PageTitle; import java.util.List; @@ -26,6 +28,13 @@ private Unbinder unbinder; private WikipediaApp app; private FeedCoordinator coordinator; + private FeedViewCallback feedCallback = new FeedCallback(); + + public interface Callback extends CallbackFragment.Callback { + void onFeedSearchRequested(); + void onFeedSelectPage(PageTitle title); + void onFeedAddPageToList(PageTitle title); + } public static FeedFragment newInstance() { return new FeedFragment(); @@ -45,7 +54,7 @@ View view = inflater.inflate(R.layout.fragment_feed, container, false); unbinder = ButterKnife.bind(this, view); - feedView.set(coordinator.getCards()); + feedView.set(coordinator.getCards(), feedCallback); coordinator.setFeedUpdateListener(new FeedCoordinator.FeedUpdateListener() { @Override @@ -78,4 +87,19 @@ feedView.update(); } + private class FeedCallback implements FeedViewCallback { + @Override + public void onSelectPage(@NonNull PageTitle title) { + if (getCallback() != null) { + getCallback().onFeedSelectPage(title); + } + } + + @Override + public void onAddPageToList(@NonNull PageTitle title) { + if (getCallback() != null) { + getCallback().onFeedAddPageToList(title); + } + } + } } diff --git a/app/src/main/java/org/wikipedia/feed/FeedViewCallback.java b/app/src/main/java/org/wikipedia/feed/FeedViewCallback.java new file mode 100644 index 0000000..7954be9 --- /dev/null +++ b/app/src/main/java/org/wikipedia/feed/FeedViewCallback.java @@ -0,0 +1,10 @@ +package org.wikipedia.feed; + +import android.support.annotation.NonNull; + +import org.wikipedia.page.PageTitle; + +public interface FeedViewCallback { + void onSelectPage(@NonNull PageTitle title); + void onAddPageToList(@NonNull PageTitle title); +} diff --git a/app/src/main/java/org/wikipedia/feed/becauseyouread/BecauseYouReadCardView.java b/app/src/main/java/org/wikipedia/feed/becauseyouread/BecauseYouReadCardView.java index 92b65e7..b579bf1 100644 --- a/app/src/main/java/org/wikipedia/feed/becauseyouread/BecauseYouReadCardView.java +++ b/app/src/main/java/org/wikipedia/feed/becauseyouread/BecauseYouReadCardView.java @@ -3,22 +3,33 @@ import android.content.Context; import android.net.Uri; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import org.wikipedia.feed.FeedViewCallback; import org.wikipedia.feed.view.CardLargeHeaderView; import org.wikipedia.feed.view.ListCardItemView; import org.wikipedia.feed.view.ListCardView; +import org.wikipedia.feed.view.PageTitleListCardItemView; +import org.wikipedia.feed.view.PageTitleListCardView; import org.wikipedia.views.DefaultViewHolder; import java.util.List; -public class BecauseYouReadCardView extends ListCardView<BecauseYouReadCard> { +public class BecauseYouReadCardView extends PageTitleListCardView<BecauseYouReadCard> { + @Nullable FeedViewCallback callback; + public BecauseYouReadCardView(Context context) { super(context); } + @NonNull public PageTitleListCardView setCallback(@Nullable FeedViewCallback callback) { + this.callback = callback; + return this; + } + public void set(@NonNull final BecauseYouReadCard card) { header(card); - set(new RecyclerAdapter(card.items())); + set(new RecyclerAdapter(card.items(), callback)); } private void header(@NonNull final BecauseYouReadCard card) { @@ -30,20 +41,19 @@ header(header); } - private static class RecyclerAdapter extends ListCardView.RecyclerAdapter<BecauseYouReadItemCard> { - RecyclerAdapter(@NonNull List<BecauseYouReadItemCard> items) { + private static class RecyclerAdapter extends PageTitleListCardView.RecyclerAdapter<BecauseYouReadItemCard> { + @Nullable FeedViewCallback callback; + + RecyclerAdapter(@NonNull List<BecauseYouReadItemCard> items, @Nullable FeedViewCallback callback) { super(items); + this.callback = callback; } - @Override public void onBindViewHolder(DefaultViewHolder<ListCardItemView> holder, int i) { + @Override + public void onBindViewHolder(DefaultViewHolder<PageTitleListCardItemView> holder, int i) { BecauseYouReadItemCard card = item(i); - holder.getView().setTitle(card.title()); - holder.getView().setSubtitle(card.subtitle()); - - Uri imageUri = card.image(); - if (imageUri != null) { - holder.getView().setImage(imageUri); - } + holder.getView().setPageTitle(card.pageTitle()); + holder.getView().setCallback(callback); } } } diff --git a/app/src/main/java/org/wikipedia/feed/becauseyouread/BecauseYouReadItemCard.java b/app/src/main/java/org/wikipedia/feed/becauseyouread/BecauseYouReadItemCard.java index 0eb4218..9aa2da5 100644 --- a/app/src/main/java/org/wikipedia/feed/becauseyouread/BecauseYouReadItemCard.java +++ b/app/src/main/java/org/wikipedia/feed/becauseyouread/BecauseYouReadItemCard.java @@ -3,33 +3,34 @@ import android.net.Uri; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.text.TextUtils; import org.wikipedia.feed.model.Card; +import org.wikipedia.page.PageTitle; public class BecauseYouReadItemCard extends Card { - @NonNull private final String title; - @Nullable private final String subtitle; - @Nullable private final Uri image; + @NonNull private final PageTitle title; - public BecauseYouReadItemCard(@NonNull String title, @Nullable String subtitle, - @Nullable String image) { + public BecauseYouReadItemCard(@NonNull PageTitle title) { this.title = title; - this.subtitle = subtitle; - this.image = image != null ? Uri.parse(image) : null; + } + + public @NonNull PageTitle pageTitle() { + return title; } @NonNull @Override public String title() { - return title; + return title.getDisplayText(); } @Nullable @Override public String subtitle() { - return subtitle; + return title.getDescription(); } @Nullable @Override public Uri image() { - return image; + return TextUtils.isEmpty(title.getThumbUrl()) ? null : Uri.parse(title.getThumbUrl()); } } diff --git a/app/src/main/java/org/wikipedia/feed/view/FeedRecyclerAdapter.java b/app/src/main/java/org/wikipedia/feed/view/FeedRecyclerAdapter.java index 7771aca..e75a628 100644 --- a/app/src/main/java/org/wikipedia/feed/view/FeedRecyclerAdapter.java +++ b/app/src/main/java/org/wikipedia/feed/view/FeedRecyclerAdapter.java @@ -2,9 +2,11 @@ import android.content.Context; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v7.widget.CardView; import android.view.ViewGroup; +import org.wikipedia.feed.FeedViewCallback; import org.wikipedia.feed.becauseyouread.BecauseYouReadCard; import org.wikipedia.feed.becauseyouread.BecauseYouReadCardView; import org.wikipedia.feed.continuereading.ContinueReadingCard; @@ -25,8 +27,11 @@ private static final int VIEW_TYPE_BECAUSE_YOU_READ = 2; private static final int VIEW_TYPE_INTEGER_LIST = 100; - public FeedRecyclerAdapter(@NonNull List<Card> items) { + @Nullable private FeedViewCallback callback; + + public FeedRecyclerAdapter(@NonNull List<Card> items, @Nullable FeedViewCallback callback) { super(items); + this.callback = callback; } @Override public DefaultViewHolder<CardView> onCreateViewHolder(ViewGroup parent, int viewType) { @@ -72,7 +77,7 @@ case VIEW_TYPE_CONTINUE_READING: return new ContinueReadingCardView(context); case VIEW_TYPE_BECAUSE_YOU_READ: - return new BecauseYouReadCardView(context); + return new BecauseYouReadCardView(context).setCallback(callback); case VIEW_TYPE_SEARCH_BAR: return new SearchCardView(context); default: diff --git a/app/src/main/java/org/wikipedia/feed/view/FeedView.java b/app/src/main/java/org/wikipedia/feed/view/FeedView.java index 6519699..e88d4b7 100644 --- a/app/src/main/java/org/wikipedia/feed/view/FeedView.java +++ b/app/src/main/java/org/wikipedia/feed/view/FeedView.java @@ -9,6 +9,7 @@ import android.widget.FrameLayout; import org.wikipedia.R; +import org.wikipedia.feed.FeedViewCallback; import org.wikipedia.feed.model.Card; import org.wikipedia.views.AutoFitRecyclerView; import org.wikipedia.views.MarginItemDecoration; @@ -45,10 +46,10 @@ init(); } - public void set(@NonNull List<Card> cards) { + public void set(@NonNull List<Card> cards, FeedViewCallback callback) { // TODO: should this class be responsible for showing a "no items in collection" view? It // would be nice to show placeholder elements while it loads. - recyclerAdapter = new FeedRecyclerAdapter(cards); + recyclerAdapter = new FeedRecyclerAdapter(cards, callback); recyclerView.setAdapter(recyclerAdapter); } @@ -70,7 +71,7 @@ R.dimen.view_list_card_margin_horizontal, R.dimen.view_list_card_margin_vertical, R.dimen.view_list_card_margin_horizontal, R.dimen.view_list_card_margin_vertical)); recyclerView.callback(new RecyclerViewColumnCallback()); - set(Collections.<Card>emptyList()); + set(Collections.<Card>emptyList(), null); } private class RecyclerViewColumnCallback implements AutoFitRecyclerView.Callback { diff --git a/app/src/main/java/org/wikipedia/feed/view/ListCardItemView.java b/app/src/main/java/org/wikipedia/feed/view/ListCardItemView.java index 1154b48..b0381be 100644 --- a/app/src/main/java/org/wikipedia/feed/view/ListCardItemView.java +++ b/app/src/main/java/org/wikipedia/feed/view/ListCardItemView.java @@ -10,6 +10,8 @@ import com.facebook.drawee.view.SimpleDraweeView; import org.wikipedia.R; +import org.wikipedia.drawable.DrawableUtil; +import org.wikipedia.views.ViewUtil; import butterknife.BindView; import butterknife.ButterKnife; diff --git a/app/src/main/java/org/wikipedia/feed/view/PageTitleListCardItemView.java b/app/src/main/java/org/wikipedia/feed/view/PageTitleListCardItemView.java new file mode 100644 index 0000000..0f4258c --- /dev/null +++ b/app/src/main/java/org/wikipedia/feed/view/PageTitleListCardItemView.java @@ -0,0 +1,50 @@ +package org.wikipedia.feed.view; + +import android.content.Context; +import android.net.Uri; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.view.View; +import android.view.ViewGroup; + +import org.wikipedia.R; +import org.wikipedia.feed.FeedViewCallback; +import org.wikipedia.page.PageTitle; +import org.wikipedia.util.ResourceUtil; + +public class PageTitleListCardItemView extends ListCardItemView { + @Nullable private FeedViewCallback callback; + @Nullable private PageTitle title; + + public PageTitleListCardItemView(Context context) { + super(context); + int paddingX = (int) getResources().getDimension(R.dimen.list_item_horizontal_padding); + int paddingY = (int) getResources().getDimension(R.dimen.list_item_vertical_padding); + setPadding(paddingX, paddingY, paddingX, paddingY); + setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.WRAP_CONTENT)); + setBackgroundResource(ResourceUtil.getThemedAttributeId(context, android.R.attr.selectableItemBackground)); + setClickable(true); + setOnClickListener(new OnClickListener() { + @Override + public void onClick(View view) { + if (callback != null && title != null) { + callback.onSelectPage(title); + } + } + }); + } + + @NonNull public PageTitleListCardItemView setCallback(@Nullable FeedViewCallback callback) { + this.callback = callback; + return this; + } + + @NonNull public PageTitleListCardItemView setPageTitle(@NonNull PageTitle title) { + this.title = title; + titleView.setText(title.getDisplayText()); + subtitleView.setText(title.getDescription()); + imageView.setImageURI(Uri.parse(title.getThumbUrl())); + return this; + } +} \ No newline at end of file diff --git a/app/src/main/java/org/wikipedia/feed/view/PageTitleListCardView.java b/app/src/main/java/org/wikipedia/feed/view/PageTitleListCardView.java new file mode 100644 index 0000000..6c9d9b1 --- /dev/null +++ b/app/src/main/java/org/wikipedia/feed/view/PageTitleListCardView.java @@ -0,0 +1,75 @@ +package org.wikipedia.feed.view; + +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.v7.widget.CardView; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.view.View; +import android.view.ViewGroup; + +import org.wikipedia.R; +import org.wikipedia.feed.model.Card; +import org.wikipedia.views.DefaultRecyclerAdapter; +import org.wikipedia.views.DefaultViewHolder; +import org.wikipedia.views.DrawableItemDecoration; +import org.wikipedia.views.MarginItemDecoration; +import org.wikipedia.views.ViewUtil; + +import java.util.List; + +import butterknife.BindView; +import butterknife.ButterKnife; + +public abstract class PageTitleListCardView<T extends Card> extends CardView { + @BindView(R.id.view_list_card_header) View headerView; + @BindView(R.id.view_list_card_footer) View footerView; + @BindView(R.id.view_list_card_list) RecyclerView recyclerView; + + public PageTitleListCardView(Context context) { + super(context); + + inflate(getContext(), R.layout.view_list_card, this); + ButterKnife.bind(this); + initRecycler(); + } + + protected void set(@Nullable RecyclerAdapter<?> adapter) { + recyclerView.setAdapter(adapter); + } + + protected void update() { + if (recyclerView.getAdapter() != null) { + recyclerView.getAdapter().notifyDataSetChanged(); + } + } + + protected void header(@NonNull View view) { + ViewUtil.replace(headerView, view); + headerView = view; + } + + protected void footer(@NonNull View view) { + ViewUtil.replace(footerView, view); + footerView = view; + } + + private void initRecycler() { + recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); + recyclerView.addItemDecoration(new DrawableItemDecoration(getContext(), + R.drawable.divider, true)); + } + + protected abstract static class RecyclerAdapter<T> + extends DefaultRecyclerAdapter<T, PageTitleListCardItemView> { + protected RecyclerAdapter(@NonNull List<T> items) { + super(items); + } + + @Override public DefaultViewHolder<PageTitleListCardItemView> onCreateViewHolder(ViewGroup parent, + int viewType) { + return new DefaultViewHolder<>(new PageTitleListCardItemView(parent.getContext())); + } + } +} \ No newline at end of file diff --git a/app/src/main/java/org/wikipedia/history/HistoryEntry.java b/app/src/main/java/org/wikipedia/history/HistoryEntry.java index 9ba7313..eb78b29 100644 --- a/app/src/main/java/org/wikipedia/history/HistoryEntry.java +++ b/app/src/main/java/org/wikipedia/history/HistoryEntry.java @@ -20,6 +20,7 @@ public static final int SOURCE_NEARBY = 9; public static final int SOURCE_DISAMBIG = 10; public static final int SOURCE_READING_LIST = 11; + public static final int SOURCE_FEED = 12; private final PageTitle title; private final Date timestamp; diff --git a/app/src/main/java/org/wikipedia/page/MwApiResultPage.java b/app/src/main/java/org/wikipedia/page/MwApiResultPage.java index a16e557..e90a733 100644 --- a/app/src/main/java/org/wikipedia/page/MwApiResultPage.java +++ b/app/src/main/java/org/wikipedia/page/MwApiResultPage.java @@ -4,6 +4,7 @@ import android.support.annotation.Nullable; import org.wikipedia.Site; +import org.wikipedia.WikipediaApp; import org.wikipedia.feed.becauseyouread.BecauseYouReadItemCard; import org.wikipedia.search.SearchResult; import org.wikipedia.search.SearchResults; @@ -56,9 +57,11 @@ @NonNull public BecauseYouReadItemCard toCard() { - return new BecauseYouReadItemCard(StringUtil.removeUnderscores(title), - description(), - thumbUrl()); + // TODO: get Site correctly? and set Namespace? + PageTitle pageTitle = new PageTitle(title, WikipediaApp.getInstance().getSite()); + pageTitle.setThumbUrl(thumbUrl()); + pageTitle.setDescription(description()); + return new BecauseYouReadItemCard(pageTitle); } @NonNull diff --git a/app/src/main/java/org/wikipedia/page/NavDrawerHelper.java b/app/src/main/java/org/wikipedia/page/NavDrawerHelper.java index 4647a52..b30f1d9 100644 --- a/app/src/main/java/org/wikipedia/page/NavDrawerHelper.java +++ b/app/src/main/java/org/wikipedia/page/NavDrawerHelper.java @@ -18,7 +18,6 @@ import org.wikipedia.WikipediaApp; import org.wikipedia.analytics.LoginFunnel; import org.wikipedia.analytics.NavMenuFunnel; -import org.wikipedia.feed.FeedActivity; import org.wikipedia.feed.FeedFragment; import org.wikipedia.history.HistoryEntry; import org.wikipedia.history.HistoryFragment; @@ -78,7 +77,7 @@ public boolean onNavigationItemSelected(MenuItem menuItem) { switch (menuItem.getItemId()) { case R.id.nav_item_feed: - launchFeedActivity(); + activity.pushFragment(new FeedFragment()); // TODO: [Feed] add Feed logging. break; case R.id.nav_item_today: @@ -246,11 +245,6 @@ for (int i = 0; i < activity.getNavMenu().size(); i++) { activity.getNavMenu().getItem(i).setChecked(false); } - } - - private void launchFeedActivity() { - activity.closeNavDrawer(); - activity.startActivity(FeedActivity.newIntent(activity)); } private void launchSettingsActivity() { diff --git a/app/src/main/java/org/wikipedia/page/PageActivity.java b/app/src/main/java/org/wikipedia/page/PageActivity.java index aa17648..fed5edf 100644 --- a/app/src/main/java/org/wikipedia/page/PageActivity.java +++ b/app/src/main/java/org/wikipedia/page/PageActivity.java @@ -59,6 +59,7 @@ import org.wikipedia.events.ChangeTextSizeEvent; import org.wikipedia.events.ThemeChangeEvent; import org.wikipedia.events.WikipediaZeroStateChangeEvent; +import org.wikipedia.feed.FeedFragment; import org.wikipedia.history.HistoryEntry; import org.wikipedia.interlanguage.LangLinksActivity; import org.wikipedia.login.LoginActivity; @@ -88,7 +89,7 @@ import static org.wikipedia.util.DeviceUtil.isBackKeyUp; import static org.wikipedia.util.UriUtil.visitInExternalBrowser; -public class PageActivity extends ThemedActionBarActivity { +public class PageActivity extends ThemedActionBarActivity implements FeedFragment.Callback { public enum TabPosition { CURRENT_TAB, @@ -767,6 +768,21 @@ finish(); } + @Override + public void onFeedSearchRequested() { + onSearchRequested(); + } + + @Override + public void onFeedSelectPage(PageTitle title) { + loadPage(title, new HistoryEntry(title, HistoryEntry.SOURCE_FEED)); + } + + @Override + public void onFeedAddPageToList(PageTitle title) { + showAddToListDialog(title, AddToReadingListDialog.InvokeSource.FEED); + } + private void loadMainPageIfNoTabs() { loadMainPage(false, TabPosition.CURRENT_TAB, true); } diff --git a/app/src/main/java/org/wikipedia/readinglist/AddToReadingListDialog.java b/app/src/main/java/org/wikipedia/readinglist/AddToReadingListDialog.java index 508ac6a..977b799 100644 --- a/app/src/main/java/org/wikipedia/readinglist/AddToReadingListDialog.java +++ b/app/src/main/java/org/wikipedia/readinglist/AddToReadingListDialog.java @@ -34,7 +34,8 @@ BOOKMARK_BUTTON(0), CONTEXT_MENU(1), LINK_PREVIEW_MENU(2), - PAGE_OVERFLOW_MENU(3); + PAGE_OVERFLOW_MENU(3), + FEED(4); private static final EnumCodeMap<InvokeSource> MAP = new EnumCodeMap<>(InvokeSource.class); -- To view, visit https://gerrit.wikimedia.org/r/295258 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I0663b26dd247f65a28655213b73e08e07078426b Gerrit-PatchSet: 1 Gerrit-Project: apps/android/wikipedia Gerrit-Branch: master Gerrit-Owner: Dbrant <dbr...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits