2019-05-18 11:10:30 +02:00
package app.fedilab.android.sqlite ;
2017-11-01 18:52:08 +01:00
/ * Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2017-11-01 18:52:08 +01: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
2017-11-01 18:52:08 +01: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 ,
2017-11-01 18:52:08 +01: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 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 ;
2017-11-01 18:52:08 +01:00
/ * *
* 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 ( ) ) ;
2018-04-28 16:54:06 +02:00
values . put ( Sqlite . COL_DATE_CREATION , Helper . dateToString ( new Date ( ) ) ) ;
2017-11-01 18:52:08 +01:00
//Inserts emoji
try {
2017-11-02 14:16:48 +01:00
db . insert ( Sqlite . TABLE_CUSTOM_EMOJI , null , values ) ;
} catch ( Exception ignored ) { }
2017-11-01 18:52:08 +01:00
}
//------- 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 ( ) ) ;
2018-04-28 16:54:06 +02:00
values . put ( Sqlite . COL_DATE_CREATION , Helper . dateToString ( new Date ( ) ) ) ;
2017-11-01 18:52:08 +01:00
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 ) ;
}
2017-11-24 07:13:30 +01:00
/ * * *
* Remove emoji by id
* /
public void removeAll ( ) {
String instance = Helper . getLiveInstance ( context ) ;
db . delete ( Sqlite . TABLE_CUSTOM_EMOJI , Sqlite . COL_INSTANCE + " = \" " + instance + " \" " , null ) ;
}
2017-11-01 18:52:08 +01:00
//------- GETTERS -------
/ * *
* Returns all emojis in db for an instance
* @return emojis List < Emojis >
* /
public List < Emojis > getAllEmojis ( ) {
String instance = Helper . getLiveInstance ( context ) ;
try {
2017-12-16 09:21:04 +01:00
Cursor c = db . query ( Sqlite . TABLE_CUSTOM_EMOJI , null , Sqlite . COL_INSTANCE + " = ' " + instance + " ' " , null , Sqlite . COL_SHORTCODE , null , Sqlite . COL_SHORTCODE + " ASC " , null ) ;
2017-11-01 18:52:08 +01:00
return cursorToListEmojis ( c ) ;
} catch ( Exception e ) {
return null ;
}
}
2019-05-29 17:58:46 +02:00
/ * *
* Returns all emojis in db for an instance
* @return emojis List < Emojis >
* /
public List < Emojis > getAllEmojis ( String instance ) {
try {
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 ;
}
}
2017-11-01 18:52:08 +01:00
/ * *
* 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 ;
}
}
2017-11-01 19:19:37 +01:00
/ * *
* Returns an emoji by its shortcode in db
* @return emoji Emojis
* /
public List < Emojis > getEmojiStartingBy ( String shortCode ) {
try {
String instance = Helper . getLiveInstance ( context ) ;
2017-12-16 09:21:04 +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 , null ) ;
2017-11-01 19:19:37 +01:00
return cursorToListEmojis ( c ) ;
} catch ( Exception e ) {
return null ;
}
}
2017-11-01 18:52:08 +01:00
/ * * *
* 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 ;
}
}