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

149 lines
4.5 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.sqlite;
2018-10-21 18:43:57 +02:00
/* Copyright 2018 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2018-10-21 18:43:57 +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
2018-10-21 18:43:57 +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,
2018-10-21 18:43:57 +02:00
* see <http://www.gnu.org/licenses>. */
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.API;
import app.fedilab.android.client.Entities.Peertube;
import app.fedilab.android.helper.Helper;
2018-10-21 18:43:57 +02:00
/**
* Created by Thomas on 21/10/2018.
* Manage Peertube favorites
*/
public class PeertubeFavoritesDAO {
public Context context;
2019-11-15 16:32:25 +01:00
private SQLiteDatabase db;
2018-10-21 18:43:57 +02:00
public PeertubeFavoritesDAO(Context context, SQLiteDatabase db) {
//Creation of the DB with tables
this.context = context;
this.db = db;
}
//------- INSERTIONS -------
2019-09-06 17:55:14 +02:00
2018-10-21 18:43:57 +02:00
/**
* Insert a status in database
2019-09-06 17:55:14 +02:00
*
2018-10-21 18:43:57 +02:00
* @param peertube Peertube
* @return boolean
*/
public long insert(Peertube peertube) {
ContentValues values = new ContentValues();
values.put(Sqlite.COL_UUID, peertube.getUuid());
values.put(Sqlite.COL_INSTANCE, peertube.getInstance());
values.put(Sqlite.COL_DATE, Helper.dateToString(new Date()));
values.put(Sqlite.COL_CACHE, peertube.getCache().toString());
//Inserts cached peertube
long last_id;
2019-09-06 17:55:14 +02:00
try {
2018-10-21 18:43:57 +02:00
last_id = db.insert(Sqlite.TABLE_PEERTUBE_FAVOURITES, null, values);
2019-09-06 17:55:14 +02:00
} catch (Exception e) {
last_id = -1;
2018-10-21 18:43:57 +02:00
}
return last_id;
}
//------- REMOVE -------
/***
* Remove stored status
* @return int
*/
2019-09-06 17:55:14 +02:00
public int remove(Peertube peertube) {
return db.delete(Sqlite.TABLE_PEERTUBE_FAVOURITES, Sqlite.COL_UUID + " = \"" + peertube.getUuid() + "\" AND " + Sqlite.COL_INSTANCE + " = \"" + peertube.getInstance() + "\"", null);
2018-10-21 18:43:57 +02:00
}
/***
* Remove stored status
* @return int
*/
2019-09-06 17:55:14 +02:00
public int removeAll() {
return db.delete(Sqlite.TABLE_PEERTUBE_FAVOURITES, null, null);
2018-10-21 18:43:57 +02:00
}
//------- GETTERS -------
/**
* Returns all cached Peertube
2019-09-06 17:55:14 +02:00
*
2018-10-21 18:43:57 +02:00
* @return stored peertube List<Peertube>
*/
2019-09-06 17:55:14 +02:00
public List<Peertube> getAllPeertube() {
2018-10-21 18:43:57 +02:00
try {
2019-09-06 17:55:14 +02:00
Cursor c = db.query(Sqlite.TABLE_PEERTUBE_FAVOURITES, null, null, null, null, null, Sqlite.COL_DATE + " DESC");
2018-10-21 18:43:57 +02:00
return cursorToListPeertube(c);
} catch (Exception e) {
return null;
}
}
/**
* Returns a cached Peertube
2019-09-06 17:55:14 +02:00
*
2018-10-21 18:43:57 +02:00
* @return stored peertube List<Peertube>
*/
2019-09-06 17:55:14 +02:00
public List<Peertube> getSinglePeertube(Peertube peertube) {
2018-10-21 18:43:57 +02:00
try {
2019-09-06 17:55:14 +02:00
Cursor c = db.query(Sqlite.TABLE_PEERTUBE_FAVOURITES, null, Sqlite.COL_UUID + " = \"" + peertube.getUuid() + "\" AND " + Sqlite.COL_INSTANCE + " = \"" + peertube.getInstance() + "\"", null, null, null, Sqlite.COL_DATE + " DESC");
2018-10-21 18:43:57 +02:00
return cursorToListPeertube(c);
} catch (Exception e) {
return null;
}
}
/***
* Method to hydrate cached statuses from database
* @param c Cursor
* @return List<Peertube>
*/
2019-09-06 17:55:14 +02:00
private List<Peertube> cursorToListPeertube(Cursor c) {
2018-10-21 18:43:57 +02:00
//No element found
2019-08-17 14:34:33 +02:00
if (c.getCount() == 0) {
c.close();
2018-10-21 18:43:57 +02:00
return null;
2019-08-17 14:34:33 +02:00
}
2018-10-21 18:43:57 +02:00
List<Peertube> peertubes = new ArrayList<>();
2019-09-06 17:55:14 +02:00
while (c.moveToNext()) {
2018-10-21 18:43:57 +02:00
//Restore cached status
try {
2020-04-25 19:20:42 +02:00
Peertube peertube = API.parsePeertube(c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE)), new JSONObject(c.getString(c.getColumnIndex(Sqlite.COL_CACHE))));
2018-10-21 18:43:57 +02:00
peertubes.add(peertube);
} catch (JSONException e) {
e.printStackTrace();
}
}
//Close the cursor
c.close();
//Peertubes list is returned
return peertubes;
}
}