Giphy / giphy-android-sdk

Home of the GIPHY SDK Android example app, along with Android SDK documentation, issue tracking, & release notes.
https://developers.giphy.com/
Mozilla Public License 2.0
91 stars 40 forks source link

Why is my search functionality not working for `Emoji` #180

Closed sambhav2358 closed 2 years ago

sambhav2358 commented 2 years ago

🐛 Bug Report

Why is my search functionality not working for Emoji

Expected behavior

I expect it to search the emojis correctly but it does not work. You can see the code given below

package com.sk.telegram.Fragments;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.giphy.sdk.core.models.Media;
import com.giphy.sdk.core.models.enums.MediaType;
import com.giphy.sdk.core.models.enums.RatingType;
import com.giphy.sdk.ui.Giphy;
import com.giphy.sdk.ui.pagination.GPHContent;
import com.giphy.sdk.ui.views.GPHGridCallback;
import com.sk.telegram.R;
import com.sk.telegram.databinding.FragmentPickGifBinding;

public class PickGifFragment extends Fragment {

    FragmentPickGifBinding b;
    String selectedMediaType = "gifs";

    public PickGifFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Giphy.INSTANCE.configure(getContext(),"7zlJiUQEB8stiG06LFARcmifhcgA8iFw");
        b = FragmentPickGifBinding.inflate(getLayoutInflater());
        b.gifsGridView.setContent(GPHContent.Companion.getTrendingGifs());
        b.gifsGridView.setCallback(new GPHGridCallback() {
            @Override
            public void contentDidUpdate(int i) {

            }

            @Override
            public void didSelectMedia(@NonNull Media media) {
                Toast.makeText(getContext(), "" + media.getBitlyUrl(), Toast.LENGTH_SHORT).show();
            }
        });
        setListeners();
        return b.getRoot();
    }

    private void setListeners() {
        b.gifs.setOnClickListener(v -> setSelectedMediaType("gifs"));
        b.stickers.setOnClickListener(v -> setSelectedMediaType("stick"));
        b.text.setOnClickListener(v -> setSelectedMediaType("text"));
        b.emoji.setOnClickListener(v -> setSelectedMediaType("emoji"));
        b.searchQuery.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void afterTextChanged(Editable s) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                setSelectedSearchMediaType(selectedMediaType,s.toString());
            }
        });
    }

    private void setSelectedMediaType(String type){
        selectedMediaType = type;
        switch (type){
            case "gifs":
                b.gifsGridView.setContent(GPHContent.Companion.getTrendingGifs());
                b.gifs.setBackgroundResource(R.drawable.gif_item_selected);
                b.stickers.setBackgroundResource(R.drawable.gif_item_normal);
                b.emoji.setBackgroundResource(R.drawable.gif_item_normal);
                b.text.setBackgroundResource(R.drawable.gif_item_normal);
                if (!b.searchQuery.getText().toString().trim().isEmpty()) setSelectedSearchMediaType(selectedMediaType,b.searchQuery.getText().toString().trim());
                break;
            case "stick":
                b.gifsGridView.setContent(GPHContent.Companion.getTrendingStickers());
                b.gifs.setBackgroundResource(R.drawable.gif_item_normal);
                b.stickers.setBackgroundResource(R.drawable.gif_item_selected);
                b.emoji.setBackgroundResource(R.drawable.gif_item_normal);
                b.text.setBackgroundResource(R.drawable.gif_item_normal);
                if (!b.searchQuery.getText().toString().trim().isEmpty()) setSelectedSearchMediaType(selectedMediaType,b.searchQuery.getText().toString().trim());
                break;
            case "text":
                b.gifsGridView.setContent(GPHContent.Companion.getTrendingText());
                b.gifs.setBackgroundResource(R.drawable.gif_item_normal);
                b.stickers.setBackgroundResource(R.drawable.gif_item_normal);
                b.emoji.setBackgroundResource(R.drawable.gif_item_normal);
                b.text.setBackgroundResource(R.drawable.gif_item_selected);
                if (!b.searchQuery.getText().toString().trim().isEmpty()) setSelectedSearchMediaType(selectedMediaType,b.searchQuery.getText().toString().trim());
                break;
            case "emoji":
                b.gifsGridView.setContent(GPHContent.Companion.getEmoji());
                b.gifs.setBackgroundResource(R.drawable.gif_item_normal);
                b.stickers.setBackgroundResource(R.drawable.gif_item_normal);
                b.emoji.setBackgroundResource(R.drawable.gif_item_selected);
                b.text.setBackgroundResource(R.drawable.gif_item_normal);
                if (!b.searchQuery.getText().toString().trim().isEmpty()) setSelectedSearchMediaType(selectedMediaType,b.searchQuery.getText().toString().trim());
                break;
        }
    }

    private void setSelectedSearchMediaType(String type,String query){
        selectedMediaType = type;
        switch (type){
            case "gifs":
                b.gifsGridView.setContent(GPHContent.Companion.searchQuery(query,MediaType.gif,RatingType.pg13));
                b.gifs.setBackgroundResource(R.drawable.gif_item_selected);
                b.stickers.setBackgroundResource(R.drawable.gif_item_normal);
                b.emoji.setBackgroundResource(R.drawable.gif_item_normal);
                b.text.setBackgroundResource(R.drawable.gif_item_normal);
                break;
            case "stick":
                b.gifsGridView.setContent(GPHContent.Companion.searchQuery(query,MediaType.sticker,RatingType.pg13));
                b.gifs.setBackgroundResource(R.drawable.gif_item_normal);
                b.stickers.setBackgroundResource(R.drawable.gif_item_selected);
                b.emoji.setBackgroundResource(R.drawable.gif_item_normal);
                b.text.setBackgroundResource(R.drawable.gif_item_normal);
                break;
            case "text":
                b.gifsGridView.setContent(GPHContent.Companion.searchQuery(query,MediaType.text,RatingType.pg13));
                b.gifs.setBackgroundResource(R.drawable.gif_item_normal);
                b.stickers.setBackgroundResource(R.drawable.gif_item_normal);
                b.emoji.setBackgroundResource(R.drawable.gif_item_normal);
                b.text.setBackgroundResource(R.drawable.gif_item_selected);
                break;
            case "emoji":
                b.gifsGridView.setContent(GPHContent.Companion.searchQuery(query,MediaType.emoji,RatingType.pg13));
                b.gifs.setBackgroundResource(R.drawable.gif_item_normal);
                b.stickers.setBackgroundResource(R.drawable.gif_item_normal);
                b.emoji.setBackgroundResource(R.drawable.gif_item_selected);
                b.text.setBackgroundResource(R.drawable.gif_item_normal);
                break;
        }
    }
}

