mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2024-11-10 12:47:26 +01:00
Migrate to ViewPager2 in ViewSubredditDetailActivity and ViewUserDetailActivity.
This commit is contained in:
parent
8d2769d45a
commit
4f40f73c98
@ -270,6 +270,8 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
|
|||||||
mLockBottomAppBar = mSharedPreferences.getBoolean(SharedPreferencesUtils.LOCK_BOTTOM_APP_BAR, false);
|
mLockBottomAppBar = mSharedPreferences.getBoolean(SharedPreferencesUtils.LOCK_BOTTOM_APP_BAR, false);
|
||||||
mDisableSwipingBetweenTabs = mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_SWIPING_BETWEEN_TABS, false);
|
mDisableSwipingBetweenTabs = mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_SWIPING_BETWEEN_TABS, false);
|
||||||
|
|
||||||
|
fragmentManager = getSupportFragmentManager();
|
||||||
|
|
||||||
if (savedInstanceState != null) {
|
if (savedInstanceState != null) {
|
||||||
mFetchUserInfoSuccess = savedInstanceState.getBoolean(FETCH_USER_INFO_STATE);
|
mFetchUserInfoSuccess = savedInstanceState.getBoolean(FETCH_USER_INFO_STATE);
|
||||||
mFetchSubscriptionsSuccess = savedInstanceState.getBoolean(FETCH_SUBSCRIPTIONS_STATE);
|
mFetchSubscriptionsSuccess = savedInstanceState.getBoolean(FETCH_SUBSCRIPTIONS_STATE);
|
||||||
@ -585,7 +587,6 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
|
|||||||
navDrawerRecyclerView.setLayoutManager(new LinearLayoutManager(this));
|
navDrawerRecyclerView.setLayoutManager(new LinearLayoutManager(this));
|
||||||
navDrawerRecyclerView.setAdapter(adapter);
|
navDrawerRecyclerView.setAdapter(adapter);
|
||||||
|
|
||||||
fragmentManager = getSupportFragmentManager();
|
|
||||||
sectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager, getLifecycle());
|
sectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager, getLifecycle());
|
||||||
viewPager2.setAdapter(sectionsPagerAdapter);
|
viewPager2.setAdapter(sectionsPagerAdapter);
|
||||||
viewPager2.setOffscreenPageLimit(3);
|
viewPager2.setOffscreenPageLimit(3);
|
||||||
@ -614,7 +615,6 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).attach();
|
}).attach();
|
||||||
//tabLayout.setupWithViewPager(viewPager2);
|
|
||||||
|
|
||||||
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
|
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
|
||||||
@Override
|
@Override
|
||||||
@ -992,7 +992,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class SectionsPagerAdapter extends FragmentStateAdapter{
|
private class SectionsPagerAdapter extends FragmentStateAdapter {
|
||||||
/*private PostFragment tab1;
|
/*private PostFragment tab1;
|
||||||
private PostFragment tab2;
|
private PostFragment tab2;
|
||||||
private PostFragment tab3;*/
|
private PostFragment tab3;*/
|
||||||
|
@ -10,7 +10,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,13 +18,15 @@ import android.widget.TextView;
|
|||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
import androidx.appcompat.widget.Toolbar;
|
import androidx.appcompat.widget.Toolbar;
|
||||||
import androidx.coordinatorlayout.widget.CoordinatorLayout;
|
import androidx.coordinatorlayout.widget.CoordinatorLayout;
|
||||||
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.viewpager.widget.ViewPager;
|
import androidx.viewpager2.adapter.FragmentStateAdapter;
|
||||||
|
import androidx.viewpager2.widget.ViewPager2;
|
||||||
|
|
||||||
import com.bumptech.glide.Glide;
|
import com.bumptech.glide.Glide;
|
||||||
import com.bumptech.glide.RequestManager;
|
import com.bumptech.glide.RequestManager;
|
||||||
@ -37,6 +38,7 @@ import com.google.android.material.chip.Chip;
|
|||||||
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
||||||
import com.google.android.material.snackbar.Snackbar;
|
import com.google.android.material.snackbar.Snackbar;
|
||||||
import com.google.android.material.tabs.TabLayout;
|
import com.google.android.material.tabs.TabLayout;
|
||||||
|
import com.google.android.material.tabs.TabLayoutMediator;
|
||||||
import com.r0adkll.slidr.Slidr;
|
import com.r0adkll.slidr.Slidr;
|
||||||
import com.r0adkll.slidr.model.SlidrInterface;
|
import com.r0adkll.slidr.model.SlidrInterface;
|
||||||
|
|
||||||
@ -66,20 +68,20 @@ import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper;
|
|||||||
import ml.docilealligator.infinityforreddit.Event.ChangeNSFWEvent;
|
import ml.docilealligator.infinityforreddit.Event.ChangeNSFWEvent;
|
||||||
import ml.docilealligator.infinityforreddit.Event.GoBackToMainPageEvent;
|
import ml.docilealligator.infinityforreddit.Event.GoBackToMainPageEvent;
|
||||||
import ml.docilealligator.infinityforreddit.Event.SwitchAccountEvent;
|
import ml.docilealligator.infinityforreddit.Event.SwitchAccountEvent;
|
||||||
import ml.docilealligator.infinityforreddit.Subreddit.FetchSubredditData;
|
|
||||||
import ml.docilealligator.infinityforreddit.Fragment.PostFragment;
|
import ml.docilealligator.infinityforreddit.Fragment.PostFragment;
|
||||||
import ml.docilealligator.infinityforreddit.Fragment.SidebarFragment;
|
import ml.docilealligator.infinityforreddit.Fragment.SidebarFragment;
|
||||||
import ml.docilealligator.infinityforreddit.FragmentCommunicator;
|
import ml.docilealligator.infinityforreddit.FragmentCommunicator;
|
||||||
import ml.docilealligator.infinityforreddit.Infinity;
|
import ml.docilealligator.infinityforreddit.Infinity;
|
||||||
|
import ml.docilealligator.infinityforreddit.Message.ReadMessage;
|
||||||
import ml.docilealligator.infinityforreddit.Post.PostDataSource;
|
import ml.docilealligator.infinityforreddit.Post.PostDataSource;
|
||||||
import ml.docilealligator.infinityforreddit.R;
|
import ml.docilealligator.infinityforreddit.R;
|
||||||
import ml.docilealligator.infinityforreddit.Message.ReadMessage;
|
|
||||||
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
|
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
|
||||||
import ml.docilealligator.infinityforreddit.SortType;
|
import ml.docilealligator.infinityforreddit.SortType;
|
||||||
import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback;
|
import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback;
|
||||||
|
import ml.docilealligator.infinityforreddit.Subreddit.FetchSubredditData;
|
||||||
import ml.docilealligator.infinityforreddit.Subreddit.SubredditData;
|
import ml.docilealligator.infinityforreddit.Subreddit.SubredditData;
|
||||||
import ml.docilealligator.infinityforreddit.Subreddit.SubredditViewModel;
|
|
||||||
import ml.docilealligator.infinityforreddit.Subreddit.SubredditSubscription;
|
import ml.docilealligator.infinityforreddit.Subreddit.SubredditSubscription;
|
||||||
|
import ml.docilealligator.infinityforreddit.Subreddit.SubredditViewModel;
|
||||||
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
|
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
|
||||||
import ml.docilealligator.infinityforreddit.Utils.Utils;
|
import ml.docilealligator.infinityforreddit.Utils.Utils;
|
||||||
import pl.droidsonroids.gif.GifImageView;
|
import pl.droidsonroids.gif.GifImageView;
|
||||||
@ -106,7 +108,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
|
|||||||
@BindView(R.id.coordinator_layout_view_subreddit_detail_activity)
|
@BindView(R.id.coordinator_layout_view_subreddit_detail_activity)
|
||||||
CoordinatorLayout coordinatorLayout;
|
CoordinatorLayout coordinatorLayout;
|
||||||
@BindView(R.id.view_pager_view_subreddit_detail_activity)
|
@BindView(R.id.view_pager_view_subreddit_detail_activity)
|
||||||
ViewPager viewPager;
|
ViewPager2 viewPager2;
|
||||||
@BindView(R.id.appbar_layout_view_subreddit_detail_activity)
|
@BindView(R.id.appbar_layout_view_subreddit_detail_activity)
|
||||||
AppBarLayout appBarLayout;
|
AppBarLayout appBarLayout;
|
||||||
@BindView(R.id.collapsing_toolbar_layout_view_subreddit_detail_activity)
|
@BindView(R.id.collapsing_toolbar_layout_view_subreddit_detail_activity)
|
||||||
@ -169,6 +171,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
|
|||||||
@Inject
|
@Inject
|
||||||
CustomThemeWrapper mCustomThemeWrapper;
|
CustomThemeWrapper mCustomThemeWrapper;
|
||||||
public SubredditViewModel mSubredditViewModel;
|
public SubredditViewModel mSubredditViewModel;
|
||||||
|
private FragmentManager fragmentManager;
|
||||||
private SectionsPagerAdapter sectionsPagerAdapter;
|
private SectionsPagerAdapter sectionsPagerAdapter;
|
||||||
private boolean mNullAccessToken = false;
|
private boolean mNullAccessToken = false;
|
||||||
private String mAccessToken;
|
private String mAccessToken;
|
||||||
@ -300,6 +303,8 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
|
|||||||
|
|
||||||
subredditName = getIntent().getStringExtra(EXTRA_SUBREDDIT_NAME_KEY);
|
subredditName = getIntent().getStringExtra(EXTRA_SUBREDDIT_NAME_KEY);
|
||||||
|
|
||||||
|
fragmentManager = getSupportFragmentManager();
|
||||||
|
|
||||||
if (savedInstanceState == null) {
|
if (savedInstanceState == null) {
|
||||||
mMessageFullname = getIntent().getStringExtra(EXTRA_MESSAGE_FULLNAME);
|
mMessageFullname = getIntent().getStringExtra(EXTRA_MESSAGE_FULLNAME);
|
||||||
mNewAccountName = getIntent().getStringExtra(EXTRA_NEW_ACCOUNT_NAME);
|
mNewAccountName = getIntent().getStringExtra(EXTRA_NEW_ACCOUNT_NAME);
|
||||||
@ -639,8 +644,8 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
|
|||||||
}
|
}
|
||||||
}).execute();
|
}).execute();
|
||||||
|
|
||||||
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
|
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), getLifecycle());
|
||||||
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
|
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onPageSelected(int position) {
|
public void onPageSelected(int position) {
|
||||||
if (position == 0) {
|
if (position == 0) {
|
||||||
@ -648,38 +653,33 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
|
|||||||
} else {
|
} else {
|
||||||
lockSwipeRightToGoBack();
|
lockSwipeRightToGoBack();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
viewPager.setAdapter(sectionsPagerAdapter);
|
|
||||||
viewPager.setOffscreenPageLimit(2);
|
|
||||||
tabLayout.setupWithViewPager(viewPager);
|
|
||||||
|
|
||||||
|
|
||||||
boolean viewSidebar = getIntent().getBooleanExtra(EXTRA_VIEW_SIDEBAR, false);
|
|
||||||
if (viewSidebar) {
|
|
||||||
viewPager.setCurrentItem(1, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
|
|
||||||
@Override
|
|
||||||
public void onPageSelected(int position) {
|
|
||||||
if (mAccessToken != null) {
|
if (mAccessToken != null) {
|
||||||
if (showBottomAppBar) {
|
if (showBottomAppBar) {
|
||||||
bottomNavigationView.performShow();
|
bottomNavigationView.performShow();
|
||||||
}
|
}
|
||||||
fab.show();
|
fab.show();
|
||||||
}
|
}
|
||||||
if (isInLazyMode) {
|
|
||||||
if (viewPager.getCurrentItem() == 0) {
|
|
||||||
sectionsPagerAdapter.resumeLazyMode();
|
|
||||||
} else {
|
|
||||||
sectionsPagerAdapter.pauseLazyMode();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sectionsPagerAdapter.displaySortTypeInToolbar();
|
sectionsPagerAdapter.displaySortTypeInToolbar();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
viewPager2.setAdapter(sectionsPagerAdapter);
|
||||||
|
viewPager2.setOffscreenPageLimit(2);
|
||||||
|
viewPager2.setUserInputEnabled(!mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_SWIPING_BETWEEN_TABS, false));
|
||||||
|
new TabLayoutMediator(tabLayout, viewPager2, (tab, position) -> {
|
||||||
|
switch (position) {
|
||||||
|
case 0:
|
||||||
|
tab.setText("Posts");
|
||||||
|
case 1:
|
||||||
|
tab.setText("Sidebar");
|
||||||
|
}
|
||||||
|
}).attach();
|
||||||
|
|
||||||
|
boolean viewSidebar = getIntent().getBooleanExtra(EXTRA_VIEW_SIDEBAR, false);
|
||||||
|
if (viewSidebar) {
|
||||||
|
viewPager2.setCurrentItem(1, false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -904,17 +904,17 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class SectionsPagerAdapter extends FragmentPagerAdapter {
|
private class SectionsPagerAdapter extends FragmentStateAdapter {
|
||||||
private PostFragment postFragment;
|
/*private PostFragment postFragment;
|
||||||
private SidebarFragment sidebarFragment;
|
private SidebarFragment sidebarFragment;*/
|
||||||
|
|
||||||
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 (position == 0) {
|
if (position == 0) {
|
||||||
PostFragment fragment = new PostFragment();
|
PostFragment fragment = new PostFragment();
|
||||||
Bundle bundle = new Bundle();
|
Bundle bundle = new Bundle();
|
||||||
@ -933,118 +933,93 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
|
|||||||
return fragment;
|
return fragment;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Nullable
|
||||||
public int getCount() {
|
private Fragment getCurrentFragment() {
|
||||||
return 2;
|
if (fragmentManager == null) {
|
||||||
}
|
return null;
|
||||||
|
|
||||||
@Override
|
|
||||||
public CharSequence getPageTitle(int position) {
|
|
||||||
switch (position) {
|
|
||||||
case 0:
|
|
||||||
return "Posts";
|
|
||||||
case 1:
|
|
||||||
return "Sidebar";
|
|
||||||
}
|
}
|
||||||
return null;
|
return fragmentManager.findFragmentByTag("f" + viewPager2.getCurrentItem());
|
||||||
}
|
|
||||||
|
|
||||||
@NonNull
|
|
||||||
@Override
|
|
||||||
public Object instantiateItem(@NonNull ViewGroup container, int position) {
|
|
||||||
Fragment fragment = (Fragment) super.instantiateItem(container, position);
|
|
||||||
switch (position) {
|
|
||||||
case 0:
|
|
||||||
postFragment = (PostFragment) fragment;
|
|
||||||
displaySortTypeInToolbar();
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
sidebarFragment = (SidebarFragment) fragment;
|
|
||||||
}
|
|
||||||
return fragment;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean handleKeyDown(int keyCode) {
|
public boolean handleKeyDown(int keyCode) {
|
||||||
return viewPager.getCurrentItem() == 0 && postFragment.handleKeyDown(keyCode);
|
if (viewPager2.getCurrentItem() == 0) {
|
||||||
|
Fragment fragment = getCurrentFragment();
|
||||||
|
if (fragment instanceof PostFragment) {
|
||||||
|
return ((PostFragment) fragment).handleKeyDown(keyCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void refresh() {
|
public void refresh() {
|
||||||
if (viewPager.getCurrentItem() == 0) {
|
Fragment fragment = getCurrentFragment();
|
||||||
if (postFragment != null) {
|
if (fragment instanceof PostFragment) {
|
||||||
postFragment.refresh();
|
((PostFragment) fragment).refresh();
|
||||||
}
|
} else if (fragment instanceof SidebarFragment) {
|
||||||
} else {
|
((SidebarFragment) fragment).fetchSubredditData();
|
||||||
if (sidebarFragment != null) {
|
|
||||||
sidebarFragment.fetchSubredditData();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean startLazyMode() {
|
boolean startLazyMode() {
|
||||||
if (postFragment != null) {
|
Fragment fragment = getCurrentFragment();
|
||||||
return ((FragmentCommunicator) postFragment).startLazyMode();
|
if (fragment instanceof FragmentCommunicator) {
|
||||||
|
return ((FragmentCommunicator) fragment).startLazyMode();
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void stopLazyMode() {
|
void stopLazyMode() {
|
||||||
if (postFragment != null) {
|
Fragment fragment = getCurrentFragment();
|
||||||
((FragmentCommunicator) postFragment).stopLazyMode();
|
if (fragment instanceof FragmentCommunicator) {
|
||||||
}
|
((FragmentCommunicator) fragment).stopLazyMode();
|
||||||
}
|
|
||||||
|
|
||||||
void resumeLazyMode() {
|
|
||||||
if (postFragment != null) {
|
|
||||||
((FragmentCommunicator) postFragment).resumeLazyMode(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void pauseLazyMode() {
|
|
||||||
if (postFragment != null) {
|
|
||||||
((FragmentCommunicator) postFragment).pauseLazyMode(false);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void changeSortType(SortType sortType) {
|
public void changeSortType(SortType sortType) {
|
||||||
if (postFragment != null) {
|
Fragment fragment = getCurrentFragment();
|
||||||
postFragment.changeSortType(sortType);
|
if (fragment instanceof PostFragment) {
|
||||||
displaySortTypeInToolbar();
|
((PostFragment) fragment).changeSortType(sortType);
|
||||||
|
Utils.displaySortTypeInToolbar(sortType, toolbar);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void changeNSFW(boolean nsfw) {
|
public void changeNSFW(boolean nsfw) {
|
||||||
if (postFragment != null) {
|
Fragment fragment = getCurrentFragment();
|
||||||
postFragment.changeNSFW(nsfw);
|
if (fragment instanceof PostFragment) {
|
||||||
|
((PostFragment) fragment).changeNSFW(nsfw);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void changePostLayout(int postLayout) {
|
void changePostLayout(int postLayout) {
|
||||||
if (postFragment != null) {
|
Fragment fragment = getCurrentFragment();
|
||||||
mPostLayoutSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_SUBREDDIT_POST_BASE + subredditName, postLayout).apply();
|
if (fragment instanceof PostFragment) {
|
||||||
((FragmentCommunicator) postFragment).changePostLayout(postLayout);
|
((PostFragment) fragment).changePostLayout(postLayout);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void goBackToTop() {
|
void goBackToTop() {
|
||||||
if (viewPager.getCurrentItem() == 0) {
|
Fragment fragment = getCurrentFragment();
|
||||||
if (postFragment != null) {
|
if (fragment instanceof PostFragment) {
|
||||||
postFragment.goBackToTop();
|
((PostFragment) fragment).goBackToTop();
|
||||||
}
|
} else if (fragment instanceof SidebarFragment) {
|
||||||
} else {
|
((SidebarFragment) fragment).goBackToTop();
|
||||||
if (sidebarFragment != null) {
|
|
||||||
sidebarFragment.goBackToTop();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void displaySortTypeInToolbar() {
|
void displaySortTypeInToolbar() {
|
||||||
if (viewPager.getCurrentItem() == 0) {
|
if (fragmentManager != null) {
|
||||||
if (postFragment != null) {
|
Fragment fragment = fragmentManager.findFragmentByTag("f" + viewPager2.getCurrentItem());
|
||||||
SortType sortType = postFragment.getSortType();
|
if (fragment instanceof PostFragment) {
|
||||||
|
SortType sortType = ((PostFragment) fragment).getSortType();
|
||||||
Utils.displaySortTypeInToolbar(sortType, toolbar);
|
Utils.displaySortTypeInToolbar(sortType, toolbar);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getItemCount() {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void lockSwipeRightToGoBack() {
|
private void lockSwipeRightToGoBack() {
|
||||||
|
@ -12,7 +12,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.LinearLayout;
|
import android.widget.LinearLayout;
|
||||||
@ -20,13 +19,15 @@ import android.widget.TextView;
|
|||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
import androidx.appcompat.widget.Toolbar;
|
import androidx.appcompat.widget.Toolbar;
|
||||||
import androidx.coordinatorlayout.widget.CoordinatorLayout;
|
import androidx.coordinatorlayout.widget.CoordinatorLayout;
|
||||||
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.viewpager.widget.ViewPager;
|
import androidx.viewpager2.adapter.FragmentStateAdapter;
|
||||||
|
import androidx.viewpager2.widget.ViewPager2;
|
||||||
|
|
||||||
import com.bumptech.glide.Glide;
|
import com.bumptech.glide.Glide;
|
||||||
import com.bumptech.glide.RequestManager;
|
import com.bumptech.glide.RequestManager;
|
||||||
@ -37,6 +38,7 @@ import com.google.android.material.chip.Chip;
|
|||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
|
||||||
import com.google.android.material.snackbar.Snackbar;
|
import com.google.android.material.snackbar.Snackbar;
|
||||||
import com.google.android.material.tabs.TabLayout;
|
import com.google.android.material.tabs.TabLayout;
|
||||||
|
import com.google.android.material.tabs.TabLayoutMediator;
|
||||||
import com.r0adkll.slidr.Slidr;
|
import com.r0adkll.slidr.Slidr;
|
||||||
import com.r0adkll.slidr.model.SlidrInterface;
|
import com.r0adkll.slidr.model.SlidrInterface;
|
||||||
|
|
||||||
@ -104,7 +106,7 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
|
|||||||
@BindView(R.id.coordinator_layout_view_user_detail_activity)
|
@BindView(R.id.coordinator_layout_view_user_detail_activity)
|
||||||
CoordinatorLayout coordinatorLayout;
|
CoordinatorLayout coordinatorLayout;
|
||||||
@BindView(R.id.view_pager_view_user_detail_activity)
|
@BindView(R.id.view_pager_view_user_detail_activity)
|
||||||
ViewPager viewPager;
|
ViewPager2 viewPager2;
|
||||||
@BindView(R.id.appbar_layout_view_user_detail)
|
@BindView(R.id.appbar_layout_view_user_detail)
|
||||||
AppBarLayout appBarLayout;
|
AppBarLayout appBarLayout;
|
||||||
@BindView(R.id.toolbar_view_user_detail_activity)
|
@BindView(R.id.toolbar_view_user_detail_activity)
|
||||||
@ -149,6 +151,7 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
|
|||||||
@Inject
|
@Inject
|
||||||
CustomThemeWrapper mCustomThemeWrapper;
|
CustomThemeWrapper mCustomThemeWrapper;
|
||||||
public UserViewModel userViewModel;
|
public UserViewModel userViewModel;
|
||||||
|
private FragmentManager fragmentManager;
|
||||||
private SectionsPagerAdapter sectionsPagerAdapter;
|
private SectionsPagerAdapter sectionsPagerAdapter;
|
||||||
private SubscribedUserDao subscribedUserDao;
|
private SubscribedUserDao subscribedUserDao;
|
||||||
private RequestManager glide;
|
private RequestManager glide;
|
||||||
@ -198,6 +201,8 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
|
|||||||
|
|
||||||
username = getIntent().getStringExtra(EXTRA_USER_NAME_KEY);
|
username = getIntent().getStringExtra(EXTRA_USER_NAME_KEY);
|
||||||
|
|
||||||
|
fragmentManager = getSupportFragmentManager();
|
||||||
|
|
||||||
if (savedInstanceState == null) {
|
if (savedInstanceState == null) {
|
||||||
mMessageFullname = getIntent().getStringExtra(EXTRA_MESSAGE_FULLNAME);
|
mMessageFullname = getIntent().getStringExtra(EXTRA_MESSAGE_FULLNAME);
|
||||||
mNewAccountName = getIntent().getStringExtra(EXTRA_NEW_ACCOUNT_NAME);
|
mNewAccountName = getIntent().getStringExtra(EXTRA_NEW_ACCOUNT_NAME);
|
||||||
@ -509,21 +514,22 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void initializeViewPager() {
|
private void initializeViewPager() {
|
||||||
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
|
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), getLifecycle());
|
||||||
viewPager.setAdapter(sectionsPagerAdapter);
|
viewPager2.setAdapter(sectionsPagerAdapter);
|
||||||
viewPager.setOffscreenPageLimit(2);
|
viewPager2.setOffscreenPageLimit(2);
|
||||||
tabLayout.setupWithViewPager(viewPager);
|
viewPager2.setUserInputEnabled(!mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_SWIPING_BETWEEN_TABS, false));
|
||||||
|
new TabLayoutMediator(tabLayout, viewPager2, (tab, position) -> {
|
||||||
|
switch (position) {
|
||||||
|
case 0:
|
||||||
|
tab.setText("Posts");
|
||||||
|
case 1:
|
||||||
|
tab.setText("Comments");
|
||||||
|
}
|
||||||
|
}).attach();
|
||||||
|
|
||||||
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
|
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onPageSelected(int position) {
|
public void onPageSelected(int position) {
|
||||||
if (isInLazyMode) {
|
|
||||||
if (viewPager.getCurrentItem() == 0) {
|
|
||||||
sectionsPagerAdapter.resumeLazyMode();
|
|
||||||
} else {
|
|
||||||
sectionsPagerAdapter.pauseLazyMode();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (position == 0) {
|
if (position == 0) {
|
||||||
unlockSwipeRightToGoBack();
|
unlockSwipeRightToGoBack();
|
||||||
} else {
|
} else {
|
||||||
@ -809,17 +815,15 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class SectionsPagerAdapter extends FragmentPagerAdapter {
|
private class SectionsPagerAdapter extends FragmentStateAdapter {
|
||||||
private PostFragment postFragment;
|
|
||||||
private CommentsListingFragment commentsListingFragment;
|
|
||||||
|
|
||||||
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 (position == 0) {
|
if (position == 0) {
|
||||||
PostFragment fragment = new PostFragment();
|
PostFragment fragment = new PostFragment();
|
||||||
Bundle bundle = new Bundle();
|
Bundle bundle = new Bundle();
|
||||||
@ -843,134 +847,100 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getCount() {
|
public int getItemCount() {
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Nullable
|
||||||
public CharSequence getPageTitle(int position) {
|
private Fragment getCurrentFragment() {
|
||||||
switch (position) {
|
if (viewPager2 == null || fragmentManager == null) {
|
||||||
case 0:
|
return null;
|
||||||
return "Posts";
|
|
||||||
case 1:
|
|
||||||
return "Comments";
|
|
||||||
}
|
}
|
||||||
return null;
|
return fragmentManager.findFragmentByTag("f" + viewPager2.getCurrentItem());
|
||||||
}
|
|
||||||
|
|
||||||
@NonNull
|
|
||||||
@Override
|
|
||||||
public Object instantiateItem(@NonNull ViewGroup container, int position) {
|
|
||||||
Fragment fragment = (Fragment) super.instantiateItem(container, position);
|
|
||||||
switch (position) {
|
|
||||||
case 0:
|
|
||||||
postFragment = (PostFragment) fragment;
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
commentsListingFragment = (CommentsListingFragment) fragment;
|
|
||||||
}
|
|
||||||
displaySortTypeInToolbar();
|
|
||||||
return fragment;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean handleKeyDown(int keyCode) {
|
public boolean handleKeyDown(int keyCode) {
|
||||||
return viewPager.getCurrentItem() == 0 && postFragment.handleKeyDown(keyCode);
|
if (viewPager2.getCurrentItem() == 0) {
|
||||||
|
Fragment fragment = getCurrentFragment();
|
||||||
|
if (fragment instanceof PostFragment) {
|
||||||
|
return ((PostFragment) fragment).handleKeyDown(keyCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void refresh() {
|
public void refresh() {
|
||||||
if (viewPager.getCurrentItem() == 0) {
|
Fragment fragment = getCurrentFragment();
|
||||||
if (postFragment != null) {
|
if (fragment instanceof PostFragment) {
|
||||||
postFragment.refresh();
|
((PostFragment) fragment).refresh();
|
||||||
}
|
} else if (fragment instanceof CommentsListingFragment) {
|
||||||
} else {
|
((CommentsListingFragment) fragment).refresh();
|
||||||
if (commentsListingFragment != null) {
|
|
||||||
commentsListingFragment.refresh();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean startLazyMode() {
|
boolean startLazyMode() {
|
||||||
if (postFragment != null) {
|
Fragment fragment = getCurrentFragment();
|
||||||
return ((FragmentCommunicator) postFragment).startLazyMode();
|
if (fragment instanceof FragmentCommunicator) {
|
||||||
|
return ((FragmentCommunicator) fragment).startLazyMode();
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void stopLazyMode() {
|
void stopLazyMode() {
|
||||||
if (postFragment != null) {
|
Fragment fragment = getCurrentFragment();
|
||||||
((FragmentCommunicator) postFragment).stopLazyMode();
|
if (fragment instanceof FragmentCommunicator) {
|
||||||
}
|
((FragmentCommunicator) fragment).stopLazyMode();
|
||||||
}
|
|
||||||
|
|
||||||
void resumeLazyMode() {
|
|
||||||
if (postFragment != null) {
|
|
||||||
((FragmentCommunicator) postFragment).resumeLazyMode(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void pauseLazyMode() {
|
|
||||||
if (postFragment != null) {
|
|
||||||
((FragmentCommunicator) postFragment).pauseLazyMode(false);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void changeSortType(SortType sortType) {
|
public void changeSortType(SortType sortType) {
|
||||||
if (viewPager.getCurrentItem() == 0) {
|
Fragment fragment = getCurrentFragment();
|
||||||
if (postFragment != null) {
|
if (fragment instanceof PostFragment) {
|
||||||
postFragment.changeSortType(sortType);
|
((PostFragment) fragment).changeSortType(sortType);
|
||||||
}
|
Utils.displaySortTypeInToolbar(sortType, toolbar);
|
||||||
} else {
|
} else if (fragment instanceof CommentsListingFragment) {
|
||||||
if (commentsListingFragment != null) {
|
mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TYPE_USER_COMMENT, sortType.getType().name()).apply();
|
||||||
mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TYPE_USER_COMMENT, sortType.getType().name()).apply();
|
if(sortType.getTime() != null) {
|
||||||
if(sortType.getTime() != null) {
|
mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TIME_USER_COMMENT, sortType.getTime().name()).apply();
|
||||||
mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TIME_USER_COMMENT, sortType.getTime().name()).apply();
|
|
||||||
}
|
|
||||||
|
|
||||||
commentsListingFragment.changeSortType(sortType);
|
|
||||||
}
|
}
|
||||||
|
((CommentsListingFragment) fragment).changeSortType(sortType);
|
||||||
|
Utils.displaySortTypeInToolbar(sortType, toolbar);
|
||||||
}
|
}
|
||||||
displaySortTypeInToolbar();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void changeNSFW(boolean nsfw) {
|
public void changeNSFW(boolean nsfw) {
|
||||||
if (postFragment != null) {
|
Fragment fragment = getCurrentFragment();
|
||||||
postFragment.changeNSFW(nsfw);
|
if (fragment instanceof PostFragment) {
|
||||||
|
((PostFragment) fragment).changeNSFW(nsfw);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void changePostLayout(int postLayout) {
|
void changePostLayout(int postLayout) {
|
||||||
if (postFragment != null) {
|
Fragment fragment = getCurrentFragment();
|
||||||
mPostLayoutSharedPreferences.edit().putInt(SharedPreferencesUtils.POST_LAYOUT_USER_POST_BASE + username, postLayout).apply();
|
if (fragment instanceof PostFragment) {
|
||||||
((FragmentCommunicator) postFragment).changePostLayout(postLayout);
|
((PostFragment) fragment).changePostLayout(postLayout);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void goBackToTop() {
|
void goBackToTop() {
|
||||||
if (viewPager.getCurrentItem() == 0) {
|
Fragment fragment = getCurrentFragment();
|
||||||
if (postFragment != null) {
|
if (fragment instanceof PostFragment) {
|
||||||
postFragment.goBackToTop();
|
((PostFragment) fragment).goBackToTop();
|
||||||
}
|
} else if (fragment instanceof CommentsListingFragment) {
|
||||||
} else {
|
((CommentsListingFragment) fragment).goBackToTop();
|
||||||
if (commentsListingFragment != null) {
|
|
||||||
commentsListingFragment.goBackToTop();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void displaySortTypeInToolbar() {
|
void displaySortTypeInToolbar() {
|
||||||
switch (viewPager.getCurrentItem()) {
|
if (fragmentManager != null) {
|
||||||
case 0:
|
Fragment fragment = fragmentManager.findFragmentByTag("f" + viewPager2.getCurrentItem());
|
||||||
if (postFragment != null) {
|
if (fragment instanceof PostFragment) {
|
||||||
SortType sortType = postFragment.getSortType();
|
SortType sortType = ((PostFragment) fragment).getSortType();
|
||||||
Utils.displaySortTypeInToolbar(sortType, toolbar);
|
Utils.displaySortTypeInToolbar(sortType, toolbar);
|
||||||
}
|
} else if (fragment instanceof CommentsListingFragment) {
|
||||||
break;
|
SortType sortType = ((CommentsListingFragment) fragment).getSortType();
|
||||||
case 1:
|
Utils.displaySortTypeInToolbar(sortType, toolbar);
|
||||||
if (commentsListingFragment != null) {
|
}
|
||||||
SortType sortType = commentsListingFragment.getSortType();
|
|
||||||
Utils.displaySortTypeInToolbar(sortType, toolbar);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
android:id="@+id/coordinator_layout_view_subreddit_detail_activity"
|
android:id="@+id/coordinator_layout_view_subreddit_detail_activity"
|
||||||
tools:application=".Activity.ViewSubredditDetailActivity">
|
tools:application=".Activity.ViewSubredditDetailActivity">
|
||||||
|
|
||||||
<androidx.viewpager.widget.ViewPager
|
<androidx.viewpager2.widget.ViewPager2
|
||||||
android:id="@+id/view_pager_view_subreddit_detail_activity"
|
android:id="@+id/view_pager_view_subreddit_detail_activity"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
android:id="@+id/coordinator_layout_view_user_detail_activity"
|
android:id="@+id/coordinator_layout_view_user_detail_activity"
|
||||||
tools:application=".ViewUserDetailActivity">
|
tools:application=".ViewUserDetailActivity">
|
||||||
|
|
||||||
<androidx.viewpager.widget.ViewPager
|
<androidx.viewpager2.widget.ViewPager2
|
||||||
android:id="@+id/view_pager_view_user_detail_activity"
|
android:id="@+id/view_pager_view_user_detail_activity"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
Loading…
Reference in New Issue
Block a user