// Path: app/src/main/java/com/silver/dan/castdemo/widgetList/OnDragListener.java
// public interface OnDragListener {
// 
//     /**
//      * Called when a view is requesting a start of a drag.
//      *
//      * @param viewHolder The holder of the view to drag.
//      */
//     void onStartDrag(RecyclerView.ViewHolder viewHolder);
// }
// 
// Path: app/src/main/java/com/silver/dan/castdemo/widgetList/SimpleItemTouchHelperCallback.java
// public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback {
// 
//     private final ItemTouchHelperAdapter mAdapter;
// 
//     public SimpleItemTouchHelperCallback(ItemTouchHelperAdapter adapter) {
//         mAdapter = adapter;
//     }
// 
//     @Override
//     public boolean isLongPressDragEnabled() {
//         return false;
//     }
// 
//     @Override
//     public boolean isItemViewSwipeEnabled() {
//         return false;
//     }
// 
//     @Override
//     public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
//         final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
//         final int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
//         return makeMovementFlags(dragFlags, swipeFlags);
//     }
// 
//     @Override
//     public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) {
//         if (source.getItemViewType() != target.getItemViewType()) {
//             return false;
//         }
// 
//         mAdapter.onItemMove(source.getAdapterPosition(), target.getAdapterPosition());
//         return true;
//     }
// 
//     @Override
//     public void onSwiped(RecyclerView.ViewHolder viewHolder, int i) {
//         mAdapter.onItemDismiss(viewHolder.getAdapterPosition());
//     }
// 
//     @Override
//     public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
//         if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
//             ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
//             itemViewHolder.onItemSelected();
//         }
// 
//         super.onSelectedChanged(viewHolder, actionState);
//     }
// 
//     @Override
//     public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
//         super.clearView(recyclerView, viewHolder);
// 
//         ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
//         itemViewHolder.onItemClear();
//     }
// }
// 
// Path: app/src/main/java/com/silver/dan/castdemo/widgets/CanBeCreatedListener.java
// public abstract class CanBeCreatedListener {
//     private int requiredCondition;
// 
//     public abstract void onCanBeCreated();
// 
//     public boolean ifConditionsAreMet(int key) {
//         return key == requiredCondition;
//     }
// 
//     public void setRequestCallbackReturnCode(int condition) {
//         this.requiredCondition = condition;
//     }
// }
// 
// Path: app/src/main/java/com/silver/dan/castdemo/widgets/UIWidget.java
// abstract public class UIWidget {
//     public Widget widget;
//     public Context context;
// 
//     public abstract JSONObject getContent() throws JSONException;
// 
//     public int requestPermissions(Activity activity) {
//         return -1;
//     }
// 
//     public abstract WidgetSettingsFragment createSettingsFragment();
// 
// 
//     /*
//      * This method should return true if there are no prerequisites to the widget being created.
//      * Return false if the widget needs special permissions before it's created.
//      */
//     public boolean canBeCreated() {
//         return true;
//     }
// 
//     public abstract String getWidgetPreviewSecondaryHeader();
// 
//     public UIWidget(Context context, Widget widget) {
//         this.widget = widget;
//         this.context = context;
//     }
// 
//     public abstract void init();
// }


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.OvershootInterpolator;
import com.afollestad.materialdialogs.MaterialDialog;
import com.google.android.gms.cast.framework.CastSession;
import com.silver.dan.castdemo.widgetList.OnDragListener;
import com.silver.dan.castdemo.widgetList.SimpleItemTouchHelperCallback;
import com.silver.dan.castdemo.widgets.CanBeCreatedListener;
import com.silver.dan.castdemo.widgets.UIWidget;
import java.util.ArrayList;
import java.util.Iterator;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import jp.wasabeef.recyclerview.animators.SlideInUpAnimator;
        adapter = new WidgetListAdapter((MainActivity) getActivity(), this);
        MainActivity.getDashboard().onLoaded(getContext(), new OnCompleteCallback() {
            @Override
            public void onComplete() {
                adapter.setWidgets(MainActivity.getDashboard().getWidgetList());
                adapter.notifyItemRangeInserted(0, MainActivity.getDashboard().getWidgetList().size());
            }

            @Override
            public void onError(Exception e) {
                // @todo
            }
        });
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.widget_list, container, false);
        ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        widgetList.setAdapter(adapter);
        widgetList.setLayoutManager(new LinearLayoutManager(getContext()));
        widgetList.setItemAnimator(new SlideInUpAnimator(new OvershootInterpolator(1f)));

