Add menusin ViewMultiRedditDetailActivity. Minor bugs fixed.

This commit is contained in:
Alex Ning 2019-12-17 12:36:48 +08:00
parent 666ac642fc
commit f834f5804c
6 changed files with 169 additions and 7 deletions

View File

@ -149,11 +149,10 @@ public class AccountPostsActivity extends BaseActivity implements SortTypeSelect
mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE);
mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE);
isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE);
mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_account_posts_activity, mFragment).commit();
if (!mNullAccessToken && mAccessToken == null) {
getCurrentAccountAndInitializeFragment();
} else {
mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_account_posts_activity, mFragment).commit();
}
} else {
getCurrentAccountAndInitializeFragment();

View File

@ -1,11 +1,15 @@
package ml.docilealligator.infinityforreddit.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
@ -16,6 +20,9 @@ import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.Fragment;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.request.RequestOptions;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.chip.Chip;
@ -25,17 +32,20 @@ import javax.inject.Named;
import butterknife.BindView;
import butterknife.ButterKnife;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import ml.docilealligator.infinityforreddit.AppBarStateChangeListener;
import ml.docilealligator.infinityforreddit.AsyncTask.GetCurrentAccountAsyncTask;
import ml.docilealligator.infinityforreddit.Fragment.PostFragment;
import ml.docilealligator.infinityforreddit.Fragment.PostLayoutBottomSheetFragment;
import ml.docilealligator.infinityforreddit.Fragment.SortTimeBottomSheetFragment;
import ml.docilealligator.infinityforreddit.Fragment.SortTypeBottomSheetFragment;
import ml.docilealligator.infinityforreddit.FragmentCommunicator;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.MultiReddit.MultiReddit;
import ml.docilealligator.infinityforreddit.PostDataSource;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.SortType;
import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback;
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
import pl.droidsonroids.gif.GifImageView;
@ -88,7 +98,10 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT
private String multiPath;
private boolean isInLazyMode = false;
private boolean showToast = false;
private RequestManager glide;
private Fragment mFragment;
private Menu mMenu;
private AppBarLayout.LayoutParams params;
private SortTypeBottomSheetFragment sortTypeBottomSheetFragment;
private SortTimeBottomSheetFragment sortTimeBottomSheetFragment;
private PostLayoutBottomSheetFragment postLayoutBottomSheetFragment;
@ -172,6 +185,29 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT
sortTimeBottomSheetFragment = new SortTimeBottomSheetFragment();
postLayoutBottomSheetFragment = new PostLayoutBottomSheetFragment();
params = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams();
//Get status bar height
int statusBarHeight = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
statusBarHeight = getResources().getDimensionPixelSize(resourceId);
}
toolbar.setTitle(multiReddit.getDisplayName());
ViewGroup.MarginLayoutParams toolbarParams = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams();
toolbarParams.topMargin = statusBarHeight;
toolbar.setLayoutParams(toolbarParams);
setSupportActionBar(toolbar);
glide = Glide.with(this);
glide.load(multiReddit.getIconUrl())
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(144, 0)))
.error(glide.load(R.drawable.subreddit_default_icon)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(144, 0))))
.into(iconGifImageView);
}
private void getCurrentAccountAndInitializeFragment() {
@ -197,6 +233,74 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_view_multi_reddit_detail_activity, mFragment).commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.view_multi_reddit_detail_activity, menu);
mMenu = menu;
MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_view_multi_reddit_detail_activity);
if (isInLazyMode) {
lazyModeItem.setTitle(R.string.action_stop_lazy_mode);
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED);
collapsingToolbarLayout.setLayoutParams(params);
} else {
lazyModeItem.setTitle(R.string.action_start_lazy_mode);
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS |
AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED);
collapsingToolbarLayout.setLayoutParams(params);
}
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
case R.id.action_sort_view_multi_reddit_detail_activity:
sortTypeBottomSheetFragment.show(getSupportFragmentManager(), sortTypeBottomSheetFragment.getTag());
return true;
case R.id.action_search_view_multi_reddit_detail_activity:
Intent intent = new Intent(this, SearchActivity.class);
intent.putExtra(SearchActivity.EXTRA_SEARCH_ONLY_SUBREDDITS, false);
startActivity(intent);
return true;
case R.id.action_refresh_view_multi_reddit_detail_activity:
if (mMenu != null) {
mMenu.findItem(R.id.action_lazy_mode_view_multi_reddit_detail_activity).setTitle(R.string.action_start_lazy_mode);
}
if (mFragment instanceof FragmentCommunicator) {
((FragmentCommunicator) mFragment).refresh();
}
return true;
case R.id.action_lazy_mode_view_multi_reddit_detail_activity:
MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_view_multi_reddit_detail_activity);
if (isInLazyMode) {
isInLazyMode = false;
((FragmentCommunicator) mFragment).stopLazyMode();
lazyModeItem.setTitle(R.string.action_start_lazy_mode);
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS |
AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED);
collapsingToolbarLayout.setLayoutParams(params);
} else {
isInLazyMode = true;
if (((FragmentCommunicator) mFragment).startLazyMode()) {
lazyModeItem.setTitle(R.string.action_stop_lazy_mode);
appBarLayout.setExpanded(false);
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED);
collapsingToolbarLayout.setLayoutParams(params);
} else {
isInLazyMode = false;
}
}
return true;
case R.id.action_change_post_layout_view_multi_reddit_detail_activity:
postLayoutBottomSheetFragment.show(getSupportFragmentManager(), postLayoutBottomSheetFragment.getTag());
return true;
}
return false;
}
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
@ -208,12 +312,33 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT
}
@Override
public SharedPreferences getSharedPreferences() {
return mSharedPreferences;
public void sortTypeSelected(SortType sortType) {
mSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TYPE_SUBREDDIT_POST, sortType.getType().name()).apply();
if(sortType.getTime() != null) {
mSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TIME_SUBREDDIT_POST, sortType.getTime().name()).apply();
}
((PostFragment) mFragment).changeSortType(sortType);
}
@Override
public void sortTypeSelected(String sortType) {
Bundle bundle = new Bundle();
bundle.putString(SortTimeBottomSheetFragment.EXTRA_SORT_TYPE, sortType);
sortTimeBottomSheetFragment.setArguments(bundle);
sortTimeBottomSheetFragment.show(getSupportFragmentManager(), sortTimeBottomSheetFragment.getTag());
}
@Override
public void postLayoutSelected(int postLayout) {
if (mFragment != null) {
//mSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_SUBREDDIT_POST_BASE + multiPath, postLayout).apply();
((FragmentCommunicator) mFragment).changePostLayout(postLayout);
}
}
@Override
public SharedPreferences getSharedPreferences() {
return mSharedPreferences;
}
}

