Fix crashes

This commit is contained in:
Thomas 2022-12-24 16:50:40 +01:00
parent c5030087d4
commit 7030b1e20a
2 changed files with 94 additions and 89 deletions

View File

@ -315,8 +315,89 @@ public class PinnedTimelineHelper {
return; return;
} }
//Pinned tab position will start after BOTTOM_TIMELINE_COUNT (ie 5) //Pinned tab position will start after BOTTOM_TIMELINE_COUNT (ie 5)
activityMainBinding.tabLayout.removeAllTabs();
int toRemove = BOTTOM_TIMELINE_COUNT; int toRemove = BOTTOM_TIMELINE_COUNT;
activityMainBinding.tabLayout.removeAllTabs();
LinearLayout tabStrip = (LinearLayout) activityMainBinding.tabLayout.getChildAt(0);
int finalToRemove = toRemove;
activityMainBinding.viewPager.setAdapter(null);
activityMainBinding.viewPager.clearOnPageChangeListeners();
activityMainBinding.tabLayout.clearOnTabSelectedListeners();
FedilabPageAdapter fedilabPageAdapter = new FedilabPageAdapter(activity, activity.getSupportFragmentManager(), pinned, bottomMenu);
activityMainBinding.viewPager.setAdapter(fedilabPageAdapter);
activityMainBinding.viewPager.setOffscreenPageLimit(tabStrip.getChildCount());
activityMainBinding.viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(activityMainBinding.tabLayout));
if (!singleBar) {
activityMainBinding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position < BOTTOM_TIMELINE_COUNT - finalToRemove) {
activityMainBinding.bottomNavView.getMenu().getItem(position).setChecked(true);
} else {
activityMainBinding.bottomNavView.getMenu().setGroupCheckable(0, true, false);
for (int i = 0; i < activityMainBinding.bottomNavView.getMenu().size(); i++) {
activityMainBinding.bottomNavView.getMenu().getItem(i).setChecked(false);
}
activityMainBinding.bottomNavView.getMenu().setGroupCheckable(0, true, true);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
activityMainBinding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
activityMainBinding.viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
if (!singleBar && tab.getTag() != null) {
if (tab.getTag().equals(Timeline.TimeLineEnum.HOME.getValue())) {
activityMainBinding.bottomNavView.removeBadge(R.id.nav_home);
} else if (tab.getTag().equals(Timeline.TimeLineEnum.LOCAL.getValue())) {
activityMainBinding.bottomNavView.removeBadge(R.id.nav_local);
} else if (tab.getTag().equals(Timeline.TimeLineEnum.PUBLIC.getValue())) {
activityMainBinding.bottomNavView.removeBadge(R.id.nav_public);
} else if (tab.getTag().equals(Timeline.TimeLineEnum.NOTIFICATION.getValue())) {
activityMainBinding.bottomNavView.removeBadge(R.id.nav_notifications);
} else if (tab.getTag().equals(Timeline.TimeLineEnum.CONVERSATION.getValue())) {
activityMainBinding.bottomNavView.removeBadge(R.id.nav_privates);
}
}
Fragment fragment = fedilabPageAdapter.getCurrentFragment();
View view = tab.getCustomView();
if (view != null) {
TextView counter = view.findViewById(R.id.tab_counter);
if (counter != null) {
counter.setVisibility(View.GONE);
counter.setText("0");
}
}
if (fragment instanceof FragmentMastodonTimeline) {
((FragmentMastodonTimeline) fragment).scrollToTop();
} else if (fragment instanceof FragmentMastodonConversation) {
((FragmentMastodonConversation) fragment).scrollToTop();
} else if (fragment instanceof FragmentNotificationContainer) {
((FragmentNotificationContainer) fragment).scrollToTop();
}
}
});
if (!singleBar) { if (!singleBar) {
//Small hack to hide first tabs (they represent the item of the bottom menu) //Small hack to hide first tabs (they represent the item of the bottom menu)
toRemove = itemToRemoveInBottomMenu(activity); toRemove = itemToRemoveInBottomMenu(activity);
@ -507,8 +588,6 @@ public class PinnedTimelineHelper {
}); });
LinearLayout tabStrip = (LinearLayout) activityMainBinding.tabLayout.getChildAt(0);
int finalToRemove = toRemove;
for (int i = 0; i < tabStrip.getChildCount(); i++) { for (int i = 0; i < tabStrip.getChildCount(); i++) {
// Set LongClick listener to each Tab // Set LongClick listener to each Tab
int finalI = i; int finalI = i;
@ -539,83 +618,7 @@ public class PinnedTimelineHelper {
}); });
} }
activityMainBinding.viewPager.setAdapter(null);
activityMainBinding.viewPager.clearOnPageChangeListeners();
activityMainBinding.tabLayout.clearOnTabSelectedListeners();
FedilabPageAdapter fedilabPageAdapter = new FedilabPageAdapter(activity, activity.getSupportFragmentManager(), pinned, bottomMenu);
activityMainBinding.viewPager.setAdapter(fedilabPageAdapter);
activityMainBinding.viewPager.setOffscreenPageLimit(tabStrip.getChildCount());
activityMainBinding.viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(activityMainBinding.tabLayout));
if (!singleBar) {
activityMainBinding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position < BOTTOM_TIMELINE_COUNT - finalToRemove) {
activityMainBinding.bottomNavView.getMenu().getItem(position).setChecked(true);
} else {
activityMainBinding.bottomNavView.getMenu().setGroupCheckable(0, true, false);
for (int i = 0; i < activityMainBinding.bottomNavView.getMenu().size(); i++) {
activityMainBinding.bottomNavView.getMenu().getItem(i).setChecked(false);
}
activityMainBinding.bottomNavView.getMenu().setGroupCheckable(0, true, true);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
activityMainBinding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
activityMainBinding.viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
if (!singleBar && tab.getTag() != null) {
if (tab.getTag().equals(Timeline.TimeLineEnum.HOME.getValue())) {
activityMainBinding.bottomNavView.removeBadge(R.id.nav_home);
} else if (tab.getTag().equals(Timeline.TimeLineEnum.LOCAL.getValue())) {
activityMainBinding.bottomNavView.removeBadge(R.id.nav_local);
} else if (tab.getTag().equals(Timeline.TimeLineEnum.PUBLIC.getValue())) {
activityMainBinding.bottomNavView.removeBadge(R.id.nav_public);
} else if (tab.getTag().equals(Timeline.TimeLineEnum.NOTIFICATION.getValue())) {
activityMainBinding.bottomNavView.removeBadge(R.id.nav_notifications);
} else if (tab.getTag().equals(Timeline.TimeLineEnum.CONVERSATION.getValue())) {
activityMainBinding.bottomNavView.removeBadge(R.id.nav_privates);
}
}
Fragment fragment = fedilabPageAdapter.getCurrentFragment();
View view = tab.getCustomView();
if (view != null) {
TextView counter = view.findViewById(R.id.tab_counter);
if (counter != null) {
counter.setVisibility(View.GONE);
counter.setText("0");
}
}
if (fragment instanceof FragmentMastodonTimeline) {
((FragmentMastodonTimeline) fragment).scrollToTop();
} else if (fragment instanceof FragmentMastodonConversation) {
((FragmentMastodonConversation) fragment).scrollToTop();
} else if (fragment instanceof FragmentNotificationContainer) {
((FragmentNotificationContainer) fragment).scrollToTop();
}
}
});
} }

