Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b478f387 authored by Kirill Rakhman's avatar Kirill Rakhman
Browse files

Save scrolling position and restore it when going back

Patch 1-3: Clean up
Patch 4: Don't scroll in the wrong direction
Patch 5: Remove logging and fix indentation

Change-Id: I5dfa92829a11ce94c4df9bb9891715b48f2b0900
parent c98ff70b
Loading
Loading
Loading
Loading
+46 −11
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import com.cyanogenmod.filemanager.model.FileSystemObject;
import com.cyanogenmod.filemanager.util.FileHelper;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@@ -40,6 +41,7 @@ public class NavigationViewInfoParcelable extends HistoryNavigable {
    private boolean mChRooted;
    private List<FileSystemObject> mFiles;
    private List<FileSystemObject> mSelectedFiles;
    private FileSystemObject mFirstVisible;

    /**
     * Constructor of <code>NavigationViewInfoParcelable</code>.
@@ -167,6 +169,24 @@ public class NavigationViewInfoParcelable extends HistoryNavigable {
        this.mSelectedFiles = selectedFiles;
    }

    /**
     * Method that returns the first visible file in the list.
     *
     * @return {@link FileSystemObject} The index of the first visible file
     */
    public FileSystemObject getFirstVisible() {
        return mFirstVisible;
    }

    /**
     * Method that sets the first visible file.
     *
     * @param {@link FileSystemObject} The index of the first visible file
     */
    public void setFirstVisible(FileSystemObject firstVisible) {
        mFirstVisible = firstVisible;
    }

    /**
     * {@inheritDoc}
     */
@@ -199,6 +219,12 @@ public class NavigationViewInfoParcelable extends HistoryNavigable {
        if (this.mFiles != null) {
            dest.writeList(this.mFiles);
        }

        // - 5
        dest.writeInt(this.mFirstVisible == null ? 0 : 1);
        if (this.mFirstVisible != null) {
            dest.writeSerializable(mFirstVisible);
        }
    }

    /**
@@ -230,6 +256,15 @@ public class NavigationViewInfoParcelable extends HistoryNavigable {
            in.readList(files, NavigationViewInfoParcelable.class.getClassLoader());
            this.mFiles = new ArrayList<FileSystemObject>(files);
        }

        // - 5
        int hasFirstVisible = in.readInt();
        if (hasFirstVisible == 1) {
            Serializable readSerializable = in.readSerializable();
            if (readSerializable instanceof FileSystemObject) {
                this.mFirstVisible = (FileSystemObject) readSerializable;
            }
        }
    }

   /**
+81 −59
Original line number Diff line number Diff line
@@ -493,6 +493,14 @@ public class NavigationView extends RelativeLayout implements
        parcel.setChRooted(this.mChRooted);
        parcel.setSelectedFiles(this.mAdapter.getSelectedItems());
        parcel.setFiles(this.mFiles);

        int firstVisiblePosition = mAdapterView.getFirstVisiblePosition();
        if (firstVisiblePosition >= 0 && firstVisiblePosition < mAdapter.getCount()) {
            FileSystemObject firstVisible = mAdapter
                    .getItem(firstVisiblePosition);
            parcel.setFirstVisible(firstVisible);
        }

        return parcel;
    }

@@ -510,8 +518,10 @@ public class NavigationView extends RelativeLayout implements
        this.mFiles = info.getFiles();
        this.mAdapter.setSelectedItems(info.getSelectedFiles());

        final FileSystemObject firstVisible = info.getFirstVisible();

        //Update the views
        refresh();
        refresh(firstVisible);
        return true;
    }

@@ -722,18 +732,33 @@ public class NavigationView extends RelativeLayout implements
     *
     * @param fso The file system object
     */
    public void scrollTo(FileSystemObject fso) {
    public void scrollTo(final FileSystemObject fso) {

        this.mAdapterView.post(new Runnable() {

            @Override
            public void run() {
                if (fso != null) {
                    try {
                int position = this.mAdapter.getPosition(fso);
                this.mAdapterView.setSelection(position);
                        int position = mAdapter.getPosition(fso);
                        mAdapterView.setSelection(position);

                        // Make the scrollbar appear
                        if (position > 0) {
                            mAdapterView.scrollBy(0, 1);
                            mAdapterView.scrollBy(0, -1);
                        }

                    } catch (Exception e) {
                this.mAdapterView.setSelection(0);
                        mAdapterView.setSelection(0);
                    }
                } else {
            this.mAdapterView.setSelection(0);
                    mAdapterView.setSelection(0);
                }
            }
        });

    }

    /**
     * Method that refresh the view data.
@@ -1007,13 +1032,6 @@ public class NavigationView extends RelativeLayout implements
                }
            }

            //Load the data
            loadData(sortedFiles);
            this.mFiles = sortedFiles;
            if (searchInfo != null) {
                searchInfo.setSuccessNavigation(true);
            }

            //Add to history?
            if (addToHistory && hasChanged && isNewHistory) {
                if (this.mOnHistoryListener != null) {
@@ -1022,15 +1040,20 @@ public class NavigationView extends RelativeLayout implements
                }
            }

            //Load the data
            loadData(sortedFiles);
            this.mFiles = sortedFiles;
            if (searchInfo != null) {
                searchInfo.setSuccessNavigation(true);
            }

            //Change the breadcrumb
            if (this.mBreadcrumb != null) {
                this.mBreadcrumb.changeBreadcrumbPath(newDir, this.mChRooted);
            }

            //Scroll to object?
            if (scrollTo != null) {
            //If scrollTo is null, the position will be set to 0
            scrollTo(scrollTo);
            }

            //The current directory is now the "newDir"
            this.mCurrentDir = newDir;
@@ -1069,7 +1092,6 @@ public class NavigationView extends RelativeLayout implements
        adapter.clear();
        adapter.addAll(files);
        adapter.notifyDataSetChanged();
        view.setSelection(0);
    }

    /**