Niedzielski has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/312148

Change subject: Null out feed card callbacks when Activity dies
......................................................................

Null out feed card callbacks when Activity dies

Bug: T146086
Change-Id: I1c56cfff175ec9d8bfeab523bcc6c3cf6b5fff24
---
M app/src/main/java/org/wikipedia/feed/FeedFragment.java
M 
app/src/main/java/org/wikipedia/feed/becauseyouread/BecauseYouReadCardView.java
M app/src/main/java/org/wikipedia/feed/mainpage/MainPageCardView.java
M app/src/main/java/org/wikipedia/feed/mostread/MostReadCardView.java
M app/src/main/java/org/wikipedia/feed/news/NewsListCardView.java
M app/src/main/java/org/wikipedia/feed/random/RandomCardView.java
M app/src/main/java/org/wikipedia/feed/view/FeedAdapter.java
7 files changed, 42 insertions(+), 57 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia 
refs/changes/48/312148/1

diff --git a/app/src/main/java/org/wikipedia/feed/FeedFragment.java 
b/app/src/main/java/org/wikipedia/feed/FeedFragment.java
index cc8ba76..bfedc71 100644
--- a/app/src/main/java/org/wikipedia/feed/FeedFragment.java
+++ b/app/src/main/java/org/wikipedia/feed/FeedFragment.java
@@ -49,10 +49,11 @@
     @BindView(R.id.feed_swipe_refresh_layout) SwipeRefreshLayout 
swipeRefreshLayout;
     @BindView(R.id.fragment_feed_feed) FeedView feedView;
     private Unbinder unbinder;
+    private FeedAdapter<?> feedAdapter;
     private WikipediaApp app;
     private FeedCoordinator coordinator;
     private FeedFunnel funnel;
-    private FeedAdapter.Callback feedCallback = new FeedCallback();
+    private final FeedAdapter.Callback feedCallback = new FeedCallback();
     private FeedScrollListener feedScrollListener = new FeedScrollListener();
     private OverflowCallback overflowCallback = new OverflowCallback();
     private boolean searchIconVisible;
@@ -93,7 +94,8 @@
         View view = inflater.inflate(R.layout.fragment_feed, container, false);
 
         unbinder = ButterKnife.bind(this, view);
-        feedView.setAdapter(new FeedAdapter<>(coordinator, feedCallback));
+        feedAdapter = new FeedAdapter<>(coordinator, feedCallback);
+        feedView.setAdapter(feedAdapter);
         feedView.setCallback(feedCallback);
         feedView.addOnScrollListener(feedScrollListener);
 
@@ -112,8 +114,8 @@
             public void update(List<Card> cards) {
                 if (isAdded()) {
                     swipeRefreshLayout.setRefreshing(false);
-                    if (feedView.getAdapter() != null) {
-                        feedView.getAdapter().notifyDataSetChanged();
+                    if (feedView != null && feedAdapter != null) {
+                        feedAdapter.notifyDataSetChanged();
                     }
                 }
             }
@@ -134,6 +136,8 @@
         swipeRefreshLayout.setOnRefreshListener(null);
         feedView.removeOnScrollListener(feedScrollListener);
         feedView.setCallback((FeedAdapter.Callback) null);
+        feedView.setAdapter(null);
+        feedAdapter = null;
         unbinder.unbind();
         unbinder = null;
         super.onDestroyView();
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 98319e5..e73fde7 100644
--- 
a/app/src/main/java/org/wikipedia/feed/becauseyouread/BecauseYouReadCardView.java
+++ 
b/app/src/main/java/org/wikipedia/feed/becauseyouread/BecauseYouReadCardView.java
@@ -2,14 +2,12 @@
 
 import android.content.Context;
 import android.support.annotation.NonNull;
-import android.support.annotation.Nullable;
 import android.support.annotation.PluralsRes;
 import android.view.View;
 
 import org.wikipedia.R;
 import org.wikipedia.feed.view.CardHeaderView;
 import org.wikipedia.feed.view.CardLargeHeaderView;
-import org.wikipedia.feed.view.FeedAdapter;
 import org.wikipedia.feed.view.ListCardView;
 import org.wikipedia.feed.view.PageTitleListCardItemView;
 import org.wikipedia.feed.view.PageTitleRecyclerAdapter;
@@ -29,7 +27,7 @@
     @Override public void setCard(@NonNull BecauseYouReadCard card) {
         super.setCard(card);
         header(card);
-        set(new RecyclerAdapter(card.items(), getCallback()));
+        set(new RecyclerAdapter(card.items()));
     }
 
     private void header(@NonNull final BecauseYouReadCard card) {
@@ -65,20 +63,16 @@
         }
     }
 
