Potentially fixed a bug that notification for all the users are the same. Show a toast message after the account is switched when clicking the notificaiton for other accounts.

This commit is contained in:
Alex Ning 2019-08-20 09:53:06 +08:00
parent 01f5dd7886
commit 6e33dcad75
7 changed files with 21 additions and 7 deletions

View File

@ -15,6 +15,7 @@ import android.view.WindowManager;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.ActionBarDrawerToggle;
@ -253,6 +254,8 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
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())); EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName()));
Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show();
mNewAccountName = null; mNewAccountName = null;
if(newAccount == null) { if(newAccount == null) {
mNullAccessToken = true; mNullAccessToken = true;

View File

@ -32,7 +32,6 @@ public class PullNotificationWorker extends Worker {
static final String WORKER_TAG = "PNWT"; static final String WORKER_TAG = "PNWT";
private Context context; private Context context;
private RedditAPI api;
@Inject @Inject
@Named("oauth_without_authenticator") @Named("oauth_without_authenticator")
@ -45,7 +44,6 @@ public class PullNotificationWorker extends Worker {
super(context, workerParams); super(context, workerParams);
this.context = context; this.context = context;
((Infinity) context.getApplicationContext()).getAppComponent().inject(this); ((Infinity) context.getApplicationContext()).getAppComponent().inject(this);
api = mOauthWithoutAuthenticatorRetrofit.create(RedditAPI.class);
} }
@NonNull @NonNull
@ -185,9 +183,10 @@ public class PullNotificationWorker extends Worker {
} }
private Response<String> fetchMessages(Account account) throws IOException, JSONException { private Response<String> fetchMessages(Account account) throws IOException, JSONException {
Response<String> response = api.getMessages( Response<String> response = mOauthWithoutAuthenticatorRetrofit.create(RedditAPI.class)
RedditUtils.getOAuthHeader(account.getAccessToken()), .getMessages(RedditUtils.getOAuthHeader(account.getAccessToken()),
FetchMessages.WHERE_INBOX, null).execute(); FetchMessages.WHERE_UNREAD, null).execute();
if(response.isSuccessful()) { if(response.isSuccessful()) {
return response; return response;
} else { } else {
@ -198,7 +197,8 @@ public class PullNotificationWorker extends Worker {
params.put(RedditUtils.GRANT_TYPE_KEY, RedditUtils.GRANT_TYPE_REFRESH_TOKEN); params.put(RedditUtils.GRANT_TYPE_KEY, RedditUtils.GRANT_TYPE_REFRESH_TOKEN);
params.put(RedditUtils.REFRESH_TOKEN_KEY, refreshToken); params.put(RedditUtils.REFRESH_TOKEN_KEY, refreshToken);
Response accessTokenResponse = api.getAccessToken(RedditUtils.getHttpBasicAuthHeader(), params).execute(); Response accessTokenResponse = mOauthWithoutAuthenticatorRetrofit.create(RedditAPI.class)
.getAccessToken(RedditUtils.getHttpBasicAuthHeader(), params).execute();
if(accessTokenResponse.isSuccessful() && accessTokenResponse.body() != null) { if(accessTokenResponse.isSuccessful() && accessTokenResponse.body() != null) {
JSONObject jsonObject = new JSONObject((String) accessTokenResponse.body()); JSONObject jsonObject = new JSONObject((String) accessTokenResponse.body());
String newAccessToken = jsonObject.getString(RedditUtils.ACCESS_TOKEN_KEY); String newAccessToken = jsonObject.getString(RedditUtils.ACCESS_TOKEN_KEY);

View File

@ -13,6 +13,7 @@ import android.view.WindowManager;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
@ -156,6 +157,8 @@ public class ViewMessageActivity extends AppCompatActivity {
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())); EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName()));
Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show();
mNewAccountName = null; mNewAccountName = null;
if(newAccount == null) { if(newAccount == null) {
mNullAccessToken = true; mNullAccessToken = true;

View File

@ -223,6 +223,8 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo
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())); EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName()));
Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show();
mNewAccountName = null; mNewAccountName = null;
if(newAccount == null) { if(newAccount == null) {
mNullAccessToken = true; mNullAccessToken = true;

View File

@ -277,6 +277,8 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
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())); EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName()));
Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show();
mNewAccountName = null; mNewAccountName = null;
if(newAccount == null) { if(newAccount == null) {
mNullAccessToken = true; mNullAccessToken = true;
@ -432,7 +434,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
} }
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case android.R.id.home: case android.R.id.home:
finish(); finish();

View File

@ -357,6 +357,8 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi
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())); EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName()));
Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show();
mNewAccountName = null; mNewAccountName = null;
if(newAccount == null) { if(newAccount == null) {
mNullAccessToken = true; mNullAccessToken = true;

View File

@ -234,4 +234,6 @@
<string name="label_account">Account</string> <string name="label_account">Account</string>
<string name="label_post">Post</string> <string name="label_post">Post</string>
<string name="account_switched">Account Switched. So all other pages are gone.</string>
</resources> </resources>