Update favorites list automaticly

This commit is contained in:
Hugo 2015-06-25 10:07:37 +02:00
parent 6f7e75d20c
commit c6c816331b
5 changed files with 76 additions and 16 deletions

View File

@ -4,7 +4,9 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
@ -20,9 +22,12 @@ import com.melnykov.fab.FloatingActionButton;
import de.cketti.library.changelog.ChangeLog; import de.cketti.library.changelog.ChangeLog;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
public class ListFavoritesActivity extends OptionsActivity { public class ListFavoritesActivity extends OptionsActivity {
private Fragment currentFragment;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -38,21 +43,24 @@ public class ListFavoritesActivity extends OptionsActivity {
} }
// Create a new Fragment to be placed in the activity layout // Create a new Fragment to be placed in the activity layout
ListShopsFragment firstFragment = new ListShopsFragment(); currentFragment = new ListShopsFragment();
// Get all favorites shops // Get all favorites shops
FavorisDao dao = FavorisDao.getInstance(getApplicationContext()); FavorisDao dao = FavorisDao.getInstance(getApplicationContext());
dao.openReadable(); dao.openReadable();
ArrayList<Shop> shops = dao.getAllFavoris(); ArrayList<Shop> shops = dao.getAllFavoris();
// Update the list
ListFavoritesSingleton.getInstance().setFavoritesShops(shops);
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putParcelableArrayList("list", shops); args.putParcelableArrayList("list", shops);
args.putString("errorEmptyList", getErrorMessageListFavoritesEmpty()); args.putString("errorEmptyList", getErrorMessageListFavoritesEmpty());
firstFragment.setArguments(args); currentFragment.setArguments(args);
// Add the fragment to the 'fragment_container' FrameLayout // 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 // 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<Shop> shops = ListFavoritesSingleton.getInstance().getFavoritesShops();
if (!shops.isEmpty()) {
((ListShopsFragment) currentFragment).updateList(shops);
}
}
}
/** /**
* Used when the fragment want to launch the Search. * Used when the fragment want to launch the Search.
*/ */

View File

@ -19,6 +19,10 @@ public class ListShopsAdapter extends RecyclerView.Adapter<ListShopsAdapter.Shop
this.shops = shop; this.shops = shop;
} }
public List<Shop> getShops() {return shops; }
public void setShops(List<Shop> shops) { this.shops = shops; }
public class ShopViewHolder extends RecyclerView.ViewHolder { public class ShopViewHolder extends RecyclerView.ViewHolder {
private CardView cv; private CardView cv;

View File

@ -16,6 +16,7 @@ import com.amine.horaires.shopdetail.DetailShopActivity;
import com.amine.horaires.util.RecyclerItemClickListener; import com.amine.horaires.util.RecyclerItemClickListener;
import com.melnykov.fab.FloatingActionButton; import com.melnykov.fab.FloatingActionButton;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class ListShopsFragment extends Fragment { public class ListShopsFragment extends Fragment {
@ -59,26 +60,31 @@ public class ListShopsFragment extends Fragment {
// Open the shop on click // Open the shop on click
favsRecyclerView.addOnItemTouchListener( favsRecyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(getActivity().getApplicationContext(), new RecyclerItemClickListener(getActivity().getApplicationContext(),
new RecyclerItemClickListener.OnItemClickListener() { new RecyclerItemClickListener.OnItemClickListener() {
@Override @Override
public void onItemClick(View view, final int position) { public void onItemClick(View view, final int position) {
final Intent i = new Intent(view.getContext(), DetailShopActivity.class); final Intent i = new Intent(view.getContext(), DetailShopActivity.class);
final Handler handler = new Handler(); final Handler handler = new Handler();
Runnable t = new Runnable() { Runnable t = new Runnable() {
public void run() { public void run() {
i.putExtra("shop", shops.get(position)); i.putExtra("shop", shops.get(position));
getActivity().startActivity(i); getActivity().startActivity(i);
} }
}; };
handler.post(t); handler.post(t);
}
} }
}
) )
); );
} }
} }
public void updateList (List<Shop> listOfShops) {
adapter.setShops(listOfShops);
adapter.notifyDataSetChanged();
}
} }

View File

@ -219,6 +219,24 @@ public class Shop implements Parcelable {
return 0; 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 @Override
public void writeToParcel(Parcel dest, int flags) { public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id); dest.writeInt(id);

View File

@ -15,6 +15,7 @@ import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import com.amine.horaires.R; import com.amine.horaires.R;
import com.amine.horaires.bdd.FavorisDao; import com.amine.horaires.bdd.FavorisDao;
import com.amine.horaires.listfavorites.ListFavoritesSingleton;
import com.amine.horaires.models.Shop; import com.amine.horaires.models.Shop;
import com.amine.horaires.updateshop.UpdateShopActivity; import com.amine.horaires.updateshop.UpdateShopActivity;
import com.amine.horaires.util.Parseur; import com.amine.horaires.util.Parseur;
@ -151,10 +152,17 @@ public class DetailShopFragment extends Fragment implements UpdateFav {
dao.open(); dao.open();
dao.deleteFavori(s.getId()); dao.deleteFavori(s.getId());
dao.close(); dao.close();
// Notify the favorites list that he remove a shop
ListFavoritesSingleton.getInstance().getFavoritesShops().remove(s);
} else { } else {
dao.open(); dao.open();
dao.insertFavori(s); dao.insertFavori(s);
dao.close(); dao.close();
// Notify the favorites list that he added a shop
ListFavoritesSingleton.getInstance().getFavoritesShops().add(s);
} }
isFav = !isFav; isFav = !isFav;