From 72ac4e5581ee2493222c337ddf0a522e0689e974 Mon Sep 17 00:00:00 2001 From: Pangoraw Date: Thu, 11 May 2017 09:11:15 +0000 Subject: [PATCH] Allow broadcasting event across activities. --- .../keylesspalace/tusky/AccountActivity.java | 10 +++++ .../tusky/fragment/TimelineFragment.java | 6 +++ .../tusky/util/TimelineReceiver.java | 41 +++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 app/src/main/java/com/keylesspalace/tusky/util/TimelineReceiver.java diff --git a/app/src/main/java/com/keylesspalace/tusky/AccountActivity.java b/app/src/main/java/com/keylesspalace/tusky/AccountActivity.java index 6711781b..ae6f99ac 100644 --- a/app/src/main/java/com/keylesspalace/tusky/AccountActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/AccountActivity.java @@ -32,6 +32,7 @@ import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; +import android.support.v4.content.LocalBroadcastManager; import android.support.v4.view.ViewCompat; import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBar; @@ -51,6 +52,7 @@ import com.keylesspalace.tusky.pager.AccountPagerAdapter; import com.keylesspalace.tusky.util.LinkHelper; import com.keylesspalace.tusky.util.Assert; import com.keylesspalace.tusky.util.Log; +import com.keylesspalace.tusky.util.TimelineReceiver; import com.keylesspalace.tusky.util.ThemeUtils; import com.pkmmte.view.CircularImageView; import com.squareup.picasso.Picasso; @@ -467,6 +469,7 @@ public class AccountActivity extends BaseActivity implements SFragment.OnUserRem Snackbar.LENGTH_LONG).show(); } else { followState = FollowState.NOT_FOLLOWING; + broadcast(TimelineReceiver.Types.UNFOLLOW_ACCOUNT, id); } updateButtons(); } else { @@ -517,6 +520,7 @@ public class AccountActivity extends BaseActivity implements SFragment.OnUserRem @Override public void onResponse(Call call, Response response) { if (response.isSuccessful()) { + broadcast(TimelineReceiver.Types.BLOCK_ACCOUNT, id); blocking = response.body().blocking; updateButtons(); } else { @@ -554,6 +558,7 @@ public class AccountActivity extends BaseActivity implements SFragment.OnUserRem @Override public void onResponse(Call call, Response response) { if (response.isSuccessful()) { + broadcast(TimelineReceiver.Types.MUTE_ACCOUNT, id); muting = response.body().muting; updateButtons(); } else { @@ -586,6 +591,11 @@ public class AccountActivity extends BaseActivity implements SFragment.OnUserRem .show(); } + private void broadcast(String action, String id) { + Intent intent = new Intent(action); + intent.putExtra("id", id); + LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent); + } @Override public boolean onOptionsItemSelected(MenuItem item) { 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 82205996..dd35d16b 100644 --- a/app/src/main/java/com/keylesspalace/tusky/fragment/TimelineFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/fragment/TimelineFragment.java @@ -23,6 +23,7 @@ import android.preference.PreferenceManager; import android.support.annotation.Nullable; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.TabLayout; +import android.support.v4.content.LocalBroadcastManager; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.LinearLayoutManager; @@ -39,6 +40,7 @@ import com.keylesspalace.tusky.interfaces.StatusActionListener; import com.keylesspalace.tusky.interfaces.StatusRemoveListener; import com.keylesspalace.tusky.util.EndlessOnScrollListener; import com.keylesspalace.tusky.util.Log; +import com.keylesspalace.tusky.util.TimelineReceiver; import com.keylesspalace.tusky.util.ThemeUtils; import java.util.List; @@ -72,6 +74,7 @@ public class TimelineFragment extends SFragment implements private EndlessOnScrollListener scrollListener; private TabLayout.OnTabSelectedListener onTabSelectedListener; private boolean hideFab; + private TimelineReceiver timelineReceiver; public static TimelineFragment newInstance(Kind kind) { TimelineFragment fragment = new TimelineFragment(); @@ -120,6 +123,8 @@ public class TimelineFragment extends SFragment implements adapter = new TimelineAdapter(this); recyclerView.setAdapter(adapter); + timelineReceiver = new TimelineReceiver(adapter); + LocalBroadcastManager.getInstance(context.getApplicationContext()).registerReceiver(timelineReceiver, TimelineReceiver.getFilter(kind)); return rootView; } @@ -204,6 +209,7 @@ public class TimelineFragment extends SFragment implements TabLayout tabLayout = (TabLayout) getActivity().findViewById(R.id.tab_layout); tabLayout.removeOnTabSelectedListener(onTabSelectedListener); } + LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(timelineReceiver); super.onDestroyView(); } diff --git a/app/src/main/java/com/keylesspalace/tusky/util/TimelineReceiver.java b/app/src/main/java/com/keylesspalace/tusky/util/TimelineReceiver.java new file mode 100644 index 00000000..f6acc0d9 --- /dev/null +++ b/app/src/main/java/com/keylesspalace/tusky/util/TimelineReceiver.java @@ -0,0 +1,41 @@ +package com.keylesspalace.tusky.util; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; + +import com.keylesspalace.tusky.adapter.TimelineAdapter; +import com.keylesspalace.tusky.fragment.TimelineFragment; + +public class TimelineReceiver extends BroadcastReceiver { + public static final class Types { + public static final String UNFOLLOW_ACCOUNT = "UNFOLLOW_ACCOUNT"; + public static final String BLOCK_ACCOUNT = "BLOCK_ACCOUNT"; + public static final String MUTE_ACCOUNT = "MUTE_ACCOUNT"; + } + + TimelineAdapter adapter; + + public TimelineReceiver(TimelineAdapter adapter) { + super(); + this.adapter = adapter; + } + + @Override + public void onReceive(Context context, final Intent intent) { + String id = intent.getStringExtra("id"); + adapter.removeAllByAccountId(id); + } + + public static IntentFilter getFilter(TimelineFragment.Kind kind) { + IntentFilter intentFilter = new IntentFilter(); + if (kind == TimelineFragment.Kind.HOME) { + intentFilter.addAction(Types.UNFOLLOW_ACCOUNT); + } + intentFilter.addAction(Types.BLOCK_ACCOUNT); + intentFilter.addAction(Types.MUTE_ACCOUNT); + + return intentFilter; + } +}