Lazy mode is available again.

This commit is contained in:
Alex Ning 2019-08-15 19:55:12 +08:00
parent 2c6ca51e5c
commit 054c5228d7
4 changed files with 158 additions and 44 deletions

View File

@ -6,4 +6,7 @@ interface FragmentCommunicator {
default void stopLazyMode() {} default void stopLazyMode() {}
default void resumeLazyMode(boolean resumeNow) {} default void resumeLazyMode(boolean resumeNow) {}
default void pauseLazyMode(boolean startTimer) {} default void pauseLazyMode(boolean startTimer) {}
default boolean isInLazyMode() {
return false;
}
} }

View File

@ -211,18 +211,18 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
mKarma = savedInstanceState.getInt(ACCOUNT_KARMA_STATE); mKarma = savedInstanceState.getInt(ACCOUNT_KARMA_STATE);
if(!mNullAccessToken && mAccessToken == null) { if(!mNullAccessToken && mAccessToken == null) {
getCurrentAccountAndBindView(false); getCurrentAccountAndBindView();
} else { } else {
bindView(false); bindView();
} }
} else { } else {
getCurrentAccountAndBindView(false); getCurrentAccountAndBindView();
} }
fab.setOnClickListener(view -> postTypeBottomSheetFragment.show(getSupportFragmentManager(), postTypeBottomSheetFragment.getTag())); fab.setOnClickListener(view -> postTypeBottomSheetFragment.show(getSupportFragmentManager(), postTypeBottomSheetFragment.getTag()));
} }
private void getCurrentAccountAndBindView(boolean afterAccountSwitch) { private void getCurrentAccountAndBindView() {
mNullAccessToken = true; mNullAccessToken = true;
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if(account == null) { if(account == null) {
@ -235,17 +235,34 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
mBannerImageUrl = account.getBannerImageUrl(); mBannerImageUrl = account.getBannerImageUrl();
mKarma = account.getKarma(); mKarma = account.getKarma();
} }
bindView(afterAccountSwitch); bindView();
}).execute(); }).execute();
} }
private void bindView(boolean afterAccountSwitch) { private void bindView() {
if(!afterAccountSwitch) {
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(sectionsPagerAdapter); viewPager.setAdapter(sectionsPagerAdapter);
viewPager.setOffscreenPageLimit(2); viewPager.setOffscreenPageLimit(2);
tabLayout.setupWithViewPager(viewPager); 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); glide = Glide.with(this);
@ -254,19 +271,9 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
@Override @Override
public void accountSelected(Account account) { public void accountSelected(Account account) {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, account.getUsername(), () -> { new SwitchAccountAsyncTask(mRedditDataRoomDatabase, account.getUsername(), () -> {
if(mAccessToken == null) {
Intent intent = new Intent(MainActivity.this, MainActivity.class); Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent); startActivity(intent);
finish(); 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);
}
}).execute(); }).execute();
} }
@ -530,20 +537,20 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
loadUserData(); loadUserData();
return true; return true;
case R.id.action_lazy_mode_main_activity: 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) { if(isInLazyMode) {
sectionsPagerAdapter.stopLazyMode();
isInLazyMode = false; isInLazyMode = false;
((FragmentCommunicator) mFragment).stopLazyMode();
lazyModeItem.setTitle(R.string.action_start_lazy_mode); lazyModeItem.setTitle(R.string.action_start_lazy_mode);
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS); params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
collapsingToolbarLayout.setLayoutParams(params); collapsingToolbarLayout.setLayoutParams(params);
} else { } else {
sectionsPagerAdapter.startLazyMode();
isInLazyMode = true; isInLazyMode = true;
((FragmentCommunicator) mFragment).startLazyMode();
lazyModeItem.setTitle(R.string.action_stop_lazy_mode); lazyModeItem.setTitle(R.string.action_stop_lazy_mode);
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL); params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL);
collapsingToolbarLayout.setLayoutParams(params); collapsingToolbarLayout.setLayoutParams(params);
}*/ }
return true; return true;
} }
return false; return false;
@ -726,15 +733,71 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
return fragment; return fragment;
} }
void changeAccessToken(String accessToken) { void startLazyMode() {
if(frontPagePostFragment != null) { switch (viewPager.getCurrentItem()) {
frontPagePostFragment.changeAccessToken(accessToken); case 0:
((FragmentCommunicator) frontPagePostFragment).startLazyMode();
break;
case 1:
((FragmentCommunicator) popularPostFragment).startLazyMode();
break;
case 2:
((FragmentCommunicator) allPostFragment).startLazyMode();
} }
if(popularPostFragment != null) {
popularPostFragment.changeAccessToken(accessToken);
} }
if(allPostFragment != null) {
allPostFragment.changeAccessToken(accessToken); 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;
}
}
void pauseLazyMode() {
switch (getCurrentLazyModeFragmentPosition()) {
case 0:
((FragmentCommunicator) frontPagePostFragment).pauseLazyMode(false);
break;
case 1:
((FragmentCommunicator) popularPostFragment).pauseLazyMode(false);
break;
case 2:
((FragmentCommunicator) allPostFragment).pauseLazyMode(false);
}
}
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;
} }
} }

