From 00188182b744bac5cdb55b9af2e8d06e021c4af0 Mon Sep 17 00:00:00 2001 From: Vavassor Date: Tue, 6 Jun 2017 18:20:55 -0400 Subject: [PATCH] Broadcasts now cause a refresh on timelines after a post is composed instead of listeners. As a side effect, pagers don't have to keep track of "registered fragments", which was a bad idea and caused crashes. --- .../com/keylesspalace/tusky/MainActivity.java | 22 +++-------- .../tusky/fragment/NotificationsFragment.java | 2 - .../tusky/fragment/SFragment.java | 11 ------ .../tusky/fragment/TimelineFragment.java | 10 +---- .../tusky/fragment/ViewThreadFragment.java | 8 +--- .../tusky/pager/AccountPagerAdapter.java | 22 ----------- .../tusky/pager/TimelinePagerAdapter.java | 38 ------------------- .../tusky/receiver/TimelineReceiver.java | 31 ++++++++++++++- 8 files changed, 36 insertions(+), 108 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java index ed800e9e..4c12a1f8 100644 --- a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java @@ -24,12 +24,11 @@ import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.os.PersistableBundle; -import android.support.annotation.NonNull; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.TabLayout; import android.support.graphics.drawable.VectorDrawableCompat; -import android.support.v4.app.Fragment; import android.support.v4.content.ContextCompat; +import android.support.v4.content.LocalBroadcastManager; import android.support.v4.view.ViewPager; import android.support.v7.app.AlertDialog; import android.text.SpannableStringBuilder; @@ -45,8 +44,8 @@ import com.arlib.floatingsearchview.FloatingSearchView; import com.arlib.floatingsearchview.suggestions.SearchSuggestionsAdapter; import com.arlib.floatingsearchview.suggestions.model.SearchSuggestion; import com.keylesspalace.tusky.entity.Account; -import com.keylesspalace.tusky.fragment.SFragment; import com.keylesspalace.tusky.pager.TimelinePagerAdapter; +import com.keylesspalace.tusky.receiver.TimelineReceiver; import com.keylesspalace.tusky.util.ThemeUtils; import com.mikepenz.google_material_typeface_library.GoogleMaterial; import com.mikepenz.materialdrawer.AccountHeader; @@ -537,10 +536,9 @@ public class MainActivity extends BaseActivity { @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == COMPOSE_RESULT && resultCode == ComposeActivity.RESULT_OK) { - TimelinePagerAdapter adapter = (TimelinePagerAdapter) viewPager.getAdapter(); - if (adapter.getCurrentFragment() instanceof SFragment) { - ((SFragment) adapter.getCurrentFragment()).onSuccessfulStatus(); - } + Intent intent = new Intent(TimelineReceiver.Types.STATUS_COMPOSED); + LocalBroadcastManager.getInstance(getApplicationContext()) + .sendBroadcast(intent); } super.onActivityResult(requestCode, resultCode, data); } @@ -557,16 +555,6 @@ public class MainActivity extends BaseActivity { } } - @Override - public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, - @NonNull int[] grantResults) { - super.onRequestPermissionsResult(requestCode, permissions, grantResults); - TimelinePagerAdapter adapter = (TimelinePagerAdapter) viewPager.getAdapter(); - for (Fragment fragment : adapter.getRegisteredFragments()) { - fragment.onRequestPermissionsResult(requestCode, permissions, grantResults); - } - } - // Fix for GitHub issues #190, #259 (MainActivity won't restart on screen rotation.) @Override public void onConfigurationChanged(Configuration newConfig) { diff --git a/app/src/main/java/com/keylesspalace/tusky/fragment/NotificationsFragment.java b/app/src/main/java/com/keylesspalace/tusky/fragment/NotificationsFragment.java index 8acbdcf9..41e78e38 100644 --- a/app/src/main/java/com/keylesspalace/tusky/fragment/NotificationsFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/fragment/NotificationsFragment.java @@ -33,7 +33,6 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; - import com.keylesspalace.tusky.MainActivity; import com.keylesspalace.tusky.adapter.NotificationsAdapter; import com.keylesspalace.tusky.R; @@ -44,7 +43,6 @@ import com.keylesspalace.tusky.receiver.TimelineReceiver; import com.keylesspalace.tusky.util.ThemeUtils; import com.keylesspalace.tusky.view.EndlessOnScrollListener; -import java.sql.Time; import java.util.List; import retrofit2.Call; diff --git a/app/src/main/java/com/keylesspalace/tusky/fragment/SFragment.java b/app/src/main/java/com/keylesspalace/tusky/fragment/SFragment.java index 0094b526..b5d82bb9 100644 --- a/app/src/main/java/com/keylesspalace/tusky/fragment/SFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/fragment/SFragment.java @@ -99,17 +99,6 @@ public abstract class SFragment extends BaseFragment { startActivityForResult(intent, COMPOSE_RESULT); } - public void onSuccessfulStatus() {} - - @Override - public void onActivityResult(int requestCode, int resultCode, Intent data) { - if (requestCode == COMPOSE_RESULT && resultCode == ComposeActivity.RESULT_OK) { - onSuccessfulStatus(); - } else { - super.onActivityResult(requestCode, resultCode, data); - } - } - protected void reblog(final Status status, final boolean reblog, final RecyclerView.Adapter adapter, final int position) { String id = status.getActionableId(); diff --git a/app/src/main/java/com/keylesspalace/tusky/fragment/TimelineFragment.java b/app/src/main/java/com/keylesspalace/tusky/fragment/TimelineFragment.java index 23722ffc..534d8b3f 100644 --- a/app/src/main/java/com/keylesspalace/tusky/fragment/TimelineFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/fragment/TimelineFragment.java @@ -125,7 +125,7 @@ public class TimelineFragment extends SFragment implements adapter = new TimelineAdapter(this); recyclerView.setAdapter(adapter); - timelineReceiver = new TimelineReceiver(adapter); + timelineReceiver = new TimelineReceiver(adapter, this); LocalBroadcastManager.getInstance(context.getApplicationContext()) .registerReceiver(timelineReceiver, TimelineReceiver.getFilter(kind)); return rootView; @@ -352,14 +352,6 @@ public class TimelineFragment extends SFragment implements } } - @Override - public void onSuccessfulStatus() { - if (kind == Kind.HOME || kind == Kind.PUBLIC_FEDERATED || kind == Kind.PUBLIC_LOCAL) { - onRefresh(); - } - super.onSuccessfulStatus(); - } - public void onReply(int position) { super.reply(adapter.getItem(position)); } diff --git a/app/src/main/java/com/keylesspalace/tusky/fragment/ViewThreadFragment.java b/app/src/main/java/com/keylesspalace/tusky/fragment/ViewThreadFragment.java index 1518b7dd..0b25998c 100644 --- a/app/src/main/java/com/keylesspalace/tusky/fragment/ViewThreadFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/fragment/ViewThreadFragment.java @@ -94,7 +94,7 @@ public class ViewThreadFragment extends SFragment implements mastodonApi = null; thisThreadsStatusId = null; - timelineReceiver = new TimelineReceiver(adapter); + timelineReceiver = new TimelineReceiver(adapter, this); LocalBroadcastManager.getInstance(context.getApplicationContext()) .registerReceiver(timelineReceiver, TimelineReceiver.getFilter(null)); @@ -187,12 +187,6 @@ public class ViewThreadFragment extends SFragment implements sendThreadRequest(thisThreadsStatusId); } - @Override - public void onSuccessfulStatus() { - onRefresh(); - super.onSuccessfulStatus(); - } - public void onReply(int position) { super.reply(adapter.getItem(position)); } diff --git a/app/src/main/java/com/keylesspalace/tusky/pager/AccountPagerAdapter.java b/app/src/main/java/com/keylesspalace/tusky/pager/AccountPagerAdapter.java index 8f04ae18..ddc87252 100644 --- a/app/src/main/java/com/keylesspalace/tusky/pager/AccountPagerAdapter.java +++ b/app/src/main/java/com/keylesspalace/tusky/pager/AccountPagerAdapter.java @@ -28,20 +28,15 @@ import com.keylesspalace.tusky.R; import com.keylesspalace.tusky.fragment.AccountListFragment; import com.keylesspalace.tusky.fragment.TimelineFragment; -import java.util.ArrayList; -import java.util.List; - public class AccountPagerAdapter extends FragmentPagerAdapter { private Context context; private String accountId; private String[] pageTitles; - private List registeredFragments; public AccountPagerAdapter(FragmentManager manager, Context context, String accountId) { super(manager); this.context = context; this.accountId = accountId; - registeredFragments = new ArrayList<>(); } public void setPageTitles(String[] titles) { @@ -82,21 +77,4 @@ public class AccountPagerAdapter extends FragmentPagerAdapter { title.setText(pageTitles[position]); return view; } - - @Override - public Object instantiateItem(ViewGroup container, int position) { - Fragment fragment = (Fragment) super.instantiateItem(container, position); - registeredFragments.add(fragment); - return fragment; - } - - @Override - public void destroyItem(ViewGroup container, int position, Object object) { - registeredFragments.remove((Fragment) object); - super.destroyItem(container, position, object); - } - - public List getRegisteredFragments() { - return registeredFragments; - } } diff --git a/app/src/main/java/com/keylesspalace/tusky/pager/TimelinePagerAdapter.java b/app/src/main/java/com/keylesspalace/tusky/pager/TimelinePagerAdapter.java index 82686041..64bada51 100644 --- a/app/src/main/java/com/keylesspalace/tusky/pager/TimelinePagerAdapter.java +++ b/app/src/main/java/com/keylesspalace/tusky/pager/TimelinePagerAdapter.java @@ -18,38 +18,13 @@ package com.keylesspalace.tusky.pager; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; -import android.view.ViewGroup; import com.keylesspalace.tusky.fragment.NotificationsFragment; import com.keylesspalace.tusky.fragment.TimelineFragment; -import java.util.ArrayList; -import java.util.List; - public class TimelinePagerAdapter extends FragmentPagerAdapter { - private int currentFragmentIndex; - private List registeredFragments; - public TimelinePagerAdapter(FragmentManager manager) { super(manager); - currentFragmentIndex = 0; - registeredFragments = new ArrayList<>(); - } - - public Fragment getCurrentFragment() { - return registeredFragments.get(currentFragmentIndex); - } - - public List getRegisteredFragments() { - return registeredFragments; - } - - @Override - public void setPrimaryItem(ViewGroup container, int position, Object object) { - if (position != currentFragmentIndex) { - currentFragmentIndex = position; - } - super.setPrimaryItem(container, position, object); } @Override @@ -82,17 +57,4 @@ public class TimelinePagerAdapter extends FragmentPagerAdapter { public CharSequence getPageTitle(int position) { return null; } - - @Override - public Object instantiateItem(ViewGroup container, int position) { - Fragment fragment = (Fragment) super.instantiateItem(container, position); - registeredFragments.add(fragment); - return fragment; - } - - @Override - public void destroyItem(ViewGroup container, int position, Object object) { - registeredFragments.remove((Fragment) object); - super.destroyItem(container, position, object); - } } diff --git a/app/src/main/java/com/keylesspalace/tusky/receiver/TimelineReceiver.java b/app/src/main/java/com/keylesspalace/tusky/receiver/TimelineReceiver.java index 89ebe925..2ab5901c 100644 --- a/app/src/main/java/com/keylesspalace/tusky/receiver/TimelineReceiver.java +++ b/app/src/main/java/com/keylesspalace/tusky/receiver/TimelineReceiver.java @@ -5,6 +5,7 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.support.annotation.Nullable; +import android.support.v4.widget.SwipeRefreshLayout; import com.keylesspalace.tusky.fragment.TimelineFragment; import com.keylesspalace.tusky.interfaces.AdapterItemRemover; @@ -14,19 +15,39 @@ public class TimelineReceiver extends BroadcastReceiver { public static final String UNFOLLOW_ACCOUNT = "UNFOLLOW_ACCOUNT"; public static final String BLOCK_ACCOUNT = "BLOCK_ACCOUNT"; public static final String MUTE_ACCOUNT = "MUTE_ACCOUNT"; + public static final String STATUS_COMPOSED = "STATUS_COMPOSED"; } AdapterItemRemover adapter; + SwipeRefreshLayout.OnRefreshListener refreshListener; public TimelineReceiver(AdapterItemRemover adapter) { super(); this.adapter = adapter; } + public TimelineReceiver(AdapterItemRemover adapter, + SwipeRefreshLayout.OnRefreshListener refreshListener) { + super(); + this.adapter = adapter; + this.refreshListener = refreshListener; + } + @Override public void onReceive(Context context, final Intent intent) { - String id = intent.getStringExtra("id"); - adapter.removeAllByAccountId(id); + switch (intent.getAction()) { + case Types.STATUS_COMPOSED: { + if (refreshListener != null) { + refreshListener.onRefresh(); + } + break; + } + default: { + String id = intent.getStringExtra("id"); + adapter.removeAllByAccountId(id); + break; + } + } } public static IntentFilter getFilter(@Nullable TimelineFragment.Kind kind) { @@ -36,6 +57,12 @@ public class TimelineReceiver extends BroadcastReceiver { } intentFilter.addAction(Types.BLOCK_ACCOUNT); intentFilter.addAction(Types.MUTE_ACCOUNT); + if (kind == null + || kind == TimelineFragment.Kind.HOME + || kind == TimelineFragment.Kind.PUBLIC_FEDERATED + || kind == TimelineFragment.Kind.PUBLIC_LOCAL) { + intentFilter.addAction(Types.STATUS_COMPOSED); + } return intentFilter; }