// Path: app/src/main/java/com/silver/dan/castdemo/widgetList/OnDragListener.java
// public interface OnDragListener {
// 
//     /**
//      * Called when a view is requesting a start of a drag.
//      *
//      * @param viewHolder The holder of the view to drag.
//      */
//     void onStartDrag(RecyclerView.ViewHolder viewHolder);
// }
// 
// Path: app/src/main/java/com/silver/dan/castdemo/widgetList/SimpleItemTouchHelperCallback.java
// public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback {
// 
//     private final ItemTouchHelperAdapter mAdapter;
// 
//     public SimpleItemTouchHelperCallback(ItemTouchHelperAdapter adapter) {
//         mAdapter = adapter;
//     }
// 
//     @Override
//     public boolean isLongPressDragEnabled() {
//         return false;
//     }
// 
//     @Override
//     public boolean isItemViewSwipeEnabled() {
//         return false;
//     }
// 
//     @Override
//     public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
//         final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
//         final int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
//         return makeMovementFlags(dragFlags, swipeFlags);
//     }
// 
//     @Override
//     public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) {
//         if (source.getItemViewType() != target.getItemViewType()) {
//             return false;
//         }
// 
//         mAdapter.onItemMove(source.getAdapterPosition(), target.getAdapterPosition());
//         return true;
//     }
// 
//     @Override
//     public void onSwiped(RecyclerView.ViewHolder viewHolder, int i) {
//         mAdapter.onItemDismiss(viewHolder.getAdapterPosition());
//     }
// 
//     @Override
//     public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
//         if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
//             ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
//             itemViewHolder.onItemSelected();
//         }
// 
//         super.onSelectedChanged(viewHolder, actionState);
//     }
// 
//     @Override
//     public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
//         super.clearView(recyclerView, viewHolder);
// 
//         ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
//         itemViewHolder.onItemClear();
//     }
// }
// 
// Path: app/src/main/java/com/silver/dan/castdemo/widgets/CanBeCreatedListener.java
// public abstract class CanBeCreatedListener {
//     private int requiredCondition;
// 
//     public abstract void onCanBeCreated();
// 
//     public boolean ifConditionsAreMet(int key) {
//         return key == requiredCondition;
//     }
// 
//     public void setRequestCallbackReturnCode(int condition) {
//         this.requiredCondition = condition;
//     }
// }
// 
// Path: app/src/main/java/com/silver/dan/castdemo/widgets/UIWidget.java
// abstract public class UIWidget {
//     public Widget widget;
//     public Context context;
// 
//     public abstract JSONObject getContent() throws JSONException;
// 
//     public int requestPermissions(Activity activity) {
//         return -1;
//     }
// 
//     public abstract WidgetSettingsFragment createSettingsFragment();
// 
// 
//     /*
//      * This method should return true if there are no prerequisites to the widget being created.
//      * Return false if the widget needs special permissions before it's created.
//      */
//     public boolean canBeCreated() {
//         return true;
//     }
// 
//     public abstract String getWidgetPreviewSecondaryHeader();
// 
//     public UIWidget(Context context, Widget widget) {
//         this.widget = widget;
//         this.context = context;
//     }
// 
//     public abstract void init();
// }


// Path: app/src/main/java/com/silver/dan/castdemo/WidgetList.java
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.OvershootInterpolator;
import com.afollestad.materialdialogs.MaterialDialog;
import com.google.android.gms.cast.framework.CastSession;
import com.silver.dan.castdemo.widgetList.OnDragListener;
import com.silver.dan.castdemo.widgetList.SimpleItemTouchHelperCallback;
import com.silver.dan.castdemo.widgets.CanBeCreatedListener;
import com.silver.dan.castdemo.widgets.UIWidget;
import java.util.ArrayList;
import java.util.Iterator;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import jp.wasabeef.recyclerview.animators.SlideInUpAnimator;

        adapter = new WidgetListAdapter((MainActivity) getActivity(), this);
        MainActivity.getDashboard().onLoaded(getContext(), new OnCompleteCallback() {
            @Override
            public void onComplete() {
                adapter.setWidgets(MainActivity.getDashboard().getWidgetList());
                adapter.notifyItemRangeInserted(0, MainActivity.getDashboard().getWidgetList().size());
            }

            @Override
            public void onError(Exception e) {
                // @todo
            }
        });
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.widget_list, container, false);
        ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        widgetList.setAdapter(adapter);
        widgetList.setLayoutManager(new LinearLayoutManager(getContext()));
        widgetList.setItemAnimator(new SlideInUpAnimator(new OvershootInterpolator(1f)));

        ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(adapter);