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

220 lines
7.0 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.sqlite;
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
*
* 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
* 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,
* see <http://www.gnu.org/licenses>. */
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
2019-09-06 17:55:14 +02:00
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.Entities.Emojis;
import app.fedilab.android.helper.Helper;
/**
* Created by Thomas on 01/11/2017.
* Manage custom emoji in DB
*/
public class CustomEmojiDAO {
public Context context;
2021-01-19 17:43:51 +01:00
private final SQLiteDatabase db;
public CustomEmojiDAO(Context context, SQLiteDatabase db) {
//Creation of the DB with tables
this.context = context;
this.db = db;
}
//------- INSERTIONS -------
/**
* Insert an emoji in database
2019-09-06 17:55:14 +02:00
*
* @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());
2018-04-28 16:54:06 +02:00
values.put(Sqlite.COL_DATE_CREATION, Helper.dateToString(new Date()));
//Inserts emoji
2019-09-06 17:55:14 +02:00
try {
2017-11-02 14:16:48 +01:00
db.insert(Sqlite.TABLE_CUSTOM_EMOJI, null, values);
2019-09-06 17:55:14 +02:00
} catch (Exception ignored) {
}
}
//------- UPDATES -------
/**
* Update an emoji in database
2019-09-06 17:55:14 +02:00
*
* @param emoji Emojis
*/
2019-09-06 17:55:14 +02:00
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());
2018-04-28 16:54:06 +02:00
values.put(Sqlite.COL_DATE_CREATION, Helper.dateToString(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
*/
2019-09-06 17:55:14 +02:00
public int remove(Emojis emoji) {
String instance = Helper.getLiveInstance(context);
2019-09-06 17:55:14 +02:00
return db.delete(Sqlite.TABLE_CUSTOM_EMOJI, Sqlite.COL_SHORTCODE + " = \"" + emoji.getShortcode() + "\" AND " + Sqlite.COL_INSTANCE + " = \"" + instance + "\"", null);
}
2017-11-24 07:13:30 +01:00
/***
* Remove emoji by id
*/
2019-09-06 17:55:14 +02:00
public void removeAll() {
2017-11-24 07:13:30 +01:00
String instance = Helper.getLiveInstance(context);
db.delete(Sqlite.TABLE_CUSTOM_EMOJI, Sqlite.COL_INSTANCE + " = \"" + instance + "\"", null);
}
//------- GETTERS -------
/**
* Returns all emojis in db for an instance
2019-09-06 17:55:14 +02:00
*
* @return emojis List<Emojis>
*/
2019-09-06 17:55:14 +02:00
public List<Emojis> getAllEmojis() {
String instance = Helper.getLiveInstance(context);
try {
2019-09-06 17:55:14 +02:00
Cursor c = db.query(Sqlite.TABLE_CUSTOM_EMOJI, null, Sqlite.COL_INSTANCE + " = '" + instance + "'", null, Sqlite.COL_SHORTCODE, null, Sqlite.COL_SHORTCODE + " ASC", null);
return cursorToListEmojis(c);
} catch (Exception e) {
return null;
}
}
/**
* Returns all emojis in db for an instance
2019-09-06 17:55:14 +02:00
*
* @return emojis List<Emojis>
*/
2019-09-06 17:55:14 +02:00
public List<Emojis> getAllEmojis(String instance) {
try {
2019-09-06 17:55:14 +02:00
Cursor c = db.query(Sqlite.TABLE_CUSTOM_EMOJI, null, Sqlite.COL_INSTANCE + " = '" + instance + "'", null, Sqlite.COL_SHORTCODE, null, Sqlite.COL_SHORTCODE + " ASC", null);
return cursorToListEmojis(c);
} catch (Exception e) {
return null;
}
}
/**
* Returns an emoji by its shortcode in db
2019-09-06 17:55:14 +02:00
*
* @return emoji Emojis
*/
2019-09-06 17:55:14 +02:00
public Emojis getEmoji(String shortCode) {
try {
String instance = Helper.getLiveInstance(context);
2019-09-06 17:55:14 +02:00
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;
}
}
2017-11-01 19:19:37 +01:00
/**
* Returns an emoji by its shortcode in db
2019-09-06 17:55:14 +02:00
*
2017-11-01 19:19:37 +01:00
* @return emoji Emojis
*/
2019-09-06 17:55:14 +02:00
public List<Emojis> getEmojiStartingBy(String shortCode) {
2017-11-01 19:19:37 +01:00
try {
String instance = Helper.getLiveInstance(context);
2019-11-24 17:38:37 +01:00
Cursor c = db.query(Sqlite.TABLE_CUSTOM_EMOJI, null, Sqlite.COL_SHORTCODE + " LIKE \"%" + shortCode + "%\" AND " + Sqlite.COL_INSTANCE + " = \"" + instance + "\"", null, Sqlite.COL_SHORTCODE, null, null, "20");
2017-11-01 19:19:37 +01:00
return cursorToListEmojis(c);
} catch (Exception e) {
return null;
}
}
/***
* Method to hydrate emoji from database
* @param c Cursor
* @return Emojis
*/
2019-09-06 17:55:14 +02:00
private Emojis cursorToEmoji(Cursor c) {
//No element found
2019-08-17 14:34:33 +02:00
if (c.getCount() == 0) {
c.close();
return null;
2019-08-17 14:34:33 +02:00
}
//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>
*/
2019-09-06 17:55:14 +02:00
private List<Emojis> cursorToListEmojis(Cursor c) {
//No element found
2019-08-17 14:34:33 +02:00
if (c.getCount() == 0) {
c.close();
return null;
2019-08-17 14:34:33 +02:00
}
List<Emojis> emojis = new ArrayList<>();
2019-09-06 17:55:14 +02:00
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;
}
}