fedilab-Android-App/app/src/main/java/app/fedilab/android/sqlite/TimelinesDAO.java

223 lines
10 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.sqlite;
2019-04-21 10:22:07 +02:00
/* Copyright 2019 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2019-04-21 10:22:07 +02:00
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
2019-05-18 11:10:30 +02:00
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2019-04-21 10:22:07 +02:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
2019-04-21 10:22:07 +02:00
* see <http://www.gnu.org/licenses>. */
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.Entities.ManageTimelines;
import app.fedilab.android.helper.Helper;
2019-04-21 10:22:07 +02:00
public class TimelinesDAO {
public Context context;
2019-11-15 16:32:25 +01:00
private SQLiteDatabase db;
2019-04-21 10:22:07 +02:00
public TimelinesDAO(Context context, SQLiteDatabase db) {
//Creation of the DB with tables
this.context = context;
this.db = db;
}
//------- INSERTIONS -------
public void insert(ManageTimelines timeline) {
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
String instance = Helper.getLiveInstance(context);
ContentValues values = new ContentValues();
values.put(Sqlite.COL_TYPE, ManageTimelines.typeToDb(timeline.getType()));
values.put(Sqlite.COL_DISPLAYED, timeline.isDisplayed());
values.put(Sqlite.COL_POSITION, timeline.getPosition());
values.put(Sqlite.COL_USER_ID, userId);
values.put(Sqlite.COL_INSTANCE, instance);
2019-09-06 17:55:14 +02:00
if (timeline.getTagTimeline() != null)
2019-04-21 10:22:07 +02:00
values.put(Sqlite.COL_TAG_TIMELINE, Helper.tagTimelineToStringStorage(timeline.getTagTimeline()));
2019-09-06 17:55:14 +02:00
if (timeline.getRemoteInstance() != null)
2019-04-21 10:22:07 +02:00
values.put(Sqlite.COL_REMOTE_INSTANCE, Helper.remoteInstanceToStringStorage(timeline.getRemoteInstance()));
2019-09-06 17:55:14 +02:00
if (timeline.getListTimeline() != null)
2019-04-21 12:09:30 +02:00
values.put(Sqlite.COL_LIST_TIMELINE, Helper.listTimelineToStringStorage(timeline.getListTimeline()));
2019-09-06 17:55:14 +02:00
try {
2019-04-21 10:22:07 +02:00
db.insert(Sqlite.TABLE_TIMELINES, null, values);
2019-09-06 17:55:14 +02:00
} catch (Exception ignored) {
}
2019-04-21 10:22:07 +02:00
}
//------- REMOVE -------
2019-09-06 17:55:14 +02:00
public int remove(ManageTimelines timeline) {
return db.delete(Sqlite.TABLE_TIMELINES, Sqlite.COL_ID + " = \"" + timeline.getId() + "\"", null);
2019-04-21 10:22:07 +02:00
}
2019-04-22 13:16:35 +02:00
//------- REMOVE -------
2019-09-06 17:55:14 +02:00
public int removeAll() {
2019-04-22 13:16:35 +02:00
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
String instance = Helper.getLiveInstance(context);
2019-09-06 17:55:14 +02:00
return db.delete(Sqlite.TABLE_TIMELINES, Sqlite.COL_USER_ID + " = '" + userId + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "'", null);
2019-04-22 13:16:35 +02:00
}
2019-04-21 10:22:07 +02:00
//------- UPDATE -------
public int update(ManageTimelines timeline) {
ContentValues values = new ContentValues();
values.put(Sqlite.COL_DISPLAYED, timeline.isDisplayed());
values.put(Sqlite.COL_POSITION, timeline.getPosition());
2019-04-26 19:05:09 +02:00
return db.update(Sqlite.TABLE_TIMELINES,
values, Sqlite.COL_ID + " = ? ",
new String[]{String.valueOf(timeline.getId())});
}
//------- UPDATE -------
public void updateTag(ManageTimelines timeline) {
ContentValues values = new ContentValues();
values.put(Sqlite.COL_DISPLAYED, timeline.isDisplayed());
2019-09-06 17:55:14 +02:00
if (timeline.getTagTimeline() != null)
2019-04-26 15:50:26 +02:00
values.put(Sqlite.COL_TAG_TIMELINE, Helper.tagTimelineToStringStorage(timeline.getTagTimeline()));
2019-04-26 19:05:09 +02:00
db.update(Sqlite.TABLE_TIMELINES,
2019-04-21 10:22:07 +02:00
values, Sqlite.COL_ID + " = ? ",
new String[]{String.valueOf(timeline.getId())});
}
2019-05-25 12:37:50 +02:00
//------- UPDATE -------
public void updateRemoteInstance(ManageTimelines timeline) {
ContentValues values = new ContentValues();
values.put(Sqlite.COL_DISPLAYED, timeline.isDisplayed());
2019-09-06 17:55:14 +02:00
if (timeline.getTagTimeline() != null)
2019-05-25 12:37:50 +02:00
values.put(Sqlite.COL_REMOTE_INSTANCE, Helper.remoteInstanceToStringStorage(timeline.getRemoteInstance()));
db.update(Sqlite.TABLE_TIMELINES,
values, Sqlite.COL_ID + " = ? ",
new String[]{String.valueOf(timeline.getId())});
}
2019-09-06 17:55:14 +02:00
public int countVisibleTimelines() {
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
String instance = Helper.getLiveInstance(context);
2019-09-06 17:55:14 +02:00
Cursor mCount = db.rawQuery("select count(*) from " + Sqlite.TABLE_TIMELINES
+ " where " + Sqlite.COL_USER_ID + " = '" + userId + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND " + Sqlite.COL_DISPLAYED + "= 1", null);
mCount.moveToFirst();
int count = mCount.getInt(0);
mCount.close();
return count;
}
2019-04-21 10:22:07 +02:00
2019-09-06 17:55:14 +02:00
public List<ManageTimelines> getAllTimelines() {
2019-04-21 10:22:07 +02:00
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
String instance = Helper.getLiveInstance(context);
try {
2019-09-06 17:55:14 +02:00
Cursor c = db.query(Sqlite.TABLE_TIMELINES, null, Sqlite.COL_USER_ID + " = '" + userId + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "'", null, null, null, Sqlite.COL_POSITION + " ASC", null);
2019-04-21 10:22:07 +02:00
return cursorToTimelines(c);
} catch (Exception e) {
return null;
}
}
2019-09-06 17:55:14 +02:00
public List<ManageTimelines> getDisplayedTimelines() {
2019-04-21 10:22:07 +02:00
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
String instance = Helper.getLiveInstance(context);
try {
2019-09-06 17:55:14 +02:00
Cursor c = db.query(Sqlite.TABLE_TIMELINES, null, Sqlite.COL_USER_ID + " = '" + userId + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND " + Sqlite.COL_DISPLAYED + " = '1'", null, null, null, Sqlite.COL_POSITION + " ASC", null);
2019-04-21 10:22:07 +02:00
return cursorToTimelines(c);
} catch (Exception e) {
return null;
}
}
2019-09-06 17:55:14 +02:00
public ManageTimelines getById(int id) {
2019-04-26 15:50:26 +02:00
try {
2019-09-06 17:55:14 +02:00
Cursor c = db.query(Sqlite.TABLE_TIMELINES, null, Sqlite.COL_ID + " = '" + id + "'", null, null, null, Sqlite.COL_POSITION + " ASC", null);
2019-04-26 15:50:26 +02:00
return cursorToTimeline(c);
} catch (Exception e) {
return null;
}
}
2019-04-21 10:22:07 +02:00
/***
* Method to hydrate a timeline
* @param c Cursor
* @return ManageTimelines
*/
2019-09-06 17:55:14 +02:00
private ManageTimelines cursorToTimeline(Cursor c) {
2019-04-21 10:22:07 +02:00
//No element found
2019-08-17 14:34:33 +02:00
if (c.getCount() == 0) {
c.close();
2019-04-21 10:22:07 +02:00
return null;
2019-08-17 14:34:33 +02:00
}
2019-04-21 10:22:07 +02:00
//Take the first element
c.moveToFirst();
//New timeline
ManageTimelines manageTimelines = new ManageTimelines();
manageTimelines.setId(c.getInt(c.getColumnIndex(Sqlite.COL_ID)));
manageTimelines.setDisplayed(c.getInt(c.getColumnIndex(Sqlite.COL_DISPLAYED)) == 1);
manageTimelines.setPosition(c.getInt(c.getColumnIndex(Sqlite.COL_POSITION)));
2019-09-06 17:55:14 +02:00
if (c.getString(c.getColumnIndex(Sqlite.COL_TAG_TIMELINE)) != null)
2019-04-21 10:22:07 +02:00
manageTimelines.setTagTimeline(Helper.restoreTagTimelineFromString(c.getString(c.getColumnIndex(Sqlite.COL_TAG_TIMELINE))));
2019-09-06 17:55:14 +02:00
if (c.getString(c.getColumnIndex(Sqlite.COL_REMOTE_INSTANCE)) != null)
2019-04-21 10:22:07 +02:00
manageTimelines.setRemoteInstance(Helper.restoreRemoteInstanceFromString(c.getString(c.getColumnIndex(Sqlite.COL_REMOTE_INSTANCE))));
2019-09-06 17:55:14 +02:00
if (c.getString(c.getColumnIndex(Sqlite.COL_LIST_TIMELINE)) != null)
2019-04-21 12:09:30 +02:00
manageTimelines.setListTimeline(Helper.restoreListtimelineFromString(c.getString(c.getColumnIndex(Sqlite.COL_LIST_TIMELINE))));
2019-04-21 10:22:07 +02:00
manageTimelines.setType(ManageTimelines.typeFromDb(c.getString(c.getColumnIndex(Sqlite.COL_TYPE))));
//Close the cursor
c.close();
//Timeline is returned
return manageTimelines;
}
/***
* Method to hydrate stored instances from database
* @param c Cursor
* @return List<RemoteInstance>
*/
2019-09-06 17:55:14 +02:00
private List<ManageTimelines> cursorToTimelines(Cursor c) {
2019-04-21 10:22:07 +02:00
//No element found
2019-08-17 14:34:33 +02:00
if (c.getCount() == 0) {
c.close();
2019-04-21 10:22:07 +02:00
return null;
2019-08-17 14:34:33 +02:00
}
2019-04-21 10:22:07 +02:00
List<ManageTimelines> remoteInstances = new ArrayList<>();
2019-09-06 17:55:14 +02:00
while (c.moveToNext()) {
2019-04-21 10:22:07 +02:00
ManageTimelines manageTimelines = new ManageTimelines();
manageTimelines.setId(c.getInt(c.getColumnIndex(Sqlite.COL_ID)));
manageTimelines.setDisplayed(c.getInt(c.getColumnIndex(Sqlite.COL_DISPLAYED)) == 1);
manageTimelines.setPosition(c.getInt(c.getColumnIndex(Sqlite.COL_POSITION)));
2019-09-06 17:55:14 +02:00
if (c.getString(c.getColumnIndex(Sqlite.COL_TAG_TIMELINE)) != null)
2019-04-21 10:22:07 +02:00
manageTimelines.setTagTimeline(Helper.restoreTagTimelineFromString(c.getString(c.getColumnIndex(Sqlite.COL_TAG_TIMELINE))));
2019-09-06 17:55:14 +02:00
if (c.getString(c.getColumnIndex(Sqlite.COL_REMOTE_INSTANCE)) != null)
2019-04-21 10:22:07 +02:00
manageTimelines.setRemoteInstance(Helper.restoreRemoteInstanceFromString(c.getString(c.getColumnIndex(Sqlite.COL_REMOTE_INSTANCE))));
2019-09-06 17:55:14 +02:00
if (c.getString(c.getColumnIndex(Sqlite.COL_LIST_TIMELINE)) != null)
2019-04-21 12:09:30 +02:00
manageTimelines.setListTimeline(Helper.restoreListtimelineFromString(c.getString(c.getColumnIndex(Sqlite.COL_LIST_TIMELINE))));
2019-04-21 10:22:07 +02:00
manageTimelines.setType(ManageTimelines.typeFromDb(c.getString(c.getColumnIndex(Sqlite.COL_TYPE))));
remoteInstances.add(manageTimelines);
}
//Close the cursor
c.close();
return remoteInstances;
}
}