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.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<Shop> 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<Shop> shops = ListFavoritesSingleton.getInstance().getFavoritesShops();
if (!shops.isEmpty()) {
((ListShopsFragment) currentFragment).updateList(shops);
}
}
}
/**
* 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;
}
public List<Shop> getShops() {return shops; }
public void setShops(List<Shop> shops) { this.shops = shops; }
public class ShopViewHolder extends RecyclerView.ViewHolder {
private CardView cv;

View File

@ -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 {
@ -79,6 +80,11 @@ public class ListShopsFragment extends Fragment {
}
}
public void updateList (List<Shop> listOfShops) {
adapter.setShops(listOfShops);
adapter.notifyDataSetChanged();
}
}

View File

@ -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);

View File

@ -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;