Restore elements between tabs

This commit is contained in:
Thomas 2022-05-06 19:18:53 +02:00
parent 5472764a9f
commit ac918aaa20
3 changed files with 241 additions and 121 deletions

View File

@ -122,6 +122,7 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt
private ActivityMainBinding binding; private ActivityMainBinding binding;
private Pinned pinned; private Pinned pinned;
private final BroadcastReceiver broadcast_data = new BroadcastReceiver() { private final BroadcastReceiver broadcast_data = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {

View File

@ -0,0 +1,76 @@
package app.fedilab.android.client.entities.app;
/* Copyright 2022 Thomas Schneider
*
* This file is a part of Fedilab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
import java.util.HashMap;
import java.util.List;
import app.fedilab.android.client.entities.Timeline;
import app.fedilab.android.client.mastodon.entities.Status;
public class SavedValues {
public static HashMap<String, SavedValues> storedStates = new HashMap<>();
public int position;
public List<Status> statusList;
/**
* Retrieves saved values
*
* @param timeLineType - Timeline.TimeLineEnum
* @param ident - the name for pinned timeline
* @return SavedValues
*/
public static SavedValues getSavedValue(Timeline.TimeLineEnum timeLineType, String ident) {
if (!canBestored(timeLineType)) {
return null;
}
String key = timeLineType.getValue();
if (ident != null) {
key += "|" + ident;
}
return storedStates.get(key);
}
/**
* Check if the current timeline can be stored
*
* @param timeLineType - Timeline.TimeLineEnum
* @return boolean
*/
private static boolean canBestored(Timeline.TimeLineEnum timeLineType) {
return timeLineType == Timeline.TimeLineEnum.HOME || timeLineType == Timeline.TimeLineEnum.LOCAL || timeLineType == Timeline.TimeLineEnum.PUBLIC || timeLineType == Timeline.TimeLineEnum.REMOTE || timeLineType == Timeline.TimeLineEnum.LIST || timeLineType == Timeline.TimeLineEnum.TAG;
}
/**
* @param position - current position in timeline
* @param timeLineType - Timeline.TimeLineEnum
* @param statusList - List<Status> to save
* @param ident - the name for pinned timeline
*/
public static void storeTimeline(int position, Timeline.TimeLineEnum timeLineType, List<Status> statusList, String ident) {
if (!canBestored(timeLineType)) {
return;
}
String key = timeLineType.getValue();
if (ident != null) {
key += "|" + ident;
}
SavedValues savedValues = new SavedValues();
savedValues.position = position;
savedValues.statusList = statusList;
storedStates.put(key, savedValues);
}
}

View File

@ -42,6 +42,7 @@ import java.util.List;
import app.fedilab.android.BaseMainActivity; import app.fedilab.android.BaseMainActivity;
import app.fedilab.android.R; import app.fedilab.android.R;
import app.fedilab.android.client.entities.Timeline; import app.fedilab.android.client.entities.Timeline;
import app.fedilab.android.client.entities.app.SavedValues;
import app.fedilab.android.client.entities.app.TagTimeline; import app.fedilab.android.client.entities.app.TagTimeline;
import app.fedilab.android.client.mastodon.entities.Account; import app.fedilab.android.client.mastodon.entities.Account;
import app.fedilab.android.client.mastodon.entities.Marker; import app.fedilab.android.client.mastodon.entities.Marker;
@ -78,6 +79,8 @@ public class FragmentMastodonTimeline extends Fragment {
private Account accountTimeline; private Account accountTimeline;
private boolean exclude_replies, exclude_reblogs, show_pinned, media_only, minified; private boolean exclude_replies, exclude_reblogs, show_pinned, media_only, minified;
private String viewModelKey, remoteInstance; private String viewModelKey, remoteInstance;
private String ident;
//Handle actions that can be done in other fragments //Handle actions that can be done in other fragments
private final BroadcastReceiver receive_action = new BroadcastReceiver() { private final BroadcastReceiver receive_action = new BroadcastReceiver() {
@ -161,6 +164,15 @@ public class FragmentMastodonTimeline extends Fragment {
minified = getArguments().getBoolean(Helper.ARG_MINIFIED, false); minified = getArguments().getBoolean(Helper.ARG_MINIFIED, false);
statusReport = (Status) getArguments().getSerializable(Helper.ARG_STATUS_REPORT); statusReport = (Status) getArguments().getSerializable(Helper.ARG_STATUS_REPORT);
} }
if (tagTimeline != null) {
ident = tagTimeline.name;
} else if (list_id != null) {
ident = list_id;
} else if (remoteInstance != null) {
ident = remoteInstance;
} else {
ident = null;
}
LocalBroadcastManager.getInstance(requireActivity()).registerReceiver(receive_action, new IntentFilter(Helper.RECEIVE_STATUS_ACTION)); LocalBroadcastManager.getInstance(requireActivity()).registerReceiver(receive_action, new IntentFilter(Helper.RECEIVE_STATUS_ACTION));
binding = FragmentPaginationBinding.inflate(inflater, container, false); binding = FragmentPaginationBinding.inflate(inflater, container, false);
binding.getRoot().setBackgroundColor(ThemeHelper.getBackgroundColor(requireActivity())); binding.getRoot().setBackgroundColor(ThemeHelper.getBackgroundColor(requireActivity()));
@ -340,6 +352,11 @@ public class FragmentMastodonTimeline extends Fragment {
@Override @Override
public void onDestroyView() { public void onDestroyView() {
super.onDestroyView(); super.onDestroyView();
if (mLayoutManager != null) {
int position = mLayoutManager.findFirstVisibleItemPosition();
new Thread(() -> SavedValues.storeTimeline(position, timelineType, statuses, ident)).start();
}
//Update last read id for home timeline //Update last read id for home timeline
storeMarker(); storeMarker();
if (binding != null) { if (binding != null) {
@ -384,6 +401,26 @@ public class FragmentMastodonTimeline extends Fragment {
* @param direction - DIRECTION null if first call, then is set to TOP or BOTTOM depending of scroll * @param direction - DIRECTION null if first call, then is set to TOP or BOTTOM depending of scroll
*/ */
private void route(DIRECTION direction) { private void route(DIRECTION direction) {
new Thread(() -> {
SavedValues savedValues = SavedValues.getSavedValue(timelineType, ident);
if (savedValues != null && savedValues.statusList != null && savedValues.statusList.size() > 0) {
Statuses statuses = new Statuses();
statuses.statuses = savedValues.statusList;
statuses.pagination = new Pagination();
statuses.pagination.max_id = savedValues.statusList.get(savedValues.statusList.size() - 1).id;
statuses.pagination.min_id = savedValues.statusList.get(0).id;
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> initializeStatusesCommonView(statuses);
mainHandler.post(myRunnable);
} else {
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> {
// --- HOME TIMELINE --- // --- HOME TIMELINE ---
if (timelineType == Timeline.TimeLineEnum.HOME) { if (timelineType == Timeline.TimeLineEnum.HOME) {
//for more visibility it's done through loadHomeStrategy method //for more visibility it's done through loadHomeStrategy method
@ -508,6 +545,12 @@ public class FragmentMastodonTimeline extends Fragment {
flagLoading = false; flagLoading = false;
} }
} }
};
mainHandler.post(myRunnable);
}
}).start();
} }
/** /**