Fix #51 - Emoji one not applied

This commit is contained in:
Thomas 2022-05-15 11:03:04 +02:00
parent 9e499d35a7
commit cc23019bc1
6 changed files with 127 additions and 51 deletions

View file

@ -0,0 +1,121 @@
package app.fedilab.android.helper;
/* Copyright 2022 Thomas Schneider
*
* 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.
*
* 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.
*
* 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.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.text.SpannableStringBuilder;
import android.util.AttributeSet;
import androidx.annotation.DimenRes;
import androidx.annotation.Px;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.preference.PreferenceManager;
import com.vanniktech.emoji.EmojiManager;
import app.fedilab.android.R;
/**
* Created by Thomas on 12/05/2018.
* Allows to fix crashes with selection see: https://stackoverflow.com/a/36740247
*/
public class CustomTextView extends AppCompatTextView {
private final boolean emoji;
private float emojiSize;
public CustomTextView(Context context) {
super(context);
final SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context);
emoji = sharedpreferences.getBoolean(context.getString(R.string.SET_DISPLAY_EMOJI), false);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
final SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context);
emoji = sharedpreferences.getBoolean(context.getString(R.string.SET_DISPLAY_EMOJI), false);
final Paint.FontMetrics fontMetrics = getPaint().getFontMetrics();
final float defaultEmojiSize = fontMetrics.descent - fontMetrics.ascent;
if (attrs == null) {
emojiSize = defaultEmojiSize;
} else {
@SuppressLint("CustomViewStyleable") final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.EmojiTextView);
try {
emojiSize = a.getDimension(R.styleable.EmojiTextView_emojiSize, defaultEmojiSize);
} finally {
a.recycle();
}
}
setText(getText());
}
@Override
public void setText(final CharSequence rawText, final BufferType type) {
if (emoji) {
final CharSequence text = rawText == null ? "" : rawText;
final SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text);
final Paint.FontMetrics fontMetrics = getPaint().getFontMetrics();
final float defaultEmojiSize = fontMetrics.descent - fontMetrics.ascent;
EmojiManager.getInstance().replaceWithImages(getContext(), spannableStringBuilder, emojiSize, defaultEmojiSize);
super.setText(spannableStringBuilder, type);
} else {
super.setText(rawText, type);
}
}
/**
* sets the emoji size in pixels and automatically invalidates the text and renders it with the new size
*/
public final void setEmojiSize(@Px final int pixels) {
setEmojiSize(pixels, true);
}
/**
* sets the emoji size in pixels and automatically invalidates the text and renders it with the new size when {@code shouldInvalidate} is true
*/
public final void setEmojiSize(@Px final int pixels, final boolean shouldInvalidate) {
emojiSize = pixels;
if (shouldInvalidate) {
setText(getText());
}
}
/**
* sets the emoji size in pixels with the provided resource and automatically invalidates the text and renders it with the new size
*/
public final void setEmojiSizeRes(@DimenRes final int res) {
setEmojiSizeRes(res, true);
}
/**
* sets the emoji size in pixels with the provided resource and invalidates the text and renders it with the new size when {@code shouldInvalidate} is true
*/
public final void setEmojiSizeRes(@DimenRes final int res, final boolean shouldInvalidate) {
setEmojiSize(getResources().getDimensionPixelSize(res), shouldInvalidate);
}
}

View file

@ -118,7 +118,7 @@
android:layout_marginTop="6dp" android:layout_marginTop="6dp"
tools:text="Warning: Lorem Ipsum below" /> tools:text="Warning: Lorem Ipsum below" />
<androidx.appcompat.widget.AppCompatTextView <app.fedilab.android.helper.CustomTextView
android:id="@+id/spoiler_expand" android:id="@+id/spoiler_expand"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -128,7 +128,7 @@
android:textColor="@color/cyanea_accent_dark_reference" android:textColor="@color/cyanea_accent_dark_reference"
android:text="@string/show_content" /> android:text="@string/show_content" />
<androidx.appcompat.widget.AppCompatTextView <app.fedilab.android.helper.CustomTextView
android:id="@+id/status_content" android:id="@+id/status_content"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View file

