Search in sources :

Example 1 with HashtagSearchCell

use of org.telegram.ui.Cells.HashtagSearchCell in project Telegram-FOSS by Telegram-FOSS-Team.

the class DialogsSearchAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    switch(holder.getItemViewType()) {
        case 0:
            {
                ProfileSearchCell cell = (ProfileSearchCell) holder.itemView;
                long oldDialogId = cell.getDialogId();
                TLRPC.User user = null;
                TLRPC.Chat chat = null;
                TLRPC.EncryptedChat encryptedChat = null;
                CharSequence username = null;
                CharSequence name = null;
                boolean isRecent = false;
                String un = null;
                Object obj = getItem(position);
                if (obj instanceof TLRPC.User) {
                    user = (TLRPC.User) obj;
                    un = user.username;
                } else if (obj instanceof TLRPC.Chat) {
                    chat = MessagesController.getInstance(currentAccount).getChat(((TLRPC.Chat) obj).id);
                    if (chat == null) {
                        chat = (TLRPC.Chat) obj;
                    }
                    un = chat.username;
                } else if (obj instanceof TLRPC.EncryptedChat) {
                    encryptedChat = MessagesController.getInstance(currentAccount).getEncryptedChat(((TLRPC.EncryptedChat) obj).id);
                    user = MessagesController.getInstance(currentAccount).getUser(encryptedChat.user_id);
                }
                if (isRecentSearchDisplayed()) {
                    isRecent = true;
                    cell.useSeparator = position != getItemCount() - 1;
                } else {
                    ArrayList<TLObject> globalSearch = searchAdapterHelper.getGlobalSearch();
                    ArrayList<Object> phoneSearch = searchAdapterHelper.getPhoneSearch();
                    int localCount = searchResult.size();
                    int localServerCount = searchAdapterHelper.getLocalServerSearch().size();
                    int phoneCount = phoneSearch.size();
                    if (phoneCount > 3 && phoneCollapsed) {
                        phoneCount = 3;
                    }
                    int phoneCount2 = phoneCount;
                    if (phoneCount > 0 && phoneSearch.get(phoneCount - 1) instanceof String) {
                        phoneCount2 -= 2;
                    }
                    int globalCount = globalSearch.isEmpty() ? 0 : globalSearch.size() + 1;
                    if (globalCount > 4 && globalSearchCollapsed) {
                        globalCount = 4;
                    }
                    cell.useSeparator = (position != getItemCount() - 1 && position != localCount + phoneCount2 + localServerCount - 1 && position != localCount + globalCount + phoneCount + localServerCount - 1);
                    if (position < searchResult.size()) {
                        name = searchResultNames.get(position);
                        if (name != null && user != null && user.username != null && user.username.length() > 0) {
                            if (name.toString().startsWith("@" + user.username)) {
                                username = name;
                                name = null;
                            }
                        }
                    } else {
                        String foundUserName = searchAdapterHelper.getLastFoundUsername();
                        if (!TextUtils.isEmpty(foundUserName)) {
                            String nameSearch = null;
                            int index;
                            if (user != null) {
                                nameSearch = ContactsController.formatName(user.first_name, user.last_name);
                            } else if (chat != null) {
                                nameSearch = chat.title;
                            }
                            if (nameSearch != null && (index = AndroidUtilities.indexOfIgnoreCase(nameSearch, foundUserName)) != -1) {
                                SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(nameSearch);
                                spannableStringBuilder.setSpan(new ForegroundColorSpanThemable(Theme.key_windowBackgroundWhiteBlueText4), index, index + foundUserName.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                                name = spannableStringBuilder;
                            } else if (un != null) {
                                if (foundUserName.startsWith("@")) {
                                    foundUserName = foundUserName.substring(1);
                                }
                                try {
                                    SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
                                    spannableStringBuilder.append("@");
                                    spannableStringBuilder.append(un);
                                    if ((index = AndroidUtilities.indexOfIgnoreCase(un, foundUserName)) != -1) {
                                        int len = foundUserName.length();
                                        if (index == 0) {
                                            len++;
                                        } else {
                                            index++;
                                        }
                                        spannableStringBuilder.setSpan(new ForegroundColorSpanThemable(Theme.key_windowBackgroundWhiteBlueText4), index, index + len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                                    }
                                    username = spannableStringBuilder;
                                } catch (Exception e) {
                                    username = un;
                                    FileLog.e(e);
                                }
                            }
                        }
                    }
                    cell.setChecked(false, false);
                }
                boolean savedMessages = false;
                if (user != null && user.id == selfUserId) {
                    name = LocaleController.getString("SavedMessages", R.string.SavedMessages);
                    username = null;
                    savedMessages = true;
                }
                if (chat != null && chat.participants_count != 0) {
                    String membersString;
                    if (ChatObject.isChannel(chat) && !chat.megagroup) {
                        membersString = LocaleController.formatPluralString("Subscribers", chat.participants_count);
                    } else {
                        membersString = LocaleController.formatPluralString("Members", chat.participants_count);
                    }
                    if (username instanceof SpannableStringBuilder) {
                        ((SpannableStringBuilder) username).append(", ").append(membersString);
                    } else if (!TextUtils.isEmpty(username)) {
                        username = TextUtils.concat(username, ", ", membersString);
                    } else {
                        username = membersString;
                    }
                }
                cell.setData(user != null ? user : chat, encryptedChat, name, username, isRecent, savedMessages);
                cell.setChecked(delegate.isSelected(cell.getDialogId()), oldDialogId == cell.getDialogId());
                break;
            }
        case 1:
            {
                GraySectionCell cell = (GraySectionCell) holder.itemView;
                if (isRecentSearchDisplayed()) {
                    int offset = (!MediaDataController.getInstance(currentAccount).hints.isEmpty() ? 1 : 0);
                    if (position < offset) {
                        cell.setText(LocaleController.getString("ChatHints", R.string.ChatHints));
                    } else {
                        cell.setText(LocaleController.getString("Recent", R.string.Recent), LocaleController.getString("ClearButton", R.string.ClearButton), v -> {
                            if (delegate != null) {
                                delegate.needClearList();
                            }
                        });
                    }
                } else if (!searchResultHashtags.isEmpty()) {
                    cell.setText(LocaleController.getString("Hashtags", R.string.Hashtags), LocaleController.getString("ClearButton", R.string.ClearButton), v -> {
                        if (delegate != null) {
                            delegate.needClearList();
                        }
                    });
                } else {
                    ArrayList<TLObject> globalSearch = searchAdapterHelper.getGlobalSearch();
                    int localCount = searchResult.size();
                    int localServerCount = searchAdapterHelper.getLocalServerSearch().size();
                    int phoneCount = searchAdapterHelper.getPhoneSearch().size();
                    if (phoneCount > 3 && phoneCollapsed) {
                        phoneCount = 3;
                    }
                    int globalCount = globalSearch.isEmpty() ? 0 : globalSearch.size() + 1;
                    if (globalCount > 4 && globalSearchCollapsed) {
                        globalCount = 4;
                    }
                    int messagesCount = searchResultMessages.isEmpty() ? 0 : searchResultMessages.size() + 1;
                    position -= localCount + localServerCount;
                    String title;
                    boolean showMore = false;
                    Runnable onClick = null;
                    if (position >= 0 && position < phoneCount) {
                        title = LocaleController.getString("PhoneNumberSearch", R.string.PhoneNumberSearch);
                        if (searchAdapterHelper.getPhoneSearch().size() > 3) {
                            showMore = phoneCollapsed;
                            onClick = () -> {
                                phoneCollapsed = !phoneCollapsed;
                                notifyDataSetChanged();
                            };
                        }
                    } else {
                        position -= phoneCount;
                        if (position >= 0 && position < globalCount) {
                            title = LocaleController.getString("GlobalSearch", R.string.GlobalSearch);
                            if (searchAdapterHelper.getGlobalSearch().size() > 3) {
                                showMore = globalSearchCollapsed;
                                onClick = () -> {
                                    globalSearchCollapsed = !globalSearchCollapsed;
                                    notifyDataSetChanged();
                                };
                            }
                        } else {
                            title = LocaleController.getString("SearchMessages", R.string.SearchMessages);
                        }
                    }
                    if (onClick == null) {
                        cell.setText(title);
                    } else {
                        final Runnable finalOnClick = onClick;
                        cell.setText(title, showMore ? LocaleController.getString("ShowMore", R.string.ShowMore) : LocaleController.getString("ShowLess", R.string.ShowLess), e -> finalOnClick.run());
                    }
                }
                break;
            }
        case 2:
            {
                DialogCell cell = (DialogCell) holder.itemView;
                cell.useSeparator = (position != getItemCount() - 1);
                MessageObject messageObject = (MessageObject) getItem(position);
                cell.setDialog(messageObject.getDialogId(), messageObject, messageObject.messageOwner.date, false);
                break;
            }
        case 4:
            {
                HashtagSearchCell cell = (HashtagSearchCell) holder.itemView;
                cell.setText(searchResultHashtags.get(position - 1));
                cell.setNeedDivider(position != searchResultHashtags.size());
                break;
            }
        case 5:
            {
                RecyclerListView recyclerListView = (RecyclerListView) holder.itemView;
                ((CategoryAdapterRecycler) recyclerListView.getAdapter()).setIndex(position / 2);
                break;
            }
        case 6:
            {
                String str = (String) getItem(position);
                TextCell cell = (TextCell) holder.itemView;
                cell.setColors(null, Theme.key_windowBackgroundWhiteBlueText2);
                cell.setText(LocaleController.formatString("AddContactByPhone", R.string.AddContactByPhone, PhoneFormat.getInstance().format("+" + str)), false);
                break;
            }
    }
}
Also used : Context(android.content.Context) ForegroundColorSpanThemable(org.telegram.ui.Components.ForegroundColorSpanThemable) Spanned(android.text.Spanned) Theme(org.telegram.ui.ActionBar.Theme) AndroidUtilities(org.telegram.messenger.AndroidUtilities) LocaleController(org.telegram.messenger.LocaleController) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SpannableStringBuilder(android.text.SpannableStringBuilder) TextCell(org.telegram.ui.Cells.TextCell) MotionEvent(android.view.MotionEvent) TLRPC(org.telegram.tgnet.TLRPC) PhoneFormat(org.telegram.PhoneFormat.PhoneFormat) TLObject(org.telegram.tgnet.TLObject) HintDialogCell(org.telegram.ui.Cells.HintDialogCell) View(android.view.View) MessageObject(org.telegram.messenger.MessageObject) MediaDataController(org.telegram.messenger.MediaDataController) RecyclerView(androidx.recyclerview.widget.RecyclerView) DialogCell(org.telegram.ui.Cells.DialogCell) ProfileSearchCell(org.telegram.ui.Cells.ProfileSearchCell) Utilities(org.telegram.messenger.Utilities) LongSparseArray(androidx.collection.LongSparseArray) DialogObject(org.telegram.messenger.DialogObject) FilteredSearchView(org.telegram.ui.FilteredSearchView) R(org.telegram.messenger.R) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TextUtils(android.text.TextUtils) SQLitePreparedStatement(org.telegram.SQLite.SQLitePreparedStatement) FileLog(org.telegram.messenger.FileLog) ConnectionsManager(org.telegram.tgnet.ConnectionsManager) ViewGroup(android.view.ViewGroup) FlickerLoadingView(org.telegram.ui.Components.FlickerLoadingView) MessagesController(org.telegram.messenger.MessagesController) UserConfig(org.telegram.messenger.UserConfig) UserObject(org.telegram.messenger.UserObject) ContactsController(org.telegram.messenger.ContactsController) MessagesStorage(org.telegram.messenger.MessagesStorage) GraySectionCell(org.telegram.ui.Cells.GraySectionCell) HashtagSearchCell(org.telegram.ui.Cells.HashtagSearchCell) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) SQLiteCursor(org.telegram.SQLite.SQLiteCursor) ChatObject(org.telegram.messenger.ChatObject) Collections(java.util.Collections) RecyclerListView(org.telegram.ui.Components.RecyclerListView) GraySectionCell(org.telegram.ui.Cells.GraySectionCell) ArrayList(java.util.ArrayList) RecyclerListView(org.telegram.ui.Components.RecyclerListView) TLRPC(org.telegram.tgnet.TLRPC) TextCell(org.telegram.ui.Cells.TextCell) HintDialogCell(org.telegram.ui.Cells.HintDialogCell) DialogCell(org.telegram.ui.Cells.DialogCell) ForegroundColorSpanThemable(org.telegram.ui.Components.ForegroundColorSpanThemable) HashtagSearchCell(org.telegram.ui.Cells.HashtagSearchCell) ProfileSearchCell(org.telegram.ui.Cells.ProfileSearchCell) TLObject(org.telegram.tgnet.TLObject) TLObject(org.telegram.tgnet.TLObject) MessageObject(org.telegram.messenger.MessageObject) DialogObject(org.telegram.messenger.DialogObject) UserObject(org.telegram.messenger.UserObject) ChatObject(org.telegram.messenger.ChatObject) MessageObject(org.telegram.messenger.MessageObject) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 2 with HashtagSearchCell

