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.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.PersistableBundle;
|
import android.os.PersistableBundle;
|
||||||
import android.support.annotation.NonNull;
|
|
||||||
import android.support.design.widget.FloatingActionButton;
|
import android.support.design.widget.FloatingActionButton;
|
||||||
import android.support.design.widget.TabLayout;
|
import android.support.design.widget.TabLayout;
|
||||||
import android.support.graphics.drawable.VectorDrawableCompat;
|
import android.support.graphics.drawable.VectorDrawableCompat;
|
||||||
import android.support.v4.app.Fragment;
|
|
||||||
import android.support.v4.content.ContextCompat;
|
import android.support.v4.content.ContextCompat;
|
||||||
|
import android.support.v4.content.LocalBroadcastManager;
|
||||||
import android.support.v4.view.ViewPager;
|
import android.support.v4.view.ViewPager;
|
||||||
import android.support.v7.app.AlertDialog;
|
import android.support.v7.app.AlertDialog;
|
||||||
import android.text.SpannableStringBuilder;
|
import android.text.SpannableStringBuilder;
|
||||||
|
@ -45,8 +44,8 @@ import com.arlib.floatingsearchview.FloatingSearchView;
|
||||||
import com.arlib.floatingsearchview.suggestions.SearchSuggestionsAdapter;
|
import com.arlib.floatingsearchview.suggestions.SearchSuggestionsAdapter;
|
||||||
import com.arlib.floatingsearchview.suggestions.model.SearchSuggestion;
|
import com.arlib.floatingsearchview.suggestions.model.SearchSuggestion;
|
||||||
import com.keylesspalace.tusky.entity.Account;
|
import com.keylesspalace.tusky.entity.Account;
|
||||||
import com.keylesspalace.tusky.fragment.SFragment;
|
|
||||||
import com.keylesspalace.tusky.pager.TimelinePagerAdapter;
|
import com.keylesspalace.tusky.pager.TimelinePagerAdapter;
|
||||||
|
import com.keylesspalace.tusky.receiver.TimelineReceiver;
|
||||||
import com.keylesspalace.tusky.util.ThemeUtils;
|
import com.keylesspalace.tusky.util.ThemeUtils;
|
||||||
import com.mikepenz.google_material_typeface_library.GoogleMaterial;
|
import com.mikepenz.google_material_typeface_library.GoogleMaterial;
|
||||||
import com.mikepenz.materialdrawer.AccountHeader;
|
import com.mikepenz.materialdrawer.AccountHeader;
|
||||||
|
@ -537,10 +536,9 @@ public class MainActivity extends BaseActivity {
|
||||||
@Override
|
@Override
|
||||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||||
if (requestCode == COMPOSE_RESULT && resultCode == ComposeActivity.RESULT_OK) {
|
if (requestCode == COMPOSE_RESULT && resultCode == ComposeActivity.RESULT_OK) {
|
||||||
TimelinePagerAdapter adapter = (TimelinePagerAdapter) viewPager.getAdapter();
|
Intent intent = new Intent(TimelineReceiver.Types.STATUS_COMPOSED);
|
||||||
if (adapter.getCurrentFragment() instanceof SFragment) {
|
LocalBroadcastManager.getInstance(getApplicationContext())
|
||||||
((SFragment) adapter.getCurrentFragment()).onSuccessfulStatus();
|
.sendBroadcast(intent);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
super.onActivityResult(requestCode, resultCode, data);
|
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.)
|
// Fix for GitHub issues #190, #259 (MainActivity won't restart on screen rotation.)
|
||||||
@Override
|
@Override
|
||||||
public void onConfigurationChanged(Configuration newConfig) {
|
public void onConfigurationChanged(Configuration newConfig) {
|
||||||
|
|
|
@ -33,7 +33,6 @@ import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
|
||||||
|
|
||||||
import com.keylesspalace.tusky.MainActivity;
|
import com.keylesspalace.tusky.MainActivity;
|
||||||
import com.keylesspalace.tusky.adapter.NotificationsAdapter;
|
import com.keylesspalace.tusky.adapter.NotificationsAdapter;
|
||||||
import com.keylesspalace.tusky.R;
|
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.util.ThemeUtils;
|
||||||
import com.keylesspalace.tusky.view.EndlessOnScrollListener;
|
import com.keylesspalace.tusky.view.EndlessOnScrollListener;
|
||||||
|
|
||||||
import java.sql.Time;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import retrofit2.Call;
|
import retrofit2.Call;
|
||||||
|
|
|
@ -99,17 +99,6 @@ public abstract class SFragment extends BaseFragment {
|
||||||
startActivityForResult(intent, COMPOSE_RESULT);
|
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,
|
protected void reblog(final Status status, final boolean reblog,
|
||||||
final RecyclerView.Adapter adapter, final int position) {
|
final RecyclerView.Adapter adapter, final int position) {
|
||||||
String id = status.getActionableId();
|
String id = status.getActionableId();
|
||||||
|
|
|
@ -125,7 +125,7 @@ public class TimelineFragment extends SFragment implements
|
||||||
adapter = new TimelineAdapter(this);
|
adapter = new TimelineAdapter(this);
|
||||||
recyclerView.setAdapter(adapter);
|
recyclerView.setAdapter(adapter);
|
||||||
|
|
||||||
timelineReceiver = new TimelineReceiver(adapter);
|
timelineReceiver = new TimelineReceiver(adapter, this);
|
||||||
LocalBroadcastManager.getInstance(context.getApplicationContext())
|
LocalBroadcastManager.getInstance(context.getApplicationContext())
|
||||||
.registerReceiver(timelineReceiver, TimelineReceiver.getFilter(kind));
|
.registerReceiver(timelineReceiver, TimelineReceiver.getFilter(kind));
|
||||||
return rootView;
|
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) {
|
public void onReply(int position) {
|
||||||
super.reply(adapter.getItem(position));
|
super.reply(adapter.getItem(position));
|
||||||
}
|
}
|
||||||
|
|
|
@ -94,7 +94,7 @@ public class ViewThreadFragment extends SFragment implements
|
||||||
mastodonApi = null;
|
mastodonApi = null;
|
||||||
thisThreadsStatusId = null;
|
thisThreadsStatusId = null;
|
||||||
|
|
||||||
timelineReceiver = new TimelineReceiver(adapter);
|
timelineReceiver = new TimelineReceiver(adapter, this);
|
||||||
LocalBroadcastManager.getInstance(context.getApplicationContext())
|
LocalBroadcastManager.getInstance(context.getApplicationContext())
|
||||||
.registerReceiver(timelineReceiver, TimelineReceiver.getFilter(null));
|
.registerReceiver(timelineReceiver, TimelineReceiver.getFilter(null));
|
||||||
|
|
||||||
|
@ -187,12 +187,6 @@ public class ViewThreadFragment extends SFragment implements
|
||||||
sendThreadRequest(thisThreadsStatusId);
|
sendThreadRequest(thisThreadsStatusId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onSuccessfulStatus() {
|
|
||||||
onRefresh();
|
|
||||||
super.onSuccessfulStatus();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void onReply(int position) {
|
public void onReply(int position) {
|
||||||
super.reply(adapter.getItem(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.AccountListFragment;
|
||||||
import com.keylesspalace.tusky.fragment.TimelineFragment;
|
import com.keylesspalace.tusky.fragment.TimelineFragment;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class AccountPagerAdapter extends FragmentPagerAdapter {
|
public class AccountPagerAdapter extends FragmentPagerAdapter {
|
||||||
private Context context;
|
private Context context;
|
||||||
private String accountId;
|
private String accountId;
|
||||||
private String[] pageTitles;
|
private String[] pageTitles;
|
||||||
private List<Fragment> registeredFragments;
|
|
||||||
|
|
||||||
public AccountPagerAdapter(FragmentManager manager, Context context, String accountId) {
|
public AccountPagerAdapter(FragmentManager manager, Context context, String accountId) {
|
||||||
super(manager);
|
super(manager);
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.accountId = accountId;
|
this.accountId = accountId;
|
||||||
registeredFragments = new ArrayList<>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPageTitles(String[] titles) {
|
public void setPageTitles(String[] titles) {
|
||||||
|
@ -82,21 +77,4 @@ public class AccountPagerAdapter extends FragmentPagerAdapter {
|
||||||
title.setText(pageTitles[position]);
|
title.setText(pageTitles[position]);
|
||||||
return view;
|
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.Fragment;
|
||||||
import android.support.v4.app.FragmentManager;
|
import android.support.v4.app.FragmentManager;
|
||||||
import android.support.v4.app.FragmentPagerAdapter;
|
import android.support.v4.app.FragmentPagerAdapter;
|
||||||
import android.view.ViewGroup;
|
|
||||||
|
|
||||||
import com.keylesspalace.tusky.fragment.NotificationsFragment;
|
import com.keylesspalace.tusky.fragment.NotificationsFragment;
|
||||||
import com.keylesspalace.tusky.fragment.TimelineFragment;
|
import com.keylesspalace.tusky.fragment.TimelineFragment;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class TimelinePagerAdapter extends FragmentPagerAdapter {
|
public class TimelinePagerAdapter extends FragmentPagerAdapter {
|
||||||
private int currentFragmentIndex;
|
|
||||||
private List<Fragment> registeredFragments;
|
|
||||||
|
|
||||||
public TimelinePagerAdapter(FragmentManager manager) {
|
public TimelinePagerAdapter(FragmentManager manager) {
|
||||||
super(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
|
@Override
|
||||||
|
@ -82,17 +57,4 @@ public class TimelinePagerAdapter extends FragmentPagerAdapter {
|
||||||
public CharSequence getPageTitle(int position) {
|
public CharSequence getPageTitle(int position) {
|
||||||
return null;
|
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.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
|
import android.support.v4.widget.SwipeRefreshLayout;
|
||||||
|
|
||||||
import com.keylesspalace.tusky.fragment.TimelineFragment;
|
import com.keylesspalace.tusky.fragment.TimelineFragment;
|
||||||
import com.keylesspalace.tusky.interfaces.AdapterItemRemover;
|
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 UNFOLLOW_ACCOUNT = "UNFOLLOW_ACCOUNT";
|
||||||
public static final String BLOCK_ACCOUNT = "BLOCK_ACCOUNT";
|
public static final String BLOCK_ACCOUNT = "BLOCK_ACCOUNT";
|
||||||
public static final String MUTE_ACCOUNT = "MUTE_ACCOUNT";
|
public static final String MUTE_ACCOUNT = "MUTE_ACCOUNT";
|
||||||
|
public static final String STATUS_COMPOSED = "STATUS_COMPOSED";
|
||||||
}
|
}
|
||||||
|
|
||||||
AdapterItemRemover adapter;
|
AdapterItemRemover adapter;
|
||||||
|
SwipeRefreshLayout.OnRefreshListener refreshListener;
|
||||||
|
|
||||||
public TimelineReceiver(AdapterItemRemover adapter) {
|
public TimelineReceiver(AdapterItemRemover adapter) {
|
||||||
super();
|
super();
|
||||||
this.adapter = adapter;
|
this.adapter = adapter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public TimelineReceiver(AdapterItemRemover adapter,
|
||||||
|
SwipeRefreshLayout.OnRefreshListener refreshListener) {
|
||||||
|
super();
|
||||||
|
this.adapter = adapter;
|
||||||
|
this.refreshListener = refreshListener;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, final Intent intent) {
|
public void onReceive(Context context, final Intent intent) {
|
||||||
String id = intent.getStringExtra("id");
|
switch (intent.getAction()) {
|
||||||
adapter.removeAllByAccountId(id);
|
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) {
|
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.BLOCK_ACCOUNT);
|
||||||
intentFilter.addAction(Types.MUTE_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;
|
return intentFilter;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue