Tries to find them =)

This commit is contained in:
stom79 2017-11-01 19:19:37 +01:00
parent 69b8237853
commit 1cbe5ce542
8 changed files with 333 additions and 2 deletions

View File

@ -45,6 +45,7 @@ import android.text.Editable;
import android.text.Html;
import android.text.InputFilter;
import android.text.InputType;
import android.text.SpannableString;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
@ -105,6 +106,7 @@ import java.util.regex.Pattern;
import cz.msebera.android.httpclient.Header;
import fr.gouv.etalab.mastodon.asynctasks.PostStatusAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveAccountsForReplyAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveEmojiAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveSearchAccountsAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveSearchAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.UpdateDescriptionAttachmentAsyncTask;
@ -112,11 +114,14 @@ import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Attachment;
import fr.gouv.etalab.mastodon.client.Entities.Emojis;
import fr.gouv.etalab.mastodon.client.Entities.Error;
import fr.gouv.etalab.mastodon.client.Entities.Mention;
import fr.gouv.etalab.mastodon.client.Entities.Results;
import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.client.Entities.StoredStatus;
import fr.gouv.etalab.mastodon.drawers.EmojisSearchAdapter;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveEmojiInterface;
import fr.gouv.etalab.mastodon.translation.Translate;
import fr.gouv.etalab.mastodon.client.Entities.Version;
import fr.gouv.etalab.mastodon.client.PatchBaseImageDownloader;
@ -146,7 +151,7 @@ import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor;
* Toot activity class
*/
public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAccountshInterface, OnRetrieveAttachmentInterface, OnPostStatusActionInterface, OnRetrieveSearchInterface, OnRetrieveAccountsReplyInterface, OnTranslatedInterface {
public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAccountshInterface, OnRetrieveAttachmentInterface, OnPostStatusActionInterface, OnRetrieveSearchInterface, OnRetrieveAccountsReplyInterface, OnTranslatedInterface, OnRetrieveEmojiInterface {
private String visibility;
@ -553,6 +558,9 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc
String patternTag = "^(.|\\s)*(#([\\w-]{2,}))$";
final Pattern tPattern = Pattern.compile(patternTag);
String patternEmoji = "^(.|\\s)*(:([\\w_]{1,}))$";
final Pattern ePattern = Pattern.compile(patternEmoji);
toot_cw_content.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@ -614,7 +622,22 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc
pp_actionBar.setVisibility(View.GONE);
}
new RetrieveSearchAsyncTask(getApplicationContext(),search,TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}else{toot_content.dismissDropDown();}
}else{
if( s.toString().charAt(0) == ':')
mt = ePattern.matcher(s.toString().substring(currentCursorPosition- searchLength, currentCursorPosition));
else
mt = ePattern.matcher(s.toString().substring(currentCursorPosition- (searchLength-1), currentCursorPosition));
if(mt.matches()) {
String shortcode = mt.group(3);
if (pp_progress != null && pp_actionBar != null) {
pp_progress.setVisibility(View.VISIBLE);
pp_actionBar.setVisibility(View.GONE);
}
new RetrieveEmojiAsyncTask(getApplicationContext(),shortcode,TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}else {
toot_content.dismissDropDown();
}
}
}
@ -1394,6 +1417,22 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc
}
}
@Override
public void onRetrieveEmoji(int position, SpannableString spannableString, Boolean error) {
}
@Override
public void onRetrieveSearchEmoji(List<Emojis> emojis) {
if( pp_progress != null && pp_actionBar != null) {
pp_progress.setVisibility(View.GONE);
pp_actionBar.setVisibility(View.VISIBLE);
}
if( emojis != null && emojis.size() > 0){
EmojisSearchAdapter tagsSearchAdapter = new EmojisSearchAdapter(TootActivity.this, emojis);
}
}
@Override
public void onRetrieveSearch(Results results, Error error) {
if( pp_progress != null && pp_actionBar != null) {
@ -1745,4 +1784,6 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc
}
}
}

View File

