Skip to content

Commit 542f122

Browse files
committed
add permanent storage for item in the list
1 parent b12823e commit 542f122

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+227
-62
lines changed

.idea/.name

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/bath-web.png

113 KB
Loading

app/src/main/bottles-web.png

104 KB
Loading

app/src/main/bread_loaf-web.png

385 KB
Loading

app/src/main/bread_stick-web.png

476 KB
Loading

app/src/main/cakes-web.png

386 KB
Loading

app/src/main/creams-web.png

177 KB
Loading

app/src/main/food_and_formula-web.png

231 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,108 @@
11
package hubjac1.mysmartshoppinglist.DAO;
22

3+
import android.content.ContentValues;
4+
import android.content.Context;
5+
import android.database.Cursor;
6+
37
import java.util.HashSet;
48
import java.util.Set;
59

610
/**
711
* DAO class to access caddy model from storage
812
*/
9-
public class CaddyDao {
13+
public class CaddyDao extends DAOBase {
1014
//private static int [] prod = new int[]{R.string.Bath, R.string.Bottles};
11-
private static Set<Integer> product = new HashSet<Integer>();
15+
private static Set<Integer> product = new HashSet<>();
16+
17+
public CaddyDao(Context pContext) {
18+
super(pContext);
19+
}
1220

1321
/**
14-
* Reset caddy. Will empty the caddy
22+
* Reset caddy. Will empty the caddy table
1523
*/
1624
public void reset(){
17-
product.clear();
25+
mDb.execSQL(DatabaseSchema.Caddy.TABLE_DROP);
26+
mDb.execSQL(DatabaseSchema.Caddy.TABLE_CREATE);
27+
}
28+
29+
/**
30+
* Add a product to a caddy
31+
* @param product CaddyModel
32+
*/
33+
private void add(CaddyModel product) {
34+
ContentValues value = new ContentValues();
35+
value.put(DatabaseSchema.Caddy.PRODUCT, product.getProduct());
36+
value.put(DatabaseSchema.Caddy.STATUS, product.getStatus());
37+
38+
mDb.insert(DatabaseSchema.Caddy.TABLE_NAME, null, value);
39+
}
40+
41+
private void delete(CaddyModel product){
42+
mDb.delete(DatabaseSchema.Caddy.TABLE_NAME,
43+
DatabaseSchema.Caddy.KEY + " = ?", new String[]{product.getKey()});
44+
}
45+
private void delete(long key){
46+
mDb.delete(DatabaseSchema.Caddy.TABLE_NAME,
47+
DatabaseSchema.Caddy.KEY + " = ?", new String[]{String.valueOf(key)});
48+
}
49+
50+
private void update(CaddyModel product){
51+
ContentValues value = new ContentValues();
52+
value.put(DatabaseSchema.Caddy.PRODUCT, product.getProduct());
53+
value.put(DatabaseSchema.Caddy.STATUS, product.getStatus());
54+
55+
mDb.update(DatabaseSchema.Caddy.TABLE_NAME, value,
56+
DatabaseSchema.Caddy.KEY + " = ?", new String[]{product.getKey()});
57+
}
58+
59+
private CaddyModel get(int id){
60+
Cursor cursor = mDb.rawQuery("select * from " + DatabaseSchema.Caddy.TABLE_NAME +
61+
"where " + DatabaseSchema.Caddy.KEY + "= ?", new String[]{String.valueOf(id)});
62+
CaddyModel product = new CaddyModel(cursor.getInt(2), cursor.getInt(1));
63+
product.setKey(cursor.getLong(0));
64+
cursor.close();
65+
return product;
1866
}
1967

2068
/**
2169
* Add product to caddy
2270
* @param productId : int
23-
* @param status
71+
* @param status: boolean
2472
*/
25-
public static void update(int productId, boolean status){
26-
if (status)
27-
product.add(productId);
28-
else if (product.contains(productId))
73+
public void update(int productId, boolean status){
74+
if (status) {
75+
CaddyModel product = new CaddyModel(1, productId);
76+
add(product);
77+
}
78+
else
2979
{
30-
product.remove(productId);
80+
delete(productId);
3181
}
3282
}
3383

3484
/**
3585
* Get list of product in the caddy
3686
* @return list of product
3787
*/
38-
public static Set<Integer> getProductsId(){
39-
return product;
88+
public Set<Integer> getProductsId(){
89+
Set<Integer> products = new HashSet<>();
90+
Cursor cursor = mDb.rawQuery("select " + DatabaseSchema.Caddy.PRODUCT + " from " + DatabaseSchema.Caddy.TABLE_NAME, new String[]{});
91+
92+
while (cursor.moveToNext()) {
93+
products.add(cursor.getInt(0));
94+
}
95+
cursor.close();
96+
97+
return products;
4098

4199
}
42100

43101
/**
44102
* get table of product in the Caddy
45103
* @return ProductModel []
46104
*/
47-
public static ProductModel [] getProducts(){
48-
return ProductDao.getProducts(product).toArray(new ProductModel []{});
105+
public ProductModel [] getProducts(ProductDao productDao){
106+
return productDao.getProducts(getProductsId()).toArray(new ProductModel[]{});
49107
}
50108
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package hubjac1.mysmartshoppinglist.DAO;
2+
3+
/**
4+
* Data model for Caddy storage
5+
*/
6+
public class CaddyModel {
7+
private long mKey;
8+
private int mStatus;
9+
private int mProduct;
10+
11+
public CaddyModel(int status, int product){
12+
mStatus = status;
13+
mProduct = product;
14+
}
15+
16+
public int getId(){
17+
return (int) mKey;
18+
}
19+
20+
public String getKey() {
21+
return String.valueOf(mKey);
22+
}
23+
24+
public void setKey(long mKey) {
25+
this.mKey = mKey;
26+
}
27+
28+
public int getStatus() {
29+
return mStatus;
30+
}
31+
32+
public void setStatus(int mStatus) {
33+
this.mStatus = mStatus;
34+
}
35+
36+
public int getProduct() {
37+
return mProduct;
38+
}
39+
40+
public void setProduct(int mProduct) {
41+
this.mProduct = mProduct;
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package hubjac1.mysmartshoppinglist.DAO;
2+
3+
import android.content.Context;
4+
import android.database.sqlite.SQLiteDatabase;
5+
6+
public abstract class DAOBase {
7+
8+
protected final static int VERSION = 1;
9+
10+
// Database file name
11+
protected final static String NOM = "smartShoppingList.db";
12+
13+
protected SQLiteDatabase mDb = null;
14+
15+
protected DatabaseHandler mHandler = null;
16+
17+
public DAOBase(Context pContext) {
18+
this.mHandler = new DatabaseHandler(pContext, NOM, null, VERSION);
19+
}
20+
21+
public SQLiteDatabase open() {
22+
// No need to close the previous database because getWritableDatabase take care
23+
mDb = mHandler.getWritableDatabase();
24+
return mDb;
25+
}
26+
27+
public void close() {
28+
mDb.close();
29+
}
30+
31+
public SQLiteDatabase getDb() {
32+
return mDb;
33+
}
34+
}

app/src/main/java/hubjac1/mysmartshoppinglist/DAO/DataBaseHandler.java

-35
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package hubjac1.mysmartshoppinglist.DAO;
2+
3+
import android.content.Context;
4+
import android.database.sqlite.SQLiteDatabase;
5+
import android.database.sqlite.SQLiteOpenHelper;
6+
7+
public class DatabaseHandler extends SQLiteOpenHelper {
8+
9+
public DatabaseHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
10+
super(context, name, factory, version);
11+
}
12+
13+
@Override
14+
public void onCreate(SQLiteDatabase db) {
15+
db.execSQL(DatabaseSchema.Caddy.TABLE_CREATE);
16+
}
17+
18+
@Override
19+
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
20+
db.execSQL(DatabaseSchema.Caddy.TABLE_DROP);
21+
onCreate(db);
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package hubjac1.mysmartshoppinglist.DAO;
2+
3+
/**
4+
* Class to describe data base schema
5+
*/
6+
public class DatabaseSchema {
7+
static public class Caddy {
8+
public static final String KEY = "id";
9+
public static final String PRODUCT = "product";
10+
public static final String STATUS = "status";
11+
12+
public static final String TABLE_NAME = "Caddy";
13+
public static final String TABLE_CREATE =
14+
"CREATE TABLE " + TABLE_NAME + " (" +
15+
KEY + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
16+
PRODUCT + " INTEGER, " +
17+
STATUS + " INTEGER);";
18+
19+
public static final String TABLE_DROP = "DROP TABLE IF EXISTS " + TABLE_NAME + ";";
20+
}
21+
22+
}

app/src/main/java/hubjac1/mysmartshoppinglist/DAO/ProductDao.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package hubjac1.mysmartshoppinglist.DAO;
22

3-
import android.util.Log;
3+
import android.content.Context;
44

55
import java.util.ArrayList;
66
import java.util.Arrays;
7-
import java.util.HashMap;
8-
import java.util.Map;
97
import java.util.Set;
108

119
import hubjac1.mysmartshoppinglist.R;
@@ -37,12 +35,16 @@ public class ProductDao {
3735
new ProductModel(R.string.Wraps, R.mipmap.wraps)
3836
}));
3937

38+
public ProductDao(Context context) {
39+
40+
}
41+
4042
/**
4143
* Get products in a given category
4244
* @param category : int
4345
* @return ArrayList<ProductModel>
4446
*/
45-
static public ArrayList<ProductModel> getProductsInCategory(int category)
47+
public ArrayList<ProductModel> getProductsInCategory(int category)
4648
{
4749
ArrayList<ProductModel> products;
4850
if (category == R.string.baby){
@@ -54,7 +56,6 @@ else if (category == R.string.bakery) {
5456
else {
5557
products = new ArrayList<>();
5658
}
57-
setProductStatus(products);
5859
return products;
5960
}
6061

@@ -63,7 +64,7 @@ else if (category == R.string.bakery) {
6364
* @param idList : Set<Integer>
6465
* @return ArrayList<ProductModel>
6566
*/
66-
static public ArrayList<ProductModel> getProducts( Set<Integer> idList)
67+
public ArrayList<ProductModel> getProducts( Set<Integer> idList)
6768
{
6869
ArrayList<ProductModel> allProducts = new ArrayList<ProductModel>(){};
6970
ArrayList<ProductModel> caddy = new ArrayList<ProductModel>(){};
@@ -82,8 +83,8 @@ static public ArrayList<ProductModel> getProducts( Set<Integer> idList)
8283
* Set product status to true for priduct in the caddy.
8384
* @param modelArray: ArrayList<ProductModel>
8485
*/
85-
private static void setProductStatus(ArrayList<ProductModel> modelArray) {
86-
Set<Integer> selectedProduct = CaddyDao.getProductsId();
86+
private void setProductStatus(ArrayList<ProductModel> modelArray, CaddyDao caddyDao) {
87+
Set<Integer> selectedProduct = caddyDao.getProductsId();
8788

8889
for (ProductModel model : modelArray) {
8990
if (selectedProduct.contains(model.getId())){

app/src/main/java/hubjac1/mysmartshoppinglist/DAO/ProductModel.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ public class ProductModel {
99
private int mId;
1010
private boolean mStatus;
1111

12+
private static CaddyDao mCaddyDao = new CaddyDao(null);
13+
public static void setCaddyDao (CaddyDao caddyDao){
14+
mCaddyDao = caddyDao;
15+
}
16+
1217
ProductModel(int text, int image){
1318
mText = text;
1419
mImage = image;
@@ -42,6 +47,6 @@ public boolean isSelected(){
4247

4348
public void setSelected(boolean selected) {
4449
mStatus = selected;
45-
CaddyDao.update(mId, selected);
50+
mCaddyDao.update(mId, selected);
4651
}
4752
}

0 commit comments

Comments
 (0)