Introduction

The ShortCutFormatter class extends the CometChatTextFormatter class to provide a mechanism for handling shortcuts within messages. This guide will walk you through the process of using ShortCutFormatter to implement shortcut extensions in your CometChat application.

Setup

  1. Create the ShortCutFormatter Class: Define the ShortCutFormatter class by extending the CometChatTextFormatter class.
  public class ShortCutFormatter extends CometChatTextFormatter {
      // Class implementation
  private HashMap<String, String> messageShortcuts;
  private List<SuggestionItem> shortcuts;
  }
  1. Constructor: Initialize the messageShortcuts map and shortcuts list in the constructor.
 public ShortCutFormatter() {
     super('!');
     messageShortcuts = new HashMap<>();
     prepareShortCuts();
     shortcuts = new ArrayList<>();
 }
  1. Prepare Shortcuts: Implement the prepareShortCuts() method to fetch shortcuts from the server using CometChat extension.
private void prepareShortCuts() {
  // Implementation to fetch shortcuts from server
  CometChat.callExtension("message-shortcuts", "GET", "/v1/fetch", null, new CometChat.CallbackListener<JSONObject>() {
      @Override
      public void onSuccess(JSONObject responseObject) {

          Iterator<String> keysItr;
          try {
              JSONObject shortcutObject = responseObject.getJSONObject("data").getJSONObject("shortcuts");
              keysItr = shortcutObject.keys();

              while (keysItr.hasNext()) {
                  String key = keysItr.next();
                  String value = shortcutObject.getString(key);
                  messageShortcuts.put(key, value);
              }
          } catch (JSONException e) {
              e.printStackTrace();
          }
      }

      @Override
      public void onError(CometChatException e) {
      }
  });
}
  1. Override Search Method: Override the search() method to search for shortcuts based on the entered query.
@Override
public void search(@NonNull Context context, String queryString) {
    // Implementation to search for shortcuts
      String query = getTrackingCharacter() + queryString;
    shortcuts.clear();
    if (messageShortcuts.containsKey(query)) {
        SuggestionItem suggestionItem = new SuggestionItem("", query + "  =>  " + messageShortcuts.get(query), null, null, messageShortcuts.get(query), null, null);
        suggestionItem.setHideLeadingIcon(true);
        shortcuts.add(suggestionItem);
    }
    setSuggestionItemList(shortcuts);
}
  1. Handle Scroll to Bottom: Override the onScrollToBottom() method if needed.
 @Override
 public void onScrollToBottom() {
     // Implementation if needed
 }

Usage

  1. Initialization: Initialize an instance of ShortCutFormatter in your application.
ShortCutFormatter shortCutFormatter = new ShortCutFormatter();
  1. Integration: Integrating the ShortCutFormatter into your CometChat application involves incorporating it within your project to handle message shortcuts. Use MessageComposer component, you can seamlessly integrate the ShortCutFormatter to manage shortcut functionalities within your application.
<com.cometchat.chatuikit.messagecomposer.CometChatMessageComposer
 android:id="@+id/composer"
 android:layout_width="match_parent"
 android:layout_height="match_parent" />
CometChatMessageComposer cometChatMessageComposer = findViewById(R.id.composer);

List<CometChatTextFormatter> cometChatTextFormatters = CometChatUIKit.getDataSource().getTextFormatters(this);
cometChatTextFormatters.add(new ShortCutFormatter());
cometChatMessageComposer.setTextFormatters(cometChatTextFormatters);

Example