use of org.telegram.ui.Cells.HashtagSearchCell in project Telegram-FOSS by Telegram-FOSS-Team.

the class DialogsSearchAdapter method onCreateViewHolder.

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;
    switch(viewType) {
        case 0:
            view = new ProfileSearchCell(mContext);
            break;
        case 1:
            view = new GraySectionCell(mContext);
            break;
        case 2:
            view = new DialogCell(null, mContext, false, true);
            break;
        case 3:
            FlickerLoadingView flickerLoadingView = new FlickerLoadingView(mContext);
            flickerLoadingView.setViewType(FlickerLoadingView.DIALOG_TYPE);
            flickerLoadingView.setIsSingleCell(true);
            view = flickerLoadingView;
            break;
        case 4:
            view = new HashtagSearchCell(mContext);
            break;
        case 5:
            RecyclerListView horizontalListView = new RecyclerListView(mContext) {

                @Override
                public boolean onInterceptTouchEvent(MotionEvent e) {
                    if (getParent() != null && getParent().getParent() != null) {
                        getParent().getParent().requestDisallowInterceptTouchEvent(canScrollHorizontally(-1) || canScrollHorizontally(1));
                    }
                    return super.onInterceptTouchEvent(e);
                }
            };
            horizontalListView.setSelectorRadius(AndroidUtilities.dp(4));
            horizontalListView.setSelectorDrawableColor(Theme.getColor(Theme.key_listSelector));
            horizontalListView.setTag(9);
            horizontalListView.setItemAnimator(null);
            horizontalListView.setLayoutAnimation(null);
            LinearLayoutManager layoutManager = new LinearLayoutManager(mContext) {

                @Override
                public boolean supportsPredictiveItemAnimations() {
                    return false;
                }
            };
            layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            horizontalListView.setLayoutManager(layoutManager);
            // horizontalListView.setDisallowInterceptTouchEvents(true);
            horizontalListView.setAdapter(new CategoryAdapterRecycler(mContext, currentAccount, false));
            horizontalListView.setOnItemClickListener((view1, position) -> {
                if (delegate != null) {
                    delegate.didPressedOnSubDialog((Long) view1.getTag());
                }
            });
            horizontalListView.setOnItemLongClickListener((view12, position) -> {
                if (delegate != null) {
                    delegate.needRemoveHint((Long) view12.getTag());
                }
                return true;
            });
            view = horizontalListView;
            innerListView = horizontalListView;
            break;
        case 6:
        default:
            view = new TextCell(mContext, 16, false);
            break;
    }
    if (viewType == 5) {
        view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, AndroidUtilities.dp(86)));
    } else {
        view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
    }
    return new RecyclerListView.Holder(view);
}
Also used : GraySectionCell(org.telegram.ui.Cells.GraySectionCell) RecyclerListView(org.telegram.ui.Components.RecyclerListView) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) FilteredSearchView(org.telegram.ui.FilteredSearchView) FlickerLoadingView(org.telegram.ui.Components.FlickerLoadingView) RecyclerListView(org.telegram.ui.Components.RecyclerListView) HashtagSearchCell(org.telegram.ui.Cells.HashtagSearchCell) MotionEvent(android.view.MotionEvent) TextCell(org.telegram.ui.Cells.TextCell) HintDialogCell(org.telegram.ui.Cells.HintDialogCell) DialogCell(org.telegram.ui.Cells.DialogCell) ProfileSearchCell(org.telegram.ui.Cells.ProfileSearchCell) RecyclerView(androidx.recyclerview.widget.RecyclerView) FlickerLoadingView(org.telegram.ui.Components.FlickerLoadingView)

Aggregations

MotionEvent (android.view.MotionEvent)2 View (android.view.View)2 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)2 RecyclerView (androidx.recyclerview.widget.RecyclerView)2 DialogCell (org.telegram.ui.Cells.DialogCell)2 GraySectionCell (org.telegram.ui.Cells.GraySectionCell)2 HashtagSearchCell (org.telegram.ui.Cells.HashtagSearchCell)2 HintDialogCell (org.telegram.ui.Cells.HintDialogCell)2 ProfileSearchCell (org.telegram.ui.Cells.ProfileSearchCell)2 TextCell (org.telegram.ui.Cells.TextCell)2 FlickerLoadingView (org.telegram.ui.Components.FlickerLoadingView)2 RecyclerListView (org.telegram.ui.Components.RecyclerListView)2 FilteredSearchView (org.telegram.ui.FilteredSearchView)2 Context (android.content.Context)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 Spanned (android.text.Spanned)1 TextUtils (android.text.TextUtils)1 ViewGroup (android.view.ViewGroup)1 LongSparseArray (androidx.collection.LongSparseArray)1 ArrayList (java.util.ArrayList)1