From c6c816331ba7fac87e5e9012a92a492db68d240d Mon Sep 17 00:00:00 2001 From: Hugo Date: Thu, 25 Jun 2015 10:07:37 +0200 Subject: [PATCH] Update favorites list automaticly --- .../listfavorites/ListFavoritesActivity.java | 30 +++++++++++++++-- .../horaires/listshop/ListShopsAdapter.java | 4 +++ .../horaires/listshop/ListShopsFragment.java | 32 +++++++++++-------- .../java/com/amine/horaires/models/Shop.java | 18 +++++++++++ .../shopdetail/DetailShopFragment.java | 8 +++++ 5 files changed, 76 insertions(+), 16 deletions(-) diff --git a/Les Horaires/src/main/java/com/amine/horaires/listfavorites/ListFavoritesActivity.java b/Les Horaires/src/main/java/com/amine/horaires/listfavorites/ListFavoritesActivity.java index 169459e..1d99490 100644 --- a/Les Horaires/src/main/java/com/amine/horaires/listfavorites/ListFavoritesActivity.java +++ b/Les Horaires/src/main/java/com/amine/horaires/listfavorites/ListFavoritesActivity.java @@ -4,7 +4,9 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; +import android.support.v4.app.Fragment; import android.telephony.TelephonyManager; +import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; @@ -20,9 +22,12 @@ import com.melnykov.fab.FloatingActionButton; import de.cketti.library.changelog.ChangeLog; import java.util.ArrayList; +import java.util.List; public class ListFavoritesActivity extends OptionsActivity { + private Fragment currentFragment; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -38,21 +43,24 @@ public class ListFavoritesActivity extends OptionsActivity { } // Create a new Fragment to be placed in the activity layout - ListShopsFragment firstFragment = new ListShopsFragment(); + currentFragment = new ListShopsFragment(); // Get all favorites shops FavorisDao dao = FavorisDao.getInstance(getApplicationContext()); dao.openReadable(); ArrayList shops = dao.getAllFavoris(); + // Update the list + ListFavoritesSingleton.getInstance().setFavoritesShops(shops); + Bundle args = new Bundle(); args.putParcelableArrayList("list", shops); args.putString("errorEmptyList", getErrorMessageListFavoritesEmpty()); - firstFragment.setArguments(args); + currentFragment.setArguments(args); // Add the fragment to the 'fragment_container' FrameLayout - getSupportFragmentManager().beginTransaction().add(R.id.fragmentContainer, firstFragment).commit(); + getSupportFragmentManager().beginTransaction().add(R.id.fragmentContainer, currentFragment).commit(); } // Save user country @@ -74,6 +82,22 @@ public class ListFavoritesActivity extends OptionsActivity { }); } + @Override + public void onResume(){ + super.onResume(); + + Log.w("com.amine.horaires", "onResume()"); + + // We should tell to the list that we have updated something + if (currentFragment != null) { + List shops = ListFavoritesSingleton.getInstance().getFavoritesShops(); + + if (!shops.isEmpty()) { + ((ListShopsFragment) currentFragment).updateList(shops); + } + } + } + /** * Used when the fragment want to launch the Search. */ diff --git a/Les Horaires/src/main/java/com/amine/horaires/listshop/ListShopsAdapter.java b/Les Horaires/src/main/java/com/amine/horaires/listshop/ListShopsAdapter.java index 1993bd0..f0da068 100644 --- a/Les Horaires/src/main/java/com/amine/horaires/listshop/ListShopsAdapter.java +++ b/Les Horaires/src/main/java/com/amine/horaires/listshop/ListShopsAdapter.java @@ -19,6 +19,10 @@ public class ListShopsAdapter extends RecyclerView.Adapter getShops() {return shops; } + + public void setShops(List shops) { this.shops = shops; } + public class ShopViewHolder extends RecyclerView.ViewHolder { private CardView cv; diff --git a/Les Horaires/src/main/java/com/amine/horaires/listshop/ListShopsFragment.java b/Les Horaires/src/main/java/com/amine/horaires/listshop/ListShopsFragment.java index 327a11b..a66732b 100644 --- a/Les Horaires/src/main/java/com/amine/horaires/listshop/ListShopsFragment.java +++ b/Les Horaires/src/main/java/com/amine/horaires/listshop/ListShopsFragment.java @@ -16,6 +16,7 @@ import com.amine.horaires.shopdetail.DetailShopActivity; import com.amine.horaires.util.RecyclerItemClickListener; import com.melnykov.fab.FloatingActionButton; +import java.util.ArrayList; import java.util.List; public class ListShopsFragment extends Fragment { @@ -59,26 +60,31 @@ public class ListShopsFragment extends Fragment { // Open the shop on click favsRecyclerView.addOnItemTouchListener( new RecyclerItemClickListener(getActivity().getApplicationContext(), - new RecyclerItemClickListener.OnItemClickListener() { - @Override - public void onItemClick(View view, final int position) { - final Intent i = new Intent(view.getContext(), DetailShopActivity.class); - final Handler handler = new Handler(); - Runnable t = new Runnable() { - public void run() { - i.putExtra("shop", shops.get(position)); - getActivity().startActivity(i); - } - }; + new RecyclerItemClickListener.OnItemClickListener() { + @Override + public void onItemClick(View view, final int position) { + final Intent i = new Intent(view.getContext(), DetailShopActivity.class); + final Handler handler = new Handler(); + Runnable t = new Runnable() { + public void run() { + i.putExtra("shop", shops.get(position)); + getActivity().startActivity(i); + } + }; - handler.post(t); + handler.post(t); + } } - } ) ); } } + public void updateList (List listOfShops) { + adapter.setShops(listOfShops); + adapter.notifyDataSetChanged(); + } + } diff --git a/Les Horaires/src/main/java/com/amine/horaires/models/Shop.java b/Les Horaires/src/main/java/com/amine/horaires/models/Shop.java index d367bc3..397dc58 100644 --- a/Les Horaires/src/main/java/com/amine/horaires/models/Shop.java +++ b/Les Horaires/src/main/java/com/amine/horaires/models/Shop.java @@ -219,6 +219,24 @@ public class Shop implements Parcelable { return 0; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Shop shop = (Shop) o; + + if (name != null ? !name.equals(shop.name) : shop.name != null) return false; + return !(adresse != null ? !adresse.equals(shop.adresse) : shop.adresse != null); + } + + @Override + public int hashCode() { + int result = name != null ? name.hashCode() : 0; + result = 31 * result + (adresse != null ? adresse.hashCode() : 0); + return result; + } + @Override public void writeToParcel(Parcel dest, int flags) { dest.writeInt(id); diff --git a/Les Horaires/src/main/java/com/amine/horaires/shopdetail/DetailShopFragment.java b/Les Horaires/src/main/java/com/amine/horaires/shopdetail/DetailShopFragment.java index 71d5a98..7a978f7 100644 --- a/Les Horaires/src/main/java/com/amine/horaires/shopdetail/DetailShopFragment.java +++ b/Les Horaires/src/main/java/com/amine/horaires/shopdetail/DetailShopFragment.java @@ -15,6 +15,7 @@ import android.widget.ImageView; import android.widget.TextView; import com.amine.horaires.R; import com.amine.horaires.bdd.FavorisDao; +import com.amine.horaires.listfavorites.ListFavoritesSingleton; import com.amine.horaires.models.Shop; import com.amine.horaires.updateshop.UpdateShopActivity; import com.amine.horaires.util.Parseur; @@ -151,10 +152,17 @@ public class DetailShopFragment extends Fragment implements UpdateFav { dao.open(); dao.deleteFavori(s.getId()); dao.close(); + + // Notify the favorites list that he remove a shop + ListFavoritesSingleton.getInstance().getFavoritesShops().remove(s); + } else { dao.open(); dao.insertFavori(s); dao.close(); + + // Notify the favorites list that he added a shop + ListFavoritesSingleton.getInstance().getFavoritesShops().add(s); } isFav = !isFav;