Version 5.1.1. Fix anonymous subreddits and multireddits do not show in MainActivity TabLayout. Fix changing tab names not working in anonymous mode.

This commit is contained in:
Alex Ning 2021-11-22 20:35:14 +08:00
parent 841fec87dc
commit 1b39925cf8
3 changed files with 45 additions and 66 deletions

View File

@ -6,8 +6,8 @@ android {
applicationId "ml.docilealligator.infinityforreddit" applicationId "ml.docilealligator.infinityforreddit"
minSdkVersion 21 minSdkVersion 21
targetSdkVersion 30 targetSdkVersion 30
versionCode 84 versionCode 85
versionName "5.1.0" versionName "5.1.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
javaCompileOptions { javaCompileOptions {
annotationProcessorOptions { annotationProcessorOptions {

View File

@ -38,7 +38,6 @@ import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.adapter.FragmentStateAdapter; import androidx.viewpager2.adapter.FragmentStateAdapter;
@ -852,51 +851,37 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
tabLayout.setTabMode(TabLayout.MODE_FIXED); tabLayout.setTabMode(TabLayout.MODE_FIXED);
} }
new TabLayoutMediator(tabLayout, viewPager2, (tab, position) -> { new TabLayoutMediator(tabLayout, viewPager2, (tab, position) -> {
if (mAccessToken == null) { switch (position) {
switch (position) { case 0:
case 0: tab.setText(mMainActivityTabsSharedPreferences.getString((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_1_TITLE, getString(R.string.home)));
tab.setText(R.string.home); break;
break; case 1:
case 1: tab.setText(mMainActivityTabsSharedPreferences.getString((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_2_TITLE, getString(R.string.popular)));
tab.setText(R.string.popular); break;
break; case 2:
case 2: tab.setText(mMainActivityTabsSharedPreferences.getString((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_3_TITLE, getString(R.string.all)));
tab.setText(R.string.all); break;
break; }
} if (position >= tabCount && (mShowFavoriteMultiReddits || mShowMultiReddits ||
} else { mShowFavoriteSubscribedSubreddits || mShowSubscribedSubreddits)
switch (position) { && sectionsPagerAdapter != null) {
case 0: if (position - tabCount < sectionsPagerAdapter.favoriteMultiReddits.size()) {
tab.setText(mMainActivityTabsSharedPreferences.getString((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_1_TITLE, getString(R.string.home))); tab.setText(sectionsPagerAdapter.favoriteMultiReddits.get(position - tabCount).getName());
break; } else if (position - tabCount - sectionsPagerAdapter.favoriteMultiReddits.size() < sectionsPagerAdapter.multiReddits.size()) {
case 1: tab.setText(sectionsPagerAdapter.multiReddits.get(position - tabCount
tab.setText(mMainActivityTabsSharedPreferences.getString((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_2_TITLE, getString(R.string.popular))); - sectionsPagerAdapter.favoriteMultiReddits.size()).getName());
break; } else if (position - tabCount - sectionsPagerAdapter.favoriteMultiReddits.size()
case 2: - sectionsPagerAdapter.multiReddits.size() < sectionsPagerAdapter.favoriteSubscribedSubreddits.size()) {
tab.setText(mMainActivityTabsSharedPreferences.getString((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_3_TITLE, getString(R.string.all))); tab.setText(sectionsPagerAdapter.favoriteSubscribedSubreddits.get(position - tabCount
break; - sectionsPagerAdapter.favoriteMultiReddits.size()
} - sectionsPagerAdapter.multiReddits.size()).getName());
if (position >= tabCount && (mShowFavoriteMultiReddits || mShowMultiReddits || } else if (position - tabCount - sectionsPagerAdapter.favoriteMultiReddits.size()
mShowFavoriteSubscribedSubreddits || mShowSubscribedSubreddits) - sectionsPagerAdapter.multiReddits.size()
&& sectionsPagerAdapter != null) { - sectionsPagerAdapter.favoriteSubscribedSubreddits.size() < sectionsPagerAdapter.subscribedSubreddits.size()) {
if (position - tabCount < sectionsPagerAdapter.favoriteMultiReddits.size()) { tab.setText(sectionsPagerAdapter.subscribedSubreddits.get(position - tabCount
tab.setText(sectionsPagerAdapter.favoriteMultiReddits.get(position - tabCount).getName()); - sectionsPagerAdapter.favoriteMultiReddits.size()
} else if (position - tabCount - sectionsPagerAdapter.favoriteMultiReddits.size() < sectionsPagerAdapter.multiReddits.size()) {
tab.setText(sectionsPagerAdapter.multiReddits.get(position - tabCount
- sectionsPagerAdapter.favoriteMultiReddits.size()).getName());
} else if (position - tabCount - sectionsPagerAdapter.favoriteMultiReddits.size()
- sectionsPagerAdapter.multiReddits.size() < sectionsPagerAdapter.favoriteSubscribedSubreddits.size()) {
tab.setText(sectionsPagerAdapter.favoriteSubscribedSubreddits.get(position - tabCount
- sectionsPagerAdapter.favoriteMultiReddits.size()
- sectionsPagerAdapter.multiReddits.size()).getName());
} else if (position - tabCount - sectionsPagerAdapter.favoriteMultiReddits.size()
- sectionsPagerAdapter.multiReddits.size() - sectionsPagerAdapter.multiReddits.size()
- sectionsPagerAdapter.favoriteSubscribedSubreddits.size() < sectionsPagerAdapter.subscribedSubreddits.size()) { - sectionsPagerAdapter.favoriteSubscribedSubreddits.size()).getName());
tab.setText(sectionsPagerAdapter.subscribedSubreddits.get(position - tabCount
- sectionsPagerAdapter.favoriteMultiReddits.size()
- sectionsPagerAdapter.multiReddits.size()
- sectionsPagerAdapter.favoriteSubscribedSubreddits.size()).getName());
}
} }
} }
}).attach(); }).attach();
@ -922,7 +907,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
loadSubscriptions(); loadSubscriptions();
multiRedditViewModel = new ViewModelProvider(this, new MultiRedditViewModel.Factory(getApplication(), multiRedditViewModel = new ViewModelProvider(this, new MultiRedditViewModel.Factory(getApplication(),
mRedditDataRoomDatabase, mAccountName)) mRedditDataRoomDatabase, mAccountName == null ? "-" : mAccountName))
.get(MultiRedditViewModel.class); .get(MultiRedditViewModel.class);
multiRedditViewModel.getAllFavoriteMultiReddits().observe(this, multiReddits -> { multiRedditViewModel.getAllFavoriteMultiReddits().observe(this, multiReddits -> {
@ -931,17 +916,14 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
} }
}); });
multiRedditViewModel.getAllMultiReddits().observe(this, new Observer<List<MultiReddit>>() { multiRedditViewModel.getAllMultiReddits().observe(this, multiReddits -> {
@Override if (mShowMultiReddits && sectionsPagerAdapter != null) {
public void onChanged(List<MultiReddit> multiReddits) { sectionsPagerAdapter.setMultiReddits(multiReddits);
if (mShowMultiReddits && sectionsPagerAdapter != null) {
sectionsPagerAdapter.setMultiReddits(multiReddits);
}
} }
}); });
subscribedSubredditViewModel = new ViewModelProvider(this, subscribedSubredditViewModel = new ViewModelProvider(this,
new SubscribedSubredditViewModel.Factory(getApplication(), mRedditDataRoomDatabase, mAccountName)) new SubscribedSubredditViewModel.Factory(getApplication(), mRedditDataRoomDatabase, mAccountName == null ? "-" : mAccountName))
.get(SubscribedSubredditViewModel.class); .get(SubscribedSubredditViewModel.class);
subscribedSubredditViewModel.getAllSubscribedSubreddits().observe(this, subscribedSubredditViewModel.getAllSubscribedSubreddits().observe(this,
subscribedSubredditData -> { subscribedSubredditData -> {
@ -1693,9 +1675,6 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
@Override @Override
public int getItemCount() { public int getItemCount() {
if (mAccessToken == null) {
return 3;
}
return tabCount + favoriteMultiReddits.size() + multiReddits.size() + return tabCount + favoriteMultiReddits.size() + multiReddits.size() +
favoriteSubscribedSubreddits.size() + subscribedSubreddits.size(); favoriteSubscribedSubreddits.size() + subscribedSubreddits.size();
} }

View File

@ -492,26 +492,26 @@ public class CustomizeMainPageTabsFragment extends Fragment {
tab3AddImageView.setOnClickListener(view -> selectName(2)); tab3AddImageView.setOnClickListener(view -> selectName(2));
showMultiredditsSwitchMaterial.setChecked(mainActivityTabsSharedPreferences.getBoolean(accountName + SharedPreferencesUtils.MAIN_PAGE_SHOW_MULTIREDDITS, false)); showMultiredditsSwitchMaterial.setChecked(mainActivityTabsSharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_MULTIREDDITS, false));
showMultiredditsSwitchMaterial.setOnCheckedChangeListener((compoundButton, b) -> mainActivityTabsSharedPreferences.edit().putBoolean(accountName + SharedPreferencesUtils.MAIN_PAGE_SHOW_MULTIREDDITS, b).apply()); showMultiredditsSwitchMaterial.setOnCheckedChangeListener((compoundButton, b) -> mainActivityTabsSharedPreferences.edit().putBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_MULTIREDDITS, b).apply());
showMultiredditsLinearLayout.setOnClickListener(view -> { showMultiredditsLinearLayout.setOnClickListener(view -> {
showMultiredditsSwitchMaterial.performClick(); showMultiredditsSwitchMaterial.performClick();
}); });
showFavoriteMultiredditsSwitchMaterial.setChecked(mainActivityTabsSharedPreferences.getBoolean(accountName + SharedPreferencesUtils.MAIN_PAGE_SHOW_FAVORITE_MULTIREDDITS, false)); showFavoriteMultiredditsSwitchMaterial.setChecked(mainActivityTabsSharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_FAVORITE_MULTIREDDITS, false));
showFavoriteMultiredditsSwitchMaterial.setOnCheckedChangeListener((compoundButton, b) -> mainActivityTabsSharedPreferences.edit().putBoolean(accountName + SharedPreferencesUtils.MAIN_PAGE_SHOW_FAVORITE_MULTIREDDITS, b).apply()); showFavoriteMultiredditsSwitchMaterial.setOnCheckedChangeListener((compoundButton, b) -> mainActivityTabsSharedPreferences.edit().putBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_FAVORITE_MULTIREDDITS, b).apply());
showFavoriteMultiredditsLinearLayout.setOnClickListener(view -> { showFavoriteMultiredditsLinearLayout.setOnClickListener(view -> {
showFavoriteMultiredditsSwitchMaterial.performClick(); showFavoriteMultiredditsSwitchMaterial.performClick();
}); });
showSubscribedSubredditsSwitchMaterial.setChecked(mainActivityTabsSharedPreferences.getBoolean(accountName + SharedPreferencesUtils.MAIN_PAGE_SHOW_SUBSCRIBED_SUBREDDITS, false)); showSubscribedSubredditsSwitchMaterial.setChecked(mainActivityTabsSharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_SUBSCRIBED_SUBREDDITS, false));
showSubscribedSubredditsSwitchMaterial.setOnCheckedChangeListener((compoundButton, b) -> mainActivityTabsSharedPreferences.edit().putBoolean(accountName + SharedPreferencesUtils.MAIN_PAGE_SHOW_SUBSCRIBED_SUBREDDITS, b).apply()); showSubscribedSubredditsSwitchMaterial.setOnCheckedChangeListener((compoundButton, b) -> mainActivityTabsSharedPreferences.edit().putBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_SUBSCRIBED_SUBREDDITS, b).apply());
showSubscribedSubredditsLinearLayout.setOnClickListener(view -> { showSubscribedSubredditsLinearLayout.setOnClickListener(view -> {
showSubscribedSubredditsSwitchMaterial.performClick(); showSubscribedSubredditsSwitchMaterial.performClick();
}); });
showFavoriteSubscribedSubredditsSwitchMaterial.setChecked(mainActivityTabsSharedPreferences.getBoolean(accountName + SharedPreferencesUtils.MAIN_PAGE_SHOW_FAVORITE_SUBSCRIBED_SUBREDDITS, false)); showFavoriteSubscribedSubredditsSwitchMaterial.setChecked(mainActivityTabsSharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_FAVORITE_SUBSCRIBED_SUBREDDITS, false));
showFavoriteSubscribedSubredditsSwitchMaterial.setOnCheckedChangeListener((compoundButton, b) -> mainActivityTabsSharedPreferences.edit().putBoolean(accountName + SharedPreferencesUtils.MAIN_PAGE_SHOW_FAVORITE_SUBSCRIBED_SUBREDDITS, b).apply()); showFavoriteSubscribedSubredditsSwitchMaterial.setOnCheckedChangeListener((compoundButton, b) -> mainActivityTabsSharedPreferences.edit().putBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_FAVORITE_SUBSCRIBED_SUBREDDITS, b).apply());
showFavoriteSubscribedSubredditsLinearLayout.setOnClickListener(view -> { showFavoriteSubscribedSubredditsLinearLayout.setOnClickListener(view -> {
showFavoriteSubscribedSubredditsSwitchMaterial.performClick(); showFavoriteSubscribedSubredditsSwitchMaterial.performClick();
}); });