Skip to content
This repository has been archived by the owner on Aug 22, 2020. It is now read-only.

Commit

Permalink
View should save/restore its state.
Browse files Browse the repository at this point in the history
Signed-off-by: Fung <[email protected]>
  • Loading branch information
fython committed Jul 24, 2017
1 parent 44527e1 commit d1712b1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
Expand All @@ -16,9 +17,7 @@ public class SimpleFileManagerActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private FileManagerAdapter mAdapter;

private FileList currentList = null;

private String currentLocation = Environment.getExternalStorageDirectory().getAbsolutePath();
private String currentLocation;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -28,6 +27,8 @@ protected void onCreate(Bundle savedInstanceState) {
setSupportActionBar(toolbar);

mRecyclerView = findViewById(R.id.recycler_view);
mBreadcrumbsView = findViewById(R.id.breadcrumbs_view);

mAdapter = new FileManagerAdapter();
mRecyclerView.setAdapter(mAdapter);
mAdapter.setCallback(new FileManagerAdapter.Callback() {
Expand All @@ -36,17 +37,14 @@ public void onItemClick(FileList.FileWrapper file) {
if (file.isDirectory()) {
BreadcrumbItem breadcrumbItem = new BreadcrumbItem(mAdapter.getFileList().getDirectoriesString());
breadcrumbItem.setSelectedItem(file.toString());
mBreadcrumbsView.addItem(breadcrumbItem);
currentLocation = getCurrentPath();
new LoadTask().execute(currentLocation);
currentLocation = getCurrentPath() + "/" + file.toString();
new LoadTask(breadcrumbItem).execute(currentLocation);
} else if (file.isFile()) {
// Nothing happen lol
}
}
});

mBreadcrumbsView = findViewById(R.id.breadcrumbs_view);
mBreadcrumbsView.addItem(BreadcrumbItem.createSimpleItem("External Storage"));
mBreadcrumbsView.setCallback(new DefaultBreadcrumbsCallback() {
@Override
public void onNavigateBack(BreadcrumbItem item, int position) {
Expand All @@ -61,6 +59,17 @@ public void onNavigateNewLocation(BreadcrumbItem newItem, int changedPosition) {
}
});

if (savedInstanceState == null) {
mBreadcrumbsView.addItem(BreadcrumbItem.createSimpleItem("External Storage"));
currentLocation = getCurrentPath();
new LoadTask().execute(currentLocation);
}
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
currentLocation = getCurrentPath();
new LoadTask().execute(currentLocation);
}

Expand Down Expand Up @@ -90,6 +99,14 @@ public void onBackPressed() {

private class LoadTask extends AsyncTask<String, Void, FileList> {

private BreadcrumbItem nextItem;

LoadTask() {}

LoadTask(BreadcrumbItem nextItem) {
this.nextItem = nextItem;
}

@Override
protected FileList doInBackground(String... path) {
try {
Expand All @@ -105,6 +122,12 @@ protected void onPostExecute(FileList list) {
if (list != null) {
mAdapter.setFileList(list);
mAdapter.notifyDataSetChanged();
if (nextItem != null) {
mBreadcrumbsView.addItem(nextItem);
}
} else if (nextItem != null) {
Snackbar.make(findViewById(R.id.coordinator_layout), "Something wrong", Snackbar.LENGTH_SHORT)
.show();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class BreadcrumbsAdapter extends RecyclerView.Adapter<BreadcrumbsAdapter.ItemHol

private final int DROPDOWN_OFFSET_Y_FIX;

private List<BreadcrumbItem> items;
private ArrayList<BreadcrumbItem> items;
private BreadcrumbsCallback callback;

private BreadcrumbsView parent;
Expand All @@ -33,7 +33,7 @@ public BreadcrumbsAdapter(BreadcrumbsView parent, ArrayList<BreadcrumbItem> item
DROPDOWN_OFFSET_Y_FIX = parent.getResources().getDimensionPixelOffset(R.dimen.dropdown_offset_y_fix_value);
}

public List<BreadcrumbItem> getItems() {
public ArrayList<BreadcrumbItem> getItems() {
return this.items;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
Expand Down Expand Up @@ -63,7 +65,7 @@ private void init() {
mRecyclerView.setAdapter(mAdapter);
}

public @Nullable List<BreadcrumbItem> getItems() {
public @Nullable ArrayList<BreadcrumbItem> getItems() {
return mAdapter.getItems();
}

Expand Down Expand Up @@ -130,4 +132,41 @@ public BreadcrumbsCallback getCallback() {
return mAdapter.getCallback();
}

@Override
public Parcelable onSaveInstanceState() {
Bundle bundle = new Bundle();
State state = new State(super.onSaveInstanceState(), getItems());
bundle.putParcelable(State.STATE, state);
return bundle;
}

@Override
public void onRestoreInstanceState(Parcelable state) {
if (state instanceof Bundle) {
Bundle bundle = (Bundle) state;
State viewState = bundle.getParcelable(State.STATE);
super.onRestoreInstanceState(viewState.getSuperState());
setItems(viewState.getItems());
return;
}
super.onRestoreInstanceState(BaseSavedState.EMPTY_STATE);
}

protected static class State extends BaseSavedState {

private static final String STATE = BreadcrumbsView.class.getSimpleName() + ".STATE";

private final ArrayList<BreadcrumbItem> items;

State(Parcelable superState, ArrayList<BreadcrumbItem> items) {
super(superState);
this.items = items;
}

ArrayList<BreadcrumbItem> getItems() {
return this.items;
}

}

}

0 comments on commit d1712b1

Please sign in to comment.