From 054c5228d70cd11c8df22b799702af2a4a5b2b76 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 15 Aug 2019 19:55:12 +0800 Subject: Lazy mode is available again. --- .../infinityforreddit/FragmentCommunicator.java | 3 + .../infinityforreddit/MainActivity.java | 135 +++++++++++++++------ .../infinityforreddit/PostFragment.java | 9 +- .../infinityforreddit/ViewUserDetailActivity.java | 55 ++++++++- 4 files changed, 158 insertions(+), 44 deletions(-) (limited to 'app/src') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java index 61a4eef7..35301e34 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java @@ -6,4 +6,7 @@ interface FragmentCommunicator { default void stopLazyMode() {} default void resumeLazyMode(boolean resumeNow) {} default void pauseLazyMode(boolean startTimer) {} + default boolean isInLazyMode() { + return false; + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index 753cc9ed..3ddb8733 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -211,18 +211,18 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe mKarma = savedInstanceState.getInt(ACCOUNT_KARMA_STATE); if(!mNullAccessToken && mAccessToken == null) { - getCurrentAccountAndBindView(false); + getCurrentAccountAndBindView(); } else { - bindView(false); + bindView(); } } else { - getCurrentAccountAndBindView(false); + getCurrentAccountAndBindView(); } fab.setOnClickListener(view -> postTypeBottomSheetFragment.show(getSupportFragmentManager(), postTypeBottomSheetFragment.getTag())); } - private void getCurrentAccountAndBindView(boolean afterAccountSwitch) { + private void getCurrentAccountAndBindView() { mNullAccessToken = true; new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { if(account == null) { @@ -235,17 +235,34 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe mBannerImageUrl = account.getBannerImageUrl(); mKarma = account.getKarma(); } - bindView(afterAccountSwitch); + bindView(); }).execute(); } - private void bindView(boolean afterAccountSwitch) { - if(!afterAccountSwitch) { - sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); - viewPager.setAdapter(sectionsPagerAdapter); - viewPager.setOffscreenPageLimit(2); - tabLayout.setupWithViewPager(viewPager); - } + private void bindView() { + sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); + viewPager.setAdapter(sectionsPagerAdapter); + viewPager.setOffscreenPageLimit(2); + tabLayout.setupWithViewPager(viewPager); + + viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { + @Override + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {} + + @Override + public void onPageSelected(int position) { + if(isInLazyMode) { + if(position == sectionsPagerAdapter.getCurrentLazyModeFragmentPosition()) { + sectionsPagerAdapter.resumeLazyMode(); + } else { + sectionsPagerAdapter.pauseLazyMode(); + } + } + } + + @Override + public void onPageScrollStateChanged(int state) {} + }); glide = Glide.with(this); @@ -254,19 +271,9 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe @Override public void accountSelected(Account account) { new SwitchAccountAsyncTask(mRedditDataRoomDatabase, account.getUsername(), () -> { - if(mAccessToken == null) { - Intent intent = new Intent(MainActivity.this, MainActivity.class); - startActivity(intent); - finish(); - } else { - getCurrentAccountAndBindView(true); - sectionsPagerAdapter.changeAccessToken(account.getAccessToken()); - drawer.closeDrawers(); - mDrawerOnAccountSwitch = false; - mDropIconImageView.setImageDrawable(getResources().getDrawable(R.drawable.ic_baseline_arrow_drop_down_24px)); - accountRecyclerView.setVisibility(View.GONE); - allDrawerItemsLinearLayout.setVisibility(View.VISIBLE); - } + Intent intent = new Intent(MainActivity.this, MainActivity.class); + startActivity(intent); + finish(); }).execute(); } @@ -530,20 +537,20 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe loadUserData(); return true; case R.id.action_lazy_mode_main_activity: - /*MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_main_activity); + MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_main_activity); if(isInLazyMode) { + sectionsPagerAdapter.stopLazyMode(); 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); collapsingToolbarLayout.setLayoutParams(params); } else { + sectionsPagerAdapter.startLazyMode(); isInLazyMode = true; - ((FragmentCommunicator) mFragment).startLazyMode(); lazyModeItem.setTitle(R.string.action_stop_lazy_mode); params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL); collapsingToolbarLayout.setLayoutParams(params); - }*/ + } return true; } return false; @@ -726,15 +733,71 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe return fragment; } - void changeAccessToken(String accessToken) { - if(frontPagePostFragment != null) { - frontPagePostFragment.changeAccessToken(accessToken); + void startLazyMode() { + switch (viewPager.getCurrentItem()) { + case 0: + ((FragmentCommunicator) frontPagePostFragment).startLazyMode(); + break; + case 1: + ((FragmentCommunicator) popularPostFragment).startLazyMode(); + break; + case 2: + ((FragmentCommunicator) allPostFragment).startLazyMode(); + } + } + + void stopLazyMode() { + switch (getCurrentLazyModeFragmentPosition()) { + case 0: + ((FragmentCommunicator) frontPagePostFragment).stopLazyMode(); + break; + case 1: + ((FragmentCommunicator) popularPostFragment).stopLazyMode(); + break; + case 2: + ((FragmentCommunicator) allPostFragment).stopLazyMode(); + break; + } + } + + void resumeLazyMode() { + switch (getCurrentLazyModeFragmentPosition()) { + case 0: + ((FragmentCommunicator) frontPagePostFragment).resumeLazyMode(false); + break; + case 1: + ((FragmentCommunicator) popularPostFragment).resumeLazyMode(false); + break; + case 2: + ((FragmentCommunicator) allPostFragment).resumeLazyMode(false); + break; } - if(popularPostFragment != null) { - popularPostFragment.changeAccessToken(accessToken); + } + + void pauseLazyMode() { + switch (getCurrentLazyModeFragmentPosition()) { + case 0: + ((FragmentCommunicator) frontPagePostFragment).pauseLazyMode(false); + break; + case 1: + ((FragmentCommunicator) popularPostFragment).pauseLazyMode(false); + break; + case 2: + ((FragmentCommunicator) allPostFragment).pauseLazyMode(false); } - if(allPostFragment != null) { - allPostFragment.changeAccessToken(accessToken); + } + + int getCurrentLazyModeFragmentPosition() { + if(!isInLazyMode) { + return -1; + } else if(frontPagePostFragment != null && ((FragmentCommunicator) frontPagePostFragment).isInLazyMode()) { + return 0; + } else if(popularPostFragment != null && ((FragmentCommunicator) popularPostFragment).isInLazyMode()) { + return 1; + } else if(allPostFragment != null && ((FragmentCommunicator) allPostFragment).isInLazyMode()) { + return 2; + } else { + return -1; } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java index 1a35b859..f18e3c5a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java @@ -340,10 +340,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { return rootView; } - void changeAccessToken(String accessToken) { - mPostViewModel.changeAccessToken(accessToken); - } - void changeSortType(String sortType) { mPostViewModel.changeSortType(sortType); } @@ -419,6 +415,11 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } + @Override + public boolean isInLazyMode() { + return isInLazyMode; + } + @Subscribe public void onPostUpdateEvent(PostUpdateEventToPostList event) { Post post = mAdapter.getCurrentList().get(event.positionInList); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java index bcecd794..d72a3b4f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java @@ -360,6 +360,29 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi viewPager.setAdapter(sectionsPagerAdapter); viewPager.setOffscreenPageLimit(2); tabLayout.setupWithViewPager(viewPager); + + viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { + @Override + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { + + } + + @Override + public void onPageSelected(int position) { + if(isInLazyMode) { + if(viewPager.getCurrentItem() == 0) { + sectionsPagerAdapter.resumeLazyMode(); + } else { + sectionsPagerAdapter.pauseLazyMode(); + } + } + } + + @Override + public void onPageScrollStateChanged(int state) { + + } + }); } private void fetchUserInfo() { @@ -439,23 +462,23 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi sectionsPagerAdapter.refresh(); return true; case R.id.action_lazy_mode_view_user_detail_activity: - /*MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_view_user_detail_activity); + MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_view_user_detail_activity); if(isInLazyMode) { isInLazyMode = false; - ((FragmentCommunicator) mFragment).stopLazyMode(); + sectionsPagerAdapter.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; - ((FragmentCommunicator) mFragment).startLazyMode(); + sectionsPagerAdapter.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); } - return true;*/ + return true; } return false; } @@ -591,6 +614,30 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi } } + void startLazyMode() { + if(postFragment != null) { + ((FragmentCommunicator) postFragment).startLazyMode(); + } + } + + void stopLazyMode() { + if(postFragment != null) { + ((FragmentCommunicator) postFragment).stopLazyMode(); + } + } + + void resumeLazyMode() { + if(postFragment != null) { + ((FragmentCommunicator) postFragment).resumeLazyMode(false); + } + } + + void pauseLazyMode() { + if(postFragment != null) { + ((FragmentCommunicator) postFragment).pauseLazyMode(false); + } + } + public void changeSortType(String sortType) { if(viewPager.getCurrentItem() == 0) { if(postFragment != null) { -- cgit v1.2.3