diff options
author | Alex Ning <chineseperson5@gmail.com> | 2021-04-06 02:38:11 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2021-04-06 02:38:11 +0000 |
commit | e4c9f7b927476c5498950516f2c1768047d56970 (patch) | |
tree | 649270f829ecc1bd571767c4a1208267e1fd7586 /app/src/main | |
parent | dd98dd879dc5ecb3e513718c990bf9b3e4e66c71 (diff) | |
download | infinity-for-reddit-e4c9f7b927476c5498950516f2c1768047d56970.tar infinity-for-reddit-e4c9f7b927476c5498950516f2c1768047d56970.tar.gz infinity-for-reddit-e4c9f7b927476c5498950516f2c1768047d56970.tar.bz2 infinity-for-reddit-e4c9f7b927476c5498950516f2c1768047d56970.tar.lz infinity-for-reddit-e4c9f7b927476c5498950516f2c1768047d56970.tar.xz infinity-for-reddit-e4c9f7b927476c5498950516f2c1768047d56970.tar.zst infinity-for-reddit-e4c9f7b927476c5498950516f2c1768047d56970.zip |
Show inbox count in the inbox menu item in the navigation drawer.
Diffstat (limited to '')
11 files changed, 88 insertions, 22 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java index 2b2aa74d..c1162ed0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java @@ -68,7 +68,6 @@ import javax.inject.Named; import butterknife.BindView; import butterknife.ButterKnife; import ml.docilealligator.infinityforreddit.ActivityToolbarInterface; -import ml.docilealligator.infinityforreddit.FetchMyInfo; import ml.docilealligator.infinityforreddit.FetchSubscribedThing; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.MarkPostAsReadInterface; @@ -107,6 +106,8 @@ import ml.docilealligator.infinityforreddit.subreddit.SubredditData; import ml.docilealligator.infinityforreddit.subscribedsubreddit.SubscribedSubredditData; import ml.docilealligator.infinityforreddit.subscribedsubreddit.SubscribedSubredditViewModel; import ml.docilealligator.infinityforreddit.subscribeduser.SubscribedUserData; +import ml.docilealligator.infinityforreddit.user.FetchUserData; +import ml.docilealligator.infinityforreddit.user.UserData; import ml.docilealligator.infinityforreddit.utils.CustomThemeSharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.Utils; @@ -131,6 +132,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb private static final String ACCOUNT_NAME_STATE = "ANS"; private static final String MESSAGE_FULLNAME_STATE = "MFS"; private static final String NEW_ACCOUNT_NAME_STATE = "NANS"; + private static final String INBOX_COUNT_STATE = "ICS"; private static final int LOGIN_ACTIVITY_REQUEST_CODE = 0; @@ -224,6 +226,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb private boolean mShowFavoriteSubscribedSubreddits; private boolean mShowSubscribedSubreddits; private int fabOption; + private int inboxCount; @Override protected void onCreate(Bundle savedInstanceState) { @@ -314,6 +317,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE); mMessageFullname = savedInstanceState.getString(MESSAGE_FULLNAME_STATE); mNewAccountName = savedInstanceState.getString(NEW_ACCOUNT_NAME_STATE); + inboxCount = savedInstanceState.getInt(INBOX_COUNT_STATE); initializeNotificationAndBindView(true); } else { mMessageFullname = getIntent().getStringExtra(EXTRA_MESSSAGE_FULLNAME); @@ -741,8 +745,6 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb } else if (stringId == R.string.multi_reddit) { intent = new Intent(MainActivity.this, SubscribedThingListingActivity.class); intent.putExtra(SubscribedThingListingActivity.EXTRA_SHOW_MULTIREDDITS, true); - } else if (stringId == R.string.inbox) { - intent = new Intent(MainActivity.this, InboxActivity.class); } else if (stringId == R.string.upvoted) { intent = new Intent(MainActivity.this, AccountPostsActivity.class); intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_UPVOTED); @@ -823,6 +825,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb }); } }); + adapter.setInboxCount(inboxCount); navDrawerRecyclerView.setLayoutManager(new LinearLayoutManager(this)); navDrawerRecyclerView.setAdapter(adapter); @@ -974,7 +977,24 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb private void loadUserData() { if (!mFetchUserInfoSuccess) { - FetchMyInfo.fetchAccountInfo(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken, + FetchUserData.fetchUserData(mRedditDataRoomDatabase, mOauthRetrofit, mAccessToken, + mAccountName, new FetchUserData.FetchUserDataListener() { + @Override + public void onFetchUserDataSuccess(UserData userData, int inboxCount) { + MainActivity.this.inboxCount = inboxCount; + mAccountName = userData.getName(); + mFetchUserInfoSuccess = true; + if (adapter != null) { + adapter.setInboxCount(inboxCount); + } + } + + @Override + public void onFetchUserDataFailed() { + mFetchUserInfoSuccess = false; + } + }); + /*FetchMyInfo.fetchAccountInfo(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken, new FetchMyInfo.FetchMyInfoListener() { @Override public void onFetchMyInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma) { @@ -986,7 +1006,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb public void onFetchMyInfoFailed(boolean parseFailed) { mFetchUserInfoSuccess = false; } - }); + });*/ } } @@ -1111,6 +1131,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb outState.putString(ACCOUNT_NAME_STATE, mAccountName); outState.putString(MESSAGE_FULLNAME_STATE, mMessageFullname); outState.putString(NEW_ACCOUNT_NAME_STATE, mNewAccountName); + outState.putInt(INBOX_COUNT_STATE, inboxCount); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java index c2227455..eb79ce17 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java @@ -840,7 +840,7 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele if (!mFetchUserInfoSuccess) { FetchUserData.fetchUserData(mRetrofit, username, new FetchUserData.FetchUserDataListener() { @Override - public void onFetchUserDataSuccess(UserData userData) { + public void onFetchUserDataSuccess(UserData userData, int inboxCount) { new ViewUserDetailActivity.InsertUserDataAsyncTask(mRedditDataRoomDatabase.userDao(), userData, () -> mFetchUserInfoSuccess = true).execute(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/NavigationDrawerRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/NavigationDrawerRecyclerViewAdapter.java index 2b53c4ca..f7046a7c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/NavigationDrawerRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/NavigationDrawerRecyclerViewAdapter.java @@ -1,5 +1,6 @@ package ml.docilealligator.infinityforreddit.adapters; +import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; import android.content.res.Resources; @@ -31,6 +32,7 @@ import butterknife.ButterKnife; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.account.Account; +import ml.docilealligator.infinityforreddit.activities.InboxActivity; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.subscribedsubreddit.SubscribedSubredditData; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; @@ -66,6 +68,7 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re private String profileImageUrl; private String bannerImageUrl; private int karma; + private int inboxCount; private boolean isNSFWEnabled; private boolean requireAuthToAccountSection; private boolean showAvatarOnTheRightInTheNavigationDrawer; @@ -420,8 +423,17 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re drawableId = R.drawable.ic_multi_reddit_24dp; break; case 5: - stringId = R.string.inbox; - drawableId = R.drawable.ic_inbox_24dp; + setOnClickListener = false; + if (inboxCount > 0) { + ((MenuItemViewHolder) holder).menuTextView.setText(appCompatActivity.getString(R.string.inbox_with_count, inboxCount)); + } else { + ((MenuItemViewHolder) holder).menuTextView.setText(R.string.inbox); + } + ((MenuItemViewHolder) holder).imageView.setImageDrawable(ContextCompat.getDrawable(appCompatActivity, R.drawable.ic_inbox_24dp)); + ((MenuItemViewHolder) holder).itemView.setOnClickListener(view -> { + Intent intent = new Intent(appCompatActivity, InboxActivity.class); + appCompatActivity.startActivity(intent); + }); break; case 7: stringId = R.string.upvoted; @@ -759,6 +771,11 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re this.showAvatarOnTheRightInTheNavigationDrawer = showAvatarOnTheRightInTheNavigationDrawer; } + public void setInboxCount(int inboxCount) { + this.inboxCount = inboxCount; + notifyDataSetChanged(); + } + class NavHeaderViewHolder extends RecyclerView.ViewHolder { @BindView(R.id.name_text_view_nav_header_main) TextView accountNameTextView; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java index 67da922d..91191e90 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java @@ -86,6 +86,9 @@ public interface RedditAPI { @GET("user/{username}/about.json?raw_json=1") Call<String> getUserData(@Path("username") String username); + @GET("user/{username}/about.json?raw_json=1") + Call<String> getUserDataOauth(@HeaderMap Map<String, String> headers, @Path("username") String username); + @GET("user/{username}/comments.json?raw_json=1") Call<String> getUserComments(@Path("username") String username, @Query("after") String after, @Query("sort") String sortType); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/LoadUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/LoadUserData.java index c6ef0ffc..2406f8f7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/LoadUserData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/LoadUserData.java @@ -22,7 +22,7 @@ public class LoadUserData { } else { handler.post(() -> FetchUserData.fetchUserData(retrofit, userName, new FetchUserData.FetchUserDataListener() { @Override - public void onFetchUserDataSuccess(UserData userData) { + public void onFetchUserDataSuccess(UserData userData, int inboxCount) { InsertUserData.insertUserData(executor, handler, redditDataRoomDatabase, userData, () -> loadUserDataAsyncTaskListener.loadUserDataSuccess(userData.getIconUrl())); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java index ced8a633..96aafb97 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java @@ -59,7 +59,7 @@ public class ParsePost { int score = data.getInt(JSONUtils.SCORE_KEY); int voteType; int nComments = data.getInt(JSONUtils.NUM_COMMENTS_KEY); - int upvoteRatio = (int) (data.getDouble(JSONUtils.UPVOTE_RATIO) * 100); + int upvoteRatio = (int) (data.getDouble(JSONUtils.UPVOTE_RATIO_KEY) * 100); boolean hidden = data.getBoolean(JSONUtils.HIDDEN_KEY); boolean spoiler = data.getBoolean(JSONUtils.SPOILER_KEY); boolean nsfw = data.getBoolean(JSONUtils.NSFW_KEY); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/user/FetchUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/user/FetchUserData.java index 60b20f55..3c5280a7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/user/FetchUserData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/user/FetchUserData.java @@ -4,24 +4,36 @@ import androidx.annotation.NonNull; import java.util.ArrayList; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.apis.RedditAPI; +import ml.docilealligator.infinityforreddit.utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Retrofit; public class FetchUserData { public static void fetchUserData(Retrofit retrofit, String userName, FetchUserDataListener fetchUserDataListener) { + fetchUserData(null, retrofit, null, userName, fetchUserDataListener); + } + + public static void fetchUserData(RedditDataRoomDatabase redditDataRoomDatabase, Retrofit retrofit, + String accessToken, String userName, FetchUserDataListener fetchUserDataListener) { RedditAPI api = retrofit.create(RedditAPI.class); - Call<String> userInfo = api.getUserData(userName); + Call<String> userInfo; + if (redditDataRoomDatabase == null) { + userInfo = api.getUserData(userName); + } else { + userInfo = api.getUserDataOauth(APIUtils.getOAuthHeader(accessToken), userName); + } userInfo.enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { - ParseUserData.parseUserData(response.body(), new ParseUserData.ParseUserDataListener() { + ParseUserData.parseUserData(redditDataRoomDatabase, response.body(), new ParseUserData.ParseUserDataListener() { @Override - public void onParseUserDataSuccess(UserData userData) { - fetchUserDataListener.onFetchUserDataSuccess(userData); + public void onParseUserDataSuccess(UserData userData, int inboxCount) { + fetchUserDataListener.onFetchUserDataSuccess(userData, inboxCount); } @Override @@ -74,7 +86,7 @@ public class FetchUserData { } public interface FetchUserDataListener { - void onFetchUserDataSuccess(UserData userData); + void onFetchUserDataSuccess(UserData userData, int inboxCount); void onFetchUserDataFailed(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/user/ParseUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/user/ParseUserData.java index 5416e056..608f78a4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/user/ParseUserData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/user/ParseUserData.java @@ -8,11 +8,13 @@ import org.json.JSONObject; import java.util.ArrayList; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.utils.JSONUtils; public class ParseUserData { - static void parseUserData(String response, ParseUserDataListener parseUserDataListener) { - new ParseUserDataAsyncTask(response, parseUserDataListener).execute(); + static void parseUserData(RedditDataRoomDatabase redditDataRoomDatabase, String response, + ParseUserDataListener parseUserDataListener) { + new ParseUserDataAsyncTask(redditDataRoomDatabase, response, parseUserDataListener).execute(); } static void parseUserListingData(String response, ParseUserListingDataListener parseUserListingDataListener) { @@ -56,7 +58,7 @@ public class ParseUserData { } interface ParseUserDataListener { - void onParseUserDataSuccess(UserData userData); + void onParseUserDataSuccess(UserData userData, int inboxCount); void onParseUserDataFailed(); } @@ -68,13 +70,16 @@ public class ParseUserData { } private static class ParseUserDataAsyncTask extends AsyncTask<Void, Void, Void> { + private RedditDataRoomDatabase redditDataRoomDatabase; private JSONObject jsonResponse; private ParseUserDataListener parseUserDataListener; private boolean parseFailed = false; private UserData userData; + private int inboxCount = -1; - ParseUserDataAsyncTask(String response, ParseUserDataListener parseUserDataListener) { + ParseUserDataAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, String response, ParseUserDataListener parseUserDataListener) { + this.redditDataRoomDatabase = redditDataRoomDatabase; this.parseUserDataListener = parseUserDataListener; try { jsonResponse = new JSONObject(response); @@ -89,6 +94,12 @@ public class ParseUserData { if (!parseFailed) { try { userData = parseUserDataBase(jsonResponse, true); + if (redditDataRoomDatabase != null) { + redditDataRoomDatabase.accountDao().updateAccountInfo(userData.getName(), userData.getIconUrl(), userData.getBanner(), userData.getTotalKarma()); + } + if (jsonResponse.getJSONObject(JSONUtils.DATA_KEY).has(JSONUtils.INBOX_COUNT_KEY)) { + inboxCount = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getInt(JSONUtils.INBOX_COUNT_KEY); + } } catch (JSONException e) { parseFailed = true; e.printStackTrace(); @@ -100,7 +111,7 @@ public class ParseUserData { @Override protected void onPostExecute(Void aVoid) { if (!parseFailed) { - parseUserDataListener.onParseUserDataSuccess(userData); + parseUserDataListener.onParseUserDataSuccess(userData, inboxCount); } else { parseUserDataListener.onParseUserDataFailed(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserFollowing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserFollowing.java index 6edbcf6d..0dd5157a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserFollowing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserFollowing.java @@ -50,7 +50,7 @@ public class UserFollowing { if (action.equals("sub")) { FetchUserData.fetchUserData(retrofit, username, new FetchUserData.FetchUserDataListener() { @Override - public void onFetchUserDataSuccess(UserData userData) { + public void onFetchUserDataSuccess(UserData userData, int inboxCount) { new UpdateSubscriptionAsyncTask(subscribedUserDao, userData, accountName, true).execute(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/JSONUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/JSONUtils.java index 5add8830..48bf1e4c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/JSONUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/JSONUtils.java @@ -132,5 +132,6 @@ public class JSONUtils { public static final String CONTENT_URLS_KEY = "content_urls";; public static final String WEBM_KEY = "webm"; public static final String WEBM_URL_KEY = "webmUrl"; - public static final String UPVOTE_RATIO = "upvote_ratio"; + public static final String UPVOTE_RATIO_KEY = "upvote_ratio"; + public static final String INBOX_COUNT_KEY = "inbox_count"; } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3fe9b283..54c20710 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -123,6 +123,7 @@ <string name="subscriptions">Subscriptions</string> <string name="multi_reddit">Multireddit</string> <string name="inbox">Inbox</string> + <string name="inbox_with_count">Inbox (%1$d)</string> <string name="upvoted">Upvoted</string> <string name="downvoted">Downvoted</string> <string name="hidden">Hidden</string> |