Manage in db timelines

This commit is contained in:
tom79 2019-04-21 10:22:07 +02:00
parent 28ef97d4cd
commit f59c53f711
6 changed files with 500 additions and 3 deletions

View File

@ -0,0 +1,173 @@
package fr.gouv.etalab.mastodon.client.Entities;
/* Copyright 2019 Thomas Schneider
*
* This file is a part of Mastalab
*
* 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.
*
* Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
* see <http://www.gnu.org/licenses>. */
public class ManageTimelines {
private int position;
private int id;
private boolean displayed;
private Type type;
private String referencedBy;
private String userId;
private String instance;
private RemoteInstance remoteInstance;
private TagTimeline tagTimeline;
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
public boolean isDisplayed() {
return displayed;
}
public void setDisplayed(boolean displayed) {
this.displayed = displayed;
}
public ManageTimelines.Type getType() {
return type;
}
public void setType(ManageTimelines.Type type) {
this.type = type;
}
public String getReferencedBy() {
return referencedBy;
}
public void setReferencedBy(String referencedBy) {
this.referencedBy = referencedBy;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserId() {
return userId;
}
public String getInstance() {
return instance;
}
public void setInstance(String instance) {
this.instance = instance;
}
public RemoteInstance getRemoteInstance() {
return remoteInstance;
}
public void setRemoteInstance(RemoteInstance remoteInstance) {
this.remoteInstance = remoteInstance;
}
public TagTimeline getTagTimeline() {
return tagTimeline;
}
public void setTagTimeline(TagTimeline tagTimeline) {
this.tagTimeline = tagTimeline;
}
public void setUserId(String userId) {
this.userId = userId;
}
public enum Type{
HOME,
DIRECT,
NOTIFICATION,
LOCAL,
PUBLIC,
ART,
PEERTUBE,
TAG,
LIST,
INSTANCE
}
public static Type typeFromDb(String value){
switch (value){
case "HOME":
return Type.HOME;
case "DIRECT":
return Type.DIRECT;
case "NOTIFICATION":
return Type.NOTIFICATION;
case "LOCAL":
return Type.LOCAL;
case "PUBLIC":
return Type.PUBLIC;
case "ART":
return Type.ART;
case "PEERTUBE":
return Type.PEERTUBE;
case "TAG":
return Type.TAG;
case "LIST":
return Type.LIST;
case "INSTANCE":
return Type.INSTANCE;
}
return null;
}
public static String typeToDb(Type type){
switch (type){
case HOME:
return "HOME";
case DIRECT:
return "DIRECT";
case NOTIFICATION:
return "NOTIFICATION";
case LOCAL:
return "LOCAL";
case PUBLIC:
return "PUBLIC";
case ART:
return "ART";
case PEERTUBE:
return "PEERTUBE";
case TAG:
return "TAG";
case LIST:
return "LIST";
case INSTANCE:
return "INSTANCE";
}
return null;
}
}

View File

@ -15,12 +15,15 @@
package fr.gouv.etalab.mastodon.client.Entities;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by Thomas on 05/10/2018.
* Manages following instances
*/
public class RemoteInstance {
public class RemoteInstance implements Parcelable {
private String host;
private String type;
@ -61,4 +64,36 @@ public class RemoteInstance {
public void setDbID(String dbID) {
this.dbID = dbID;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.host);
dest.writeString(this.type);
dest.writeString(this.id);
dest.writeString(this.dbID);
}
protected RemoteInstance(Parcel in) {
this.host = in.readString();
this.type = in.readString();
this.id = in.readString();
this.dbID = in.readString();
}
public static final Parcelable.Creator<RemoteInstance> CREATOR = new Parcelable.Creator<RemoteInstance>() {
@Override
public RemoteInstance createFromParcel(Parcel source) {
return new RemoteInstance(source);
}
@Override
public RemoteInstance[] newArray(int size) {
return new RemoteInstance[size];
}
};
}

View File

@ -14,6 +14,9 @@
* see <http://www.gnu.org/licenses>. */
package fr.gouv.etalab.mastodon.client.Entities;
import android.os.Parcel;
import android.os.Parcelable;
import java.util.List;
/**
@ -21,7 +24,7 @@ import java.util.List;
* Manage Tags timeline settings
*/
public class TagTimeline {
public class TagTimeline implements Parcelable {
private String name;
private String displayname;
@ -86,4 +89,45 @@ public class TagTimeline {
public void setDisplayname(String displayname) {
this.displayname = displayname;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.name);
dest.writeString(this.displayname);
dest.writeByte(this.isART ? (byte) 1 : (byte) 0);
dest.writeByte(this.isNSFW ? (byte) 1 : (byte) 0);
dest.writeStringList(this.any);
dest.writeStringList(this.all);
dest.writeStringList(this.none);
}
public TagTimeline() {
}
protected TagTimeline(Parcel in) {
this.name = in.readString();
this.displayname = in.readString();
this.isART = in.readByte() != 0;
this.isNSFW = in.readByte() != 0;
this.any = in.createStringArrayList();
this.all = in.createStringArrayList();
this.none = in.createStringArrayList();
}
public static final Parcelable.Creator<TagTimeline> CREATOR = new Parcelable.Creator<TagTimeline>() {
@Override
public TagTimeline createFromParcel(Parcel source) {
return new TagTimeline(source);
}
@Override
public TagTimeline[] newArray(int size) {
return new TagTimeline[size];
}
};
}

View File

@ -178,8 +178,10 @@ import fr.gouv.etalab.mastodon.client.Entities.Card;
import fr.gouv.etalab.mastodon.client.Entities.Emojis;
import fr.gouv.etalab.mastodon.client.Entities.Filters;
import fr.gouv.etalab.mastodon.client.Entities.Mention;
import fr.gouv.etalab.mastodon.client.Entities.RemoteInstance;
import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.client.Entities.Tag;
import fr.gouv.etalab.mastodon.client.Entities.TagTimeline;
import fr.gouv.etalab.mastodon.client.Entities.Version;
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
import fr.gouv.etalab.mastodon.sqlite.SearchDAO;
@ -2422,6 +2424,64 @@ public class Helper {
}
/**
* Unserialized a TagTimeline
* @param serializedTagTimeline String serialized TagTimeline
* @return TagTimeline
*/
public static TagTimeline restoreTagTimelineFromString(String serializedTagTimeline){
Gson gson = new Gson();
try {
return gson.fromJson(serializedTagTimeline, TagTimeline.class);
}catch (Exception e){
return null;
}
}
/**
* Serialized a TagTimeline class
* @param tagTimeline TagTimeline to serialize
* @return String serialized TagTimeline
*/
public static String tagTimelineToStringStorage(TagTimeline tagTimeline){
Gson gson = new Gson();
try {
return gson.toJson(tagTimeline);
}catch (Exception e){
return null;
}
}
/**
* Unserialized a RemoteInstance
* @param serializedRemoteInstance String serialized RemoteInstance
* @return RemoteInstance
*/
public static RemoteInstance restoreRemoteInstanceFromString(String serializedRemoteInstance){
Gson gson = new Gson();
try {
return gson.fromJson(serializedRemoteInstance, RemoteInstance.class);
}catch (Exception e){
return null;
}
}
/**
* Serialized a RemoteInstance class
* @param remoteInstance RemoteInstance to serialize
* @return String serialized RemoteInstance
*/
public static String remoteInstanceToStringStorage(RemoteInstance remoteInstance){
Gson gson = new Gson();
try {
return gson.toJson(remoteInstance);
}catch (Exception e){
return null;
}
}
/**
* Unserialized a Locale
* @param serializedLocale String serialized locale

View File

@ -46,7 +46,7 @@ import fr.gouv.etalab.mastodon.helper.Helper;
public class Sqlite extends SQLiteOpenHelper {
public static final int DB_VERSION = 27;
public static final int DB_VERSION = 28;
public static final String DB_NAME = "mastodon_etalab_db";
public static SQLiteDatabase db;
private static Sqlite sInstance;
@ -87,6 +87,9 @@ public class Sqlite extends SQLiteOpenHelper {
//Table for blocking tracking domains
public static final String TABLE_TRACKING_BLOCK = "TRACKING_BLOCK";
//Table for timelines
public static final String TABLE_TIMELINES = "TIMELINES";
static final String COL_USER_ID = "USER_ID";
static final String COL_USERNAME = "USERNAME";
static final String COL_ACCT = "ACCT";
@ -255,6 +258,22 @@ public class Sqlite extends SQLiteOpenHelper {
+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COL_DOMAIN + " TEXT NOT NULL)";
static final String COL_TYPE = "TYPE";
static final String COL_REFERENCED_BY = "REFERENCED_BY";
static final String COL_DISPLAYED = "DISPLAYED";
static final String COL_POSITION = "POSITION";
static final String COL_REMOTE_INSTANCE = "REMOTE_INSTANCE";
static final String COL_TAG_TIMELINE = "TAG_TIMELINE";
private static final String CREATE_TABLE_TIMELINES = "CREATE TABLE " + TABLE_TIMELINES + " ("
+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COL_POSITION + " INTEGER NOT NULL, "
+ COL_USER_ID + " TEXT NOT NULL, " + COL_INSTANCE + " TEXT NOT NULL, "
+ COL_TYPE + " TEXT NOT NULL, "
+ COL_REMOTE_INSTANCE + " TEXT, "
+ COL_TAG_TIMELINE + " TEXT, "
+ COL_DISPLAYED + " INTEGER NOT NULL, "
+ COL_REFERENCED_BY + " TEXT)";
public Sqlite(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
@ -283,6 +302,7 @@ public class Sqlite extends SQLiteOpenHelper {
db.execSQL(CREATE_TABLE_CACHE_TAGS);
db.execSQL(CREATE_TABLE_BOOST_SCHEDULE);
db.execSQL(CREATE_TABLE_TRACKING_BLOCK);
db.execSQL(CREATE_TABLE_TIMELINES);
}
@Override
@ -362,6 +382,8 @@ public class Sqlite extends SQLiteOpenHelper {
db.execSQL("ALTER TABLE " + TABLE_USER_ACCOUNT + " ADD COLUMN " + COL_UPDATED_AT + " TEXT");
case 26:
db.execSQL(CREATE_TABLE_TRACKING_BLOCK);
case 27:
db.execSQL(CREATE_TABLE_TIMELINES);
default:
break;
}

View File

@ -0,0 +1,163 @@
package fr.gouv.etalab.mastodon.sqlite;
/* Copyright 2019 Thomas Schneider
*
* This file is a part of Mastalab
*
* 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.
*
* Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
* 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;
import fr.gouv.etalab.mastodon.client.Entities.ManageTimelines;
import fr.gouv.etalab.mastodon.helper.Helper;
public class TimelinesDAO {
private SQLiteDatabase db;
public Context context;
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_REFERENCED_BY, timeline.getReferencedBy());
values.put(Sqlite.COL_POSITION, timeline.getPosition());
values.put(Sqlite.COL_USER_ID, userId);
values.put(Sqlite.COL_INSTANCE, instance);
if( timeline.getTagTimeline() != null)
values.put(Sqlite.COL_TAG_TIMELINE, Helper.tagTimelineToStringStorage(timeline.getTagTimeline()));
if( timeline.getRemoteInstance() != null)
values.put(Sqlite.COL_REMOTE_INSTANCE, Helper.remoteInstanceToStringStorage(timeline.getRemoteInstance()));
try{
db.insert(Sqlite.TABLE_TIMELINES, null, values);
}catch (Exception ignored) {}
}
//------- REMOVE -------
public int remove(ManageTimelines timeline){
return db.delete(Sqlite.TABLE_TIMELINES, Sqlite.COL_ID + " = \"" + timeline.getId() + "\"", null);
}
//------- UPDATE -------
public int update(ManageTimelines timeline) {
ContentValues values = new ContentValues();
values.put(Sqlite.COL_DISPLAYED, timeline.isDisplayed());
values.put(Sqlite.COL_POSITION, timeline.getPosition());
return db.update(Sqlite.TABLE_TIMELINES,
values, Sqlite.COL_ID + " = ? ",
new String[]{String.valueOf(timeline.getId())});
}
public List<ManageTimelines> getAllTimelines(){
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 {
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);
return cursorToTimelines(c);
} catch (Exception e) {
return null;
}
}
public List<ManageTimelines> getDisplayedTimelines(){
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 {
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);
return cursorToTimelines(c);
} catch (Exception e) {
return null;
}
}
/***
* Method to hydrate a timeline
* @param c Cursor
* @return ManageTimelines
*/
private ManageTimelines cursorToTimeline(Cursor c){
//No element found
if (c.getCount() == 0)
return null;
//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)));
if( c.getString(c.getColumnIndex(Sqlite.COL_TAG_TIMELINE)) != null )
manageTimelines.setTagTimeline(Helper.restoreTagTimelineFromString(c.getString(c.getColumnIndex(Sqlite.COL_TAG_TIMELINE))));
if( c.getString(c.getColumnIndex(Sqlite.COL_REMOTE_INSTANCE)) != null )
manageTimelines.setRemoteInstance(Helper.restoreRemoteInstanceFromString(c.getString(c.getColumnIndex(Sqlite.COL_REMOTE_INSTANCE))));
manageTimelines.setReferencedBy(c.getString(c.getColumnIndex(Sqlite.COL_REFERENCED_BY)));
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>
*/
private List<ManageTimelines> cursorToTimelines(Cursor c){
//No element found
if (c.getCount() == 0)
return null;
List<ManageTimelines> remoteInstances = new ArrayList<>();
while (c.moveToNext() ) {
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)));
if( c.getString(c.getColumnIndex(Sqlite.COL_TAG_TIMELINE)) != null )
manageTimelines.setTagTimeline(Helper.restoreTagTimelineFromString(c.getString(c.getColumnIndex(Sqlite.COL_TAG_TIMELINE))));
if( c.getString(c.getColumnIndex(Sqlite.COL_REMOTE_INSTANCE)) != null )
manageTimelines.setRemoteInstance(Helper.restoreRemoteInstanceFromString(c.getString(c.getColumnIndex(Sqlite.COL_REMOTE_INSTANCE))));
manageTimelines.setReferencedBy(c.getString(c.getColumnIndex(Sqlite.COL_REFERENCED_BY)));
manageTimelines.setType(ManageTimelines.typeFromDb(c.getString(c.getColumnIndex(Sqlite.COL_TYPE))));
remoteInstances.add(manageTimelines);
}
//Close the cursor
c.close();
return remoteInstances;
}
}