Finish all the activities after switching account by view another account's messages.

This commit is contained in:
Alex Ning 2019-08-19 23:58:35 +08:00
parent 556047aa42
commit 01f5dd7886
20 changed files with 250 additions and 10 deletions

View File

@ -19,6 +19,9 @@ import androidx.fragment.app.Fragment;
import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout; import com.google.android.material.appbar.CollapsingToolbarLayout;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import javax.inject.Inject; import javax.inject.Inject;
import butterknife.BindView; import butterknife.BindView;
@ -62,6 +65,8 @@ public class AccountPostsActivity extends AppCompatActivity implements UserThing
((Infinity) getApplication()).getAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
EventBus.getDefault().register(this);
Resources resources = getResources(); Resources resources = getResources();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1
@ -233,10 +238,21 @@ public class AccountPostsActivity extends AppCompatActivity implements UserThing
outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken);
} }
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
@Override @Override
public void userThingSortTypeSelected(String sortType) { public void userThingSortTypeSelected(String sortType) {
if(mFragment != null) { if(mFragment != null) {
((PostFragment) mFragment).changeSortType(sortType); ((PostFragment) mFragment).changeSortType(sortType);
} }
} }
@Subscribe
public void onAccountSwitchEvent(SwitchAccountEvent event) {
finish();
}
} }

View File

@ -19,6 +19,9 @@ import androidx.core.content.ContextCompat;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -63,6 +66,8 @@ public class EditCommentActivity extends AppCompatActivity {
((Infinity) getApplication()).getAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
EventBus.getDefault().register(this);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Window window = getWindow(); Window window = getWindow();
if((getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) != Configuration.UI_MODE_NIGHT_YES) { if((getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) != Configuration.UI_MODE_NIGHT_YES) {
@ -134,4 +139,15 @@ public class EditCommentActivity extends AppCompatActivity {
} }
return false; return false;
} }
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
@Subscribe
public void onAccountSwitchEvent(SwitchAccountEvent event) {
finish();
}
} }

View File

@ -20,6 +20,9 @@ import androidx.core.content.ContextCompat;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -62,6 +65,8 @@ public class EditPostActivity extends AppCompatActivity {
((Infinity) getApplication()).getAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
EventBus.getDefault().register(this);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Window window = getWindow(); Window window = getWindow();
if((getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) != Configuration.UI_MODE_NIGHT_YES) { if((getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) != Configuration.UI_MODE_NIGHT_YES) {
@ -124,4 +129,15 @@ public class EditPostActivity extends AppCompatActivity {
} }
return false; return false;
} }
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
@Subscribe
public void onAccountSwitchEvent(SwitchAccountEvent event) {
finish();
}
} }

View File

@ -18,6 +18,9 @@ import androidx.fragment.app.Fragment;
import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.AppBarLayout;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import javax.inject.Inject; import javax.inject.Inject;
import butterknife.BindView; import butterknife.BindView;
@ -65,6 +68,8 @@ public class FilteredThingActivity extends AppCompatActivity implements SortType
((Infinity) getApplication()).getAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
EventBus.getDefault().register(this);
Resources resources = getResources(); Resources resources = getResources();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1
@ -268,6 +273,12 @@ public class FilteredThingActivity extends AppCompatActivity implements SortType
outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken);
} }
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
@Override @Override
public void searchSortTypeSelected(String sortType) { public void searchSortTypeSelected(String sortType) {
((PostFragment)mFragment).changeSortType(sortType); ((PostFragment)mFragment).changeSortType(sortType);
@ -282,4 +293,9 @@ public class FilteredThingActivity extends AppCompatActivity implements SortType
public void userThingSortTypeSelected(String sortType) { public void userThingSortTypeSelected(String sortType) {
((PostFragment)mFragment).changeSortType(sortType); ((PostFragment)mFragment).changeSortType(sortType);
} }
@Subscribe
public void onAccountSwitchEvent(SwitchAccountEvent event) {
finish();
}
} }

View File