View File

@ -1433,18 +1433,20 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
} }
holder.binding.poll.refreshPoll.setOnClickListener(v -> statusesVM.getPoll(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, statusToDeal.poll.id) holder.binding.poll.refreshPoll.setOnClickListener(v -> statusesVM.getPoll(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, statusToDeal.poll.id)
.observe((LifecycleOwner) context, poll -> { .observe((LifecycleOwner) context, poll -> {
//Store span elements if (poll != null) {
int i = 0; //Store span elements
for (Poll.PollItem item : statusToDeal.poll.options) { int i = 0;
if (item.span_title != null) { for (Poll.PollItem item : statusToDeal.poll.options) {
poll.options.get(i).span_title = item.span_title; if (item.span_title != null) {
} else { poll.options.get(i).span_title = item.span_title;
poll.options.get(i).span_title = new SpannableString(item.title); } else {
poll.options.get(i).span_title = new SpannableString(item.title);
}
i++;
} }
i++; statusToDeal.poll = poll;
adapter.notifyItemChanged(holder.getBindingAdapterPosition());
} }
statusToDeal.poll = poll;
adapter.notifyItemChanged(holder.getBindingAdapterPosition());
})); }));
holder.binding.poll.pollContainer.setVisibility(View.VISIBLE); holder.binding.poll.pollContainer.setVisibility(View.VISIBLE);
String pollInfo = context.getResources().getQuantityString(R.plurals.number_of_voters, statusToDeal.poll.voters_count, statusToDeal.poll.voters_count); String pollInfo = context.getResources().getQuantityString(R.plurals.number_of_voters, statusToDeal.poll.voters_count, statusToDeal.poll.voters_count);