package agersant.polaris.activity.browse;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.android.volley.Response;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

import agersant.polaris.R;
import agersant.polaris.api.ServerAPI;

public class BrowseActivity extends AppCompatActivity {

    private ExplorerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browse);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.browse_recycler_view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);

        adapter = new ExplorerAdapter(this);
        recyclerView.setAdapter(adapter);

        browseTo("");
    }

    void browseTo(String path) {
        final BrowseActivity that = this;
        Response.Listener<JSONArray> success = new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                that.onReceiveContent(response);
            }
        };

        ServerAPI server = ServerAPI.getInstance(getApplicationContext());
        server.browse(path, success);
    }

    private void onReceiveContent(JSONArray content) {
        ArrayList<ExplorerItem> newItems = new ArrayList<>();
        for (int i = 0; i < content.length(); i++) {
            try {
                JSONObject item = content.getJSONObject(i);
                boolean isDirectory = item.getString("variant").equals("Directory");
                JSONObject fields = item.getJSONArray("fields").getJSONObject(0);
                String name = fields.getString("path");
                ExplorerItem explorerItem = new ExplorerItem(name, isDirectory);
                newItems.add(explorerItem);
            } catch (Exception e) {
                System.err.println("Unexpected response structure");
            }
        }
        adapter.setItems(newItems);
    }
}

@@ -333,16 +333,40 @@
 rayList;
+%0Aimport java.util.Stack;
 %0A%0Aimport
@@ -519,16 +519,54 @@
 adapter;
+%0A    private Stack%3CJSONArray%3E history;
 %0A%0A    @O
@@ -1059,58 +1059,140 @@
     
-browseTo(%22%22);%0A    %7D%0A%0A    void browseTo(String path
+history = new Stack%3C%3E();%0A        browseTo(%22%22);%0A    %7D%0A%0A    @Override%0A    public void onBackPressed() %7B%0A        if (history.size() %3C 2
 ) %7B%0A
@@ -1203,41 +1203,168 @@
     
+   
-final BrowseActivity that = this;
+ super.onBackPressed();%0A        %7D else %7B%0A            history.pop();%0A            setContent(history.peek());%0A        %7D%0A    %7D%0A%0A    void browseTo(String path) %7B
 %0A   
@@ -1543,22 +1543,51 @@
     
-that.onReceive
+history.push(response);%0A                set
 Cont
@@ -1768,17 +1768,11 @@
 oid 
-onReceive
+set
 Cont

Allow back navigation in explorer