Skip to content

Commit

Permalink
Add emoji-material module for material bindings. (#394)
Browse files Browse the repository at this point in the history
* Add emoji-material module for material bindings.

* Add new lines.

* More new lines.


Co-authored-by: Ruben Gees <[email protected]>
  • Loading branch information
vanniktech and rubengees authored Oct 3, 2019
1 parent a2475e9 commit 3b132a3
Show file tree
Hide file tree
Showing 29 changed files with 320 additions and 42 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ dependencies {
implementation project(':emoji-google-compat')
implementation project(':emoji-twitter')
implementation project(':emoji-kotlin')
implementation project(':emoji-material')

implementation rootProject.ext.supportAppCompat
implementation rootProject.ext.supportRecyclerView
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.vanniktech.emoji.sample;

import android.annotation.SuppressLint;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
Expand Down Expand Up @@ -38,13 +40,15 @@
ImageView emojiButton;
EmojiCompat emojiCompat;

@Override protected void onCreate(final Bundle savedInstanceState) {
@Override @SuppressLint("SetTextI18n") protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

chatAdapter = new ChatAdapter();

final Button button = findViewById(R.id.main_activity_material_button);
button.setText("\uD83D\uDE18\uD83D\uDE02\uD83E\uDD8C");
editText = findViewById(R.id.main_activity_chat_bottom_message_edittext);
rootView = findViewById(R.id.main_activity_root_view);
emojiButton = findViewById(R.id.main_activity_emoji);
Expand All @@ -54,6 +58,7 @@
sendButton.setColorFilter(ContextCompat.getColor(this, R.color.emoji_icons), PorterDuff.Mode.SRC_IN);

final CheckBox forceEmojisOnly = findViewById(R.id.main_activity_force_emojis_only);
forceEmojisOnly.setText("Force emojis only \uD83D\uDE18");
forceEmojisOnly.setOnCheckedChangeListener((ignore, isChecked) -> {
if (isChecked) {
editText.clearFocus();
Expand Down
17 changes: 13 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,28 @@
android:orientation="vertical"
tools:context=".MainActivity"
>
<CheckBox
android:id="@+id/main_activity_force_emojis_only"
<com.vanniktech.emoji.material.EmojiMaterialButton
android:id="@+id/main_activity_material_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/force_emojis_only"
android:layout_alignParentTop="true"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
/>
<com.vanniktech.emoji.material.EmojiMaterialCheckBox
android:id="@+id/main_activity_force_emojis_only"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/main_activity_material_button"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/main_activity_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/main_activity_force_emojis_only"
android:layout_above="@+id/main_activity_emoji_bar"
android:layout_below="@id/main_activity_force_emojis_only"
/>
<LinearLayout
android:id="@+id/main_activity_emoji_bar"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-night/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="colorSecondary">#FF4081</color>
<color name="chat_input_background">#212121</color>
</resources>
2 changes: 1 addition & 1 deletion app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="colorSecondary">#FF4081</color>
<color name="chat_input_background">#FFFFFF</color>
</resources>
1 change: 0 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@
<string name="variant_google_compat_title">GoogleCompat</string>
<string name="variant_twitter_title">Twitter</string>
<string name="show_dialog_title">Show Dialog</string>
<string name="force_emojis_only">Force EmojisE only</string>
</resources>
4 changes: 2 additions & 2 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="colorSecondary">@color/colorSecondary</item>
</style>
</resources>
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ buildscript {
'runner': "com.android.support.test:runner:${versions.testRules}",
'rules': "com.android.support.test:rules:${versions.testRules}",
],
'material': "com.google.android.material:material:1.1.0-alpha10",
],
'fastLaneScreenGrab': "tools.fastlane:screengrab:1.2.0",
]
Expand Down Expand Up @@ -112,4 +113,11 @@ wrapper {

subprojects {
task allDependencies(type: DependencyReportTask) {}

project.plugins.withId("com.android.library") {
// TODO replace with https://issuetracker.google.com/issues/72050365 once released.
project.android.libraryVariants.all {
it.generateBuildConfigProvider.configure { it.enabled = false }
}
}
}
4 changes: 0 additions & 4 deletions emoji-google-compat/build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
apply plugin: 'com.android.library'

afterEvaluate {
generateReleaseBuildConfig.enabled = false
}

android {
compileSdkVersion rootProject.ext.compileSdkVersion as int

Expand Down
2 changes: 1 addition & 1 deletion emoji-google-compat/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
POM_NAME=Emoji
POM_ARTIFACT_ID=emoji-google-compat
POM_PACKAGING=aar
POM_PACKAGING=aar
4 changes: 0 additions & 4 deletions emoji-google/build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
apply plugin: 'com.android.library'

afterEvaluate {
generateReleaseBuildConfig.enabled = false
}

android {
compileSdkVersion rootProject.ext.compileSdkVersion as int

Expand Down
2 changes: 1 addition & 1 deletion emoji-google/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
POM_NAME=Emoji
POM_ARTIFACT_ID=emoji-google
POM_PACKAGING=aar
POM_PACKAGING=aar
4 changes: 0 additions & 4 deletions emoji-ios/build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
apply plugin: 'com.android.library'

afterEvaluate {
generateReleaseBuildConfig.enabled = false
}

android {
compileSdkVersion rootProject.ext.compileSdkVersion as int

Expand Down
2 changes: 1 addition & 1 deletion emoji-ios/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
POM_NAME=Emoji
POM_ARTIFACT_ID=emoji-ios
POM_PACKAGING=aar
POM_PACKAGING=aar
4 changes: 0 additions & 4 deletions emoji-kotlin/build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

afterEvaluate {
generateReleaseBuildConfig.enabled = false
}

android {
compileSdkVersion rootProject.ext.compileSdkVersion as int

Expand Down
2 changes: 1 addition & 1 deletion emoji-kotlin/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
POM_NAME=Emoji
POM_ARTIFACT_ID=emoji-kotlin
POM_PACKAGING=aar
POM_PACKAGING=aar
23 changes: 23 additions & 0 deletions emoji-material/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion rootProject.ext.compileSdkVersion as int

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}

defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion as int
}

resourcePrefix 'emoji_'
}

dependencies {
api project(':emoji')
api deps.support.material
}

apply plugin: "com.vanniktech.maven.publish"
3 changes: 3 additions & 0 deletions emoji-material/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
POM_NAME=Emoji
POM_ARTIFACT_ID=emoji-material
POM_PACKAGING=aar
1 change: 1 addition & 0 deletions emoji-material/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest package="com.vanniktech.emoji.material" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.vanniktech.emoji.material;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.text.SpannableStringBuilder;
import android.util.AttributeSet;
import androidx.annotation.CallSuper;
import androidx.annotation.DimenRes;
import androidx.annotation.Px;
import com.google.android.material.button.MaterialButton;
import com.vanniktech.emoji.EmojiManager;

@SuppressWarnings("CPD-START") public class EmojiMaterialButton extends MaterialButton {
private float emojiSize;

public EmojiMaterialButton(final Context context) {
this(context, null);
}

public EmojiMaterialButton(final Context context, final AttributeSet attrs) {
super(context, attrs);

if (!isInEditMode()) {
EmojiManager.getInstance().verifyInstalled();
}

final Paint.FontMetrics fontMetrics = getPaint().getFontMetrics();
final float defaultEmojiSize = fontMetrics.descent - fontMetrics.ascent;

if (attrs == null) {
emojiSize = defaultEmojiSize;
} else {
final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.EmojiMaterialButton);

try {
emojiSize = a.getDimension(R.styleable.EmojiMaterialButton_emojiSize, defaultEmojiSize);
} finally {
a.recycle();
}
}

setText(getText());
}

@Override @CallSuper public void setText(final CharSequence rawText, final BufferType type) {
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);
}

/** 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.vanniktech.emoji.material;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.text.SpannableStringBuilder;
import android.util.AttributeSet;
import androidx.annotation.CallSuper;
import androidx.annotation.DimenRes;
import androidx.annotation.Px;
import com.google.android.material.checkbox.MaterialCheckBox;
import com.vanniktech.emoji.EmojiManager;

@SuppressWarnings("CPD-START") public class EmojiMaterialCheckBox extends MaterialCheckBox {
private float emojiSize;

public EmojiMaterialCheckBox(final Context context) {
this(context, null);
}

public EmojiMaterialCheckBox(final Context context, final AttributeSet attrs) {
super(context, attrs);

if (!isInEditMode()) {
EmojiManager.getInstance().verifyInstalled();
}

final Paint.FontMetrics fontMetrics = getPaint().getFontMetrics();
final float defaultEmojiSize = fontMetrics.descent - fontMetrics.ascent;

if (attrs == null) {
emojiSize = defaultEmojiSize;
} else {
final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.EmojiMaterialCheckBox);

try {
emojiSize = a.getDimension(R.styleable.EmojiMaterialCheckBox_emojiSize, defaultEmojiSize);
} finally {
a.recycle();
}
}

setText(getText());
}

@Override @CallSuper public void setText(final CharSequence rawText, final BufferType type) {
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);
}

/** 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);
}
}
Loading

0 comments on commit 3b132a3

Please sign in to comment.