· 6 years ago · Aug 15, 2019, 04:10 PM
1package danonek.crystal4b.databasehelper;
2
3import android.content.Context;
4import android.database.Cursor;
5import android.database.sqlite.SQLiteDatabase;
6import android.database.sqlite.SQLiteOpenHelper;
7
8import java.util.ArrayList;
9
10public class DatabaseHelper extends SQLiteOpenHelper
11{
12 private final String tables[] = {"task_list", "list_names"};
13
14 private final String task_id = "task_id";
15 private final String task_name = "task_name";
16 private final String starred = "starred";
17
18 private final String created = "created";
19 private final String finished = "finished";
20
21 private final String notes = "notes";
22
23 private final String alarmSet = "alarmSet";
24 private final String dueDate = "dueDate";
25
26 private final String list_id = "list_id";
27 private final String list_name = "list_name";
28
29 private SQLiteDatabase db = this.getWritableDatabase();
30
31 private Config Config;
32 private Add Add;
33 private Delete Delete;
34 private Get Get;
35 private Update Update;
36
37 // Important List Ids
38 private int Starred = 1;
39 private int AllTasks = 2;
40 private int TodaysTasks = 3;
41 private int Home = 4;
42
43
44 public DatabaseHelper(Context context)
45 {
46 super(context, "tasks", null, 1);
47
48 // PERFECTION
49 Config = new Config(db);
50 Add = new Add(Config);
51 Delete = new Delete(Config);
52 Get = new Get(Config);
53 Update = new Update(Config);
54 // END OF PERFECTION
55 }
56
57 @Override
58 public void onCreate(SQLiteDatabase db)
59 {
60 String createTask = "CREATE TABLE " + tables[0] + " (" + task_id + " INTEGER PRIMARY KEY AUTOINCREMENT, " + task_name + " TEXT NOT NULL DEFAULT '', " + list_id + " INTEGER DEFAULT NULL, " + starred + " INTEGER DEFAULT 0, " + created + " TEXT NOT NULL DEFAULT '', " + notes + " TEXT NOT NULL DEFAULT ''," + finished + " TEXT NOT NULL DEFAULT '', " + alarmSet + " TEXT NOT NULL DEFAULT '', " + dueDate + " TEXT NOT NULL DEFAULT '')";
61 String createTable = "CREATE TABLE " + tables[1] + " (" + list_id + " INTEGER PRIMARY KEY AUTOINCREMENT, " + list_name + " TEXT NOT NULL DEFAULT '')";
62 String createHome = "INSERT INTO " + tables[1] + " (" + list_id + "," + list_name + ") VALUES ('4', 'Home')";
63 db.execSQL(createTask);
64 db.execSQL(createTable);
65 db.execSQL(createHome);
66 }
67
68 @Override
69 public void onUpgrade(SQLiteDatabase db, int i, int i1)
70 {
71 for (String table: tables)
72 {
73 db.execSQL("DROP TABLE IF EXISTS " + table);
74 onCreate(db);
75 }
76 }
77 /*
78
79 _ _ _
80 / \ __| | __| |
81 / _ \ / _` |/ _` |
82 / ___ \ (_| | (_| |
83 /_/ \_\__,_|\__,_|
84
85
86 */
87 public boolean addTask(String name)
88 {
89 return Add.addTask(name);
90 }
91
92 public boolean addList(String listName)
93 {
94 boolean result = Add.addList(listName);
95 updateCurrentTableId(getListIdByName(listName));
96 return result;
97 }
98
99 /*
100
101 ____ _ _
102 | _ \ ___| | ___| |_ ___
103 | | | |/ _ \ |/ _ \ __/ _ \
104 | |_| | __/ | __/ || __/
105 |____/ \___|_|\___|\__\___|
106
107
108 */
109 public void deleteTasks()
110 {
111 Delete.deleteTasks();
112 }
113
114 public void deleteTables()
115 {
116 updateCurrentTableId(Home);
117 Delete.deleteTables();
118 }
119
120 public void deleteListById(int id)
121 {
122 updateCurrentTableId(Home);
123 Delete.deleteListById(id);
124 }
125
126 public void deleteNameById(int id)
127 {
128 Delete.deleteNameById(id);
129 }
130
131 public void deleteMultiSelect(ArrayList<Integer> id)
132 {
133 Delete.deleteMultiSelect(id);
134 }
135
136 /*
137
138 ____ _
139 / ___| ___| |_
140 | | _ / _ \ __|
141 | |_| | __/ |_
142 \____|\___|\__|
143
144
145 */
146 public int getHighestListId()
147 {
148 return Get.getHighestListId();
149 }
150
151 public int getHighestTaskId()
152 {
153 return Get.getHighestTaskId();
154 }
155
156 public int getCurrentTableId()
157 {
158 return Config.getCurrentTableId();
159 }
160
161 public int getStarredById(int id)
162 {
163 return Get.getStarredById(id);
164 }
165
166 public int getItemIdByName(String name)
167 {
168 return Get.getItemIdByName(name);
169 }
170
171 public int getListIdByName(String name)
172 {
173 return Get.getListIdByName(name);
174 }
175
176 public String getTaskNameById(int id)
177 {
178 return Get.getTaskNameById(id);
179 }
180
181 public Cursor getAllTaskNames()
182 {
183 return Get.getAllTaskNames();
184 }
185
186 public int getListIdByTaskId(int id)
187 {
188 return Get.getListIdByTaskId(id);
189 }
190
191 public String getListNameById(int id)
192 {
193 return Get.getListNameById(id);
194 }
195
196 public String getNotesContent(int id)
197 {
198 return Get.getNotesContent(id);
199 }
200
201 public Cursor getTodaysTasks()
202 {
203 return Get.getTodaysTasks();
204 }
205
206 public Cursor getAllTaskIds()
207 {
208 return Get.getAllTaskIds();
209 }
210
211 public Cursor getAllFromTasks()
212 {
213 return Get.getAllFromTasks();
214 }
215
216 public Cursor getAllFromLists()
217 {
218 return Get.getAllFromLists();
219 }
220
221 public Cursor getStarredItems()
222 {
223 return Get.getStarredItems();
224 }
225
226 public Cursor getDataFromList(int id)
227 {
228 updateCurrentTableId(id);
229 return Get.getDataFromList(id);
230 }
231
232 public Cursor getDataFromCurrentList()
233 {
234 return Get.getDataFromList(Config.getCurrentTableId());
235 }
236
237 public ArrayList<Integer> getItemIdFromList()
238 {
239 return Get.getItemIdFromList();
240 }
241
242 public Cursor getAlarmSetById(int id)
243 {
244 return Get.getAlarmSetById(id);
245 }
246
247 public Cursor getDueDateById(int id)
248 {
249 return Get.getDueDateById(id);
250 }
251
252 public String getFinishedDateById(int id)
253 {
254 return Get.getFinishedDateById(id);
255 }
256
257 public Cursor getAllFinished()
258 {
259 return Get.getAllFinished();
260 }
261
262 public Cursor getCreatedOnDateById(int id)
263 {
264 return Get.getCreatedOnDateById(id);
265 }
266
267 public String getCurrentTime()
268 {
269 return Config.getCurrentTime();
270 }
271
272 public String getCurrentDate()
273 {
274 return Config.getCurrentDate();
275 }
276
277 /*
278
279 _ _ _ _
280 | | | |_ __ __| | __ _| |_ ___
281 | | | | '_ \ / _` |/ _` | __/ _ \
282 | |_| | |_) | (_| | (_| | || __/
283 \___/| .__/ \__,_|\__,_|\__\___|
284 |_|
285
286
287 */
288 public void updateTaskName(String newName, int id)
289 {
290 Update.updateTaskName(newName, id);
291 }
292
293 public void updateListName(String newName, int id)
294 {
295 Update.updateListName(newName, id);
296 }
297
298 public void updateStarItem(int id)
299 {
300 Update.updateStarItem(id);
301 }
302
303 public void updateCurrentTableId(int tableId)
304 {
305 Config.setCurrentTableId(tableId);
306 }
307
308 public void moveTaskToTable(ArrayList<Integer> id, int newListId)
309 {
310 Update.moveTaskToTable(id, getCurrentTableId(), newListId);
311 }
312
313 public void updateStarredMultiSelect(ArrayList<Integer> id, boolean unStar)
314 {
315 Update.updateStarredMultiSelect(id, unStar);
316 }
317
318 public void updateAlarm(String time, int id)
319 {
320 Update.updateAlarm(time, id);
321 }
322
323 public void updateDueDate(String date, int id)
324 {
325 Update.updateDueDate(date, id);
326 }
327
328 public void updateFinishedTime(int id)
329 {
330 Update.updateFinishedTime(Config.getCurrentTime(), id);
331 }
332
333 public void updateFinishedTime(String time, int id)
334 {
335 Update.updateFinishedTime(time, id);
336 }
337
338 public void updateNotesContent(String content, int id)
339 {
340 Update.updateNotesContent(content, id);
341 }
342}