-    private static class RecyclerAdapter extends 
PageTitleRecyclerAdapter<BecauseYouReadItemCard> {
-        @Nullable private FeedAdapter.Callback callback;
-
-        RecyclerAdapter(@NonNull List<BecauseYouReadItemCard> items,
-                        @Nullable FeedAdapter.Callback callback) {
+    private class RecyclerAdapter extends 
PageTitleRecyclerAdapter<BecauseYouReadItemCard> {
+        RecyclerAdapter(@NonNull List<BecauseYouReadItemCard> items) {
             super(items);
-            this.callback = callback;
         }
 
         @Override
         public void 
onBindViewHolder(DefaultViewHolder<PageTitleListCardItemView> holder, int i) {
             BecauseYouReadItemCard card = item(i);
             holder.getView().setHistoryEntry(new 
HistoryEntry(card.pageTitle(), HistoryEntry.SOURCE_FEED_BECAUSE_YOU_READ));
-            holder.getView().setCallback(callback);
+            holder.getView().setCallback(getCallback());
         }
     }
 }
diff --git 
a/app/src/main/java/org/wikipedia/feed/mainpage/MainPageCardView.java 
b/app/src/main/java/org/wikipedia/feed/mainpage/MainPageCardView.java
index 1fe5bd1..0ceb3a0 100644
--- a/app/src/main/java/org/wikipedia/feed/mainpage/MainPageCardView.java
+++ b/app/src/main/java/org/wikipedia/feed/mainpage/MainPageCardView.java
@@ -31,23 +31,18 @@
 
     @Override public void setCallback(@Nullable FeedAdapter.Callback callback) 
{
         super.setCallback(callback);
-        setOnClickListener(new CallbackAdapter(callback));
+        setOnClickListener(new CallbackAdapter());
     }
 
-    private static class CallbackAdapter implements OnClickListener {
+    private class CallbackAdapter implements OnClickListener {
         @NonNull private WikipediaApp app = WikipediaApp.getInstance();
-        @Nullable private final FeedAdapter.Callback callback;
-
-        CallbackAdapter(@Nullable FeedAdapter.Callback callback) {
-            this.callback = callback;
-        }
 
         @Override
         public void onClick(View view) {
-            if (callback != null) {
+            if (getCallback() != null) {
                 PageTitle title = new PageTitle(MainPageNameData
                         .valueFor(app.getAppOrSystemLanguageCode()), 
app.getSite());
-                callback.onSelectPage(new HistoryEntry(title, 
HistoryEntry.SOURCE_FEED_MAIN_PAGE));
+                getCallback().onSelectPage(new HistoryEntry(title, 
HistoryEntry.SOURCE_FEED_MAIN_PAGE));
             }
         }
     }
diff --git 
a/app/src/main/java/org/wikipedia/feed/mostread/MostReadCardView.java 
b/app/src/main/java/org/wikipedia/feed/mostread/MostReadCardView.java
index 4f2febf..b30e350 100644
--- a/app/src/main/java/org/wikipedia/feed/mostread/MostReadCardView.java
+++ b/app/src/main/java/org/wikipedia/feed/mostread/MostReadCardView.java
@@ -2,11 +2,9 @@
 
 import android.content.Context;
 import android.support.annotation.NonNull;
-import android.support.annotation.Nullable;
 
 import org.wikipedia.R;
 import org.wikipedia.feed.view.CardHeaderView;
-import org.wikipedia.feed.view.FeedAdapter;
 import org.wikipedia.feed.view.ListCardView;
 import org.wikipedia.feed.view.PageTitleListCardItemView;
 import org.wikipedia.feed.view.PageTitleRecyclerAdapter;
@@ -25,7 +23,7 @@
     @Override public void setCard(@NonNull MostReadListCard card) {
         super.setCard(card);
         header(card);
-        set(new RecyclerAdapter(card.items(), getCallback()));
+        set(new RecyclerAdapter(card.items()));
     }
 
     private void header(@NonNull MostReadListCard card) {
@@ -39,20 +37,16 @@
         header(header);
     }
 
-    private static class RecyclerAdapter extends 
PageTitleRecyclerAdapter<MostReadItemCard> {
-        @Nullable private FeedAdapter.Callback callback;
-
-        RecyclerAdapter(@NonNull List<MostReadItemCard> items,
-                        @Nullable FeedAdapter.Callback callback) {
+    private class RecyclerAdapter extends 
PageTitleRecyclerAdapter<MostReadItemCard> {
+        RecyclerAdapter(@NonNull List<MostReadItemCard> items) {
             super(items);
-            this.callback = callback;
         }
 
         @Override
         public void 
onBindViewHolder(DefaultViewHolder<PageTitleListCardItemView> holder, int 
position) {
             MostReadItemCard card = item(position);
             holder.getView().setHistoryEntry(new 
HistoryEntry(card.pageTitle(), HistoryEntry.SOURCE_FEED_MOST_READ));
-            holder.getView().setCallback(callback);
+            holder.getView().setCallback(getCallback());
         }
     }
 }
diff --git a/app/src/main/java/org/wikipedia/feed/news/NewsListCardView.java 
b/app/src/main/java/org/wikipedia/feed/news/NewsListCardView.java
index d34a217..37e1e5c 100644
--- a/app/src/main/java/org/wikipedia/feed/news/NewsListCardView.java
+++ b/app/src/main/java/org/wikipedia/feed/news/NewsListCardView.java
@@ -2,12 +2,10 @@
 
 import android.content.Context;
 import android.support.annotation.NonNull;
-import android.support.annotation.Nullable;
 import android.view.View;
 
 import org.wikipedia.R;
 import org.wikipedia.feed.view.CardHeaderView;
-import org.wikipedia.feed.view.FeedAdapter;
 import org.wikipedia.feed.view.HorizontalScrollingListCardItemView;
 import org.wikipedia.feed.view.HorizontalScrollingListCardView;
 import org.wikipedia.util.DateUtil;
@@ -29,7 +27,7 @@
     @Override public void setCard(@NonNull NewsListCard card) {
         super.setCard(card);
         header(card);
-        set(new RecyclerAdapter(card.items(), getCallback()));
+        set(new RecyclerAdapter(card.items()));
     }
 
     private void header(@NonNull NewsListCard card) {
@@ -43,12 +41,9 @@
         header(header);
     }
 
-    private static class RecyclerAdapter extends 
HorizontalScrollingListCardView.RecyclerAdapter<NewsItemCard> {
-        @Nullable private FeedAdapter.Callback callback;
-
-        RecyclerAdapter(@NonNull List<NewsItemCard> items, @Nullable 
FeedAdapter.Callback callback) {
+    private class RecyclerAdapter extends 
HorizontalScrollingListCardView.RecyclerAdapter<NewsItemCard> {
+        RecyclerAdapter(@NonNull List<NewsItemCard> items) {
             super(items);
-            this.callback = callback;
         }
 
         @Override
@@ -56,12 +51,12 @@
             final NewsItemCard card = item(i);
             holder.getView().setText(card.text());
             holder.getView().setImage(card.image());
-            holder.getView().setCallback(callback);
+            holder.getView().setCallback(getCallback());
             holder.getView().setOnClickListener(new OnClickListener() {
                 @Override
                 public void onClick(View view) {
-                    if (callback != null) {
-                        callback.onNewsItemSelected(card);
+                    if (getCallback() != null) {
+                        getCallback().onNewsItemSelected(card);
                     }
                 }
             });
diff --git a/app/src/main/java/org/wikipedia/feed/random/RandomCardView.java 
b/app/src/main/java/org/wikipedia/feed/random/RandomCardView.java
index 7d6af36..5bf5b77 100644
--- a/app/src/main/java/org/wikipedia/feed/random/RandomCardView.java
+++ b/app/src/main/java/org/wikipedia/feed/random/RandomCardView.java
@@ -27,19 +27,13 @@
 
     @Override public void setCallback(@Nullable FeedAdapter.Callback callback) 
{
         super.setCallback(callback);
-        setOnClickListener(new CallbackAdapter(callback));
+        setOnClickListener(new CallbackAdapter());
     }
 
     private class CallbackAdapter implements OnClickListener {
-        @Nullable private final FeedAdapter.Callback callback;
-
-        CallbackAdapter(@Nullable FeedAdapter.Callback callback) {
-            this.callback = callback;
-        }
-
         @Override
         public void onClick(View view) {
-            if (callback != null && getCard() != null) {
+            if (getCallback() != null && getCard() != null) {
                 new RandomSummaryService(getCard().site(), 
serviceCallback).get();
             }
         }
@@ -48,8 +42,9 @@
                 = new RandomSummaryService.RandomSummaryCallback() {
             @Override
             public void onSuccess(PageTitle title) {
-                if (callback != null) {
-                    callback.onSelectPage(new HistoryEntry(title, 
HistoryEntry.SOURCE_FEED_RANDOM));
+                if (getCallback() != null) {
+                    getCallback().onSelectPage(new HistoryEntry(title,
+                            HistoryEntry.SOURCE_FEED_RANDOM));
                 }
             }
 
diff --git a/app/src/main/java/org/wikipedia/feed/view/FeedAdapter.java 
b/app/src/main/java/org/wikipedia/feed/view/FeedAdapter.java
index 88ae1f8..79a7352 100644
--- a/app/src/main/java/org/wikipedia/feed/view/FeedAdapter.java
+++ b/app/src/main/java/org/wikipedia/feed/view/FeedAdapter.java
@@ -56,15 +56,23 @@
         ((FeedCardView<Card>) view).setCard(item);
     }
 
+    @Override public void onViewAttachedToWindow(DefaultViewHolder<T> holder) {
+        super.onViewAttachedToWindow(holder);
+        holder.getView().setCallback(callback);
+    }
+
+    @Override public void onViewDetachedFromWindow(DefaultViewHolder<T> 
holder) {
+        holder.getView().setCallback(null);
+        super.onViewDetachedFromWindow(holder);
+    }
+
     @Override public int getItemViewType(int position) {
         return item(position).type().code();
     }
 
     @NonNull private T newView(@NonNull Context context, int viewType) {
-        FeedCardView<?> view = CardType.of(viewType).newView(context);
-        view.setCallback(callback);
         //noinspection unchecked
-        return (T) view;
+        return (T) CardType.of(viewType).newView(context);
     }
 
     private boolean isCardAssociatedWithView(@NonNull View view, @NonNull Card 
card) {

-- 
To view, visit https://gerrit.wikimedia.org/r/312148
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1c56cfff175ec9d8bfeab523bcc6c3cf6b5fff24
Gerrit-PatchSet: 1
Gerrit-Project: apps/android/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Niedzielski <sniedziel...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to