package io.github.rayanperoumal.droiddesigner.file;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.io.File;
import java.io.IOException;

import io.github.rayanperoumal.droiddesigner.R;

/**
 * Created by rayanperoumal on 05/10/2017.
 */

public class FileListView extends RecyclerView{
    private static File [] files;
    private final File parent;

    public FileListView(Context context, File parent) {
        super(context);
        this.parent = parent;
        files = catchFiles(parent);
        setLayoutManager(new LinearLayoutManager(context));
        setAdapter(new FileListViewAdapter(context));
    }

    public File[] catchFiles(File file){
        if(file.exists()){
            File[] list= file.listFiles();
            for(int i=0;i<list.length;i++){
                try {
                    Log.i("Project:found:",list[i].getCanonicalPath());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return list;
        }
        return new File[]{file};
    }

    public class FileListViewAdapter extends RecyclerView.Adapter<FileListViewAdapter.ViewHolder>{
        public Context context;
        ViewHolder viewHolder;
        public FileListViewAdapter(Context context){
            this.context = context;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_file,parent,false);
            viewHolder = new ViewHolder(view);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.display(position);
        }

        @Override
        public int getItemCount() {
            return files.length;

        }


        public class ViewHolder extends RecyclerView.ViewHolder{
            public TextView text;
            public View view;
            public ViewHolder(View itemView) {
                super(itemView);
                view = itemView;
                text  = (TextView) itemView.findViewById(R.id.file_name);
            }

            public void display(int position){
                String name = files[position].getAbsolutePath();
                text.setText(name);
                Log.i("Recycler:display","file:"+name);

            }
        }
    }

}

@@ -563,16 +563,60 @@
  files;%0A
+    public static OnClickListener listener;%0A
     priv
@@ -1854,24 +1854,237 @@
 lder(view);%0A
+            if(listener==null)%7B%0A                listener = view1 -%3E%0A                        Log.i(%22FileListView%22,%22On click works%22);%0A            %7D%0A%0A            if(listener!=null) view.setOnClickListener(listener);%0A
             
@@ -2351,17 +2351,16 @@
      %7D%0A%0A
-%0A
         

Handle click event on a File is OK