@ -0,0 +1,61 @@
/* 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>. */
package fr.gouv.etalab.mastodon.asynctasks;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import java.lang.ref.WeakReference;
import java.util.List;
import fr.gouv.etalab.mastodon.client.Entities.Emojis;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveEmojiInterface;
import fr.gouv.etalab.mastodon.sqlite.CustomEmojiDAO;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
/**
* Created by Thomas on 01/11/2017.
* Retrieves emojis
*/
public class RetrieveEmojiAsyncTask extends AsyncTask<Void, Void, Void> {
private String shortcode;
private List<Emojis> emojis;
private OnRetrieveEmojiInterface listener;
private WeakReference<Context> contextReference;
public RetrieveEmojiAsyncTask(Context context, String shortcode, OnRetrieveEmojiInterface onRetrieveEmojiInterface){
this.contextReference = new WeakReference<>(context);
this.shortcode = shortcode;
this.listener = onRetrieveEmojiInterface;
}
@Override
protected Void doInBackground(Void... params) {
SQLiteDatabase db = Sqlite.getInstance(contextReference.get(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
emojis = new CustomEmojiDAO(contextReference.get(), db).getEmojiStartingBy(shortcode);
return null;
}
@Override
protected void onPostExecute(Void result) {
listener.onRetrieveSearchEmoji(emojis);
}
}

View File

@ -0,0 +1,156 @@
package fr.gouv.etalab.mastodon.drawers;
/* 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.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import java.util.ArrayList;
import java.util.List;
import fr.gouv.etalab.mastodon.client.Entities.Emojis;
import mastodon.etalab.gouv.fr.mastodon.R;
/**
* Created by Thomas on 01/11/2017.
* Adapter for emojis when searching
*/
public class EmojisSearchAdapter extends ArrayAdapter<Emojis> implements Filterable {
private List<Emojis> emojis, tempEmojis, suggestions ;
private LayoutInflater layoutInflater;
private ImageLoader imageLoader;
private DisplayImageOptions options;
private Context context;
public EmojisSearchAdapter(Context context, List<Emojis> emojis){
super(context, android.R.layout.simple_list_item_1, emojis);
this.emojis = emojis;
this.context = context;
this.tempEmojis = new ArrayList<>(emojis);
this.suggestions = new ArrayList<>(emojis);
layoutInflater = LayoutInflater.from(context);
imageLoader = ImageLoader.getInstance();
options = new DisplayImageOptions.Builder().displayer(new SimpleBitmapDisplayer()).cacheInMemory(false)
.cacheOnDisk(true).resetViewBeforeLoading(true).build();
}
@Override
public int getCount() {
return emojis.size();
}
@Override
public Emojis getItem(int position) {
return emojis.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@NonNull
@Override
public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
final Emojis emoji = emojis.get(position);
final ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.drawer_emoji_search, parent, false);
holder = new ViewHolder();
holder.emoji_icon = convertView.findViewById(R.id.emoji_icon);
holder.emoji_shortcode = convertView.findViewById(R.id.emoji_shortcode);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.emoji_shortcode.setText(String.format("%s", emoji.getShortcode()));
//Profile picture
imageLoader.displayImage(emoji.getUrl(), holder.emoji_icon, options);
return convertView;
}
@NonNull
@Override
public Filter getFilter() {
return emojiFilter;
}
private Filter emojiFilter = new Filter() {
@Override
public CharSequence convertResultToString(Object resultValue) {
Emojis emoji = (Emojis) resultValue;
return emoji.getShortcode();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
if (constraint != null) {
suggestions.clear();
for (Emojis emoji : tempEmojis) {
suggestions.add(emoji);
}
FilterResults filterResults = new FilterResults();
filterResults.values = suggestions;
filterResults.count = suggestions.size();
return filterResults;
} else {
return new FilterResults();
}
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
ArrayList<Emojis> c = (ArrayList<Emojis>) results.values;
if (results.count > 0) {
clear();
for (Emojis cust : c) {
add(cust);
notifyDataSetChanged();
}
} else{
clear();
notifyDataSetChanged();
}
}
};
private class ViewHolder {
ImageView emoji_icon;
TextView emoji_shortcode;
}
}

View File

@ -67,6 +67,7 @@ import fr.gouv.etalab.mastodon.asynctasks.RetrieveFeedsAsyncTask;
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Attachment;
import fr.gouv.etalab.mastodon.client.Entities.Emojis;
import fr.gouv.etalab.mastodon.client.Entities.Error;
import fr.gouv.etalab.mastodon.helper.CrossActions;
import fr.gouv.etalab.mastodon.interfaces.OnPostActionInterface;
@ -894,6 +895,11 @@ public class NotificationsListAdapter extends RecyclerView.Adapter implements On
}
}
@Override
public void onRetrieveSearchEmoji(List<Emojis> emojis) {
}
class ViewHolder extends RecyclerView.ViewHolder {

View File

@ -81,6 +81,7 @@ import fr.gouv.etalab.mastodon.asynctasks.RetrieveRepliesAsyncTask;
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Attachment;
import fr.gouv.etalab.mastodon.client.Entities.Emojis;
import fr.gouv.etalab.mastodon.client.Entities.Error;
import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.translation.Translate;
@ -1364,6 +1365,11 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct
}
}
@Override
public void onRetrieveSearchEmoji(List<Emojis> emojis) {
}
@Override
public void onTranslatedTextview(Translate translate, Status status, String translatedResult, Boolean error) {
if( error){

View File

@ -16,6 +16,10 @@ package fr.gouv.etalab.mastodon.interfaces;
import android.text.SpannableString;
import java.util.List;
import fr.gouv.etalab.mastodon.client.Entities.Emojis;
/**
* Created by Thomas on 19/10/2017.
@ -23,4 +27,5 @@ import android.text.SpannableString;
*/
public interface OnRetrieveEmojiInterface {
void onRetrieveEmoji(int position, SpannableString spannableString, Boolean error);
void onRetrieveSearchEmoji(List<Emojis> emojis);
}

View File

@ -127,6 +127,20 @@ public class CustomEmojiDAO {
}
/**
* Returns an emoji by its shortcode in db
* @return emoji Emojis
*/
public List<Emojis> getEmojiStartingBy(String shortCode){
try {
String instance = Helper.getLiveInstance(context);
Cursor c = db.query(Sqlite.TABLE_CUSTOM_EMOJI, null, Sqlite.COL_SHORTCODE + " LIKE \"%" + shortCode + "%\" AND " + Sqlite.COL_INSTANCE + " = \"" + instance+ "\"", null, null, null, null, null);
return cursorToListEmojis(c);
} catch (Exception e) {
return null;
}
}
/***
* Method to hydrate emoji from database
* @param c Cursor

View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
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>.
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:id="@+id/account_container"
android:orientation="horizontal"
tools:ignore="UseCompoundDrawables">
<ImageView
android:layout_margin="5dp"
android:layout_gravity="center"
android:id="@+id/emoji_icon"
android:layout_width="30dp"
android:layout_height="30dp"
tools:ignore="ContentDescription" />
<TextView
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:maxLines="1"
android:id="@+id/emoji_shortcode"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>