@ -69,7 +69,7 @@
android:layout_marginTop="5dp" android:layout_marginTop="5dp"
android:orientation="horizontal"> android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatTextView <app.fedilab.android.helper.CustomTextView
android:id="@+id/status_content" android:id="@+id/status_content"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View file

@ -59,7 +59,7 @@
android:layout_marginTop="5dp" android:layout_marginTop="5dp"
android:orientation="horizontal"> android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatTextView <app.fedilab.android.helper.CustomTextView
android:id="@+id/status_content" android:id="@+id/status_content"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View file

@ -78,7 +78,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="1dp" /> android:layout_height="1dp" />
<androidx.appcompat.widget.AppCompatTextView <app.fedilab.android.helper.CustomTextView
android:id="@+id/spoiler" android:id="@+id/spoiler"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -86,7 +86,7 @@
android:layout_marginTop="6dp" android:layout_marginTop="6dp"
tools:text="Warning: Lorem Ipsum below" /> tools:text="Warning: Lorem Ipsum below" />
<androidx.appcompat.widget.AppCompatTextView <app.fedilab.android.helper.CustomTextView
android:id="@+id/status_content" android:id="@+id/status_content"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View file

@ -5,17 +5,6 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:key="app_prefs"> android:key="app_prefs">
<SwitchPreferenceCompat
app:defaultValue="false"
app:iconSpaceReserved="false"
app:key="@string/SET_MEDIA_URLS"
app:title="@string/set_auto_add_media_url" />
<SwitchPreferenceCompat
app:defaultValue="true"
app:iconSpaceReserved="false"
app:key="@string/SET_AUTO_STORE"
app:title="@string/set_auto_store_toot" />
<SwitchPreferenceCompat <SwitchPreferenceCompat
app:defaultValue="true" app:defaultValue="true"
@ -50,21 +39,6 @@
app:key="@string/SET_WATERMARK_TEXT" app:key="@string/SET_WATERMARK_TEXT"
app:useSimpleSummaryProvider="true" /> app:useSimpleSummaryProvider="true" />
<SwitchPreferenceCompat
app:defaultValue="false"
app:iconSpaceReserved="false"
app:key="@string/SET_AUTOMATICALLY_SPLIT_TOOTS"
app:title="@string/set_automatically_split_toot" />
<SeekBarPreference
android:max="3000"
app:defaultValue="500"
app:dependency="@string/SET_AUTOMATICALLY_SPLIT_TOOTS"
app:iconSpaceReserved="false"
app:key="@string/SET_AUTOMATICALLY_SPLIT_TOOTS_SIZE"
app:min="500"
app:seekBarIncrement="500"
app:showSeekBarValue="true" />
<SwitchPreferenceCompat <SwitchPreferenceCompat
app:defaultValue="false" app:defaultValue="false"
@ -84,12 +58,6 @@
app:key="@string/SET_RETRIEVE_METADATA_IF_URL_FROM_EXTERAL" app:key="@string/SET_RETRIEVE_METADATA_IF_URL_FROM_EXTERAL"
app:title="@string/set_retrieve_metadata_share_from_extras" /> app:title="@string/set_retrieve_metadata_share_from_extras" />
<ListPreference
app:entries="@array/toot_visibility"
app:entryValues="@array/TOOT_VISIBILITY"
app:iconSpaceReserved="false"
app:key="@string/SET_TOOT_VISIBILITY"
app:title="@string/toots_visibility_title" />
<SwitchPreferenceCompat <SwitchPreferenceCompat
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
@ -109,18 +77,5 @@
app:key="@string/SET_FORWARD_TAGS_IN_REPLY" app:key="@string/SET_FORWARD_TAGS_IN_REPLY"
app:title="@string/set_forward_tags" /> app:title="@string/set_forward_tags" />
<SwitchPreferenceCompat
app:iconSpaceReserved="false"
app:key="@string/SET_PHOTO_EDITOR"
app:title="@string/set_photo_editor" />
<SeekBarPreference
android:max="10"
app:defaultValue="3"
app:iconSpaceReserved="false"
app:key="@string/MAX_UPLOAD_IMG_RETRY_TIMES"
app:min="0"
app:showSeekBarValue="true"
app:title="@string/upload_image_maximum_retry_times" />
</androidx.preference.PreferenceScreen> </androidx.preference.PreferenceScreen>