Fix issue #1011 - Position lost when switching between accounts

This commit is contained in:
Thomas 2024-01-19 14:30:29 +01:00
parent 1e03a039b8
commit 5692573d9c
3 changed files with 19 additions and 14 deletions

View File

@ -15,6 +15,8 @@ package app.fedilab.android.mastodon.ui.fragment.timeline;
* see <http://www.gnu.org/licenses>. */
import static app.fedilab.android.BaseMainActivity.currentAccount;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
@ -39,6 +41,7 @@ import app.fedilab.android.R;
import app.fedilab.android.databinding.FragmentPaginationBinding;
import app.fedilab.android.mastodon.client.entities.api.Conversation;
import app.fedilab.android.mastodon.client.entities.api.Conversations;
import app.fedilab.android.mastodon.client.entities.app.BaseAccount;
import app.fedilab.android.mastodon.client.entities.app.StatusCache;
import app.fedilab.android.mastodon.client.entities.app.Timeline;
import app.fedilab.android.mastodon.exception.DBException;
@ -229,7 +232,7 @@ public class FragmentMastodonConversation extends Fragment implements Conversati
}
private void storeMarker() {
private void storeMarker(BaseAccount connectedAccount) {
if (mLayoutManager != null) {
int position = mLayoutManager.findFirstVisibleItemPosition();
if (conversationList != null && conversationList.size() > position) {
@ -237,7 +240,7 @@ public class FragmentMastodonConversation extends Fragment implements Conversati
Conversation conversation = conversationList.get(position);
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(requireActivity());
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(getString(R.string.SET_INNER_MARKER) + BaseMainActivity.currentUserID + BaseMainActivity.currentInstance + Timeline.TimeLineEnum.CONVERSATION, conversation.id);
editor.putString(getString(R.string.SET_INNER_MARKER) + connectedAccount.user_id + connectedAccount.instance + Timeline.TimeLineEnum.CONVERSATION, conversation.id);
editor.apply();
} catch (Exception ignored) {
}
@ -355,14 +358,14 @@ public class FragmentMastodonConversation extends Fragment implements Conversati
@Override
public void onDestroyView() {
if (isAdded()) {
storeMarker();
storeMarker(currentAccount);
}
super.onDestroyView();
}
@Override
public void onPause() {
storeMarker();
storeMarker(currentAccount);
super.onPause();
}

View File

@ -49,6 +49,7 @@ import app.fedilab.android.databinding.FragmentPaginationBinding;
import app.fedilab.android.mastodon.client.entities.api.Notification;
import app.fedilab.android.mastodon.client.entities.api.Notifications;
import app.fedilab.android.mastodon.client.entities.api.Status;
import app.fedilab.android.mastodon.client.entities.app.BaseAccount;
import app.fedilab.android.mastodon.client.entities.app.CachedBundle;
import app.fedilab.android.mastodon.client.entities.app.StatusCache;
import app.fedilab.android.mastodon.client.entities.app.Timeline;
@ -554,7 +555,7 @@ public class FragmentMastodonNotification extends Fragment implements Notificati
}
private void storeMarker() {
private void storeMarker(BaseAccount connectedAccount) {
if (mLayoutManager != null) {
int position = mLayoutManager.findFirstVisibleItemPosition();
if (notificationList != null && notificationList.size() > position) {
@ -563,10 +564,10 @@ public class FragmentMastodonNotification extends Fragment implements Notificati
Notification notification = notificationList.get(position);
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(requireActivity());
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(getString(R.string.SET_INNER_MARKER) + BaseMainActivity.currentUserID + BaseMainActivity.currentInstance + Timeline.TimeLineEnum.NOTIFICATION, notification.id);
editor.putString(getString(R.string.SET_INNER_MARKER) + connectedAccount.user_id + connectedAccount.instance + Timeline.TimeLineEnum.NOTIFICATION, notification.id);
editor.apply();
TimelinesVM timelinesVM = new ViewModelProvider(FragmentMastodonNotification.this).get(TimelinesVM.class);
timelinesVM.addMarker(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, null, notification.id);
timelinesVM.addMarker(connectedAccount.instance, connectedAccount.token, null, notification.id);
}
} catch (Exception ignored) {
}
@ -690,14 +691,14 @@ public class FragmentMastodonNotification extends Fragment implements Notificati
e.printStackTrace();
}
if (isAdded()) {
storeMarker();
storeMarker(currentAccount);
}
super.onDestroyView();
}
@Override
public void onPause() {
storeMarker();
storeMarker(currentAccount);
super.onPause();
}

View File

@ -56,6 +56,7 @@ import app.fedilab.android.mastodon.client.entities.api.Attachment;
import app.fedilab.android.mastodon.client.entities.api.Pagination;
import app.fedilab.android.mastodon.client.entities.api.Status;
import app.fedilab.android.mastodon.client.entities.api.Statuses;
import app.fedilab.android.mastodon.client.entities.app.BaseAccount;
import app.fedilab.android.mastodon.client.entities.app.BubbleTimeline;
import app.fedilab.android.mastodon.client.entities.app.CachedBundle;
import app.fedilab.android.mastodon.client.entities.app.PinnedTimeline;
@ -855,7 +856,7 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter.
@Override
public void onPause() {
storeMarker();
storeMarker(currentAccount);
super.onPause();
}
@ -863,7 +864,7 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter.
public void onDestroyView() {
//Update last read id for home timeline
if (isAdded()) {
storeMarker();
storeMarker(currentAccount);
}
try {
requireActivity().unregisterReceiver(receive_action);
@ -873,7 +874,7 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter.
}
private void storeMarker() {
private void storeMarker(BaseAccount connectedAccount) {
if (mLayoutManager != null) {
int position = mLayoutManager.findFirstVisibleItemPosition();
if (timelineStatuses != null && timelineStatuses.size() > position) {
@ -881,10 +882,10 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter.
Status status = timelineStatuses.get(position);
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(requireActivity());
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(getString(R.string.SET_INNER_MARKER) + BaseMainActivity.currentUserID + BaseMainActivity.currentInstance + slug, status.id);
editor.putString(getString(R.string.SET_INNER_MARKER) + connectedAccount.user_id + connectedAccount.instance + slug, status.id);
editor.apply();
if (timelineType == Timeline.TimeLineEnum.HOME) {
timelinesVM.addMarker(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, status.id, null);
timelinesVM.addMarker(connectedAccount.instance, connectedAccount.token, status.id, null);
}
} catch (Exception ignored) {
}