Fix crashes

This commit is contained in:
Thomas 2022-10-23 11:59:50 +02:00
parent e8e5af7abe
commit ea31842dba
6 changed files with 55 additions and 28 deletions

View file

@ -164,7 +164,7 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt
public static String regex_home, regex_local, regex_public;
public static BaseAccount currentAccount;
Fragment currentFragment;
public static String slugOfFirstFragment;
private AppBarConfiguration mAppBarConfiguration;
private ActivityMainBinding binding;
private final BroadcastReceiver broadcast_error_message = new BroadcastReceiver() {
@ -871,7 +871,6 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt
.observe(this, pinned -> {
this.pinned = pinned;
//Initialize the slug of the first fragment
slugOfFirstFragment = PinnedTimelineHelper.firstTimelineSlug(BaseMainActivity.this, pinned, bottomMenu);
//First it's taken from db (last stored values)
PinnedTimelineHelper.redrawTopBarPinned(BaseMainActivity.this, binding, pinned, bottomMenu, null);
//Fetch remote lists for the authenticated account and update them

View file

@ -153,6 +153,7 @@ import app.fedilab.android.client.entities.api.Status;
import app.fedilab.android.client.entities.app.Account;
import app.fedilab.android.client.entities.app.BaseAccount;
import app.fedilab.android.client.entities.app.ReleaseNote;
import app.fedilab.android.client.entities.app.Timeline;
import app.fedilab.android.databinding.PopupReleaseNotesBinding;
import app.fedilab.android.exception.DBException;
import app.fedilab.android.interfaces.OnDownloadInterface;
@ -217,6 +218,7 @@ public class Helper {
public static final String ARG_STATUS_DRAFT = "ARG_STATUS_DRAFT";
public static final String ARG_STATUS_SCHEDULED = "ARG_STATUS_SCHEDULED";
public static final String ARG_SLUG_OF_FIRST_FRAGMENT = "ARG_SLUG_OF_FIRST_FRAGMENT";
public static final String ARG_STATUS_DRAFT_ID = "ARG_STATUS_DRAFT_ID";
public static final String ARG_STATUS_REPLY = "ARG_STATUS_REPLY";
@ -1871,4 +1873,26 @@ public class Helper {
public interface OnAttachmentCopied {
void onAttachmentCopied(Attachment attachment);
}
//Allow to store in shared preference first visible fragment when the app starts
private static String slugOfFirstFragment;
public static String getSlugOfFirstFragment(Context context, String userId, String instance) {
if (slugOfFirstFragment != null) {
return slugOfFirstFragment;
}
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context);
return sharedpreferences.getString(Helper.ARG_SLUG_OF_FIRST_FRAGMENT + userId + instance, Timeline.TimeLineEnum.HOME.getValue());
}
public static void setSlugOfFirstFragment(Context context, String slug, String userId, String instance) {
if (slug != null) {
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedpreferences.edit();
slugOfFirstFragment = slug;
editor.putString(Helper.ARG_SLUG_OF_FIRST_FRAGMENT + userId + instance, slug);
editor.apply();
}
}
}

View file

@ -102,11 +102,12 @@ public class PinnedTimelineHelper {
* @return String - slug
*/
public static String firstTimelineSlug(Context context, Pinned pinned, BottomMenu bottomMenu) {
String slug = null;
String slug = Timeline.TimeLineEnum.HOME.getValue();
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context);
boolean singleBar = sharedpreferences.getBoolean(context.getString(R.string.SET_USE_SINGLE_TOPBAR), false);
PinnedTimeline pinnedTimelineMin = null;
if (singleBar) {
if (pinned != null && pinned.pinnedTimelines != null) {
for (PinnedTimeline pinnedTimeline : pinned.pinnedTimelines) {
if (pinnedTimeline.displayed) {
if (pinnedTimelineMin == null) {
@ -116,6 +117,7 @@ public class PinnedTimelineHelper {
}
}
}
}
} else {
if (bottomMenu != null && bottomMenu.bottom_menu != null && bottomMenu.bottom_menu.size() > 0) {
BottomMenu.MenuItem menuItem = bottomMenu.bottom_menu.get(0);
@ -155,6 +157,10 @@ public class PinnedTimelineHelper {
if (pinned.pinnedTimelines == null) {
pinned.pinnedTimelines = new ArrayList<>();
}
//Set the slug of first visible fragment
String slugOfFirstFragment = PinnedTimelineHelper.firstTimelineSlug(activity, pinned, bottomMenu);
Helper.setSlugOfFirstFragment(activity, slugOfFirstFragment, currentUserID, currentInstance);
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(activity);
boolean singleBar = sharedpreferences.getBoolean(activity.getString(R.string.SET_USE_SINGLE_TOPBAR), false);
boolean timeInList = sharedpreferences.getBoolean(activity.getString(R.string.SET_TIMELINES_IN_A_LIST), false);

View file

@ -15,7 +15,8 @@ package app.fedilab.android.ui.fragment.timeline;
* see <http://www.gnu.org/licenses>. */
import static app.fedilab.android.BaseMainActivity.slugOfFirstFragment;
import static app.fedilab.android.BaseMainActivity.currentInstance;
import static app.fedilab.android.BaseMainActivity.currentUserID;
import android.content.SharedPreferences;
import android.os.Bundle;
@ -42,6 +43,7 @@ import app.fedilab.android.client.entities.app.StatusCache;
import app.fedilab.android.client.entities.app.Timeline;
import app.fedilab.android.databinding.FragmentPaginationBinding;
import app.fedilab.android.exception.DBException;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.helper.MastodonHelper;
import app.fedilab.android.helper.ThemeHelper;
import app.fedilab.android.ui.drawer.ConversationAdapter;
@ -83,7 +85,7 @@ public class FragmentMastodonConversation extends Fragment implements Conversati
@Override
public void onResume() {
super.onResume();
if (Timeline.TimeLineEnum.CONVERSATION.getValue().compareTo(slugOfFirstFragment) != 0 && !isViewInitialized) {
if (Timeline.TimeLineEnum.CONVERSATION.getValue().compareTo(Helper.getSlugOfFirstFragment(requireActivity(), currentUserID, currentInstance)) != 0 && !isViewInitialized) {
isViewInitialized = true;
initializeConversationCommonView(initialConversations);
}
@ -218,6 +220,7 @@ public class FragmentMastodonConversation extends Fragment implements Conversati
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
isViewInitialized = Timeline.TimeLineEnum.CONVERSATION.getValue().compareTo(Helper.getSlugOfFirstFragment(requireActivity(), currentUserID, currentInstance)) == 0;
int c1 = getResources().getColor(R.color.cyanea_accent_reference);
binding.swipeContainer.setProgressBackgroundColorSchemeColor(getResources().getColor(R.color.cyanea_primary_reference));
binding.swipeContainer.setColorSchemeColors(

View file

@ -14,7 +14,8 @@ package app.fedilab.android.ui.fragment.timeline;
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
import static app.fedilab.android.BaseMainActivity.slugOfFirstFragment;
import static app.fedilab.android.BaseMainActivity.currentInstance;
import static app.fedilab.android.BaseMainActivity.currentUserID;
import android.content.BroadcastReceiver;
import android.content.Context;
@ -138,7 +139,7 @@ public class FragmentMastodonNotification extends Fragment implements Notificati
ViewGroup container, Bundle savedInstanceState) {
flagLoading = false;
isViewInitialized = Timeline.TimeLineEnum.NOTIFICATION.getValue().compareTo(slugOfFirstFragment) == 0;
isViewInitialized = Timeline.TimeLineEnum.NOTIFICATION.getValue().compareTo(Helper.getSlugOfFirstFragment(requireActivity(), currentUserID, currentInstance)) == 0;
binding = FragmentPaginationBinding.inflate(inflater, container, false);
View root = binding.getRoot();
if (getArguments() != null) {
@ -294,7 +295,7 @@ public class FragmentMastodonNotification extends Fragment implements Notificati
@Override
public void onResume() {
super.onResume();
if (Timeline.TimeLineEnum.NOTIFICATION.getValue().compareTo(slugOfFirstFragment) != 0 && !isViewInitialized) {
if (Timeline.TimeLineEnum.NOTIFICATION.getValue().compareTo(Helper.getSlugOfFirstFragment(requireActivity(), currentUserID, currentInstance)) != 0 && !isViewInitialized) {
isViewInitialized = true;
initializeNotificationView(initialNotifications);
}

View file

@ -15,8 +15,9 @@ package app.fedilab.android.ui.fragment.timeline;
* see <http://www.gnu.org/licenses>. */
import static app.fedilab.android.BaseMainActivity.currentInstance;
import static app.fedilab.android.BaseMainActivity.currentUserID;
import static app.fedilab.android.BaseMainActivity.networkAvailable;
import static app.fedilab.android.BaseMainActivity.slugOfFirstFragment;
import android.content.BroadcastReceiver;
import android.content.Context;
@ -145,7 +146,7 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter.
@Override
public void onResume() {
super.onResume();
if (slug != null && slugOfFirstFragment != null && slug.compareTo(slugOfFirstFragment) != 0 && !isViewInitialized) {
if (slug != null && slug.compareTo(Helper.getSlugOfFirstFragment(requireActivity(), currentUserID, currentInstance)) != 0 && !isViewInitialized) {
isViewInitialized = true;
initializeStatusesCommonView(initialStatuses);
}
@ -255,10 +256,8 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter.
//Only fragment in main view pager should not have the view initialized
//AND Only the first fragment will initialize its view
if (!isViewInitialized) {
if (slug != null && slugOfFirstFragment != null) {
isViewInitialized = slug.compareTo(slugOfFirstFragment) == 0;
} else if (timelineType != null) {
isViewInitialized = timelineType.compareTo(Timeline.TimeLineEnum.HOME) == 0;
if (slug != null) {
isViewInitialized = slug.compareTo(Helper.getSlugOfFirstFragment(requireActivity(), currentUserID, currentInstance)) == 0;
}
}
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(requireActivity());
@ -546,8 +545,8 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter.
* @param direction - DIRECTION null if first call, then is set to TOP or BOTTOM depending of scroll
*/
private void routeCommon(DIRECTION direction, boolean fetchingMissing, Status status) {
if (direction == null && !isViewInitialized && slug != null && slugOfFirstFragment != null) {
isViewInitialized = slug.compareTo(slugOfFirstFragment) == 0;
if (direction == null && !isViewInitialized && slug != null) {
isViewInitialized = slug.compareTo(Helper.getSlugOfFirstFragment(requireActivity(), currentUserID, currentInstance)) == 0;
}
if (binding == null || getActivity() == null || !isAdded()) {
return;
@ -604,18 +603,13 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter.
boolean useCache = sharedpreferences.getBoolean(getString(R.string.SET_USE_CACHE), true);
Handler handler = new Handler();
//The action for fetching new messages is delayed for other timelines than HOME
if (slugOfFirstFragment == null) {
slugOfFirstFragment = slug;
}
handler.postDelayed(() -> {
if (useCache && direction != DIRECTION.SCROLL_TOP && direction != DIRECTION.FETCH_NEW) {
getCachedStatus(direction, fetchingMissing, timelineParams);
} else {
getLiveStatus(direction, fetchingMissing, timelineParams, status);
}
}, slug.compareTo(slugOfFirstFragment) == 0 ? 0 : 1000);
}, slug.compareTo(Helper.getSlugOfFirstFragment(requireActivity(), currentUserID, currentInstance)) == 0 ? 0 : 1000);
}