Migrate to ViewPager2 in MainActivity. Make POST_LAYOUT_POPULAR_POST and POST_LAYOUT_ALL_POST legacy settings. Fix post layout cannot be remembered in MainActivity.

This commit is contained in:
Alex Ning 2020-09-15 14:45:59 +08:00
parent 908cd0aaab
commit 8ce4a0ef6b
6 changed files with 222 additions and 79 deletions

View File

@ -11,7 +11,6 @@ import android.view.KeyEvent;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.view.Window; import android.view.Window;
import android.view.WindowManager; import android.view.WindowManager;
import android.widget.ImageView; import android.widget.ImageView;
@ -19,6 +18,7 @@ import android.widget.LinearLayout;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatDelegate; import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
@ -27,11 +27,12 @@ import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout; import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter; import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager; import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;
import androidx.work.Constraints; import androidx.work.Constraints;
import androidx.work.ExistingPeriodicWorkPolicy; import androidx.work.ExistingPeriodicWorkPolicy;
import androidx.work.NetworkType; import androidx.work.NetworkType;
@ -45,6 +46,7 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.navigation.NavigationView; import com.google.android.material.navigation.NavigationView;
import com.google.android.material.tabs.TabLayout; import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.Subscribe;
@ -129,7 +131,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
@BindView(R.id.appbar_layout_main_activity) @BindView(R.id.appbar_layout_main_activity)
AppBarLayout appBarLayout; AppBarLayout appBarLayout;
@BindView(R.id.view_pager_main_activity) @BindView(R.id.view_pager_main_activity)
ViewPager viewPager; ViewPager2 viewPager2;
@BindView(R.id.collapsing_toolbar_layout_main_activity) @BindView(R.id.collapsing_toolbar_layout_main_activity)
CollapsingToolbarLayout collapsingToolbarLayout; CollapsingToolbarLayout collapsingToolbarLayout;
@BindView(R.id.toolbar) @BindView(R.id.toolbar)
@ -173,6 +175,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
SharedPreferences mMainActivityTabsSharedPreferences; SharedPreferences mMainActivityTabsSharedPreferences;
@Inject @Inject
CustomThemeWrapper mCustomThemeWrapper; CustomThemeWrapper mCustomThemeWrapper;
private FragmentManager fragmentManager;
private SectionsPagerAdapter sectionsPagerAdapter; private SectionsPagerAdapter sectionsPagerAdapter;
private AppBarLayout.LayoutParams params; private AppBarLayout.LayoutParams params;
private PostTypeBottomSheetFragment postTypeBottomSheetFragment; private PostTypeBottomSheetFragment postTypeBottomSheetFragment;
@ -578,12 +581,57 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
navDrawerRecyclerView.setLayoutManager(new LinearLayoutManager(this)); navDrawerRecyclerView.setLayoutManager(new LinearLayoutManager(this));
navDrawerRecyclerView.setAdapter(adapter); navDrawerRecyclerView.setAdapter(adapter);
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); fragmentManager = getSupportFragmentManager();
viewPager.setAdapter(sectionsPagerAdapter); sectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager, getLifecycle());
viewPager.setOffscreenPageLimit(3); viewPager2.setAdapter(sectionsPagerAdapter);
tabLayout.setupWithViewPager(viewPager); viewPager2.setOffscreenPageLimit(3);
viewPager2.requestDisallowInterceptTouchEvent(true);
new TabLayoutMediator(tabLayout, viewPager2, (tab, position) -> {
if (mAccessToken == null) {
switch (position) {
case 0:
tab.setText(R.string.popular);
break;
case 1:
tab.setText(R.string.all);
break;
}
} else {
switch (position) {
case 0:
tab.setText(mMainActivityTabsSharedPreferences.getString((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_1_TITLE, getString(R.string.home)));
break;
case 1:
tab.setText(mMainActivityTabsSharedPreferences.getString((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_2_TITLE, getString(R.string.popular)));
break;
case 2:
tab.setText(mMainActivityTabsSharedPreferences.getString((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_3_TITLE, getString(R.string.all)));
break;
}
}
}).attach();
//tabLayout.setupWithViewPager(viewPager2);
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
if (mAccessToken != null) {
if (showBottomAppBar) {
bottomNavigationView.performShow();
}
fab.show();
}
/*if (isInLazyMode) {
if (position == sectionsPagerAdapter.getCurrentLazyModeFragmentPosition()) {
sectionsPagerAdapter.resumeLazyMode();
} else {
sectionsPagerAdapter.pauseLazyMode();
}
}*/
sectionsPagerAdapter.displaySortTypeInToolbar();
}
});
/*viewPager2.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override @Override
public void onPageSelected(int position) { public void onPageSelected(int position) {
if (mAccessToken != null) { if (mAccessToken != null) {
@ -601,7 +649,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
} }
sectionsPagerAdapter.displaySortTypeInToolbar(); sectionsPagerAdapter.displaySortTypeInToolbar();
} }
}); });*/
loadSubscriptions(); loadSubscriptions();
@ -618,9 +666,9 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
if (getIntent().hasExtra(EXTRA_POST_TYPE)) { if (getIntent().hasExtra(EXTRA_POST_TYPE)) {
String type = getIntent().getStringExtra(EXTRA_POST_TYPE); String type = getIntent().getStringExtra(EXTRA_POST_TYPE);
if (type != null && type.equals("popular")) { if (type != null && type.equals("popular")) {
viewPager.setCurrentItem(1); viewPager2.setCurrentItem(1);
} else { } else {
viewPager.setCurrentItem(2); viewPager2.setCurrentItem(2);
} }
} }
@ -725,6 +773,10 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
@Override @Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) { public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.action_search_main_activity:
Intent intent = new Intent(this, SearchActivity.class);
startActivity(intent);
return true;
case R.id.action_sort_main_activity: case R.id.action_sort_main_activity:
int currentPostType = sectionsPagerAdapter.getCurrentPostType(); int currentPostType = sectionsPagerAdapter.getCurrentPostType();
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
@ -736,10 +788,6 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
sortTypeBottomSheetFragment.setArguments(bundle); sortTypeBottomSheetFragment.setArguments(bundle);
sortTypeBottomSheetFragment.show(getSupportFragmentManager(), sortTypeBottomSheetFragment.getTag()); sortTypeBottomSheetFragment.show(getSupportFragmentManager(), sortTypeBottomSheetFragment.getTag());
return true; return true;
case R.id.action_search_main_activity:
Intent intent = new Intent(this, SearchActivity.class);
startActivity(intent);
return true;
case R.id.action_refresh_main_activity: case R.id.action_refresh_main_activity:
if (mMenu != null) { if (mMenu != null) {
mMenu.findItem(R.id.action_lazy_mode_main_activity).setTitle(R.string.action_start_lazy_mode); mMenu.findItem(R.id.action_lazy_mode_main_activity).setTitle(R.string.action_start_lazy_mode);
@ -767,6 +815,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
return true; return true;
case R.id.action_change_post_layout_main_activity: case R.id.action_change_post_layout_main_activity:
postLayoutBottomSheetFragment.show(getSupportFragmentManager(), postLayoutBottomSheetFragment.getTag()); postLayoutBottomSheetFragment.show(getSupportFragmentManager(), postLayoutBottomSheetFragment.getTag());
return true;
} }
return false; return false;
} }
@ -928,18 +977,18 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
} }
} }
private class SectionsPagerAdapter extends FragmentPagerAdapter { private class SectionsPagerAdapter extends FragmentStateAdapter{
private PostFragment tab1; /*private PostFragment tab1;
private PostFragment tab2; private PostFragment tab2;
private PostFragment tab3; private PostFragment tab3;*/
SectionsPagerAdapter(FragmentManager fm) { SectionsPagerAdapter(FragmentManager fm, Lifecycle lifecycle) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT); super(fm, lifecycle);
} }
@NonNull @NonNull
@Override @Override
public Fragment getItem(int position) { public Fragment createFragment(int position) {
if (mAccessToken == null) { if (mAccessToken == null) {
if (position == 0) { if (position == 0) {
PostFragment fragment = new PostFragment(); PostFragment fragment = new PostFragment();
@ -1045,14 +1094,14 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
} }
@Override @Override
public int getCount() { public int getItemCount() {
if (mAccessToken == null) { if (mAccessToken == null) {
return 2; return 2;
} }
return 3; return 3;
} }
@Override /*@Override
public CharSequence getPageTitle(int position) { public CharSequence getPageTitle(int position) {
if (mAccessToken == null) { if (mAccessToken == null) {
switch (position) { switch (position) {
@ -1072,9 +1121,9 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
} }
} }
return null; return null;
} }*/
@NonNull /*@NonNull
@Override @Override
public Object instantiateItem(@NonNull ViewGroup container, int position) { public Object instantiateItem(@NonNull ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position); Fragment fragment = (Fragment) super.instantiateItem(container, position);
@ -1100,18 +1149,34 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
} }
displaySortTypeInToolbar(); displaySortTypeInToolbar();
return fragment; return fragment;
}*/
@Nullable
private PostFragment getCurrentFragment() {
if (viewPager2 == null || fragmentManager == null) {
return null;
}
Fragment fragment = fragmentManager.findFragmentByTag("f" + viewPager2.getCurrentItem());
if (fragment instanceof PostFragment) {
return (PostFragment) fragment;
}
return null;
} }
boolean handleKeyDown(int keyCode) { boolean handleKeyDown(int keyCode) {
if (mAccessToken == null) { PostFragment currentFragment = getCurrentFragment();
switch (viewPager.getCurrentItem()) { if (currentFragment != null) {
return currentFragment.handleKeyDown(keyCode);
}
/*if (mAccessToken == null) {
switch (viewPager2.getCurrentItem()) {
case 0: case 0:
return tab2.handleKeyDown(keyCode); return tab2.handleKeyDown(keyCode);
case 1: case 1:
return tab3.handleKeyDown(keyCode); return tab3.handleKeyDown(keyCode);
} }
} else { } else {
switch (viewPager.getCurrentItem()) { switch (viewPager2.getCurrentItem()) {
case 0: case 0:
return tab1.handleKeyDown(keyCode); return tab1.handleKeyDown(keyCode);
case 1: case 1:
@ -1119,20 +1184,24 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
case 2: case 2:
return tab3.handleKeyDown(keyCode); return tab3.handleKeyDown(keyCode);
} }
} }*/
return false; return false;
} }
boolean startLazyMode() { boolean startLazyMode() {
if (mAccessToken == null) { PostFragment currentFragment = getCurrentFragment();
switch (viewPager.getCurrentItem()) { if (currentFragment != null) {
return currentFragment.startLazyMode();
}
/*if (mAccessToken == null) {
switch (viewPager2.getCurrentItem()) {
case 0: case 0:
return tab2.startLazyMode(); return tab2.startLazyMode();
case 1: case 1:
return tab3.startLazyMode(); return tab3.startLazyMode();
} }
} else { } else {
switch (viewPager.getCurrentItem()) { switch (viewPager2.getCurrentItem()) {
case 0: case 0:
return tab1.startLazyMode(); return tab1.startLazyMode();
case 1: case 1:
@ -1140,13 +1209,19 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
case 2: case 2:
return tab3.startLazyMode(); return tab3.startLazyMode();
} }
} }*/
return false; return false;
} }
void stopLazyMode() { void stopLazyMode() {
if (mAccessToken == null) { for (int i = 0; i < getItemCount(); i++) {
Fragment fragment = fragmentManager.findFragmentByTag("f" + i);
if (fragment instanceof PostFragment && ((PostFragment) fragment).isInLazyMode()) {
((PostFragment) fragment).stopLazyMode();
}
}
/*if (mAccessToken == null) {
switch (getCurrentLazyModeFragmentPosition()) { switch (getCurrentLazyModeFragmentPosition()) {
case 0: case 0:
tab2.stopLazyMode(); tab2.stopLazyMode();
@ -1167,11 +1242,15 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
tab3.stopLazyMode(); tab3.stopLazyMode();
break; break;
} }
} }*/
} }
void resumeLazyMode() { void resumeLazyMode() {
if (mAccessToken == null) { PostFragment currentFragment = getCurrentFragment();
if (currentFragment != null) {
currentFragment.resumeLazyMode(false);
}
/*if (mAccessToken == null) {
switch (getCurrentLazyModeFragmentPosition()) { switch (getCurrentLazyModeFragmentPosition()) {
case 0: case 0:
tab2.resumeLazyMode(false); tab2.resumeLazyMode(false);
@ -1192,11 +1271,15 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
tab3.resumeLazyMode(false); tab3.resumeLazyMode(false);
break; break;
} }
} }*/
} }
void pauseLazyMode() { void pauseLazyMode() {
if (mAccessToken == null) { PostFragment currentFragment = getCurrentFragment();
if (currentFragment != null) {
currentFragment.pauseLazyMode(false);
}
/*if (mAccessToken == null) {
switch (getCurrentLazyModeFragmentPosition()) { switch (getCurrentLazyModeFragmentPosition()) {
case 0: case 0:
tab2.pauseLazyMode(false); tab2.pauseLazyMode(false);
@ -1215,10 +1298,10 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
case 2: case 2:
tab3.pauseLazyMode(false); tab3.pauseLazyMode(false);
} }
} }*/
} }
int getCurrentLazyModeFragmentPosition() { /*int getCurrentLazyModeFragmentPosition() {
if (mAccessToken == null) { if (mAccessToken == null) {
if (!isInLazyMode) { if (!isInLazyMode) {
return -1; return -1;
@ -1242,17 +1325,21 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
return -1; return -1;
} }
} }
} }*/
int getCurrentPostType() { int getCurrentPostType() {
if (mAccessToken == null) { PostFragment currentFragment = getCurrentFragment();
if (viewPager.getCurrentItem() == 0) { if (currentFragment != null) {
return currentFragment.getPostType();
}
/*if (mAccessToken == null) {
if (viewPager2.getCurrentItem() == 0) {
return tab2.getPostType(); return tab2.getPostType();
} else { } else {
return tab3.getPostType(); return tab3.getPostType();
} }
} else { } else {
switch (viewPager.getCurrentItem()) { switch (viewPager2.getCurrentItem()) {
case 1: case 1:
return tab2.getPostType(); return tab2.getPostType();
case 2: case 2:
@ -1260,18 +1347,23 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
default: default:
return tab1.getPostType(); return tab1.getPostType();
} }
} }*/
return PostDataSource.TYPE_SUBREDDIT;
} }
void changeSortType(SortType sortType) { void changeSortType(SortType sortType) {
if (mAccessToken == null) { PostFragment currentFragment = getCurrentFragment();
if (viewPager.getCurrentItem() == 0) { if (currentFragment != null) {
currentFragment.changeSortType(sortType);
}
/*if (mAccessToken == null) {
if (viewPager2.getCurrentItem() == 0) {
tab2.changeSortType(sortType); tab2.changeSortType(sortType);
} else { } else {
tab3.changeSortType(sortType); tab3.changeSortType(sortType);
} }
} else { } else {
switch (viewPager.getCurrentItem()) { switch (viewPager2.getCurrentItem()) {
case 0: case 0:
tab1.changeSortType(sortType); tab1.changeSortType(sortType);
break; break;
@ -1281,13 +1373,17 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
case 2: case 2:
tab3.changeSortType(sortType); tab3.changeSortType(sortType);
} }
} }*/
displaySortTypeInToolbar(); displaySortTypeInToolbar();
} }
public void refresh() { public void refresh() {
if (mAccessToken == null) { PostFragment currentFragment = getCurrentFragment();
if (viewPager.getCurrentItem() == 0) { if (currentFragment != null) {
currentFragment.refresh();
}
/*if (mAccessToken == null) {
if (viewPager2.getCurrentItem() == 0) {
if (tab2 != null) { if (tab2 != null) {
tab2.refresh(); tab2.refresh();
} }
@ -1297,7 +1393,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
} }
} }
} else { } else {
switch (viewPager.getCurrentItem()) { switch (viewPager2.getCurrentItem()) {
case 0: case 0:
if (tab1 != null) { if (tab1 != null) {
tab1.refresh(); tab1.refresh();
@ -1313,11 +1409,17 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
tab3.refresh(); tab3.refresh();
} }
} }
} }*/
} }
void changeNSFW(boolean nsfw) { void changeNSFW(boolean nsfw) {
if (tab1 != null) { for (int i = 0; i < getItemCount(); i++) {
Fragment fragment = fragmentManager.findFragmentByTag("f" + i);
if (fragment instanceof PostFragment) {
((PostFragment) fragment).changeNSFW(nsfw);
}
}
/*if (tab1 != null) {
tab1.changeNSFW(nsfw); tab1.changeNSFW(nsfw);
} }
if (tab2 != null) { if (tab2 != null) {
@ -1325,12 +1427,16 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
} }
if (tab3 != null) { if (tab3 != null) {
tab3.changeNSFW(nsfw); tab3.changeNSFW(nsfw);
} }*/
} }
void changePostLayout(int postLayout) { void changePostLayout(int postLayout) {
if (mAccessToken == null) { PostFragment currentFragment = getCurrentFragment();
if (viewPager.getCurrentItem() == 0) { if (currentFragment != null) {
currentFragment.changePostLayout(postLayout);
}
/*if (mAccessToken == null) {
if (viewPager2.getCurrentItem() == 0) {
if (tab2 != null) { if (tab2 != null) {
mPostLayoutSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_POPULAR_POST, postLayout).apply(); mPostLayoutSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_POPULAR_POST, postLayout).apply();
tab2.changePostLayout(postLayout); tab2.changePostLayout(postLayout);
@ -1342,7 +1448,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
} }
} }
} else { } else {
switch (viewPager.getCurrentItem()) { switch (viewPager2.getCurrentItem()) {
case 0: case 0:
if (tab1 != null) { if (tab1 != null) {
mPostLayoutSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_FRONT_PAGE_POST, postLayout).apply(); mPostLayoutSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_FRONT_PAGE_POST, postLayout).apply();
@ -1361,17 +1467,21 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
tab3.changePostLayout(postLayout); tab3.changePostLayout(postLayout);
} }
} }
} }*/
} }
void goBackToTop() { void goBackToTop() {
if (viewPager.getCurrentItem() == 0) { PostFragment currentFragment = getCurrentFragment();
if (currentFragment != null) {
currentFragment.goBackToTop();
}
/*if (viewPager2.getCurrentItem() == 0) {
if (mAccessToken != null && tab1 != null) { if (mAccessToken != null && tab1 != null) {
tab1.goBackToTop(); tab1.goBackToTop();
} else if (tab2 != null) { } else if (tab2 != null) {
tab2.goBackToTop(); tab2.goBackToTop();
} }
} else if (viewPager.getCurrentItem() == 1) { } else if (viewPager2.getCurrentItem() == 1) {
if (mAccessToken != null && tab2 != null) { if (mAccessToken != null && tab2 != null) {
tab2.goBackToTop(); tab2.goBackToTop();
} else if (tab3 != null) { } else if (tab3 != null) {
@ -1379,11 +1489,16 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
} }
} else { } else {
tab3.goBackToTop(); tab3.goBackToTop();
} }*/
} }
void displaySortTypeInToolbar() { void displaySortTypeInToolbar() {
switch (viewPager.getCurrentItem()) { PostFragment currentFragment = getCurrentFragment();
if (currentFragment != null) {
SortType sortType = currentFragment.getSortType();
Utils.displaySortTypeInToolbar(sortType, toolbar);
}
/*switch (viewPager2.getCurrentItem()) {
case 0: case 0:
if (mAccessToken != null) { if (mAccessToken != null) {
if (tab1 != null) { if (tab1 != null) {
@ -1416,7 +1531,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
Utils.displaySortTypeInToolbar(sortType, toolbar); Utils.displaySortTypeInToolbar(sortType, toolbar);
} }
break; break;
} }*/
} }
} }
} }

View File

@ -34,8 +34,8 @@ public class DeleteAllPostLayoutsAsyncTask extends AsyncTask<Void, Void, Void> {
String key = entry.getKey(); String key = entry.getKey();
if (key.startsWith(SharedPreferencesUtils.POST_LAYOUT_SHARED_PREFERENCES_FILE) if (key.startsWith(SharedPreferencesUtils.POST_LAYOUT_SHARED_PREFERENCES_FILE)
|| key.startsWith(SharedPreferencesUtils.POST_LAYOUT_FRONT_PAGE_POST) || key.startsWith(SharedPreferencesUtils.POST_LAYOUT_FRONT_PAGE_POST)
|| key.startsWith(SharedPreferencesUtils.POST_LAYOUT_POPULAR_POST) || key.startsWith(SharedPreferencesUtils.POST_LAYOUT_POPULAR_POST_LEGACY)
|| key.startsWith(SharedPreferencesUtils.POST_LAYOUT_ALL_POST) || key.startsWith(SharedPreferencesUtils.POST_LAYOUT_ALL_POST_LEGACY)
|| key.startsWith(SharedPreferencesUtils.POST_LAYOUT_SUBREDDIT_POST_BASE) || key.startsWith(SharedPreferencesUtils.POST_LAYOUT_SUBREDDIT_POST_BASE)
|| key.startsWith(SharedPreferencesUtils.POST_LAYOUT_MULTI_REDDIT_POST_BASE) || key.startsWith(SharedPreferencesUtils.POST_LAYOUT_MULTI_REDDIT_POST_BASE)
|| key.startsWith(SharedPreferencesUtils.POST_LAYOUT_USER_POST_BASE) || key.startsWith(SharedPreferencesUtils.POST_LAYOUT_USER_POST_BASE)

View File

@ -450,15 +450,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
sortTime = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TIME_SUBREDDIT_POST_BASE + subredditName, SortType.Time.ALL.name()); sortTime = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TIME_SUBREDDIT_POST_BASE + subredditName, SortType.Time.ALL.name());
} }
boolean displaySubredditName = subredditName != null && (subredditName.equals("popular") || subredditName.equals("all")); boolean displaySubredditName = subredditName != null && (subredditName.equals("popular") || subredditName.equals("all"));
if(displaySubredditName) { postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_SUBREDDIT_POST_BASE + subredditName, defaultPostLayout);
if(subredditName.equals("popular")) {
postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_POPULAR_POST, defaultPostLayout);
} else {
postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_ALL_POST, defaultPostLayout);
}
} else {
postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_SUBREDDIT_POST_BASE + subredditName, defaultPostLayout);
}
if(sortTime != null) { if(sortTime != null) {
sortType = new SortType(SortType.Type.valueOf(sort), SortType.Time.valueOf(sortTime)); sortType = new SortType(SortType.Type.valueOf(sort), SortType.Time.valueOf(sortTime));
@ -974,6 +966,24 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
@Override @Override
public void changePostLayout(int postLayout) { public void changePostLayout(int postLayout) {
switch (postType) {
case PostDataSource.TYPE_FRONT_PAGE:
mPostLayoutSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_FRONT_PAGE_POST, postLayout).apply();
break;
case PostDataSource.TYPE_SUBREDDIT:
mPostLayoutSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_SUBREDDIT_POST_BASE + subredditName, postLayout).apply();
break;
case PostDataSource.TYPE_USER:
mPostLayoutSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_USER_POST_BASE + username, postLayout).apply();
break;
case PostDataSource.TYPE_SEARCH:
mPostLayoutSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_SEARCH_POST, postLayout).apply();
break;
case PostDataSource.TYPE_MULTI_REDDIT:
mPostLayoutSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_MULTI_REDDIT_POST_BASE + multiRedditPath, postLayout).apply();
break;
}
if (mAdapter != null) { if (mAdapter != null) {
mAdapter.setPostLayout(postLayout); mAdapter.setPostLayout(postLayout);
refreshAdapter(); refreshAdapter();

View File

@ -185,8 +185,13 @@ public class AdvancedPreferenceFragment extends PreferenceFragmentCompat {
sortTypeEditor.remove(SharedPreferencesUtils.SORT_TYPE_POPULAR_POST_LEGACY); sortTypeEditor.remove(SharedPreferencesUtils.SORT_TYPE_POPULAR_POST_LEGACY);
sortTypeEditor.remove(SharedPreferencesUtils.SORT_TIME_POPULAR_POST_LEGACY); sortTypeEditor.remove(SharedPreferencesUtils.SORT_TIME_POPULAR_POST_LEGACY);
SharedPreferences.Editor postLayoutEditor = mPostLayoutSharedPreferences.edit();
postLayoutEditor.remove(SharedPreferencesUtils.POST_LAYOUT_ALL_POST_LEGACY);
postLayoutEditor.remove(SharedPreferencesUtils.POST_LAYOUT_POPULAR_POST_LEGACY);
editor.apply(); editor.apply();
sortTypeEditor.apply(); sortTypeEditor.apply();
postLayoutEditor.apply();
Toast.makeText(activity, R.string.delete_all_legacy_settings_success, Toast.LENGTH_SHORT).show(); Toast.makeText(activity, R.string.delete_all_legacy_settings_success, Toast.LENGTH_SHORT).show();
}) })
.setNegativeButton(R.string.no, null) .setNegativeButton(R.string.no, null)

View File

@ -60,8 +60,6 @@ public class SharedPreferencesUtils {
public static final String POST_LAYOUT_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.post_layout"; public static final String POST_LAYOUT_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.post_layout";
public static final String POST_LAYOUT_FRONT_PAGE_POST = "post_layout_best_post"; public static final String POST_LAYOUT_FRONT_PAGE_POST = "post_layout_best_post";
public static final String POST_LAYOUT_POPULAR_POST = "post_layout_popular_post";
public static final String POST_LAYOUT_ALL_POST = "post_layout_all_post";
public static final String POST_LAYOUT_SUBREDDIT_POST_BASE = "post_layout_subreddit_post_"; public static final String POST_LAYOUT_SUBREDDIT_POST_BASE = "post_layout_subreddit_post_";
public static final String POST_LAYOUT_MULTI_REDDIT_POST_BASE = "post_layout_multi_reddit_post_"; public static final String POST_LAYOUT_MULTI_REDDIT_POST_BASE = "post_layout_multi_reddit_post_";
public static final String POST_LAYOUT_USER_POST_BASE = "post_layout_user_post_"; public static final String POST_LAYOUT_USER_POST_BASE = "post_layout_user_post_";
@ -161,4 +159,7 @@ public class SharedPreferencesUtils {
public static final String SORT_TIME_ALL_POST_LEGACY = "sort_time_all_post"; public static final String SORT_TIME_ALL_POST_LEGACY = "sort_time_all_post";
public static final String SORT_TYPE_POPULAR_POST_LEGACY = "sort_type_popular_post"; public static final String SORT_TYPE_POPULAR_POST_LEGACY = "sort_type_popular_post";
public static final String SORT_TIME_POPULAR_POST_LEGACY = "sort_time_popular_post"; public static final String SORT_TIME_POPULAR_POST_LEGACY = "sort_time_popular_post";
public static final String POST_LAYOUT_POPULAR_POST_LEGACY = "post_layout_popular_post";
public static final String POST_LAYOUT_ALL_POST_LEGACY = "post_layout_all_post";
} }

View File

@ -46,7 +46,19 @@
</com.google.android.material.appbar.AppBarLayout> </com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager <!--<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout_main_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:layout_scrollFlags="scroll|enterAlways"
app:tabGravity="fill"
app:tabIndicatorHeight="3dp"
app:tabMode="fixed"
app:tabRippleColor="?attr/colorControlHighlight"
app:tabUnboundedRipple="false" />-->
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager_main_activity" android:id="@+id/view_pager_main_activity"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"