Support showing subscribed subreddits in the main page.

This commit is contained in:
Alex Ning 2020-09-24 15:20:11 +08:00
parent 396501e350
commit cf1d14264d
7 changed files with 127 additions and 18 deletions

View File

@ -52,6 +52,7 @@ import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
@ -205,6 +206,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
private boolean mConfirmToExit;
private boolean mLockBottomAppBar;
private boolean mDisableSwipingBetweenTabs;
private boolean mShowSubscribedSubreddits;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -599,10 +601,16 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
navDrawerRecyclerView.setLayoutManager(new LinearLayoutManager(this));
navDrawerRecyclerView.setAdapter(adapter);
sectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager, getLifecycle());
mShowSubscribedSubreddits = mMainActivityTabsSharedPreferences.getBoolean((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_SUBSCRIBED_SUBREDDITS, false);
sectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager, getLifecycle(), mShowSubscribedSubreddits);
viewPager2.setAdapter(sectionsPagerAdapter);
viewPager2.setOffscreenPageLimit(3);
viewPager2.setOffscreenPageLimit(1);
viewPager2.setUserInputEnabled(!mDisableSwipingBetweenTabs);
if (mShowSubscribedSubreddits) {
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
} else {
tabLayout.setTabMode(TabLayout.MODE_FIXED);
}
new TabLayoutMediator(tabLayout, viewPager2, (tab, position) -> {
if (mAccessToken == null) {
switch (position) {
@ -625,6 +633,12 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
tab.setText(mMainActivityTabsSharedPreferences.getString((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_3_TITLE, getString(R.string.all)));
break;
}
if (position >= 3 && mShowSubscribedSubreddits && sectionsPagerAdapter != null) {
List<SubscribedSubredditData> subscribedSubreddits = sectionsPagerAdapter.subscribedSubreddits;
if (position - 3 < subscribedSubreddits.size()) {
tab.setText(subscribedSubreddits.get(position - 3).getName());
}
}
}
}).attach();
@ -647,7 +661,12 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
new SubscribedSubredditViewModel.Factory(getApplication(), mRedditDataRoomDatabase, mAccountName))
.get(SubscribedSubredditViewModel.class);
subscribedSubredditViewModel.getAllSubscribedSubreddits().observe(this,
subscribedSubredditData -> adapter.setSubscribedSubreddits(subscribedSubredditData));
subscribedSubredditData -> {
adapter.setSubscribedSubreddits(subscribedSubredditData);
if (mShowSubscribedSubreddits && sectionsPagerAdapter != null) {
sectionsPagerAdapter.setSubscribedSubreddits(subscribedSubredditData);
}
});
accountViewModel = new ViewModelProvider(this,
new AccountViewModel.Factory(getApplication(), mRedditDataRoomDatabase, mAccountName)).get(AccountViewModel.class);
@ -978,8 +997,13 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
}
private class SectionsPagerAdapter extends FragmentStateAdapter {
SectionsPagerAdapter(FragmentManager fm, Lifecycle lifecycle) {
boolean showSubscribedSubreddits;
List<SubscribedSubredditData> subscribedSubreddits;
SectionsPagerAdapter(FragmentManager fm, Lifecycle lifecycle, boolean showSubscribedSubreddits) {
super(fm, lifecycle);
subscribedSubreddits = new ArrayList<>();
this.showSubscribedSubreddits = showSubscribedSubreddits;
}
@NonNull
@ -1018,14 +1042,25 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
String name = mMainActivityTabsSharedPreferences.getString((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_2_NAME, "");
return generatePostFragment(postType, name);
} else {
if (showSubscribedSubreddits) {
if (position > 2 && position - 3 < subscribedSubreddits.size()) {
int postType = SharedPreferencesUtils.MAIN_PAGE_TAB_POST_TYPE_SUBREDDIT;
String name = subscribedSubreddits.get(position - 3).getName();
return generatePostFragment(postType, name);
}
}
int postType = mMainActivityTabsSharedPreferences.getInt((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_3_POST_TYPE, SharedPreferencesUtils.MAIN_PAGE_TAB_POST_TYPE_ALL);
String name = mMainActivityTabsSharedPreferences.getString((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_3_NAME, "");
return generatePostFragment(postType, name);
}
}
private Fragment generatePostFragment(int postType, String name) {
public void setSubscribedSubreddits(List<SubscribedSubredditData> subscribedSubreddits) {
this.subscribedSubreddits = subscribedSubreddits;
notifyDataSetChanged();
}
private Fragment generatePostFragment(int postType, String name) {
if (postType == SharedPreferencesUtils.MAIN_PAGE_TAB_POST_TYPE_HOME) {
PostFragment fragment = new PostFragment();
Bundle bundle = new Bundle();
@ -1094,6 +1129,9 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
if (mAccessToken == null) {
return 2;
}
if (showSubscribedSubreddits) {
return 3 + subscribedSubreddits.size();
}
return 3;
}

View File

@ -16,6 +16,7 @@ import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.switchmaterial.SwitchMaterial;
import javax.inject.Inject;
import javax.inject.Named;
@ -86,6 +87,14 @@ public class CustomizeMainPageTabsFragment extends Fragment {
TextView tab3NameTitleTextView;
@BindView(R.id.tab_3_name_summary_text_view_customize_main_page_tabs_fragment)
TextView tab3NameSummaryTextView;
@BindView(R.id.divider_4_customize_main_page_tabs_fragment)
View divider4;
@BindView(R.id.more_tabs_group_summary_customize_main_page_tabs_fragment)
TextView moreTabsGroupSummaryTextView;
@BindView(R.id.show_subscribed_subreddits_linear_layout_customize_main_page_tabs_fragment)
LinearLayout showSubscribedSubredditsLinearLayout;
@BindView(R.id.show_subscribed_subreddits_switch_material_customize_main_page_tabs_fragment)
SwitchMaterial showSubscribedSubredditsSwitchMaterial;
@Inject
@Named("main_activity_tabs")
SharedPreferences sharedPreferences;
@ -130,6 +139,10 @@ public class CustomizeMainPageTabsFragment extends Fragment {
tab3GroupSummaryTextView.setVisibility(View.GONE);
tab3TitleLinearLayout.setVisibility(View.GONE);
tab3TypeLinearLayout.setVisibility(View.GONE);
divider4.setVisibility(View.GONE);
moreTabsGroupSummaryTextView.setVisibility(View.GONE);
showSubscribedSubredditsLinearLayout.setVisibility(View.GONE);
showSubscribedSubredditsSwitchMaterial.setVisibility(View.GONE);
return rootView;
}
@ -433,6 +446,13 @@ public class CustomizeMainPageTabsFragment extends Fragment {
.show();
});
showSubscribedSubredditsSwitchMaterial.setChecked(sharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_SUBSCRIBED_SUBREDDITS, false));
showSubscribedSubredditsLinearLayout.setOnClickListener(view -> {
showSubscribedSubredditsSwitchMaterial.performClick();
});
showSubscribedSubredditsSwitchMaterial.setOnCheckedChangeListener((compoundButton, b) -> sharedPreferences.edit().putBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_SUBSCRIBED_SUBREDDITS, b).apply());
return rootView;
}

View File

@ -146,6 +146,7 @@ public class SharedPreferencesUtils {
public static final int MAIN_PAGE_TAB_POST_TYPE_SUBREDDIT = 3;
public static final int MAIN_PAGE_TAB_POST_TYPE_MULTIREDDIT = 4;
public static final int MAIN_PAGE_TAB_POST_TYPE_USER = 5;
public static final String MAIN_PAGE_SHOW_SUBSCRIBED_SUBREDDITS = "_main_page_show_subscribed_subreddits";
public static final String NSFW_AND_SPOILER_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.nsfw_and_spoiler";
public static final String NSFW_BASE = "_nsfw";

View File

@ -40,7 +40,6 @@
app:layout_scrollFlags="scroll|enterAlways"
app:tabGravity="fill"
app:tabIndicatorHeight="3dp"
app:tabMode="fixed"
app:tabRippleColor="?attr/colorControlHighlight"
app:tabUnboundedRipple="false" />

View File

@ -60,7 +60,7 @@
android:layout_height="wrap_content"
android:text="@string/settings_tab_title"
android:textColor="@color/primaryTextColor"
android:textSize="?attr/font_18"
android:textSize="?attr/font_16"
android:fontFamily="?attr/font_family" />
<TextView
@ -91,7 +91,7 @@
android:layout_height="wrap_content"
android:text="@string/settings_tab_post_type"
android:textColor="@color/primaryTextColor"
android:textSize="?attr/font_18"
android:textSize="?attr/font_16"
android:fontFamily="?attr/font_family" />
<TextView
@ -123,7 +123,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/primaryTextColor"
android:textSize="?attr/font_18"
android:textSize="?attr/font_16"
android:fontFamily="?attr/font_family" />
<TextView
@ -172,7 +172,7 @@
android:layout_height="wrap_content"
android:text="@string/settings_tab_title"
android:textColor="@color/primaryTextColor"
android:textSize="?attr/font_18"
android:textSize="?attr/font_16"
android:fontFamily="?attr/font_family" />
<TextView
@ -203,7 +203,7 @@
android:layout_height="wrap_content"
android:text="@string/settings_tab_post_type"
android:textColor="@color/primaryTextColor"
android:textSize="?attr/font_18"
android:textSize="?attr/font_16"
android:fontFamily="?attr/font_family" />
<TextView
@ -235,7 +235,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/primaryTextColor"
android:textSize="?attr/font_18"
android:textSize="?attr/font_16"
android:fontFamily="?attr/font_family" />
<TextView
@ -284,7 +284,7 @@
android:layout_height="wrap_content"
android:text="@string/settings_tab_title"
android:textColor="@color/primaryTextColor"
android:textSize="?attr/font_18"
android:textSize="?attr/font_16"
android:fontFamily="?attr/font_family" />
<TextView
@ -315,7 +315,7 @@
android:layout_height="wrap_content"
android:text="@string/settings_tab_post_type"
android:textColor="@color/primaryTextColor"
android:textSize="?attr/font_18"
android:textSize="?attr/font_16"
android:fontFamily="?attr/font_family" />
<TextView
@ -347,7 +347,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/primaryTextColor"
android:textSize="?attr/font_18"
android:textSize="?attr/font_16"
android:fontFamily="?attr/font_family" />
<TextView
@ -360,6 +360,55 @@
</LinearLayout>
<View
android:id="@+id/divider_4_customize_main_page_tabs_fragment"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/dividerColor" />
<TextView
android:id="@+id/more_tabs_group_summary_customize_main_page_tabs_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:paddingStart="72dp"
android:paddingEnd="16dp"
android:text="@string/settings_more_tabs_summary"
android:textColor="@color/colorAccent"
android:textSize="?attr/font_default"
android:fontFamily="?attr/font_family" />
<LinearLayout
android:id="@+id/show_subscribed_subreddits_linear_layout_customize_main_page_tabs_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:paddingStart="72dp"
android:paddingEnd="16dp"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginEnd="16dp"
android:layout_gravity="center_vertical"
android:text="@string/settings_more_tabs_show_subscribed_subreddits_title"
android:textColor="?attr/primaryTextColor"
android:textSize="?attr/font_16"
android:fontFamily="?attr/font_family" />
<com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/show_subscribed_subreddits_switch_material_customize_main_page_tabs_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
</LinearLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>

View File

@ -34,7 +34,7 @@
android:drawablePadding="32dp"
android:text="@string/settings_enable_nsfw_title"
android:textColor="?attr/primaryTextColor"
android:textSize="?attr/font_default"
android:textSize="?attr/font_16"
android:fontFamily="?attr/font_family" />
<com.google.android.material.switchmaterial.SwitchMaterial
@ -66,7 +66,7 @@
android:layout_gravity="center_vertical"
android:text="@string/settings_blur_nsfw_title"
android:textColor="?attr/primaryTextColor"
android:textSize="?attr/font_default"
android:textSize="?attr/font_16"
android:fontFamily="?attr/font_family" />
<com.google.android.material.switchmaterial.SwitchMaterial
@ -97,7 +97,7 @@
android:layout_gravity="center_vertical"
android:text="@string/settings_blur_spoiler_title"
android:textColor="?attr/primaryTextColor"
android:textSize="?attr/font_default"
android:textSize="?attr/font_16"
android:fontFamily="?attr/font_family" />
<com.google.android.material.switchmaterial.SwitchMaterial

View File

@ -450,6 +450,8 @@
<string name="settings_tab_1_summary">Tab 1</string>
<string name="settings_tab_2_summary">Tab 2</string>
<string name="settings_tab_3_summary">Tab 3</string>
<string name="settings_more_tabs_summary">More Tabs</string>
<string name="settings_more_tabs_show_subscribed_subreddits_title">Show Subscribed Subreddits</string>
<string name="settings_tab_title">Title</string>
<string name="settings_tab_post_type">Type</string>
<string name="settings_tab_subreddit_name">Subreddit Name (Without r/ prefix)</string>