package com.martindisch.chronoscopy.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.martindisch.chronoscopy.R;
import com.martindisch.chronoscopy.logic.ChrIndividual;
import com.martindisch.chronoscopy.logic.ChrUsage;
import com.martindisch.chronoscopy.logic.Util;

import java.util.List;

public class UsagesFragment extends Fragment {

    private RecyclerView mRvUsages;
    private UsageAdapter mAdapter;

    public UsagesFragment() {
        // Required empty public constructor
    }

    public static UsagesFragment newInstance() {
        return new UsagesFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_usages, container, false);
        // Get views
        mRvUsages = (RecyclerView) view.findViewById(R.id.usages_rvUsages);

        // Prepare RecyclerView
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRvUsages.setLayoutManager(layoutManager);
        mRvUsages.addItemDecoration(new DividerItemDecoration(getActivity(),
                layoutManager.getOrientation()));
        mRvUsages.setHasFixedSize(true);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        // Request from DB off UI thread
        new Thread(new Runnable() {
            @Override
            public void run() {
                // Read usages from DB
                List<ChrUsage> usages = ChrUsage.find(
                        ChrUsage.class, null, null, null, "date DESC, id DESC", null
                );
                // Build ChrIndividual from SharedPreferences
                ChrIndividual individual = Util.getIndividual(getContext());
                mAdapter = new UsageAdapter(usages, individual);
                // Update RecyclerView in UI thread
                mRvUsages.post(new Runnable() {
                    @Override
                    public void run() {
                        mRvUsages.setAdapter(mAdapter);
                    }
                });
            }
        }).start();
    }
}

@@ -253,24 +253,81 @@
 cyclerView;%0A
+import android.support.v7.widget.helper.ItemTouchHelper;%0A
 import andro
@@ -346,24 +346,24 @@
 utInflater;%0A
-
 import andro
@@ -743,16 +743,52 @@
 Adapter;
+%0A    private List%3CChrUsage%3E mUsages;
 %0A%0A    pu
@@ -1730,24 +1730,1167 @@
 Size(true);%0A
+        ItemTouchHelper.SimpleCallback swipeCallback = new ItemTouchHelper.SimpleCallback(%0A                0, ItemTouchHelper.LEFT %7C ItemTouchHelper.RIGHT) %7B%0A            @Override%0A            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,%0A                                  RecyclerView.ViewHolder target) %7B%0A                return false;%0A            %7D%0A%0A            @Override%0A            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) %7B%0A                int position = viewHolder.getAdapterPosition();%0A                // Get usage that was swiped away%0A                ChrUsage toDelete = mUsages.get(position);%0A                // Delete the usage from DB%0A                ChrUsage.delete(toDelete);%0A                // Remove the usage from the list used by the adapter%0A                mUsages.remove(position);%0A                // Notify adapter of removal%0A                mRvUsages.getAdapter().notifyItemRemoved(position);%0A            %7D%0A        %7D;%0A        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(swipeCallback);%0A        itemTouchHelper.attachToRecyclerView(mRvUsages);%0A
         retu
@@ -3164,24 +3164,10 @@
     
-List%3CChrUsage%3E u
+mU
 sage
@@ -3472,17 +3472,18 @@
 Adapter(
-u
+mU
 sages, i

Add ItemTouchHelper for deleting a usage by swiping