From 9f8713979411a27caee062b77f893617029b3d67 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 19 Feb 2020 16:27:05 +0800 Subject: The new nav bar is now fully functional. Fix wrong icon color in dark theme. --- .../infinityforreddit/Activity/MainActivity.java | 40 +++- .../Adapter/AccountRecyclerViewAdapter.java | 8 +- .../NavigationDrawerRecyclerViewAdapter.java | 260 +++++++++++++++------ .../SubscribedSubredditsListingFragment.java | 9 +- .../main/res/drawable-night/ic_anonymous_24dp.xml | 9 + .../main/res/drawable-night/ic_log_out_24dp.xml | 9 + .../ic_outline_add_circle_outline_24dp.xml | 9 + app/src/main/res/drawable/ic_anonymous_24dp.xml | 9 + app/src/main/res/drawable/ic_log_out_24dp.xml | 9 + .../ic_outline_add_circle_outline_24dp.xml | 9 + .../ic_outline_add_circle_outline_24px.xml | 9 - .../main/res/drawable/ic_outline_block_24px.xml | 9 - .../main/res/drawable/ic_outline_public_24px.xml | 9 - app/src/main/res/layout/item_account.xml | 26 --- .../main/res/layout/item_nav_drawer_account.xml | 26 +++ 15 files changed, 316 insertions(+), 134 deletions(-) create mode 100644 app/src/main/res/drawable-night/ic_anonymous_24dp.xml create mode 100644 app/src/main/res/drawable-night/ic_log_out_24dp.xml create mode 100644 app/src/main/res/drawable-night/ic_outline_add_circle_outline_24dp.xml create mode 100644 app/src/main/res/drawable/ic_anonymous_24dp.xml create mode 100644 app/src/main/res/drawable/ic_log_out_24dp.xml create mode 100644 app/src/main/res/drawable/ic_outline_add_circle_outline_24dp.xml delete mode 100644 app/src/main/res/drawable/ic_outline_add_circle_outline_24px.xml delete mode 100644 app/src/main/res/drawable/ic_outline_block_24px.xml delete mode 100644 app/src/main/res/drawable/ic_outline_public_24px.xml delete mode 100644 app/src/main/res/layout/item_account.xml create mode 100644 app/src/main/res/layout/item_nav_drawer_account.xml (limited to 'app/src') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java index eba098b1..4148993f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java @@ -66,6 +66,7 @@ import ml.docilealligator.infinityforreddit.AppBarStateChangeListener; import ml.docilealligator.infinityforreddit.AsyncTask.GetCurrentAccountAsyncTask; import ml.docilealligator.infinityforreddit.AsyncTask.InsertSubscribedThingsAsyncTask; import ml.docilealligator.infinityforreddit.AsyncTask.SwitchAccountAsyncTask; +import ml.docilealligator.infinityforreddit.AsyncTask.SwitchToAnonymousAccountAsyncTask; import ml.docilealligator.infinityforreddit.Event.ChangeConfirmToExitEvent; import ml.docilealligator.infinityforreddit.Event.ChangeLockBottomAppBarEvent; import ml.docilealligator.infinityforreddit.Event.ChangeNSFWEvent; @@ -540,6 +541,11 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_GILDED); break; case R.string.light_theme: + mSharedPreferences.edit().putString(SharedPreferencesUtils.THEME_KEY, "0").apply(); + AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_NO); + getTheme().applyStyle(R.style.Theme_Default, true); + break; + case R.string.dark_theme: mSharedPreferences.edit().putString(SharedPreferencesUtils.THEME_KEY, "1").apply(); AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_YES); if(mSharedPreferences.getBoolean(SharedPreferencesUtils.AMOLED_DARK_KEY, false)) { @@ -548,13 +554,28 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb getTheme().applyStyle(R.style.Theme_Default_NormalDark, true); } break; - case R.string.dark_theme: - mSharedPreferences.edit().putString(SharedPreferencesUtils.THEME_KEY, "0").apply(); - AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_NO); - getTheme().applyStyle(R.style.Theme_Default, true); - break; case R.string.settings: intent = new Intent(MainActivity.this, SettingsActivity.class); + break; + case R.string.add_account: + Intent addAccountIntent = new Intent(MainActivity.this, LoginActivity.class); + startActivityForResult(addAccountIntent, LOGIN_ACTIVITY_REQUEST_CODE); + break; + case R.string.anonymous_account: + new SwitchToAnonymousAccountAsyncTask(mRedditDataRoomDatabase, false, + () -> { + Intent anonymousIntent = new Intent(MainActivity.this, MainActivity.class); + startActivity(anonymousIntent); + finish(); + }).execute(); + break; + case R.string.log_out: + new SwitchToAnonymousAccountAsyncTask(mRedditDataRoomDatabase, true, + () -> { + Intent logOutIntent = new Intent(MainActivity.this, MainActivity.class); + startActivity(logOutIntent); + finish(); + }).execute(); } if (intent != null) { @@ -569,6 +590,15 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, subredditName); startActivity(intent); } + + @Override + public void onAccountClick(String accountName) { + new SwitchAccountAsyncTask(mRedditDataRoomDatabase, accountName, newAccount -> { + Intent intent = new Intent(MainActivity.this, MainActivity.class); + startActivity(intent); + finish(); + }).execute(); + } }); navDrawerRecyclerView.setLayoutManager(new LinearLayoutManager(this)); navDrawerRecyclerView.setAdapter(adapter); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/AccountRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/AccountRecyclerViewAdapter.java index 3900bb0e..2476a1b6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/AccountRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/AccountRecyclerViewAdapter.java @@ -39,7 +39,7 @@ public class AccountRecyclerViewAdapter extends RecyclerView.Adapter mItemSelectedListener.addAccountSelected()); } else if (position == mAccounts.size() + 1) { holder.profileImageGifImageView.setColorFilter(ContextCompat.getColor(mContext, R.color.primaryTextColor), android.graphics.PorterDuff.Mode.SRC_IN); - mGlide.load(R.drawable.ic_outline_public_24px) + mGlide.load(R.drawable.ic_anonymous_24dp) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(128, 0))) .into(holder.profileImageGifImageView); holder.usernameTextView.setText(R.string.anonymous_account); holder.itemView.setOnClickListener(view -> mItemSelectedListener.anonymousSelected()); } else { holder.profileImageGifImageView.setColorFilter(ContextCompat.getColor(mContext, R.color.primaryTextColor), android.graphics.PorterDuff.Mode.SRC_IN); - mGlide.load(R.drawable.ic_outline_block_24px) + mGlide.load(R.drawable.ic_log_out_24dp) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(128, 0))) .into(holder.profileImageGifImageView); holder.usernameTextView.setText(R.string.log_out); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/NavigationDrawerRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/NavigationDrawerRecyclerViewAdapter.java index 2d6cd5bd..d852efbf 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/NavigationDrawerRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/NavigationDrawerRecyclerViewAdapter.java @@ -28,9 +28,11 @@ import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.Subscrib import pl.droidsonroids.gif.GifImageView; public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter { + public interface ItemClickListener { void onMenuClick(int stringId); void onSubscribedSubredditClick(String subredditName); + void onAccountClick(String accountName); } private static final int VIEW_TYPE_NAV_HEADER = 0; @@ -38,6 +40,7 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter= 15) { - return VIEW_TYPE_SUBSCRIBED_SUBREDDIT; - } else if (position == 0) { - return VIEW_TYPE_NAV_HEADER; - } else if (position == 1 || position == 6) { - return VIEW_TYPE_MENU_GROUP_TITLE; - } else if (position == 12) { - return VIEW_TYPE_DIVIDER; + if (isInMainPage) { + if (isLoggedIn) { + if (position >= 15) { + return VIEW_TYPE_SUBSCRIBED_SUBREDDIT; + } else if (position == 0) { + return VIEW_TYPE_NAV_HEADER; + } else if (position == 1 || position == 6) { + return VIEW_TYPE_MENU_GROUP_TITLE; + } else if (position == 12) { + return VIEW_TYPE_DIVIDER; + } else { + return VIEW_TYPE_MENU_ITEM; + } } else { - return VIEW_TYPE_MENU_ITEM; + if (position == 0) { + return VIEW_TYPE_NAV_HEADER; + } else { + return VIEW_TYPE_MENU_ITEM; + } } } else { if (position == 0) { return VIEW_TYPE_NAV_HEADER; + } + if (accounts != null) { + if (position > accounts.size()) { + return VIEW_TYPE_MENU_ITEM; + } else { + return VIEW_TYPE_ACCOUNT; + } } else { return VIEW_TYPE_MENU_ITEM; } @@ -105,6 +123,9 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter { if (isInMainPage) { - isInMainPage = false; ((NavHeaderViewHolder) holder).dropIconImageView.setImageDrawable(resources.getDrawable(R.drawable.ic_baseline_arrow_drop_up_24px)); + notifyItemRangeRemoved(1, getItemCount() - 1); + if (accounts != null) { + notifyItemRangeInserted(1, accounts.size() + 3); + } else { + if (isLoggedIn) { + notifyItemRangeInserted(1, 3); + } else { + notifyItemInserted(1); + } + } + isInMainPage = false; } else { - isInMainPage = true; ((NavHeaderViewHolder) holder).dropIconImageView.setImageDrawable(resources.getDrawable(R.drawable.ic_baseline_arrow_drop_down_24px)); + notifyItemRangeRemoved(1, getItemCount() - 1); + if (isLoggedIn) { + if (subscribedSubreddits != null) { + notifyItemRangeInserted(1, subscribedSubreddits.size() + CURRENT_MENU_ITEMS - 1); + } else { + notifyItemRangeInserted(1, CURRENT_MENU_ITEMS - 1); + } + } else { + notifyItemRangeInserted(1, 2); + } + isInMainPage = true; } }); } else if (holder instanceof MenuGroupTitleViewHolder) { @@ -162,63 +206,100 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter itemClickListener.onMenuClick(finalStringId)); + if (stringId != 0) { + ((MenuItemViewHolder) holder).menuTextView.setText(stringId); + ((MenuItemViewHolder) holder).menuTextView.setCompoundDrawablesWithIntrinsicBounds( + drawableId, 0, 0, 0); + int finalStringId = stringId; + ((MenuItemViewHolder) holder).itemView.setOnClickListener(view -> itemClickListener.onMenuClick(finalStringId)); + } } else if (holder instanceof SubscribedThingViewHolder) { SubscribedSubredditData subreddit = subscribedSubreddits.get(position - CURRENT_MENU_ITEMS); String subredditName = subreddit.getName(); @@ -239,16 +320,43 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter { itemClickListener.onSubscribedSubredditClick(subredditName); }); + } else if (holder instanceof AccountViewHolder) { + glide.load(accounts.get(position - 1).getProfileImageUrl()) + .error(glide.load(R.drawable.subreddit_default_icon)) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(128, 0))) + .into(((AccountViewHolder) holder).profileImageGifImageView); + ((AccountViewHolder) holder).usernameTextView.setText(accounts.get(position - 1).getUsername()); + ((AccountViewHolder) holder).itemView.setOnClickListener(view -> + itemClickListener.onAccountClick(accounts.get(position - 1).getUsername())); } } @Override public int getItemCount() { - if (subscribedSubreddits != null) { - return CURRENT_MENU_ITEMS + subscribedSubreddits.size(); + if (isInMainPage) { + if (isLoggedIn) { + if (subscribedSubreddits != null) { + return CURRENT_MENU_ITEMS + subscribedSubreddits.size(); + } + return CURRENT_MENU_ITEMS; + } else { + return 3; + } + } else { + if (isLoggedIn) { + if (accounts != null && !accounts.isEmpty()) { + return 4 + accounts.size(); + } else { + return 4; + } + } else { + if (accounts != null && !accounts.isEmpty()) { + return 2 + accounts.size(); + } else { + return 2; + } + } } - - return CURRENT_MENU_ITEMS; } public void setSubscribedSubreddits(List subscribedSubreddits) { @@ -315,4 +423,16 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter= Build.VERSION_CODES.O_MR1) { @@ -153,6 +152,12 @@ public class SubscribedSubredditsListingFragment extends Fragment implements Fra return rootView; } + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + mActivity = (Activity) context; + } + @Override public void stopRefreshProgressbar() { mSwipeRefreshLayout.setRefreshing(false); diff --git a/app/src/main/res/drawable-night/ic_anonymous_24dp.xml b/app/src/main/res/drawable-night/ic_anonymous_24dp.xml new file mode 100644 index 00000000..7a00ccf5 --- /dev/null +++ b/app/src/main/res/drawable-night/ic_anonymous_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable-night/ic_log_out_24dp.xml b/app/src/main/res/drawable-night/ic_log_out_24dp.xml new file mode 100644 index 00000000..4885d221 --- /dev/null +++ b/app/src/main/res/drawable-night/ic_log_out_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable-night/ic_outline_add_circle_outline_24dp.xml b/app/src/main/res/drawable-night/ic_outline_add_circle_outline_24dp.xml new file mode 100644 index 00000000..8c721cf1 --- /dev/null +++ b/app/src/main/res/drawable-night/ic_outline_add_circle_outline_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_anonymous_24dp.xml b/app/src/main/res/drawable/ic_anonymous_24dp.xml new file mode 100644 index 00000000..aafeefba --- /dev/null +++ b/app/src/main/res/drawable/ic_anonymous_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_log_out_24dp.xml b/app/src/main/res/drawable/ic_log_out_24dp.xml new file mode 100644 index 00000000..0c5dbe45 --- /dev/null +++ b/app/src/main/res/drawable/ic_log_out_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_outline_add_circle_outline_24dp.xml b/app/src/main/res/drawable/ic_outline_add_circle_outline_24dp.xml new file mode 100644 index 00000000..8611136a --- /dev/null +++ b/app/src/main/res/drawable/ic_outline_add_circle_outline_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_outline_add_circle_outline_24px.xml b/app/src/main/res/drawable/ic_outline_add_circle_outline_24px.xml deleted file mode 100644 index 8611136a..00000000 --- a/app/src/main/res/drawable/ic_outline_add_circle_outline_24px.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - diff --git a/app/src/main/res/drawable/ic_outline_block_24px.xml b/app/src/main/res/drawable/ic_outline_block_24px.xml deleted file mode 100644 index 0c5dbe45..00000000 --- a/app/src/main/res/drawable/ic_outline_block_24px.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - diff --git a/app/src/main/res/drawable/ic_outline_public_24px.xml b/app/src/main/res/drawable/ic_outline_public_24px.xml deleted file mode 100644 index aafeefba..00000000 --- a/app/src/main/res/drawable/ic_outline_public_24px.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - diff --git a/app/src/main/res/layout/item_account.xml b/app/src/main/res/layout/item_account.xml deleted file mode 100644 index 35405b82..00000000 --- a/app/src/main/res/layout/item_account.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/item_nav_drawer_account.xml b/app/src/main/res/layout/item_nav_drawer_account.xml new file mode 100644 index 00000000..35405b82 --- /dev/null +++ b/app/src/main/res/layout/item_nav_drawer_account.xml @@ -0,0 +1,26 @@ + + + + + + + + \ No newline at end of file -- cgit v1.2.3