And if you want, the xml is below


<?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="@dimen/_250sdp"
android:orientation="vertical"
tools:context=".Fragments.PickGifFragment">
    <EditText
        android:id="@+id/searchQuery"
        android:layout_width="match_parent"
        android:layout_marginHorizontal="@dimen/_10sdp"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded_gray_bg"
        android:hint="Search Giphy"
        android:textColor="@color/black"
        android:textColorHint="#AAAAAA"
        android:textSize="@dimen/_12sdp"
        tools:ignore="Autofill,HardcodedText,TextFields"
        android:paddingHorizontal="@dimen/_9sdp"
        android:paddingVertical="@dimen/_7sdp"
        android:layout_marginTop="@dimen/_10sdp"
        android:enabled="true"
        android:textCursorDrawable="@drawable/cursor"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginHorizontal="@dimen/_10sdp"
    android:layout_marginTop="@dimen/_10sdp">

    <TextView
        android:id="@+id/gifs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="@dimen/_5sdp"
        android:background="@drawable/gif_item_selected"
        android:gravity="center"
        android:paddingHorizontal="@dimen/_13sdp"
        android:paddingVertical="@dimen/_5sdp"
        android:text="GIFs"
        android:textColor="#525252"
        android:textSize="@dimen/_12sdp"
        android:textStyle="bold"
        tools:ignore="HardcodedText" />

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/stickers"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="@dimen/_5sdp"
        android:background="@drawable/gif_item_normal"
        android:gravity="center"
        android:paddingHorizontal="@dimen/_13sdp"
        android:paddingVertical="@dimen/_5sdp"
        android:text="Stickers"
        android:textColor="#525252"
        android:textSize="@dimen/_12sdp"
        android:textStyle="bold"
        tools:ignore="HardcodedText" />

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="@dimen/_5sdp"
        android:background="@drawable/gif_item_normal"
        android:gravity="center"
        android:paddingHorizontal="@dimen/_13sdp"
        android:paddingVertical="@dimen/_5sdp"
        android:text="Text"
        android:textColor="#525252"
        android:textSize="@dimen/_12sdp"
        android:textStyle="bold"
        tools:ignore="HardcodedText" />

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/emoji"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="@dimen/_5sdp"
        android:background="@drawable/gif_item_normal"
        android:gravity="center"
        android:paddingHorizontal="@dimen/_13sdp"
        android:paddingVertical="@dimen/_5sdp"
        android:text="Emoji"
        android:textColor="#525252"
        android:textSize="@dimen/_12sdp"
        android:textStyle="bold"
        tools:ignore="HardcodedText" />

</LinearLayout>

<com.giphy.sdk.ui.views.GiphyGridView
    android:id="@+id/gifsGridView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginTop="@dimen/_10sdp"
    android:layout_weight="1"
    app:gphCellPadding="12dp"
    app:gphDirection="vertical"
    app:gphShowCheckeredBackground="false"
    app:gphSpanCount="2" />



### Error GIF is below

https://user-images.githubusercontent.com/97180951/153765797-7252d10e-6d37-4362-9e68-abe2f94abca1.mp4

It works fine if I click on Emoji button without a search query. But when I click on Emoji with a a query, it shows the gifs. Not the emojis. Please help me. Thanks.

**PS. You can reproduce the problem by using the code above.**
ALexanderLonsky commented 2 years ago

@sambhav2358 You can search only through gifs/stickers/text/clips. GiphyDialogFragment hides emojis when it's the search mode.

sambhav2358 commented 2 years ago

Thanks.

sambhav2358 commented 2 years ago

Can you pleases your team to add feature for searching Emojis?

ALexanderLonsky commented 2 years ago

Unfortunately, we don't have this on the roadmap.

sambhav2358 commented 2 years ago

Ok sure. Thanks. But please add it asap.