package com.cwainner.chris.recipecentral.ui;

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

import com.cwainner.chris.recipecentral.Constants;
import com.cwainner.chris.recipecentral.R;
import com.cwainner.chris.recipecentral.adapters.FirebaseRecipeViewHolder;
import com.cwainner.chris.recipecentral.models.Recipe;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import butterknife.Bind;
import butterknife.ButterKnife;

public class SavedRecipeListActivity extends AppCompatActivity {
    private DatabaseReference recipeReference;
    private FirebaseRecyclerAdapter firebaseAdapter;

    @Bind(R.id.recyclerView) RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_recipes);
        ButterKnife.bind(this);

        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        String uid = user.getUid();

        recipeReference = FirebaseDatabase
                .getInstance()
                .getReference(Constants.FIREBASE_CHILD_RECIPES)
                .child(uid);

        setUpFirebaseAdapter();
    }

    private void setUpFirebaseAdapter(){
        firebaseAdapter = new FirebaseRecyclerAdapter<Recipe, FirebaseRecipeViewHolder>
                (Recipe.class, R.layout.recipe_list_item_drag, FirebaseRecipeViewHolder.class, recipeReference) {
            @Override
            protected void populateViewHolder(FirebaseRecipeViewHolder viewHolder, Recipe model, int position) {
                viewHolder.bindRecipe(model);
            }
        };
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(firebaseAdapter);
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        firebaseAdapter.cleanup();
    }
}

@@ -266,16 +266,73 @@
 lerView;
+%0Aimport android.support.v7.widget.helper.ItemTouchHelper;
 %0A%0Aimport
@@ -419,16 +419,92 @@
 tral.R;%0A
+import com.cwainner.chris.recipecentral.adapters.FirebaseRecipeListAdapter;%0A
 import c
@@ -625,16 +625,82 @@
 Recipe;%0A
+import com.cwainner.chris.recipecentral.util.OnStartDragListener;%0A
 import c
@@ -1071,16 +1071,46 @@
 ctivity 
+implements OnStartDragListener
 %7B%0A    pr
@@ -1202,16 +1202,61 @@
 Adapter;
+%0A    private ItemTouchHelper itemTouchHelper;
 %0A%0A    @B
@@ -1502,24 +1502,104 @@
 ind(this);%0A%0A
+        setUpFirebaseAdapter();%0A    %7D%0A%0A    private void setUpFirebaseAdapter()%7B%0A
         Fire
@@ -1876,487 +1876,164 @@
     
-setUpFirebaseAdapter();%0A    %7D%0A%0A    private void setUpFirebaseAdapter()%7B%0A        firebaseAdapter = new FirebaseRecyclerAdapter%3CRecipe, FirebaseRecipeViewHolder%3E%0A                (Recipe.class, R.layout.recipe_list_item_drag, FirebaseRecipeViewHolder.class, recipeReference) %7B%0A            @Override%0A            protected void populateViewHolder(FirebaseRecipeViewHolder viewHolder, Recipe model, int position) %7B%0A                viewHolder.bindRecipe(model);%0A            %7D%0A        %7D;
+firebaseAdapter = new FirebaseRecipeListAdapter(Recipe.class, R.layout.recipe_list_item_drag, FirebaseRecipeViewHolder.class, recipeReference, this, this);%0A
 %0A   
@@ -2313,11 +2313,144 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public void onStartDrag(RecyclerView.ViewHolder viewHolder)%7B%0A        itemTouchHelper.startDrag(viewHolder);%0A    %7D
 %0A%7D%0A

Add new FirebaseRecipeListAdapter to SaveRecipeListActivity