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.
This commit is contained in:
parent
9c56f0e7bc
commit
a4ee128e26
8 changed files with 36 additions and 108 deletions
|
@ -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) {
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
|
|
@ -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<Fragment> 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<Fragment> getRegisteredFragments() {
|
||||
return registeredFragments;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Fragment> registeredFragments;
|
||||
|
||||
public TimelinePagerAdapter(FragmentManager manager) {
|
||||
super(manager);
|
||||
currentFragmentIndex = 0;
|
||||
registeredFragments = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Fragment getCurrentFragment() {
|
||||
return registeredFragments.get(currentFragmentIndex);
|
||||
}
|
||||
|
||||
public List<Fragment> 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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue