Implemented ItemClickListener for the nav drawer.

This commit is contained in:
Alex Ning 2020-02-19 13:53:59 +08:00
parent 248aca0fb9
commit 61cc6089fa
2 changed files with 93 additions and 5 deletions

View File

@ -22,6 +22,7 @@ import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.GravityCompat; import androidx.core.view.GravityCompat;
@ -93,6 +94,9 @@ import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUse
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
import retrofit2.Retrofit; 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, public class MainActivity extends BaseActivity implements SortTypeSelectionCallback,
PostTypeBottomSheetFragment.PostTypeSelectionCallback, PostLayoutBottomSheetFragment.PostLayoutSelectionCallback { PostTypeBottomSheetFragment.PostTypeSelectionCallback, PostLayoutBottomSheetFragment.PostLayoutSelectionCallback {
@ -497,7 +501,75 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
fab.setVisibility(View.VISIBLE); 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.setLayoutManager(new LinearLayoutManager(this));
navDrawerRecyclerView.setAdapter(adapter); navDrawerRecyclerView.setAdapter(adapter);

View File

@ -28,6 +28,11 @@ import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.Subscrib
import pl.droidsonroids.gif.GifImageView; import pl.droidsonroids.gif.GifImageView;
public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
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_NAV_HEADER = 0;
private static final int VIEW_TYPE_MENU_GROUP_TITLE = 1; private static final int VIEW_TYPE_MENU_GROUP_TITLE = 1;
private static final int VIEW_TYPE_MENU_ITEM = 2; private static final int VIEW_TYPE_MENU_ITEM = 2;
@ -42,12 +47,14 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re
private String userIconUrl; private String userIconUrl;
private String userBannerUrl; private String userBannerUrl;
private int karma; private int karma;
private ItemClickListener itemClickListener;
private boolean isLoggedIn; private boolean isLoggedIn;
private boolean isInMainPage = true; private boolean isInMainPage = true;
private ArrayList<SubscribedSubredditData> subscribedSubreddits; private ArrayList<SubscribedSubredditData> subscribedSubreddits;
private ArrayList<Account> accounts;
public NavigationDrawerRecyclerViewAdapter(Context context, String accountName, String userIconUrl, public NavigationDrawerRecyclerViewAdapter(Context context, String accountName, String userIconUrl,
String userBannerUrl, int karma) { String userBannerUrl, int karma, ItemClickListener itemClickListener) {
this.context = context; this.context = context;
resources = context.getResources(); resources = context.getResources();
glide = Glide.with(context); glide = Glide.with(context);
@ -56,6 +63,7 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re
this.userBannerUrl = userBannerUrl; this.userBannerUrl = userBannerUrl;
this.karma = karma; this.karma = karma;
isLoggedIn = accountName != null; isLoggedIn = accountName != null;
this.itemClickListener = itemClickListener;
} }
@Override @Override
@ -209,10 +217,13 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re
((MenuItemViewHolder) holder).menuTextView.setText(stringId); ((MenuItemViewHolder) holder).menuTextView.setText(stringId);
((MenuItemViewHolder) holder).menuTextView.setCompoundDrawablesWithIntrinsicBounds( ((MenuItemViewHolder) holder).menuTextView.setCompoundDrawablesWithIntrinsicBounds(
drawableId, 0, 0, 0); drawableId, 0, 0, 0);
int finalStringId = stringId;
((MenuItemViewHolder) holder).itemView.setOnClickListener(view -> itemClickListener.onMenuClick(finalStringId));
} else if (holder instanceof SubscribedThingViewHolder) { } else if (holder instanceof SubscribedThingViewHolder) {
SubscribedSubredditData subreddit = subscribedSubreddits.get(position - CURRENT_MENU_ITEMS); SubscribedSubredditData subreddit = subscribedSubreddits.get(position - CURRENT_MENU_ITEMS);
((SubscribedThingViewHolder) holder).subredditNameTextView.setText(subreddit.getName()); String subredditName = subreddit.getName();
String iconUrl = subreddit.getIconUrl(); String iconUrl = subreddit.getIconUrl();
((SubscribedThingViewHolder) holder).subredditNameTextView.setText(subredditName);
if (iconUrl != null && !iconUrl.equals("")) { if (iconUrl != null && !iconUrl.equals("")) {
glide.load(iconUrl) glide.load(iconUrl)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
@ -224,6 +235,10 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.into(((SubscribedThingViewHolder) holder).iconGifImageView); .into(((SubscribedThingViewHolder) holder).iconGifImageView);
} }
((SubscribedThingViewHolder) holder).itemView.setOnClickListener(view -> {
itemClickListener.onSubscribedSubredditClick(subredditName);
});
} }
} }
@ -242,7 +257,8 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re
} }
public void changeAccountsDataset(List<Account> accounts) { public void changeAccountsDataset(List<Account> accounts) {
this.accounts = (ArrayList<Account>) accounts;
notifyDataSetChanged();
} }
class NavHeaderViewHolder extends RecyclerView.ViewHolder { class NavHeaderViewHolder extends RecyclerView.ViewHolder {
@ -257,7 +273,7 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re
@BindView(R.id.account_switcher_image_view_nav_header_main) @BindView(R.id.account_switcher_image_view_nav_header_main)
ImageView dropIconImageView; ImageView dropIconImageView;
public NavHeaderViewHolder(@NonNull View itemView) { NavHeaderViewHolder(@NonNull View itemView) {
super(itemView); super(itemView);
ButterKnife.bind(this, itemView); ButterKnife.bind(this, itemView);
} }