emoji_keyboard: first keyboard, supports only unicode emojis

main
Alibek Omarov 5 years ago
parent cbcb3838dd
commit 06eda38140
  1. 94
      app/src/main/java/com/keylesspalace/tusky/adapter/UnicodeEmojiAdapter.java
  2. 89
      app/src/main/java/com/keylesspalace/tusky/view/EmojiKeyboard.java
  3. 8
      app/src/main/res/layout/dialog_emoji_keyboard.xml
  4. 20
      app/src/main/res/layout/item_emoji_keyboard_emoji.xml
  5. 8
      app/src/main/res/layout/item_emoji_keyboard_page.xml
  6. 24
      app/src/main/res/layout/item_emoji_picker.xml

@ -0,0 +1,94 @@
package com.keylesspalace.tusky.adapter;
import android.view.*;
import android.util.*;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.google.android.flexbox.FlexboxLayoutManager;
import androidx.viewpager2.widget.ViewPager2;
import androidx.recyclerview.widget.*;
import androidx.emoji.widget.EmojiAppCompatButton;
import androidx.emoji.text.EmojiCompat;
import com.keylesspalace.tusky.R;
import com.keylesspalace.tusky.view.EmojiKeyboard;
import com.keylesspalace.tusky.util.Emojis;
public class UnicodeEmojiAdapter
extends RecyclerView.Adapter<SingleViewHolder>
implements TabLayoutMediator.TabConfigurationStrategy {
private String id;
private EmojiKeyboard.OnEmojiSelectedListener listener;
private final static float BUTTON_WIDTH_DP = 65.0f; // empirically found value :(
public UnicodeEmojiAdapter(String id, EmojiKeyboard.OnEmojiSelectedListener listener) {
super();
this.id = id;
this.listener = listener;
}
@Override
public void onConfigureTab(TabLayout.Tab tab, int position) {
tab.setText(Emojis.EMOJIS[position][0]);
}
@Override
public int getItemCount() {
return Emojis.EMOJIS.length;
}
@Override
public void onBindViewHolder(SingleViewHolder holder, int position) {
((RecyclerView)holder.itemView).setAdapter(new UnicodeEmojiPageAdapter(Emojis.EMOJIS[position], id, listener));
}
@Override
public SingleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_emoji_keyboard_page, parent, false);
SingleViewHolder holder = new SingleViewHolder(view);
DisplayMetrics dm = parent.getContext().getResources().getDisplayMetrics();
float wdp = dm.widthPixels / dm.density;
int rows = (int) (wdp / BUTTON_WIDTH_DP + 0.5);
((RecyclerView)view).setLayoutManager(new GridLayoutManager(view.getContext(), rows));
return holder;
}
private class UnicodeEmojiPageAdapter extends RecyclerView.Adapter<SingleViewHolder> {
private final String[] emojis;
private final String id;
private final EmojiKeyboard.OnEmojiSelectedListener listener;
public UnicodeEmojiPageAdapter(String[] emojis, String id, EmojiKeyboard.OnEmojiSelectedListener listener) {
this.emojis = emojis;
this.id = id;
this.listener = listener;
}
@Override
public int getItemCount() {
return emojis.length;
}
@Override
public void onBindViewHolder(SingleViewHolder holder, int position) {
String emoji = emojis[position];
EmojiAppCompatButton btn = (EmojiAppCompatButton)holder.itemView;
btn.setText(emoji);
btn.setOnClickListener(v -> listener.onEmojiSelected(id, emoji));
}
@Override
public SingleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_emoji_keyboard_emoji, parent, false);
return new SingleViewHolder(view);
}
}
}

@ -0,0 +1,89 @@
package com.keylesspalace.tusky.view;
import android.view.*;
import android.content.*;
import android.util.*;
import android.widget.*;
import android.app.*;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import androidx.viewpager2.widget.ViewPager2;
import androidx.recyclerview.widget.RecyclerView;
import com.keylesspalace.tusky.R;
import com.keylesspalace.tusky.adapter.UnicodeEmojiAdapter;
public class EmojiKeyboard extends LinearLayout {
private TabLayout tabs;
private ViewPager2 pager;
private TabLayoutMediator currentMediator;
public EmojiKeyboard(Context context) {
super(context);
init(context);
}
public EmojiKeyboard(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public EmojiKeyboard(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
void init(Context context) {
inflate(context, R.layout.item_emoji_picker, this);
tabs = findViewById(R.id.picker_tabs);
pager = findViewById(R.id.picker_pager);
}
public static final int UNICODE_MODE = 0;
public static final int CUSTOM_MODE = 1;
public static final int STICKER_MODE = 2;
void setupKeyboard(String id, int mode, OnEmojiSelectedListener listener) {
RecyclerView.Adapter adapter;
switch(mode) {
case CUSTOM_MODE:
// UNDONE
//break;
case STICKER_MODE:
// UNDONE
//break;
default:
case UNICODE_MODE:
adapter = new UnicodeEmojiAdapter(id, listener);
break;
}
pager.setAdapter(adapter);
if(currentMediator != null)
currentMediator.detach();
currentMediator = new TabLayoutMediator(tabs, pager, (TabLayoutMediator.TabConfigurationStrategy)adapter);
currentMediator.attach();
}
public interface OnEmojiSelectedListener {
void onEmojiSelected(String id, String emoji);
}
public static void show(Context ctx, String id, int mode, OnEmojiSelectedListener listener) {
final Dialog dialog = new Dialog(ctx);
dialog.setTitle(null);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_emoji_keyboard);
EmojiKeyboard kbd = (EmojiKeyboard)dialog.findViewById(R.id.dialog_emoji_keyboard);
kbd.setupKeyboard(id, mode, (_id, _emoji) -> {
listener.onEmojiSelected(_id, _emoji);
dialog.dismiss();
});
dialog.show();
}
}

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<com.keylesspalace.tusky.view.EmojiKeyboard
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/dialog_emoji_keyboard"
android:layout_width="320dp"
android:layout_height="250dp" />

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- not a best way but I'm lazy to do proper button -->
<androidx.emoji.widget.EmojiAppCompatButton
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/emoji_keyboard_emoji"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="0dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:minWidth="0dp"
android:lines="1"
android:textAlignment="viewStart"
android:textColor="?android:attr/textColorPrimary"
android:textSize="?attr/status_text_medium"
app:backgroundTint="@color/emoji_reaction_button"
tools:text="A" />

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false" />

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="250dp"
android:orientation="vertical">
<com.google.android.material.tabs.TabLayout
android:id="@+id/picker_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMaxWidth="0dp"
app:tabMinWidth="0dp"
app:tabGravity="fill"
app:tabMode="scrollable" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/picker_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="?attr/windowBackgroundColor" />
</LinearLayout>
Loading…
Cancel
Save