From 61cc6089faea9d66ee8e8149f0d10c8a17b1676a Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 19 Feb 2020 13:53:59 +0800 Subject: Implemented ItemClickListener for the nav drawer. --- .../infinityforreddit/Activity/MainActivity.java | 74 +++++++++++++++++++++- .../NavigationDrawerRecyclerViewAdapter.java | 24 +++++-- 2 files changed, 93 insertions(+), 5 deletions(-) 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 4480124a..eba098b1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java @@ -22,6 +22,7 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBarDrawerToggle; +import androidx.appcompat.app.AppCompatDelegate; import androidx.appcompat.widget.Toolbar; import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.core.view.GravityCompat; @@ -93,6 +94,9 @@ import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUse import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import retrofit2.Retrofit; +import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO; +import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES; + public class MainActivity extends BaseActivity implements SortTypeSelectionCallback, PostTypeBottomSheetFragment.PostTypeSelectionCallback, PostLayoutBottomSheetFragment.PostLayoutSelectionCallback { @@ -497,7 +501,75 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb fab.setVisibility(View.VISIBLE); } - adapter = new NavigationDrawerRecyclerViewAdapter(this, mAccountName, mProfileImageUrl, mBannerImageUrl, mKarma); + adapter = new NavigationDrawerRecyclerViewAdapter(this, mAccountName, mProfileImageUrl, + mBannerImageUrl, mKarma, new NavigationDrawerRecyclerViewAdapter.ItemClickListener() { + @Override + public void onMenuClick(int stringId) { + Intent intent = null; + switch (stringId) { + case R.string.profile: + intent = new Intent(MainActivity.this, ViewUserDetailActivity.class); + intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, mAccountName); + break; + case R.string.subscriptions: + intent = new Intent(MainActivity.this, SubscribedThingListingActivity.class); + break; + case R.string.multi_reddit: + intent = new Intent(MainActivity.this, MultiRedditListingActivity.class); + break; + case R.string.inbox: + intent = new Intent(MainActivity.this, ViewMessageActivity.class); + break; + case R.string.upvoted: + intent = new Intent(MainActivity.this, AccountPostsActivity.class); + intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_UPVOTED); + break; + case R.string.downvoted: + intent = new Intent(MainActivity.this, AccountPostsActivity.class); + intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_DOWNVOTED); + break; + case R.string.hidden: + intent = new Intent(MainActivity.this, AccountPostsActivity.class); + intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_HIDDEN); + break; + case R.string.saved: + intent = new Intent(MainActivity.this, AccountSavedThingActivity.class); + break; + case R.string.gilded: + intent = new Intent(MainActivity.this, AccountPostsActivity.class); + intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_GILDED); + break; + case R.string.light_theme: + mSharedPreferences.edit().putString(SharedPreferencesUtils.THEME_KEY, "1").apply(); + AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_YES); + if(mSharedPreferences.getBoolean(SharedPreferencesUtils.AMOLED_DARK_KEY, false)) { + getTheme().applyStyle(R.style.Theme_Default_AmoledDark, true); + } else { + 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); + + } + if (intent != null) { + startActivity(intent); + } + drawer.closeDrawers(); + } + + @Override + public void onSubscribedSubredditClick(String subredditName) { + Intent intent = new Intent(MainActivity.this, ViewSubredditDetailActivity.class); + intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, subredditName); + startActivity(intent); + } + }); navDrawerRecyclerView.setLayoutManager(new LinearLayoutManager(this)); navDrawerRecyclerView.setAdapter(adapter); 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 63069e9e..2d6cd5bd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/NavigationDrawerRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/NavigationDrawerRecyclerViewAdapter.java @@ -28,6 +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); + } + private static final int VIEW_TYPE_NAV_HEADER = 0; private static final int VIEW_TYPE_MENU_GROUP_TITLE = 1; private static final int VIEW_TYPE_MENU_ITEM = 2; @@ -42,12 +47,14 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter subscribedSubreddits; + private ArrayList accounts; public NavigationDrawerRecyclerViewAdapter(Context context, String accountName, String userIconUrl, - String userBannerUrl, int karma) { + String userBannerUrl, int karma, ItemClickListener itemClickListener) { this.context = context; resources = context.getResources(); glide = Glide.with(context); @@ -56,6 +63,7 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter itemClickListener.onMenuClick(finalStringId)); } else if (holder instanceof SubscribedThingViewHolder) { SubscribedSubredditData subreddit = subscribedSubreddits.get(position - CURRENT_MENU_ITEMS); - ((SubscribedThingViewHolder) holder).subredditNameTextView.setText(subreddit.getName()); + String subredditName = subreddit.getName(); String iconUrl = subreddit.getIconUrl(); + ((SubscribedThingViewHolder) holder).subredditNameTextView.setText(subredditName); if (iconUrl != null && !iconUrl.equals("")) { glide.load(iconUrl) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) @@ -224,6 +235,10 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter { + itemClickListener.onSubscribedSubredditClick(subredditName); + }); } } @@ -242,7 +257,8 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter accounts) { - + this.accounts = (ArrayList) accounts; + notifyDataSetChanged(); } class NavHeaderViewHolder extends RecyclerView.ViewHolder { @@ -257,7 +273,7 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter