Skip to content

Commit

Permalink
emojis vertical swipe
Browse files Browse the repository at this point in the history
  • Loading branch information
dslul committed Sep 10, 2020
1 parent 449749c commit 98b9a8f
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 72 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dependencies {
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.core:core-ktx:1.3.1'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.viewpager2:viewpager2:1.0.0'
}
repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.dslul.openboard.inputmethod.latin.utils.ResourceUtils;

import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

final class EmojiLayoutParams {
private static final int DEFAULT_KEYBOARD_ROWS = 4;
Expand Down Expand Up @@ -61,7 +62,7 @@ public EmojiLayoutParams(final Resources res) {
mEmojiKeyboardHeight = mEmojiPagerHeight - mEmojiPagerBottomMargin - 1;
}

public void setPagerProperties(final ViewPager vp) {
public void setPagerProperties(final ViewPager2 vp) {
final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) vp.getLayoutParams();
lp.height = mEmojiKeyboardHeight;
lp.bottomMargin = mEmojiPagerBottomMargin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
import org.dslul.openboard.inputmethod.keyboard.KeyboardView;
import org.dslul.openboard.inputmethod.latin.R;

import androidx.viewpager.widget.PagerAdapter;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

final class EmojiPalettesAdapter extends PagerAdapter {
final class EmojiPalettesAdapter extends RecyclerView.Adapter<EmojiPalettesAdapter.ViewHolder>{
private static final String TAG = EmojiPalettesAdapter.class.getSimpleName();
private static final boolean DEBUG_PAGER = false;

Expand Down Expand Up @@ -83,25 +84,7 @@ public void releaseCurrentKey(final boolean withKeyRegistering) {
currentKeyboardView.releaseCurrentKey(withKeyRegistering);
}

@Override
public int getCount() {
return mEmojiCategory.getTotalPageCountOfAllCategories();
}

@Override
public void setPrimaryItem(final ViewGroup container, final int position,
final Object object) {
if (mActivePosition == position) {
return;
}
final EmojiPageKeyboardView oldKeyboardView = mActiveKeyboardViews.get(mActivePosition);
if (oldKeyboardView != null) {
oldKeyboardView.releaseCurrentKey(false /* withKeyRegistering */);
oldKeyboardView.deallocateMemory();
}
mActivePosition = position;
}

/*
@Override
public Object instantiateItem(final ViewGroup container, final int position) {
if (DEBUG_PAGER) {
Expand All @@ -116,15 +99,29 @@ public Object instantiateItem(final ViewGroup container, final int position) {
final Keyboard keyboard =
mEmojiCategory.getKeyboardFromPagePosition(position);
final LayoutInflater inflater = LayoutInflater.from(container.getContext());
final EmojiPageKeyboardView keyboardView = (EmojiPageKeyboardView)inflater.inflate(
R.layout.emoji_keyboard_page, container, false /* attachToRoot */);
final EmojiPageKeyboardView keyboardView = (EmojiPageKeyboardView) inflater.inflate(
R.layout.emoji_keyboard_page, container, false);
keyboardView.setKeyboard(keyboard);
keyboardView.setOnKeyEventListener(mListener);
container.addView(keyboardView);
mActiveKeyboardViews.put(position, keyboardView);
return keyboardView;
}
@Override
public void setPrimaryItem(final ViewGroup container, final int position,
final Object object) {
if (mActivePosition == position) {
return;
}
final EmojiPageKeyboardView oldKeyboardView = mActiveKeyboardViews.get(mActivePosition);
if (oldKeyboardView != null) {
oldKeyboardView.releaseCurrentKey(false);
oldKeyboardView.deallocateMemory();
}
mActivePosition = position;
}
@Override
public boolean isViewFromObject(final View view, final Object object) {
return view == object;
Expand All @@ -147,4 +144,79 @@ public void destroyItem(final ViewGroup container, final int position,
Log.w(TAG, "Warning!!! Emoji palette may be leaking. " + object);
}
}
*/

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
/*if (DEBUG_PAGER) {
Log.d(TAG, "instantiate item: " + viewType);
}
final EmojiPageKeyboardView oldKeyboardView = mActiveKeyboardViews.get(viewType);
if (oldKeyboardView != null) {
oldKeyboardView.deallocateMemory();
// This may be redundant but wanted to be safer..
mActiveKeyboardViews.remove(viewType);
}
final Keyboard keyboard =
mEmojiCategory.getKeyboardFromPagePosition(parent.getVerticalScrollbarPosition());*/
final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
final EmojiPageKeyboardView keyboardView = (EmojiPageKeyboardView)inflater.inflate(
R.layout.emoji_keyboard_page, parent, false);
/*keyboardView.setKeyboard(keyboard);
keyboardView.setOnKeyEventListener(mListener);
parent.addView(keyboardView);
mActiveKeyboardViews.put(parent.getVerticalScrollbarPosition(), keyboardView);*/
return new ViewHolder(keyboardView);
}

@Override
public void onBindViewHolder(@NonNull EmojiPalettesAdapter.ViewHolder holder, int position) {
if (DEBUG_PAGER) {
Log.d(TAG, "instantiate item: " + position);
}
final EmojiPageKeyboardView oldKeyboardView = mActiveKeyboardViews.get(position);
if (oldKeyboardView != null) {
oldKeyboardView.deallocateMemory();
// This may be redundant but wanted to be safer..
mActiveKeyboardViews.remove(position);
}
final Keyboard keyboard =
mEmojiCategory.getKeyboardFromPagePosition(position);
holder.getKeyboardView().setKeyboard(keyboard);
holder.getKeyboardView().setOnKeyEventListener(mListener);
//parent.addView(keyboardView);
mActiveKeyboardViews.put(position, holder.getKeyboardView());

/*if (mActivePosition == position) {
return;
}
final EmojiPageKeyboardView oldKeyboardView = mActiveKeyboardViews.get(mActivePosition);
if (oldKeyboardView != null) {
oldKeyboardView.releaseCurrentKey(false);
oldKeyboardView.deallocateMemory();
}
mActivePosition = position;*/
}

@Override
public int getItemCount() {
return mEmojiCategory.getTotalPageCountOfAllCategories();
}


static class ViewHolder extends RecyclerView.ViewHolder {
private EmojiPageKeyboardView customView;

public ViewHolder(View v) {
super(v);
customView = (EmojiPageKeyboardView) v;
}

public EmojiPageKeyboardView getKeyboardView() {
return customView;
}

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.dslul.openboard.inputmethod.latin.utils.ResourceUtils;

import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import static org.dslul.openboard.inputmethod.latin.common.Constants.NOT_A_COORDINATE;

Expand All @@ -63,9 +64,9 @@
* </ol>
* Because of the above reasons, this class doesn't extend {@link KeyboardView}.
*/
public final class EmojiPalettesView extends LinearLayout implements OnTabChangeListener,
ViewPager.OnPageChangeListener, View.OnClickListener, View.OnTouchListener,
EmojiPageKeyboardView.OnKeyEventListener {
public final class EmojiPalettesView extends LinearLayout
implements OnTabChangeListener, View.OnClickListener, View.OnTouchListener,
EmojiPageKeyboardView.OnKeyEventListener{
private final int mFunctionalKeyBackgroundId;
private final int mSpacebarBackgroundId;
private final boolean mCategoryIndicatorEnabled;
Expand All @@ -83,7 +84,7 @@ public final class EmojiPalettesView extends LinearLayout implements OnTabChange
// TODO: Remove this workaround.
private View mSpacebarIcon;
private TabHost mTabHost;
private ViewPager mEmojiPager;
private ViewPager2 mEmojiPager;
private int mCurrentPagerPosition = 0;
private EmojiCategoryPageIndicatorView mEmojiCategoryPageIndicatorView;

Expand Down Expand Up @@ -183,8 +184,46 @@ protected void onFinishInflate() {

mEmojiPager = findViewById(R.id.emoji_keyboard_pager);
mEmojiPager.setAdapter(mEmojiPalettesAdapter);
mEmojiPager.setOnPageChangeListener(this);
mEmojiPager.setOffscreenPageLimit(0);

mEmojiPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {mEmojiPalettesAdapter.onPageScrolled();
final Pair<Integer, Integer> newPos =
mEmojiCategory.getCategoryIdAndPageIdFromPagePosition(position);
final int newCategoryId = newPos.first;
final int newCategorySize = mEmojiCategory.getCategoryPageSize(newCategoryId);
final int currentCategoryId = mEmojiCategory.getCurrentCategoryId();
final int currentCategoryPageId = mEmojiCategory.getCurrentCategoryPageId();
final int currentCategorySize = mEmojiCategory.getCurrentCategoryPageSize();
if (newCategoryId == currentCategoryId) {
mEmojiCategoryPageIndicatorView.setCategoryPageId(
newCategorySize, newPos.second, positionOffset);
} else if (newCategoryId > currentCategoryId) {
mEmojiCategoryPageIndicatorView.setCategoryPageId(
currentCategorySize, currentCategoryPageId, positionOffset);
} else if (newCategoryId < currentCategoryId) {
mEmojiCategoryPageIndicatorView.setCategoryPageId(
currentCategorySize, currentCategoryPageId, positionOffset - 1);
}
}

@Override
public void onPageSelected(int position) {
final Pair<Integer, Integer> newPos =
mEmojiCategory.getCategoryIdAndPageIdFromPagePosition(position);
setCurrentCategoryId(newPos.first /* categoryId */, false /* force */);
mEmojiCategory.setCurrentCategoryPageId(newPos.second /* categoryPageId */);
updateEmojiCategoryPageIdView();
mCurrentPagerPosition = position;
}

@Override
public void onPageScrollStateChanged(int state) {
// Ignore this message. Only want the actual page selected.
}
});

mEmojiPager.setOffscreenPageLimit(ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT);
mEmojiPager.setPersistentDrawingCache(PERSISTENT_NO_CACHE);
mEmojiLayoutParams.setPagerProperties(mEmojiPager);

Expand Down Expand Up @@ -243,44 +282,6 @@ public void onTabChanged(final String tabId) {
updateEmojiCategoryPageIdView();
}

@Override
public void onPageSelected(final int position) {
final Pair<Integer, Integer> newPos =
mEmojiCategory.getCategoryIdAndPageIdFromPagePosition(position);
setCurrentCategoryId(newPos.first /* categoryId */, false /* force */);
mEmojiCategory.setCurrentCategoryPageId(newPos.second /* categoryPageId */);
updateEmojiCategoryPageIdView();
mCurrentPagerPosition = position;
}

@Override
public void onPageScrollStateChanged(final int state) {
// Ignore this message. Only want the actual page selected.
}

@Override
public void onPageScrolled(final int position, final float positionOffset,
final int positionOffsetPixels) {
mEmojiPalettesAdapter.onPageScrolled();
final Pair<Integer, Integer> newPos =
mEmojiCategory.getCategoryIdAndPageIdFromPagePosition(position);
final int newCategoryId = newPos.first;
final int newCategorySize = mEmojiCategory.getCategoryPageSize(newCategoryId);
final int currentCategoryId = mEmojiCategory.getCurrentCategoryId();
final int currentCategoryPageId = mEmojiCategory.getCurrentCategoryPageId();
final int currentCategorySize = mEmojiCategory.getCurrentCategoryPageSize();
if (newCategoryId == currentCategoryId) {
mEmojiCategoryPageIndicatorView.setCategoryPageId(
newCategorySize, newPos.second, positionOffset);
} else if (newCategoryId > currentCategoryId) {
mEmojiCategoryPageIndicatorView.setCategoryPageId(
currentCategorySize, currentCategoryPageId, positionOffset);
} else if (newCategoryId < currentCategoryId) {
mEmojiCategoryPageIndicatorView.setCategoryPageId(
currentCategorySize, currentCategoryPageId, positionOffset - 1);
}
}

/**
* Called from {@link EmojiPageKeyboardView} through {@link android.view.View.OnTouchListener}
* interface to handle touch events from View-based elements such as the space bar.
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/emoji_keyboard_page.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/emoji_keyboard_page"
android:layoutDirection="ltr"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_width="match_parent"
android:layout_height="match_parent" />
5 changes: 3 additions & 2 deletions app/src/main/res/layout/emoji_palettes_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@
</FrameLayout>
</TabHost>
</LinearLayout>
<androidx.viewpager.widget.ViewPager
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/emoji_keyboard_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
android:orientation="vertical" />
<org.dslul.openboard.inputmethod.keyboard.emoji.EmojiCategoryPageIndicatorView
android:id="@+id/emoji_category_page_id_view"
android:layout_width="match_parent"
Expand Down

0 comments on commit 98b9a8f

Please sign in to comment.