View File

@ -340,10 +340,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
return rootView; return rootView;
} }
void changeAccessToken(String accessToken) {
mPostViewModel.changeAccessToken(accessToken);
}
void changeSortType(String sortType) { void changeSortType(String sortType) {
mPostViewModel.changeSortType(sortType); mPostViewModel.changeSortType(sortType);
} }
@ -419,6 +415,11 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
} }
} }
@Override
public boolean isInLazyMode() {
return isInLazyMode;
}
@Subscribe @Subscribe
public void onPostUpdateEvent(PostUpdateEventToPostList event) { public void onPostUpdateEvent(PostUpdateEventToPostList event) {
Post post = mAdapter.getCurrentList().get(event.positionInList); Post post = mAdapter.getCurrentList().get(event.positionInList);

View File

@ -360,6 +360,29 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi
viewPager.setAdapter(sectionsPagerAdapter); viewPager.setAdapter(sectionsPagerAdapter);
viewPager.setOffscreenPageLimit(2); viewPager.setOffscreenPageLimit(2);
tabLayout.setupWithViewPager(viewPager); 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() { private void fetchUserInfo() {
@ -439,23 +462,23 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi
sectionsPagerAdapter.refresh(); sectionsPagerAdapter.refresh();
return true; return true;
case R.id.action_lazy_mode_view_user_detail_activity: 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) { if(isInLazyMode) {
isInLazyMode = false; isInLazyMode = false;
((FragmentCommunicator) mFragment).stopLazyMode(); sectionsPagerAdapter.stopLazyMode();
lazyModeItem.setTitle(R.string.action_start_lazy_mode); lazyModeItem.setTitle(R.string.action_start_lazy_mode);
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS | params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS |
AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED); AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED);
collapsingToolbarLayout.setLayoutParams(params); collapsingToolbarLayout.setLayoutParams(params);
} else { } else {
isInLazyMode = true; isInLazyMode = true;
((FragmentCommunicator) mFragment).startLazyMode(); sectionsPagerAdapter.startLazyMode();
lazyModeItem.setTitle(R.string.action_stop_lazy_mode); lazyModeItem.setTitle(R.string.action_stop_lazy_mode);
appBarLayout.setExpanded(false); appBarLayout.setExpanded(false);
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED); params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED);
collapsingToolbarLayout.setLayoutParams(params); collapsingToolbarLayout.setLayoutParams(params);
} }
return true;*/ return true;
} }
return false; 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) { public void changeSortType(String sortType) {
if(viewPager.getCurrentItem() == 0) { if(viewPager.getCurrentItem() == 0) {
if(postFragment != null) { if(postFragment != null) {