From 627bad649b9be722f07d2bc373868823f763a2ca Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 16 Aug 2019 09:37:23 +0800 Subject: [PATCH] Lazy mode is available in AccountPostsActivity. Minor bugs fixed. --- .../AccountPostsActivity.java | 40 +++++++++++++++++-- .../FollowedUsersListingFragment.java | 4 +- .../infinityforreddit/MainActivity.java | 4 +- .../SubredditListingFragment.java | 4 +- .../SubscribedSubredditsListingFragment.java | 4 +- .../UserListingFragment.java | 4 +- .../ViewSubredditDetailActivity.java | 17 +++----- .../ViewUserDetailActivity.java | 39 ++++++++---------- .../res/layout/activity_account_posts.xml | 1 + 9 files changed, 68 insertions(+), 49 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AccountPostsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AccountPostsActivity.java index 991fdd33..d366891d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AccountPostsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AccountPostsActivity.java @@ -17,6 +17,7 @@ import androidx.appcompat.widget.Toolbar; import androidx.fragment.app.Fragment; import com.google.android.material.appbar.AppBarLayout; +import com.google.android.material.appbar.CollapsingToolbarLayout; import javax.inject.Inject; @@ -27,14 +28,17 @@ public class AccountPostsActivity extends AppCompatActivity implements UserThing static final String EXTRA_USER_WHERE = "EUW"; + private static final String IS_IN_LAZY_MODE_STATE = "IILMS"; private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; private static final String ACCESS_TOKEN_STATE = "ATS"; private static final String ACCOUNT_NAME_STATE = "ANS"; private static final String FRAGMENT_OUT_STATE = "FOS"; + @BindView(R.id.collapsing_toolbar_layout_account_posts_activity) CollapsingToolbarLayout collapsingToolbarLayout; @BindView(R.id.appbar_layout_account_posts_activity) AppBarLayout appBarLayout; @BindView(R.id.toolbar_account_posts_activity) Toolbar toolbar; + private boolean isInLazyMode = false; private boolean mNullAccessToken = false; private String mAccessToken; private String mAccountName; @@ -42,6 +46,7 @@ public class AccountPostsActivity extends AppCompatActivity implements UserThing private Fragment mFragment; private Menu mMenu; + private AppBarLayout.LayoutParams params; private UserThingSortTypeBottomSheetFragment userThingSortTypeBottomSheetFragment; @@ -117,25 +122,27 @@ public class AccountPostsActivity extends AppCompatActivity implements UserThing setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); + params = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams(); + if(savedInstanceState != null) { mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE); + isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE); if(!mNullAccessToken && mAccessToken == null) { - getCurrentAccountAndBindView(); + getCurrentAccountAndInitializeFragment(); } else { mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE); getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_account_posts_activity, mFragment).commit(); - initializeFragment(); } } else { - getCurrentAccountAndBindView(); + getCurrentAccountAndInitializeFragment(); } userThingSortTypeBottomSheetFragment = new UserThingSortTypeBottomSheetFragment(); } - private void getCurrentAccountAndBindView() { + private void getCurrentAccountAndInitializeFragment() { new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { if(account == null) { mNullAccessToken = true; @@ -167,6 +174,16 @@ public class AccountPostsActivity extends AppCompatActivity implements UserThing if(mUserWhere != null && mUserWhere.equals(PostDataSource.USER_WHERE_GILDED)) { menu.findItem(R.id.action_sort_account_posts_activity).setVisible(true); } + MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_account_posts_activity); + if(isInLazyMode) { + lazyModeItem.setTitle(R.string.action_stop_lazy_mode); + params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL); + 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); + collapsingToolbarLayout.setLayoutParams(params); + } return true; } @@ -182,6 +199,20 @@ public class AccountPostsActivity extends AppCompatActivity implements UserThing } return true; case R.id.action_lazy_mode_account_posts_activity: + MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_account_posts_activity); + if(isInLazyMode) { + ((FragmentCommunicator) mFragment).stopLazyMode(); + isInLazyMode = false; + lazyModeItem.setTitle(R.string.action_start_lazy_mode); + params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS); + collapsingToolbarLayout.setLayoutParams(params); + } else { + ((FragmentCommunicator) mFragment).startLazyMode(); + isInLazyMode = true; + lazyModeItem.setTitle(R.string.action_stop_lazy_mode); + params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL); + collapsingToolbarLayout.setLayoutParams(params); + } return true; case android.R.id.home: finish(); @@ -196,6 +227,7 @@ public class AccountPostsActivity extends AppCompatActivity implements UserThing if (mFragment != null) { getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment); } + outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode); outState.putString(ACCESS_TOKEN_STATE, mAccessToken); outState.putString(ACCOUNT_NAME_STATE, mAccountName); outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersListingFragment.java index ffd2cf95..9a371b8a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersListingFragment.java @@ -13,7 +13,7 @@ import android.widget.ImageView; import android.widget.LinearLayout; import androidx.fragment.app.Fragment; -import androidx.lifecycle.ViewModelProviders; +import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -79,7 +79,7 @@ public class FollowedUsersListingFragment extends Fragment { FollowedUsersRecyclerViewAdapter adapter = new FollowedUsersRecyclerViewAdapter(mActivity); mRecyclerView.setAdapter(adapter); - mSubscribedUserViewModel = ViewModelProviders.of(this, + mSubscribedUserViewModel = new ViewModelProvider(this, new SubscribedUserViewModel.Factory(mActivity.getApplication(), mRedditDataRoomDatabase, getArguments().getString(EXTRA_ACCOUNT_NAME))) .get(SubscribedUserViewModel.class); mSubscribedUserViewModel.getAllSubscribedUsers().observe(this, subscribedUserData -> { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index 3ddb8733..11717ed7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -26,7 +26,7 @@ import androidx.drawerlayout.widget.DrawerLayout; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentPagerAdapter; -import androidx.lifecycle.ViewModelProviders; +import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.viewpager.widget.ViewPager; @@ -313,7 +313,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe accountRecyclerView.setNestedScrollingEnabled(false); accountRecyclerView.setAdapter(adapter); - accountViewModel = ViewModelProviders.of(this, + accountViewModel = new ViewModelProvider(this, new AccountViewModel.Factory(getApplication(), mRedditDataRoomDatabase, mAccountName)).get(AccountViewModel.class); accountViewModel.getAccountsExceptCurrentAccountLiveData().observe(this, adapter::changeAccountsDataset); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingFragment.java index bf6e3894..dce606c6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingFragment.java @@ -17,7 +17,7 @@ import android.widget.TextView; import androidx.annotation.NonNull; import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.fragment.app.Fragment; -import androidx.lifecycle.ViewModelProviders; +import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -124,7 +124,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun SubredditListingViewModel.Factory factory = new SubredditListingViewModel.Factory(mRetrofit, query, PostDataSource.SORT_TYPE_RELEVANCE); - mSubredditListingViewModel = ViewModelProviders.of(this, factory).get(SubredditListingViewModel.class); + mSubredditListingViewModel = new ViewModelProvider(this, factory).get(SubredditListingViewModel.class); mSubredditListingViewModel.getSubreddits().observe(this, subredditData -> mAdapter.submitList(subredditData)); mSubredditListingViewModel.getInitialLoadingState().observe(this, networkState -> { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java index d43eb602..6006da7f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java @@ -14,7 +14,7 @@ import android.widget.LinearLayout; import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; -import androidx.lifecycle.ViewModelProviders; +import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -93,7 +93,7 @@ public class SubscribedSubredditsListingFragment extends Fragment { mRecyclerView.setAdapter(adapter); - mSubscribedSubredditViewModel = ViewModelProviders.of(this, + mSubscribedSubredditViewModel = new ViewModelProvider(this, new SubscribedSubredditViewModel.Factory(mActivity.getApplication(), mRedditDataRoomDatabase, accountName)) .get(SubscribedSubredditViewModel.class); mSubscribedSubredditViewModel.getAllSubscribedSubreddits().observe(this, subscribedSubredditData -> { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingFragment.java index 46971ffe..495890e5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingFragment.java @@ -14,7 +14,7 @@ import android.widget.TextView; import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.fragment.app.Fragment; -import androidx.lifecycle.ViewModelProviders; +import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -103,7 +103,7 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato UserListingViewModel.Factory factory = new UserListingViewModel.Factory(mRetrofit, mQuery, PostDataSource.SORT_TYPE_RELEVANCE); - mUserListingViewModel = ViewModelProviders.of(this, factory).get(UserListingViewModel.class); + mUserListingViewModel = new ViewModelProvider(this, factory).get(UserListingViewModel.class); mUserListingViewModel.getUsers().observe(this, UserData -> mAdapter.submitList(UserData)); mUserListingViewModel.getInitialLoadingState().observe(this, networkState -> { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java index 610c4a23..39053d1a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java @@ -20,7 +20,7 @@ import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.fragment.app.Fragment; -import androidx.lifecycle.ViewModelProviders; +import androidx.lifecycle.ViewModelProvider; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; @@ -135,13 +135,6 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So } }); - int statusBarResourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); - if (statusBarResourceId > 0) { - ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams(); - params.topMargin = getResources().getDimensionPixelSize(statusBarResourceId); - toolbar.setLayoutParams(params); - } - int navBarResourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); if (navBarResourceId > 0) { CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) fab.getLayoutParams(); @@ -195,14 +188,14 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So subredditNameTextView.setText(title); toolbar.setTitle(title); + ViewGroup.MarginLayoutParams toolbarParams = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams(); + toolbarParams.topMargin = statusBarHeight; + toolbar.setLayoutParams(toolbarParams); setSupportActionBar(toolbar); - ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams(); - params.topMargin = statusBarHeight; - glide = Glide.with(this); - mSubredditViewModel = ViewModelProviders.of(this, + mSubredditViewModel = new ViewModelProvider(this, new SubredditViewModel.Factory(getApplication(), mRedditDataRoomDatabase, subredditName)) .get(SubredditViewModel.class); mSubredditViewModel.getSubredditLiveData().observe(this, subredditData -> { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java index d72a3b4f..e9c54118 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java @@ -22,7 +22,7 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentPagerAdapter; -import androidx.lifecycle.ViewModelProviders; +import androidx.lifecycle.ViewModelProvider; import androidx.viewpager.widget.ViewPager; import com.bumptech.glide.Glide; @@ -145,15 +145,16 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi statusBarHeight = resources.getDimensionPixelSize(resourceId); } - String title = "u/" + username; - userNameTextView.setText(title); - Toolbar toolbar = findViewById(R.id.toolbar); - toolbar.setTitle(title); - setSupportActionBar(toolbar); - ViewGroup.MarginLayoutParams toolbarLayoutParams = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams(); toolbarLayoutParams.topMargin = statusBarHeight; + toolbar.setLayoutParams(toolbarLayoutParams); + + String title = "u/" + username; + userNameTextView.setText(title); + toolbar.setTitle(title); + + setSupportActionBar(toolbar); expandedTabTextColor = resources.getColor(R.color.tabLayoutWithExpandedCollapsingToolbarTextColor); expandedTabBackgroundColor = resources.getColor(R.color.tabLayoutWithExpandedCollapsingToolbarTabBackground); @@ -197,15 +198,7 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi } }); - int statusBarResourceId = resources.getIdentifier("status_bar_height", "dimen", "android"); - if (statusBarResourceId > 0) { - int navBarHeight = resources.getDimensionPixelSize(statusBarResourceId); - ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams(); - params.topMargin = navBarHeight; - toolbar.setLayoutParams(params); - - showToast = true; - } + showToast = true; } else { appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() { @Override @@ -226,7 +219,7 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi subscribedUserDao = mRedditDataRoomDatabase.subscribedUserDao(); glide = Glide.with(this); - userViewModel = ViewModelProviders.of(this, new UserViewModel.Factory(getApplication(), mRedditDataRoomDatabase, username)) + userViewModel = new ViewModelProvider(this, new UserViewModel.Factory(getApplication(), mRedditDataRoomDatabase, username)) .get(UserViewModel.class); userViewModel.getUserLiveData().observe(this, userData -> { if (userData != null) { @@ -280,13 +273,13 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi public void onUserFollowingSuccess() { subscribeUserChip.setText(R.string.unfollow); subscribeUserChip.setChipBackgroundColor(resources.getColorStateList(R.color.colorAccent)); - makeSnackbar(R.string.followed, false); + showMessage(R.string.followed, false); subscriptionReady = true; } @Override public void onUserFollowingFail() { - makeSnackbar(R.string.follow_failed, false); + showMessage(R.string.follow_failed, false); subscriptionReady = true; } }); @@ -297,13 +290,13 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi public void onUserFollowingSuccess() { subscribeUserChip.setText(R.string.follow); subscribeUserChip.setChipBackgroundColor(resources.getColorStateList(R.color.backgroundColorPrimaryDark)); - makeSnackbar(R.string.unfollowed, false); + showMessage(R.string.unfollowed, false); subscriptionReady = true; } @Override public void onUserFollowingFail() { - makeSnackbar(R.string.unfollow_failed, false); + showMessage(R.string.unfollow_failed, false); subscriptionReady = true; } }); @@ -396,7 +389,7 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi @Override public void onFetchUserDataFailed() { - makeSnackbar(R.string.cannot_fetch_user_info, true); + showMessage(R.string.cannot_fetch_user_info, true); mFetchUserInfoSuccess = false; } }); @@ -493,7 +486,7 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi outState.putString(ACCOUNT_NAME_STATE, mAccountName); } - private void makeSnackbar(int resId, boolean retry) { + private void showMessage(int resId, boolean retry) { if(showToast) { Toast.makeText(this, resId, Toast.LENGTH_SHORT).show(); } else { diff --git a/app/src/main/res/layout/activity_account_posts.xml b/app/src/main/res/layout/activity_account_posts.xml index ecdda049..bc2af273 100644 --- a/app/src/main/res/layout/activity_account_posts.xml +++ b/app/src/main/res/layout/activity_account_posts.xml @@ -13,6 +13,7 @@ android:theme="@style/AppTheme.AppBarOverlay">