View File

@ -58,6 +58,7 @@
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="@string/no_multi_reddits"
android:textSize="?attr/font_default" />
</LinearLayout>

View File

@ -19,6 +19,7 @@
android:id="@+id/collapsing_toolbar_layout_view_multi_reddit_detail_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/backgroundColor"
app:titleEnabled="false"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
@ -32,7 +33,6 @@
android:id="@+id/icon_gif_image_view_view_multi_reddit_detail_activity"
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_marginTop="-36dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:elevation="4dp" />
@ -41,7 +41,6 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="36dp"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:layout_below="@id/icon_gif_image_view_view_multi_reddit_detail_activity"

View File

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:application="ml.docilealligator.infinityforreddit.Activity.ViewMultiRedditDetailActivity">
<item
android:id="@+id/action_sort_view_multi_reddit_detail_activity"
android:orderInCategory="1"
android:title="@string/action_sort"
android:icon="@drawable/ic_outline_sort_24px"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_search_view_multi_reddit_detail_activity"
android:orderInCategory="2"
android:title="@string/action_search"
android:icon="@drawable/ic_search_white_24dp"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_refresh_view_multi_reddit_detail_activity"
android:orderInCategory="3"
android:title="@string/action_refresh"
android:icon="@drawable/ic_refresh_white_24dp"
app:showAsAction="never" />
<item
android:id="@+id/action_lazy_mode_view_multi_reddit_detail_activity"
android:orderInCategory="4"
android:title="@string/action_start_lazy_mode"
app:showAsAction="never" />
<item
android:id="@+id/action_change_post_layout_view_multi_reddit_detail_activity"
android:orderInCategory="5"
android:title="@string/action_change_post_layout"
app:showAsAction="never" />
</menu>

View File

@ -60,6 +60,7 @@
<string name="no_comments">No comments found</string>
<string name="no_subreddits">No subreddits found</string>
<string name="no_users">No users found</string>
<string name="no_multi_reddits">No Multireddits found</string>
<string name="no_storage_permission">No storage permission to save this file</string>
<string name="load_comments_failed">Error loading comments.\nTap to retry.</string>