Merge pull request #757 from nextcloud/fix-scroll-position-reset

fix location reset of browser by orientation changes
This commit is contained in:
David Luhmer 2019-04-29 20:20:18 -03:00 committed by GitHub
commit 66bbcfff01
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 76 additions and 60 deletions

View file

@ -43,8 +43,8 @@
</activity>
<activity
android:name=".NewsDetailActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="@string/title_activity_news_detail" />
<!-- android:configChanges="keyboardHidden|orientation|screenSize" -->
<activity
android:name=".SettingsActivity"
android:configChanges="keyboardHidden|orientation|screenSize"

View file

@ -21,14 +21,13 @@
package de.luhmer.owncloudnewsreader;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import androidx.browser.customtabs.CustomTabsIntent;
import androidx.core.content.ContextCompat;
@ -144,8 +143,9 @@ public class NewsDetailActivity extends PodcastFragmentActivity {
getSupportActionBar().setTitle(rssItem.getTitle());
break;
}
else
else {
item_id++;
}
}
}
@ -162,7 +162,10 @@ public class NewsDetailActivity extends PodcastFragmentActivity {
try {
mViewPager.setCurrentItem(item_id, true);
pageChanged(item_id);
if(savedInstanceState == null) {
// Only do that when activity is started for the first time. Not on orientation changes etc..
pageChanged(item_id);
}
} catch(Exception ex) {
ex.printStackTrace();
}
@ -247,23 +250,25 @@ public class NewsDetailActivity extends PodcastFragmentActivity {
private void pageChanged(int position)
{
StopVideoOnCurrentPage();
stopVideoOnCurrentPage();
currentPosition = position;
ResumeVideoPlayersOnCurrentPage();
resumeVideoPlayersOnCurrentPage();
progressIndicator.setProgress(position + 1);
getSupportActionBar().setTitle(rssItems.get(position).getTitle());
if(!rssItems.get(position).getRead_temp())
{
markItemAsReadUnread(rssItems.get(position), true);
RssItem rssItem = rssItems.get(position);
if(!rssItem.getRead_temp()) {
if (!NewsReaderListActivity.stayUnreadItems.contains(rssItem.getId())) {
markItemAsReadOrUnread(rssItems.get(position), true);
}
mPostDelayHandler.delayTimer();
mPostDelayHandler.delayTimer();
Log.v("PAGE CHANGED", "PAGE: " + position + " - IDFEED: " + rssItems.get(position).getId());
}
else { //Only in else because the function markItemAsReas updates the ActionBar items as well
UpdateActionBarIcons();
Log.v("PAGE CHANGED", "PAGE: " + position + " - IDFEED: " + rssItems.get(position).getId());
}
else {
updateActionBarIcons();
}
}
@ -274,33 +279,30 @@ public class NewsDetailActivity extends PodcastFragmentActivity {
return null;
}
private void ResumeVideoPlayersOnCurrentPage()
{
private void resumeVideoPlayersOnCurrentPage() {
NewsDetailFragment fragment = getNewsDetailFragmentAtPosition(currentPosition);
if(fragment != null) // could be null if not instantiated yet
fragment.resumeCurrentPage();
if(fragment != null) { // could be null if not instantiated yet
fragment.resumeCurrentPage();
}
}
private void StopVideoOnCurrentPage()
{
private void stopVideoOnCurrentPage() {
NewsDetailFragment fragment = getNewsDetailFragmentAtPosition(currentPosition);
if(fragment != null) // could be null if not instantiated yet
fragment.pauseCurrentPage();
if(fragment != null) { // could be null if not instantiated yet
fragment.pauseCurrentPage();
}
}
public void UpdateActionBarIcons()
{
public void updateActionBarIcons() {
RssItem rssItem = rssItems.get(currentPosition);
boolean isStarred = rssItem.getStarred_temp();
boolean isRead = rssItem.getRead_temp();
PodcastItem podcastItem = DatabaseConnectionOrm.ParsePodcastItemFromRssItem(this, rssItem);
boolean podcastAvailable = !"".equals(podcastItem.link);
if(menuItem_PlayPodcast != null)
menuItem_PlayPodcast.setVisible(podcastAvailable);
@ -310,8 +312,6 @@ public class NewsDetailActivity extends PodcastFragmentActivity {
else if(menuItem_Starred != null)
menuItem_Starred.setIcon(R.drawable.ic_action_star_border_dark);
if(isRead && menuItem_Read != null) {
menuItem_Read.setIcon(R.drawable.ic_check_box_white);
menuItem_Read.setChecked(true);
@ -357,7 +357,7 @@ public class NewsDetailActivity extends PodcastFragmentActivity {
}
}
UpdateActionBarIcons();
updateActionBarIcons();
return true;
}
@ -372,8 +372,8 @@ public class NewsDetailActivity extends PodcastFragmentActivity {
return true;
case R.id.action_read:
markItemAsReadUnread(rssItem, !menuItem_Read.isChecked());
UpdateActionBarIcons();
markItemAsReadOrUnread(rssItem, !menuItem_Read.isChecked());
updateActionBarIcons();
mPostDelayHandler.delayTimer();
break;
@ -382,7 +382,7 @@ public class NewsDetailActivity extends PodcastFragmentActivity {
rssItem.setStarred_temp(!curState);
dbConn.updateRssItem(rssItem);
UpdateActionBarIcons();
updateActionBarIcons();
mPostDelayHandler.delayTimer();
break;
@ -489,10 +489,12 @@ public class NewsDetailActivity extends PodcastFragmentActivity {
return (resolveInfo.loadLabel(getPackageManager()).toString().contains("Chrome"));
}
private void markItemAsReadUnread(RssItem item, boolean read) {
private void markItemAsReadOrUnread(RssItem item, boolean read) {
NewsReaderListActivity.stayUnreadItems.add(item.getId());
item.setRead_temp(read);
dbConn.updateRssItem(item);
UpdateActionBarIcons();
dbConn.updateRssItem(item);
updateActionBarIcons();
}
@Override
@ -510,20 +512,19 @@ public class NewsDetailActivity extends PodcastFragmentActivity {
//public class SectionsPagerAdapter extends FragmentPagerAdapter {
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
SparseArray<WeakReference<NewsDetailFragment>> items = new SparseArray<>();
SparseArray<WeakReference<NewsDetailFragment>> items = new SparseArray<>();
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
if(fm.getFragments() != null) {
for (Fragment fragment : fm.getFragments()) {
if (fragment instanceof NewsDetailFragment) {
int id = ((NewsDetailFragment) fragment).getSectionNumber();
items.put(id, new WeakReference<>((NewsDetailFragment) fragment));
}
}
}
}
for (Fragment fragment : fm.getFragments()) {
if (fragment instanceof NewsDetailFragment) {
int id = ((NewsDetailFragment) fragment).getSectionNumber();
Log.v(TAG, "Retaining NewsDetailFragment with ID: " + id);
items.put(id, new WeakReference<>((NewsDetailFragment) fragment));
}
}
}
@Override
public Fragment getItem(int position) {
@ -545,8 +546,7 @@ public class NewsDetailActivity extends PodcastFragmentActivity {
}
@Override
public void destroyItem(ViewGroup container, int position, Object object)
{
public void destroyItem(ViewGroup container, int position, Object object) {
items.remove(position);
super.destroyItem(container, position, object);

View file

@ -90,9 +90,7 @@ public class NewsDetailFragment extends Fragment implements RssItemToHtmlTask.Li
boolean changedUrl = false;
public NewsDetailFragment() {
//setRetainInstance(true);
}
public NewsDetailFragment() { }
public int getSectionNumber() {
return section_number;
@ -102,6 +100,10 @@ public class NewsDetailFragment extends Fragment implements RssItemToHtmlTask.Li
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((NewsReaderApplication) getActivity().getApplication()).getAppComponent().injectFragment(this);
// Retain this fragment across configuration changes.
setRetainInstance(true);
}
@Override
@ -170,11 +172,22 @@ public class NewsDetailFragment extends Fragment implements RssItemToHtmlTask.Li
ButterKnife.bind(this, rootView);
startLoadRssItemToWebViewTask();
// Do not reload webview if retained
if(savedInstanceState == null) {
startLoadRssItemToWebViewTask();
} else {
mWebView.restoreState(savedInstanceState);
mProgressBarLoading.setVisibility(View.GONE);
}
return rootView;
}
@Override
public void onSaveInstanceState(Bundle outState) {
mWebView.saveState(outState);
}
private void startLoadRssItemToWebViewTask() {
Log.d(TAG, "startLoadRssItemToWebViewTask() called");
mWebView.setVisibility(View.GONE);

View file

@ -61,6 +61,7 @@ import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.TimeUnit;
@ -138,6 +139,8 @@ public class NewsReaderListActivity extends PodcastFragmentActivity implements
public static final String ITEM_ID = "ITEM_ID";
public static final String TITEL = "TITEL";
public static HashSet<Long> stayUnreadItems = new HashSet<>();
private static MenuItem menuItemUpdater;
private static MenuItem menuItemDownloadMoreItems;

View file

@ -18,6 +18,7 @@ import java.util.List;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import de.luhmer.owncloudnewsreader.NewsReaderListActivity;
import de.luhmer.owncloudnewsreader.R;
import de.luhmer.owncloudnewsreader.SettingsActivity;
import de.luhmer.owncloudnewsreader.database.DatabaseConnectionOrm;
@ -42,7 +43,6 @@ public class NewsListRecyclerAdapter extends RecyclerView.Adapter {
private DatabaseConnectionOrm dbConn;
private PostDelayHandler pDelayHandler;
private FragmentActivity activity;
private HashSet<Long> stayUnreadItems = new HashSet<>();
private int totalItemCount = 0;
private int cachedPages = 1;
@ -214,7 +214,7 @@ public class NewsListRecyclerAdapter extends RecyclerView.Adapter {
final ViewHolder holder = (ViewHolder) viewHolder;
RssItem item = lazyList.get(position);
holder.setRssItem(item);
holder.setStayUnread(stayUnreadItems.contains(item.getId()));
holder.setStayUnread(NewsReaderListActivity.stayUnreadItems.contains(item.getId()));
//Podcast stuff
if (DatabaseConnectionOrm.ALLOWED_PODCASTS_TYPES.contains(item.getEnclosureMime())) {
@ -252,7 +252,7 @@ public class NewsListRecyclerAdapter extends RecyclerView.Adapter {
viewHolder.setReadState(isChecked);
//notifyItemChanged(viewHolder.getAdapterPosition());
stayUnreadItems.add(rssItem.getId());
NewsReaderListActivity.stayUnreadItems.add(rssItem.getId());
}
}
@ -309,13 +309,13 @@ public class NewsListRecyclerAdapter extends RecyclerView.Adapter {
public void updateAdapterData(List<RssItem> rssItems) {
stayUnreadItems.clear();
NewsReaderListActivity.stayUnreadItems.clear();
cachedPages = 1;
if (this.lazyList != null) {
//if (this.lazyList != null) {
//this.lazyList.close();
}
//}
//new ReloadAdapterAsyncTask().execute();
totalItemCount = ((Long) dbConn.getCurrentRssItemViewCount()).intValue();