· 10 years ago · Sep 27, 2016, 09:06 AM
1package com.example.alsaint.dagger2retrofitrxdatabasemvp.mvp.model;
2
3
4import android.content.ContentValues;
5import android.content.Context;
6import android.database.Cursor;
7import android.database.SQLException;
8import android.database.sqlite.SQLiteDatabase;
9import android.database.sqlite.SQLiteOpenHelper;
10import android.util.Log;
11
12import java.util.ArrayList;
13import java.util.List;
14
15import javax.inject.Inject;
16
17public class Storage extends SQLiteOpenHelper {
18
19 private static final String TAG = Storage.class.getSimpleName();
20 private static final String ID = "id";
21 private static final String TITLE = "title";
22 private static final String PREVIEW_DESCRIPTION = "previewDescription";
23 private static final String DETAIL_DESCRIPTION = "detailDescription";
24 private static final String IMAGE_URL = "imageUrl";
25 private static final String TABLE_NAME = "cakes";
26
27 private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
28
29 private static final String SELECT_QUERY = "SELECT * FROM " + TABLE_NAME;
30
31 public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" +
32 ID + " INTEGER PRIMARY KEY, " +
33 TITLE + " TEXT NOT NULL, " +
34 PREVIEW_DESCRIPTION + " TEXT NOT NULL, " +
35 DETAIL_DESCRIPTION + " TEXT NOT NULL, " +
36 IMAGE_URL + " TEXT NOT NULL);";
37
38 @Inject
39 public Storage(Context context) {
40 super(context, "cake_db", null, 1);
41 }
42
43 @Override
44 public void onCreate(SQLiteDatabase db) {
45 try {
46 db.execSQL(CREATE_TABLE);
47 } catch (SQLException ex) {
48 Log.d(TAG, ex.getMessage());
49 }
50 }
51
52 @Override
53 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
54 db.execSQL(DROP_TABLE);
55 onCreate(db);
56 }
57
58 public void addCake(Cake cake) {
59 SQLiteDatabase db = this.getWritableDatabase();
60
61 ContentValues values = new ContentValues();
62 values.put(TITLE, cake.getTitle());
63 values.put(PREVIEW_DESCRIPTION, cake.getPreviewDescription());
64 values.put(DETAIL_DESCRIPTION, cake.getDetailDescription());
65 values.put(IMAGE_URL, cake.getImageUrl());
66
67 try {
68 db.insert(TABLE_NAME, null, values);
69 } catch (SQLException ex) {
70 Log.d(TAG, ex.getMessage());
71 }
72
73 db.close();
74 }
75
76 public List<Cake> getSavedCakes() {
77 List<Cake> cakeList = new ArrayList<>();
78 SQLiteDatabase db = this.getWritableDatabase();
79
80 try {
81 Cursor cursor = db.rawQuery(SELECT_QUERY, null);
82 if (cursor != null) {
83 if (cursor.getCount() > 0) {
84 if (cursor.moveToFirst()) {
85
86 do {
87 Cake cake = new Cake();
88 cake.setTitle(cursor.getString(cursor.getColumnIndex(TITLE)));
89 cake.setPreviewDescription(cursor.getString(cursor.getColumnIndex(PREVIEW_DESCRIPTION)));
90 cake.setDetailDescription(cursor.getString(cursor.getColumnIndex(DETAIL_DESCRIPTION)));
91 cake.setImageUrl(cursor.getString(cursor.getColumnIndex(IMAGE_URL)));
92 } while (cursor.moveToNext());
93 }
94 }
95 }
96 } catch (SQLException e) {
97 Log.d(TAG, e.getMessage());
98 }
99
100 return cakeList;
101 }
102}