package es.craftsmanship.toledo.katangapp.activities;

import es.craftsmanship.toledo.katangapp.db.FavoriteDAO;
import es.craftsmanship.toledo.katangapp.db.model.Favorite;
import es.craftsmanship.toledo.katangapp.models.BusStop;

import android.content.Intent;

import android.os.Bundle;

import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import android.view.View;

/**
 * @author Manuel de la Peña
 */
public class BusStopActivity extends AppCompatActivity {

    private BusStop busStop;
    private FavoriteDAO favoriteDAO;

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

        setContentView(R.layout.activity_bus_stop);

        Intent intent = getIntent();

        if (intent.hasExtra("busStop") && (intent.getSerializableExtra("busStop") != null)) {
            busStop = (BusStop) intent.getSerializableExtra("busStop");

            favoriteDAO = new FavoriteDAO(this);

            favoriteDAO.open();

            String title = busStop.getId() + " (" + busStop.getAddress() + ")";

            this.setTitle(title);

            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

            setSupportActionBar(toolbar);

            FloatingActionButton floatingActionButton = (FloatingActionButton) findViewById(
                R.id.fab);

            floatingActionButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    boolean exits = favoriteDAO.exits(busStop.getId());

                    String message = "Parada AÑADIDA a favoritas con éxito";

                    if (!exits) {
                        // add to favorites database
                        favoriteDAO.createFavorite(busStop.getId());
                    }
                    else {
                        message = "Parada ELIMINADA de favoritas con éxito";

                        Favorite favorite = new Favorite();

                        favorite.setBusStopId(busStop.getId());

                        favoriteDAO.deleteFavorite(favorite);
                    }

                    Snackbar.make(
                        view, message, Snackbar.LENGTH_LONG).setAction("Action", null).show();
                }

            });

            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

}
@@ -255,16 +255,60 @@
 ntent;%0A%0A
+import android.graphics.drawable.Drawable;%0A%0A
 import a
@@ -432,16 +432,65 @@
 ackbar;%0A
+import android.support.v4.content.ContextCompat;%0A
 import a
@@ -724,16 +724,48 @@
 usStop;%0A
+    private boolean isFavorite;%0A
     priv
@@ -1491,16 +1491,143 @@
         
+Favorite favorite = favoriteDAO.getFavorite(busStop.getId());%0A%0A            isFavorite = (favorite != null);%0A%0A            final 
 Floating
@@ -1723,24 +1723,110 @@
 R.id.fab);%0A%0A
+            floatingActionButton.setImageDrawable(getFavoritedDrawable(isFavorite));%0A%0A
             
@@ -1974,81 +1974,8 @@
 ) %7B%0A
-                    boolean exits = favoriteDAO.exits(busStop.getId());%0A%0A
     
@@ -2077,69 +2077,21 @@
 f (!
-exits) %7B%0A                        // add to favorites database
+isFavorite) %7B
 %0A   
@@ -2486,32 +2486,173 @@
              %7D%0A%0A
+                    isFavorite = !isFavorite;%0A%0A                    floatingActionButton.setImageDrawable(getFavoritedDrawable(isFavorite));%0A%0A
                 
@@ -2885,9 +2885,311 @@
 %0A    %7D%0A%0A
+    private Drawable getFavoritedDrawable(boolean favorited) %7B%0A        if (favorited) %7B%0A            return ContextCompat.getDrawable(BusStopActivity.this, android.R.drawable.ic_delete);%0A        %7D%0A%0A        return ContextCompat.getDrawable(BusStopActivity.this, R.drawable.ic_star_favorite_24dp);%0A    %7D%0A%0A
 %7D

Change the Add-to-Favorites icon depending on its fav state