Creates the table in db + stores/updates custom emojis when they are seen in a toot

This commit is contained in:
stom79 2017-11-01 18:52:08 +01:00
parent bab4bfeffa
commit 69b8237853
3 changed files with 205 additions and 4 deletions

View File

@ -137,6 +137,7 @@ import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.client.PatchBaseImageDownloader;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveEmojiInterface;
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
import fr.gouv.etalab.mastodon.sqlite.CustomEmojiDAO;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import mastodon.etalab.gouv.fr.mastodon.R;
@ -1171,7 +1172,7 @@ public class Helper {
@SuppressWarnings("SameParameterValue")
public static SpannableString clickableElements(final Context context, String fullContent, List<Mention> mentions, final List<Emojis> emojis, final int position, boolean useHTML, final OnRetrieveEmojiInterface listener) {
final SpannableString spannableString;
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
if( useHTML) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
spannableString = new SpannableString(Html.fromHtml(fullContent, Html.FROM_HTML_MODE_LEGACY));
@ -1231,6 +1232,7 @@ public class Helper {
.cacheOnDisk(true).resetViewBeforeLoading(true).build();
imageLoader = ImageLoader.getInstance();
for (final Emojis emoji : emojis) {
storeEmoji(context, db, emoji);
final String targetedEmoji = ":" + emoji.getShortcode() + ":";
if (spannableString.toString().contains(targetedEmoji)) {
//emojis can be used several times so we have to loop
@ -1318,7 +1320,21 @@ public class Helper {
return spannableString;
}
/**
* Manage custom emoji to store or update them
* @param context Context
* @param db SQLiteDatabase
* @param emojis Emojis
*/
private static void storeEmoji(Context context, SQLiteDatabase db, Emojis emojis){
try{
Emojis emoji_ = new CustomEmojiDAO(context, db).getEmoji(emojis.getShortcode());
if( emoji_ == null)
new CustomEmojiDAO(context, db).insertEmoji(emojis);
else
new CustomEmojiDAO(context, db).updateEmoji(emojis);
}catch (Exception ignored){}
}
/**
* Check if the account bio contents urls & tags and fills the content with ClickableSpan

View File

@ -0,0 +1,175 @@
package fr.gouv.etalab.mastodon.sqlite;
/* Copyright 2017 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.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import fr.gouv.etalab.mastodon.client.Entities.Emojis;
import fr.gouv.etalab.mastodon.helper.Helper;
/**
* Created by Thomas on 01/11/2017.
* Manage custom emoji in DB
*/
public class CustomEmojiDAO {
private SQLiteDatabase db;
public Context context;
public CustomEmojiDAO(Context context, SQLiteDatabase db) {
//Creation of the DB with tables
this.context = context;
this.db = db;
}
//------- INSERTIONS -------
/**
* Insert an emoji in database
* @param emoji Emoji
*/
public void insertEmoji(Emojis emoji) {
ContentValues values = new ContentValues();
String instance = Helper.getLiveInstance(context);
values.put(Sqlite.COL_SHORTCODE, emoji.getShortcode());
values.put(Sqlite.COL_INSTANCE, instance);
values.put(Sqlite.COL_URL, emoji.getUrl());
values.put(Sqlite.COL_URL_STATIC, emoji.getStatic_url());
values.put(Sqlite.COL_DATE_CREATION, Helper.dateToString(context, new Date()));
//Inserts emoji
long last_id;
try{
last_id = db.insert(Sqlite.TABLE_CUSTOM_EMOJI, null, values);
}catch (Exception e) {
last_id = -1;
}
}
//------- UPDATES -------
/**
* Update an emoji in database
* @param emoji Emojis
*/
public void updateEmoji(Emojis emoji ) {
ContentValues values = new ContentValues();
String instance = Helper.getLiveInstance(context);
values.put(Sqlite.COL_URL, emoji.getUrl());
values.put(Sqlite.COL_URL_STATIC, emoji.getStatic_url());
values.put(Sqlite.COL_DATE_CREATION, Helper.dateToString(context, new Date()));
db.update(Sqlite.TABLE_CUSTOM_EMOJI,
values, Sqlite.COL_SHORTCODE + " = ? AND " + Sqlite.COL_INSTANCE + " = ? ",
new String[]{emoji.getShortcode(), instance});
}
//------- REMOVE -------
/***
* Remove emoji by id
* @return int
*/
public int remove(Emojis emoji){
String instance = Helper.getLiveInstance(context);
return db.delete(Sqlite.TABLE_CUSTOM_EMOJI, Sqlite.COL_SHORTCODE + " = \"" + emoji.getShortcode() + "\" AND " + Sqlite.COL_INSTANCE + " = \"" + instance+ "\"", null);
}
//------- GETTERS -------
/**
* Returns all emojis in db for an instance
* @return emojis List<Emojis>
*/
public List<Emojis> getAllEmojis(){
String instance = Helper.getLiveInstance(context);
try {
Cursor c = db.query(Sqlite.TABLE_CUSTOM_EMOJI, null, Sqlite.COL_INSTANCE + " = '" + instance+ "'", null, null, null, Sqlite.COL_SHORTCODE + " ASC", null);
return cursorToListEmojis(c);
} catch (Exception e) {
return null;
}
}
/**
* Returns an emoji by its shortcode in db
* @return emoji Emojis
*/
public Emojis getEmoji(String shortCode){
try {
String instance = Helper.getLiveInstance(context);
Cursor c = db.query(Sqlite.TABLE_CUSTOM_EMOJI, null, Sqlite.COL_SHORTCODE + " = \"" + shortCode + "\" AND " + Sqlite.COL_INSTANCE + " = \"" + instance+ "\"", null, null, null, null, null);
return cursorToEmoji(c);
} catch (Exception e) {
return null;
}
}
/***
* Method to hydrate emoji from database
* @param c Cursor
* @return Emojis
*/
private Emojis cursorToEmoji(Cursor c){
//No element found
if (c.getCount() == 0)
return null;
//Take the first element
c.moveToFirst();
//New user
Emojis emoji = new Emojis();
emoji.setShortcode(c.getString(c.getColumnIndex(Sqlite.COL_SHORTCODE)));
emoji.setUrl(c.getString(c.getColumnIndex(Sqlite.COL_URL)));
emoji.setStatic_url(c.getString(c.getColumnIndex(Sqlite.COL_URL_STATIC)));
//Close the cursor
c.close();
//Stored emoji is returned
return emoji;
}
/***
* Method to hydrate stored emojis from database
* @param c Cursor
* @return List<Emojis>
*/
private List<Emojis> cursorToListEmojis(Cursor c){
//No element found
if (c.getCount() == 0)
return null;
List<Emojis> emojis = new ArrayList<>();
while (c.moveToNext() ) {
//Restore the emojis
Emojis emoji = new Emojis();
emoji.setShortcode(c.getString(c.getColumnIndex(Sqlite.COL_SHORTCODE)));
emoji.setUrl(c.getString(c.getColumnIndex(Sqlite.COL_URL)));
emoji.setStatic_url(c.getString(c.getColumnIndex(Sqlite.COL_URL_STATIC)));
emojis.add(emoji);
}
//Close the cursor
c.close();
//Emjois list is returned
return emojis;
}
}

View File

@ -26,7 +26,7 @@ import android.database.sqlite.SQLiteOpenHelper;
@SuppressWarnings("WeakerAccess")
public class Sqlite extends SQLiteOpenHelper {
public static final int DB_VERSION = 3;
public static final int DB_VERSION = 4;
public static final String DB_NAME = "mastodon_etalab_db";
public static SQLiteDatabase db;
private static Sqlite sInstance;
@ -38,7 +38,8 @@ public class Sqlite extends SQLiteOpenHelper {
static final String TABLE_USER_ACCOUNT = "USER_ACCOUNT";
//Table of stored status
static final String TABLE_STATUSES_STORED = "STATUSES_STORED";
//Table for custom emoji
static final String TABLE_CUSTOM_EMOJI = "CUSTOM_EMOJI";
public static final String COL_USER_ID = "USER_ID";
public static final String COL_USERNAME = "USERNAME";
@ -87,6 +88,13 @@ public class Sqlite extends SQLiteOpenHelper {
+ COL_SENT + " INTEGER NOT NULL, " + COL_DATE_SENT + " TEXT)";
public static final String COL_SHORTCODE = "SHORTCODE";
public static final String COL_URL_STATIC = "URL_STATIC";
private final String CREATE_TABLE_CUSTOM_EMOJI = "CREATE TABLE " + TABLE_CUSTOM_EMOJI + " ("
+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COL_SHORTCODE + " TEXT NOT NULL, " + COL_INSTANCE + " TEXT NOT NULL, "
+ COL_URL + " TEXT NOT NULL, " + COL_URL_STATIC + " TEXT NOT NULL, " + COL_DATE_CREATION + " TEXT NOT NULL)";
public Sqlite(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@ -114,6 +122,8 @@ public class Sqlite extends SQLiteOpenHelper {
case 2:
db.execSQL("DROP TABLE IF EXISTS " + TABLE_STATUSES_STORED);
db.execSQL(CREATE_TABLE_STATUSES_STORED);
case 3:
db.execSQL(CREATE_TABLE_CUSTOM_EMOJI);
default:
break;
}