package es.craftsmanship.toledo.katangapp.activities;

import es.craftsmanship.toledo.katangapp.adapters.FavoritesAdapter;
import es.craftsmanship.toledo.katangapp.db.FavoriteDAO;
import es.craftsmanship.toledo.katangapp.db.model.Favorite;
import es.craftsmanship.toledo.katangapp.models.BusStopResult;
import es.craftsmanship.toledo.katangapp.models.QueryResult;

import android.content.Intent;

import android.os.Bundle;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.squareup.otto.Subscribe;

import java.util.List;

/**
 * @author Manuel de la Peña
 */
public class FavoritesActivity extends BaseAndroidBusRegistrableActivity {

    private RecyclerView recyclerView;

    @Subscribe
    public void favoritesReceived(QueryResult queryResult) {
        Intent intent = new Intent(FavoritesActivity.this, ShowBusStopsActivity.class);

        intent.putExtra("queryResult", queryResult);
        intent.putExtra("favorites", true);

        List<BusStopResult> results = queryResult.getResults();

        // there should be only one
        BusStopResult busStopResult = results.get(0);

        intent.putExtra("busStop", busStopResult.getBusStop());

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        getApplicationContext().startActivity(intent);
    }

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

        setContentView(R.layout.favorites);

        try(FavoriteDAO favoriteDAO = new FavoriteDAO(this)) {
            favoriteDAO.open();

            List<Favorite> favorites = favoriteDAO.getAllFavorites();

            recyclerView = (RecyclerView) findViewById(R.id.favoritesList);

            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            recyclerView.setAdapter(new FavoritesAdapter(favorites));
        }

    }

}
@@ -112,24 +112,103 @@
 tesAdapter;%0A
+import es.craftsmanship.toledo.katangapp.adapters.DismissFavoritesTouchHelper;%0A
 import es.cr
@@ -596,16 +596,73 @@
 lerView;
+%0Aimport android.support.v7.widget.helper.ItemTouchHelper;
 %0A%0Aimport
@@ -1955,48 +1955,281 @@
 ));%0A
-            recyclerView.setAdapter(new 
+%0A            FavoritesAdapter favoritesAdapter = new FavoritesAdapter(favorites);%0A%0A            setUpDismissFavoritesTouchGesture(favoritesAdapter);%0A%0A            recyclerView.setAdapter(favoritesAdapter);%0A        %7D%0A%0A    %7D%0A%0A    private void setUpDismissFavoritesTouchGesture(
 Favo
@@ -2232,33 +2232,33 @@
 FavoritesAdapter
-(
+ 
 favorites));%0A   
@@ -2254,20 +2254,123 @@
 ites
-)
+Adapter) %7B%0A        ItemTouchHelper.Callback callback = new DismissFavoritesTouchHelper(favoritesAdapter
 );%0A
+%0A
         
 %7D%0A%0A 
@@ -2357,27 +2357,132 @@
 pter);%0A%0A        
-%7D%0A
+ItemTouchHelper helper = new ItemTouchHelper(callback);%0A%0A        helper.attachToRecyclerView(recyclerView);
 %0A    %7D%0A%0A%7D

Attach the swipe gesture to the favorites adapter