@ -252,6 +252,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
if(mNewAccountName != null) { if(mNewAccountName != null) {
if(account == null || !account.getUsername().equals(mNewAccountName)) { if(account == null || !account.getUsername().equals(mNewAccountName)) {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> { new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> {
EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName()));
mNewAccountName = null; mNewAccountName = null;
if(newAccount == null) { if(newAccount == null) {
mNullAccessToken = true; mNullAccessToken = true;
@ -272,7 +273,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
.build(); .build();
WorkManager.getInstance(this).enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, WorkManager.getInstance(this).enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG,
ExistingPeriodicWorkPolicy.REPLACE, pullNotificationRequest); ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest);
} }
bindView(); bindView();
@ -294,7 +295,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
.build(); .build();
WorkManager.getInstance(this).enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, WorkManager.getInstance(this).enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG,
ExistingPeriodicWorkPolicy.REPLACE, pullNotificationRequest); ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest);
bindView(); bindView();
} }
@ -318,7 +319,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
.build(); .build();
WorkManager.getInstance(this).enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, WorkManager.getInstance(this).enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG,
ExistingPeriodicWorkPolicy.REPLACE, pullNotificationRequest); ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest);
} }
bindView(); bindView();
@ -327,7 +328,6 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
} }
private void bindView() { private void bindView() {
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(sectionsPagerAdapter); viewPager.setAdapter(sectionsPagerAdapter);
viewPager.setOffscreenPageLimit(2); viewPager.setOffscreenPageLimit(2);
@ -733,7 +733,9 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
@Subscribe @Subscribe
public void onAccountSwitchEvent(SwitchAccountEvent event) { public void onAccountSwitchEvent(SwitchAccountEvent event) {
finish(); if(!getClass().getName().equals(event.excludeActivityClassName)) {
finish();
}
} }
private class SectionsPagerAdapter extends FragmentPagerAdapter { private class SectionsPagerAdapter extends FragmentPagerAdapter {

View File

@ -474,6 +474,11 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
flairTextView.setBackgroundColor(getResources().getColor(R.color.backgroundColorPrimaryDark)); flairTextView.setBackgroundColor(getResources().getColor(R.color.backgroundColorPrimaryDark));
} }
@Subscribe
public void onAccountSwitchEvent(SwitchAccountEvent event) {
finish();
}
@Subscribe @Subscribe
public void onSubmitImagePostEvent(SubmitImagePostEvent submitImagePostEvent) { public void onSubmitImagePostEvent(SubmitImagePostEvent submitImagePostEvent) {
isPosting = false; isPosting = false;

View File

@ -379,6 +379,11 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh
flairTextView.setBackgroundColor(getResources().getColor(R.color.backgroundColorPrimaryDark)); flairTextView.setBackgroundColor(getResources().getColor(R.color.backgroundColorPrimaryDark));
} }
@Subscribe
public void onAccountSwitchEvent(SwitchAccountEvent event) {
finish();
}
@Subscribe @Subscribe
public void onSubmitLinkPostEvent(SubmitTextOrLinkPostEvent submitTextOrLinkPostEvent) { public void onSubmitLinkPostEvent(SubmitTextOrLinkPostEvent submitTextOrLinkPostEvent) {
isPosting = false; isPosting = false;

View File

@ -375,6 +375,11 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh
flairTextView.setBackgroundColor(getResources().getColor(R.color.backgroundColorPrimaryDark)); flairTextView.setBackgroundColor(getResources().getColor(R.color.backgroundColorPrimaryDark));
} }
@Subscribe
public void onAccountSwitchEvent(SwitchAccountEvent event) {
finish();
}
@Subscribe @Subscribe
public void onSubmitTextPostEvent(SubmitTextOrLinkPostEvent submitTextOrLinkPostEvent) { public void onSubmitTextPostEvent(SubmitTextOrLinkPostEvent submitTextOrLinkPostEvent) {
isPosting = false; isPosting = false;

View File

@ -474,6 +474,11 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
flairTextView.setBackgroundColor(getResources().getColor(R.color.backgroundColorPrimaryDark)); flairTextView.setBackgroundColor(getResources().getColor(R.color.backgroundColorPrimaryDark));
} }
@Subscribe
public void onAccountSwitchEvent(SwitchAccountEvent event) {
finish();
}
@Subscribe @Subscribe
public void onSubmitVideoPostEvent(SubmitVideoPostEvent submitVideoPostEvent) { public void onSubmitVideoPostEvent(SubmitVideoPostEvent submitVideoPostEvent) {
isPosting = false; isPosting = false;

View File

@ -21,6 +21,8 @@ import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.AppBarLayout;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -64,6 +66,8 @@ public class RulesActivity extends AppCompatActivity {
((Infinity) getApplication()).getAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
EventBus.getDefault().register(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
Resources resources = getResources(); Resources resources = getResources();
@ -173,6 +177,17 @@ public class RulesActivity extends AppCompatActivity {
return false; return false;
} }
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
@Subscribe
public void onAccountSwitchEvent(SwitchAccountEvent event) {
finish();
}
private static class ParseRulesAsyncTask extends AsyncTask<Void, ArrayList<Rule>, ArrayList<Rule>> { private static class ParseRulesAsyncTask extends AsyncTask<Void, ArrayList<Rule>, ArrayList<Rule>> {
private String response; private String response;
private ParseRulesAsyncTaskListener parseRulesAsyncTaskListener; private ParseRulesAsyncTaskListener parseRulesAsyncTaskListener;

View File

@ -21,6 +21,9 @@ import androidx.core.content.ContextCompat;
import com.ferfalk.simplesearchview.SimpleSearchView; import com.ferfalk.simplesearchview.SimpleSearchView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
@ -53,6 +56,8 @@ public class SearchActivity extends AppCompatActivity {
ButterKnife.bind(this); ButterKnife.bind(this);
EventBus.getDefault().register(this);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Window window = getWindow(); Window window = getWindow();
if((getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) != Configuration.UI_MODE_NIGHT_YES) { if((getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) != Configuration.UI_MODE_NIGHT_YES) {
@ -213,4 +218,15 @@ public class SearchActivity extends AppCompatActivity {
outState.putString(SUBREDDIT_NAME_STATE, subredditName); outState.putString(SUBREDDIT_NAME_STATE, subredditName);
outState.putBoolean(SUBREDDIT_IS_USER_STATE, subredditIsUser); outState.putBoolean(SUBREDDIT_IS_USER_STATE, subredditIsUser);
} }
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
@Subscribe
public void onAccountSwitchEvent(SwitchAccountEvent event) {
finish();
}
} }

View File

@ -23,6 +23,9 @@ import androidx.viewpager.widget.ViewPager;
import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.tabs.TabLayout; import com.google.android.material.tabs.TabLayout;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import javax.inject.Inject; import javax.inject.Inject;
import butterknife.BindView; import butterknife.BindView;
@ -65,6 +68,8 @@ public class SearchResultActivity extends AppCompatActivity implements SearchPos
((Infinity) getApplication()).getAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
EventBus.getDefault().register(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
Resources resources = getResources(); Resources resources = getResources();
@ -206,6 +211,12 @@ public class SearchResultActivity extends AppCompatActivity implements SearchPos
outState.putString(ACCOUNT_NAME_STATE, mAccountName); outState.putString(ACCOUNT_NAME_STATE, mAccountName);
} }
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
@Override @Override
public void searchSortTypeSelected(String sortType) { public void searchSortTypeSelected(String sortType) {
sectionsPagerAdapter.changeSortType(sortType, 0); sectionsPagerAdapter.changeSortType(sortType, 0);
@ -216,6 +227,11 @@ public class SearchResultActivity extends AppCompatActivity implements SearchPos
sectionsPagerAdapter.changeSortType(sortType, fragmentPosition); sectionsPagerAdapter.changeSortType(sortType, fragmentPosition);
} }
@Subscribe
public void onAccountSwitchEvent(SwitchAccountEvent event) {
finish();
}
private class SectionsPagerAdapter extends FragmentPagerAdapter { private class SectionsPagerAdapter extends FragmentPagerAdapter {
private PostFragment postFragment; private PostFragment postFragment;

View File

@ -10,6 +10,9 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import javax.inject.Inject; import javax.inject.Inject;
import butterknife.BindView; import butterknife.BindView;
@ -46,6 +49,8 @@ public class SearchSubredditsResultActivity extends AppCompatActivity {
((Infinity) getApplication()).getAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
EventBus.getDefault().register(this);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
@ -113,4 +118,15 @@ public class SearchSubredditsResultActivity extends AppCompatActivity {
outState.putString(ACCESS_TOKEN_STATE, mAccessToken); outState.putString(ACCESS_TOKEN_STATE, mAccessToken);
outState.putString(ACCOUNT_NAME_STATE, mAccountName); outState.putString(ACCOUNT_NAME_STATE, mAccountName);
} }
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
@Subscribe
public void onAccountSwitchEvent(SwitchAccountEvent event) {
finish();
}
} }

View File

@ -21,6 +21,9 @@ import androidx.fragment.app.Fragment;
import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.AppBarLayout;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.ArrayList; import java.util.ArrayList;
import javax.inject.Inject; import javax.inject.Inject;
@ -75,6 +78,8 @@ public class SubredditSelectionActivity extends AppCompatActivity {
((Infinity) getApplication()).getAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
EventBus.getDefault().register(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
Resources resources = getResources(); Resources resources = getResources();
@ -254,4 +259,15 @@ public class SubredditSelectionActivity extends AppCompatActivity {
outState.putString(ACCOUNT_NAME_STATE, mAccountName); outState.putString(ACCOUNT_NAME_STATE, mAccountName);
outState.putString(ACCOUNT_PROFILE_IMAGE_URL, mAccountProfileImageUrl); outState.putString(ACCOUNT_PROFILE_IMAGE_URL, mAccountProfileImageUrl);
} }
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
@Subscribe
public void onAccountSwitchEvent(SwitchAccountEvent event) {
finish();
}
} }

View File

@ -21,6 +21,9 @@ import androidx.viewpager.widget.ViewPager;
import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.tabs.TabLayout; import com.google.android.material.tabs.TabLayout;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.ArrayList; import java.util.ArrayList;
import javax.inject.Inject; import javax.inject.Inject;
@ -68,6 +71,8 @@ public class SubscribedThingListingActivity extends AppCompatActivity {
((Infinity) getApplication()).getAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
EventBus.getDefault().register(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
Resources resources = getResources(); Resources resources = getResources();
@ -167,6 +172,12 @@ public class SubscribedThingListingActivity extends AppCompatActivity {
outState.putString(ACCOUNT_NAME_STATE, mAccountName); outState.putString(ACCOUNT_NAME_STATE, mAccountName);
} }
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
private void loadSubscriptions() { private void loadSubscriptions() {
if (!mInsertSuccess) { if (!mInsertSuccess) {
FetchSubscribedThing.fetchSubscribedThing(mOauthRetrofit, mAccessToken, mAccountName, null, FetchSubscribedThing.fetchSubscribedThing(mOauthRetrofit, mAccessToken, mAccountName, null,
@ -195,6 +206,11 @@ public class SubscribedThingListingActivity extends AppCompatActivity {
} }
} }
@Subscribe
public void onAccountSwitchEvent(SwitchAccountEvent event) {
finish();
}
private class SectionsPagerAdapter extends FragmentPagerAdapter { private class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) { public SectionsPagerAdapter(FragmentManager fm) {

View File

@ -1,5 +1,9 @@
package ml.docilealligator.infinityforreddit; package ml.docilealligator.infinityforreddit;
public class SwitchAccountEvent { public class SwitchAccountEvent {
public void accountSwitched(){} String excludeActivityClassName;
public SwitchAccountEvent() {}
public SwitchAccountEvent(String excludeActivityClassName) {
this.excludeActivityClassName = excludeActivityClassName;
}
} }

View File

@ -29,6 +29,7 @@ import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar; import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -80,6 +81,8 @@ public class ViewMessageActivity extends AppCompatActivity {
((Infinity) getApplication()).getAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
EventBus.getDefault().register(this);
mGlide = Glide.with(this); mGlide = Glide.with(this);
Resources resources = getResources(); Resources resources = getResources();
@ -152,7 +155,7 @@ public class ViewMessageActivity extends AppCompatActivity {
if(mNewAccountName != null) { if(mNewAccountName != null) {
if(account == null || !account.getUsername().equals(mNewAccountName)) { if(account == null || !account.getUsername().equals(mNewAccountName)) {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> { new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> {
EventBus.getDefault().post(new SwitchAccountEvent()); EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName()));
mNewAccountName = null; mNewAccountName = null;
if(newAccount == null) { if(newAccount == null) {
mNullAccessToken = true; mNullAccessToken = true;
@ -251,4 +254,17 @@ public class ViewMessageActivity extends AppCompatActivity {
outState.putString(ACCESS_TOKEN_STATE, mAccessToken); outState.putString(ACCESS_TOKEN_STATE, mAccessToken);
outState.putString(NEW_ACCOUNT_NAME_STATE, mNewAccountName); outState.putString(NEW_ACCOUNT_NAME_STATE, mNewAccountName);
} }
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
@Subscribe
public void onAccountSwitchEvent(SwitchAccountEvent event) {
if(!getClass().getName().equals(event.excludeActivityClassName)) {
finish();
}
}
} }

View File

@ -222,7 +222,7 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo
if(mNewAccountName != null) { if(mNewAccountName != null) {
if(account == null || !account.getUsername().equals(mNewAccountName)) { if(account == null || !account.getUsername().equals(mNewAccountName)) {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> { new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> {
EventBus.getDefault().post(new SwitchAccountEvent()); EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName()));
mNewAccountName = null; mNewAccountName = null;
if(newAccount == null) { if(newAccount == null) {
mNullAccessToken = true; mNullAccessToken = true;
@ -704,6 +704,13 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo
} }
} }
@Subscribe
public void onAccountSwitchEvent(SwitchAccountEvent event) {
if(!getClass().getName().equals(event.excludeActivityClassName)) {
finish();
}
}
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.view_post_detail_activity, menu); getMenuInflater().inflate(R.menu.view_post_detail_activity, menu);

View File

@ -32,6 +32,7 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -114,6 +115,8 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
((Infinity) getApplication()).getAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
EventBus.getDefault().register(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
Resources resources = getResources(); Resources resources = getResources();
@ -273,7 +276,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
if(mNewAccountName != null) { if(mNewAccountName != null) {
if(account == null || !account.getUsername().equals(mNewAccountName)) { if(account == null || !account.getUsername().equals(mNewAccountName)) {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> { new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> {
EventBus.getDefault().post(new SwitchAccountEvent()); EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName()));
mNewAccountName = null; mNewAccountName = null;
if(newAccount == null) { if(newAccount == null) {
mNullAccessToken = true; mNullAccessToken = true;
@ -483,6 +486,12 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE_KEY, mFragment); getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE_KEY, mFragment);
} }
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
private void makeSnackbar(int resId, boolean retry) { private void makeSnackbar(int resId, boolean retry) {
if(showToast) { if(showToast) {
Toast.makeText(this, resId, Toast.LENGTH_SHORT).show(); Toast.makeText(this, resId, Toast.LENGTH_SHORT).show();
@ -527,6 +536,13 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
} }
} }
@Subscribe
public void onAccountSwitchEvent(SwitchAccountEvent event) {
if(!getClass().getName().equals(event.excludeActivityClassName)) {
finish();
}
}
private static class InsertSubredditDataAsyncTask extends AsyncTask<Void, Void, Void> { private static class InsertSubredditDataAsyncTask extends AsyncTask<Void, Void, Void> {
interface InsertSubredditDataAsyncTaskListener { interface InsertSubredditDataAsyncTaskListener {

View File

@ -36,6 +36,7 @@ import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.tabs.TabLayout; import com.google.android.material.tabs.TabLayout;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -122,6 +123,8 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi
((Infinity) getApplication()).getAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
EventBus.getDefault().register(this);
username = getIntent().getStringExtra(EXTRA_USER_NAME_KEY); username = getIntent().getStringExtra(EXTRA_USER_NAME_KEY);
if (savedInstanceState == null) { if (savedInstanceState == null) {
@ -353,7 +356,7 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi
if(mNewAccountName != null) { if(mNewAccountName != null) {
if(account == null || !account.getUsername().equals(mNewAccountName)) { if(account == null || !account.getUsername().equals(mNewAccountName)) {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> { new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> {
EventBus.getDefault().post(new SwitchAccountEvent()); EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName()));
mNewAccountName = null; mNewAccountName = null;
if(newAccount == null) { if(newAccount == null) {
mNullAccessToken = true; mNullAccessToken = true;
@ -536,6 +539,12 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi
outState.putString(NEW_ACCOUNT_NAME_STATE, mNewAccountName); outState.putString(NEW_ACCOUNT_NAME_STATE, mNewAccountName);
} }
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
private void showMessage(int resId, boolean retry) { private void showMessage(int resId, boolean retry) {
if(showToast) { if(showToast) {
Toast.makeText(this, resId, Toast.LENGTH_SHORT).show(); Toast.makeText(this, resId, Toast.LENGTH_SHORT).show();
@ -554,6 +563,13 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi
sectionsPagerAdapter.changeSortType(sortType); sectionsPagerAdapter.changeSortType(sortType);
} }
@Subscribe
public void onAccountSwitchEvent(SwitchAccountEvent event) {
if(!getClass().getName().equals(event.excludeActivityClassName)) {
finish();
}
}
private static class InsertUserDataAsyncTask extends AsyncTask<Void, Void, Void> { private static class InsertUserDataAsyncTask extends AsyncTask<Void, Void, Void> {
interface InsertUserDataAsyncTaskListener { interface InsertUserDataAsyncTaskListener {