diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-08-15 11:55:12 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-08-15 11:55:12 +0000 |
commit | 054c5228d70cd11c8df22b799702af2a4a5b2b76 (patch) | |
tree | fc17d84e3bfcfa49ca6b1531248b58638c278282 /app/src | |
parent | 2c6ca51e5c772f30490a35bf73305b43eef91b56 (diff) | |
download | infinity-for-reddit-054c5228d70cd11c8df22b799702af2a4a5b2b76.tar infinity-for-reddit-054c5228d70cd11c8df22b799702af2a4a5b2b76.tar.gz infinity-for-reddit-054c5228d70cd11c8df22b799702af2a4a5b2b76.tar.bz2 infinity-for-reddit-054c5228d70cd11c8df22b799702af2a4a5b2b76.tar.lz infinity-for-reddit-054c5228d70cd11c8df22b799702af2a4a5b2b76.tar.xz infinity-for-reddit-054c5228d70cd11c8df22b799702af2a4a5b2b76.tar.zst infinity-for-reddit-054c5228d70cd11c8df22b799702af2a4a5b2b76.zip |
Lazy mode is available again.
Diffstat (limited to '')
4 files changed, 158 insertions, 44 deletions
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) { |