From 6e33dcad75e2619b33f241806ef6d86bbc8e3db0 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 20 Aug 2019 09:53:06 +0800 Subject: 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. --- .../ml/docilealligator/infinityforreddit/MainActivity.java | 3 +++ .../infinityforreddit/PullNotificationWorker.java | 12 ++++++------ .../infinityforreddit/ViewMessageActivity.java | 3 +++ .../infinityforreddit/ViewPostDetailActivity.java | 2 ++ .../infinityforreddit/ViewSubredditDetailActivity.java | 4 +++- .../infinityforreddit/ViewUserDetailActivity.java | 2 ++ app/src/main/res/values/strings.xml | 2 ++ 7 files changed, 21 insertions(+), 7 deletions(-) (limited to 'app') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index 65530111..b398b201 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -15,6 +15,7 @@ import android.view.WindowManager; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBarDrawerToggle; @@ -253,6 +254,8 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe if(account == null || !account.getUsername().equals(mNewAccountName)) { new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> { EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName())); + Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show(); + mNewAccountName = null; if(newAccount == null) { mNullAccessToken = true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java index 66d1b3f2..0a90e9a4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java @@ -32,7 +32,6 @@ public class PullNotificationWorker extends Worker { static final String WORKER_TAG = "PNWT"; private Context context; - private RedditAPI api; @Inject @Named("oauth_without_authenticator") @@ -45,7 +44,6 @@ public class PullNotificationWorker extends Worker { super(context, workerParams); this.context = context; ((Infinity) context.getApplicationContext()).getAppComponent().inject(this); - api = mOauthWithoutAuthenticatorRetrofit.create(RedditAPI.class); } @NonNull @@ -185,9 +183,10 @@ public class PullNotificationWorker extends Worker { } private Response fetchMessages(Account account) throws IOException, JSONException { - Response response = api.getMessages( - RedditUtils.getOAuthHeader(account.getAccessToken()), - FetchMessages.WHERE_INBOX, null).execute(); + Response response = mOauthWithoutAuthenticatorRetrofit.create(RedditAPI.class) + .getMessages(RedditUtils.getOAuthHeader(account.getAccessToken()), + FetchMessages.WHERE_UNREAD, null).execute(); + if(response.isSuccessful()) { return response; } else { @@ -198,7 +197,8 @@ public class PullNotificationWorker extends Worker { params.put(RedditUtils.GRANT_TYPE_KEY, RedditUtils.GRANT_TYPE_REFRESH_TOKEN); 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) { JSONObject jsonObject = new JSONObject((String) accessTokenResponse.body()); String newAccessToken = jsonObject.getString(RedditUtils.ACCESS_TOKEN_KEY); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewMessageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewMessageActivity.java index a35a9c66..f2c9d277 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewMessageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewMessageActivity.java @@ -13,6 +13,7 @@ import android.view.WindowManager; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; @@ -156,6 +157,8 @@ public class ViewMessageActivity extends AppCompatActivity { if(account == null || !account.getUsername().equals(mNewAccountName)) { new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> { EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName())); + Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show(); + mNewAccountName = null; if(newAccount == null) { mNullAccessToken = true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index f8d2a41b..e7d14b6f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -223,6 +223,8 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo if(account == null || !account.getUsername().equals(mNewAccountName)) { new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> { EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName())); + Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show(); + mNewAccountName = null; if(newAccount == null) { mNullAccessToken = true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java index f981c9c1..2bac9cae 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java @@ -277,6 +277,8 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So if(account == null || !account.getUsername().equals(mNewAccountName)) { new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> { EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName())); + Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show(); + mNewAccountName = null; if(newAccount == null) { mNullAccessToken = true; @@ -432,7 +434,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So } @Override - public boolean onOptionsItemSelected(MenuItem item) { + public boolean onOptionsItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java index f3b1d1ba..98b653a8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java @@ -357,6 +357,8 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi if(account == null || !account.getUsername().equals(mNewAccountName)) { new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> { EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName())); + Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show(); + mNewAccountName = null; if(newAccount == null) { mNullAccessToken = true; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2d02c866..77b3dcd2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -234,4 +234,6 @@ Account Post + + Account Switched. So all other pages are gone. -- cgit v1.2.3