diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/MediaDataController.java b/TMessagesProj/src/main/java/org/telegram/messenger/MediaDataController.java index 7f79c73c7d..d7e4bc1a4d 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/MediaDataController.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/MediaDataController.java @@ -3347,7 +3347,11 @@ public boolean isMessageFound(int messageId, boolean mergeDialog) { } public void searchMessagesInChat(String query, long dialogId, long mergeDialogId, int guid, int direction, int replyMessageId, TLRPC.User user, TLRPC.Chat chat) { - searchMessagesInChat(query, dialogId, mergeDialogId, guid, direction, replyMessageId, false, user, chat, true); + searchMessagesInChat(query, dialogId, mergeDialogId, guid, direction, replyMessageId, user, chat, false); + } + + public void searchMessagesInChat(String query, long dialogId, long mergeDialogId, int guid, int direction, int replyMessageId, TLRPC.User user, TLRPC.Chat chat, boolean firstSearch) { + searchMessagesInChat(query, dialogId, mergeDialogId, guid, direction, replyMessageId, false, user, chat, true, firstSearch); } public void jumpToSearchedMessage(int guid, int index) { @@ -3359,8 +3363,20 @@ public void jumpToSearchedMessage(int guid, int index) { getNotificationCenter().postNotificationName(NotificationCenter.chatSearchResultsAvailable, guid, messageObject.getId(), getMask(), messageObject.getDialogId(), lastReturnedNum, messagesSearchCount[0] + messagesSearchCount[1], true); } + public void setCurrentMessage(int index) { + lastReturnedNum = index; + } + + public void setCurrentMaxMessage() { + lastReturnedNum = searchResultMessages.size() - 1; + } + public void loadMoreSearchMessages() { - if (loadingMoreSearchMessages || messagesSearchEndReached[0] && lastMergeDialogId == 0 && messagesSearchEndReached[1]) { + loadMoreSearchMessages(false); + } + + public void loadMoreSearchMessages(boolean force) { + if (!force && (loadingMoreSearchMessages || messagesSearchEndReached[0] && lastMergeDialogId == 0 && messagesSearchEndReached[1])) { return; } int temp = searchResultMessages.size(); @@ -3371,6 +3387,10 @@ public void loadMoreSearchMessages() { } private void searchMessagesInChat(String query, long dialogId, long mergeDialogId, int guid, int direction, int replyMessageId, boolean internal, TLRPC.User user, TLRPC.Chat chat, boolean jumpToMessage) { + searchMessagesInChat(query, dialogId, mergeDialogId, guid, direction, replyMessageId, internal, user, chat, jumpToMessage, false); + } + + private void searchMessagesInChat(String query, long dialogId, long mergeDialogId, int guid, int direction, int replyMessageId, boolean internal, TLRPC.User user, TLRPC.Chat chat, boolean jumpToMessage, boolean firstSearch) { int max_id = 0; long queryWithDialog = dialogId; boolean firstQuery = !internal; @@ -3390,7 +3410,7 @@ private void searchMessagesInChat(String query, long dialogId, long mergeDialogI lastReturnedNum++; if (lastReturnedNum < searchResultMessages.size()) { MessageObject messageObject = searchResultMessages.get(lastReturnedNum); - getNotificationCenter().postNotificationName(NotificationCenter.chatSearchResultsAvailable, guid, messageObject.getId(), getMask(), messageObject.getDialogId(), lastReturnedNum, messagesSearchCount[0] + messagesSearchCount[1], jumpToMessage); + getNotificationCenter().postNotificationName(NotificationCenter.chatSearchResultsAvailable, guid, messageObject.getId(), getMask(), messageObject.getDialogId(), lastReturnedNum, messagesSearchCount[0] + messagesSearchCount[1], jumpToMessage, firstSearch); return; } else { if (messagesSearchEndReached[0] && mergeDialogId == 0 && messagesSearchEndReached[1]) { diff --git a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java index 84353b264a..fd7fa57bc9 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java @@ -19582,6 +19582,48 @@ public void didReceivedNotification(int id, int account, final Object... args) { boolean jumpToMessage = (Boolean) args[6]; if (jumpToMessage) { int messageId = (Integer) args[1]; + + boolean firstSearch = args.length > 7 && (boolean) args[7]; + if (NaConfig.INSTANCE.getSearchInPlace().Bool() && firstSearch) { + int mask = (Integer) args[2]; + int num = (Integer) args[4]; + int currentMessageId = getFirstVisibleMessage(); + if (currentMessageId != 0) { + ArrayList foundMessageObjects = getMediaDataController().getFoundMessageObjects(); + List foundMessageIds = new ArrayList<>(); + for (MessageObject message: foundMessageObjects) { + foundMessageIds.add(message.getId()); + } + Collections.sort(foundMessageIds); + if (foundMessageIds.get(0) > currentMessageId) { + Runnable loop = () -> { + getMediaDataController().setCurrentMaxMessage(); + getMediaDataController().searchMessagesInChat(null, dialog_id, mergeDialogId, classGuid, 1, threadMessageId, searchingUserMessages, + searchingChatMessages, firstSearch); + }; + if (Looper.myLooper() == Looper.getMainLooper()) { + new Thread(loop).start(); + } else { + loop.run(); + } + return; + } + + int insertionPoint = Collections.binarySearch(foundMessageIds, currentMessageId); + int nextLargerIndex = (insertionPoint < 0) ? Math.max(-(insertionPoint + 1) - 1, 0) : insertionPoint; + if (nextLargerIndex < foundMessageIds.size()) { + Integer nextLargerMessageId = foundMessageIds.get(nextLargerIndex); + if (Math.abs(nextLargerMessageId) < Math.abs(messageId)) { + mask = mask | 2; + } + messageId = nextLargerMessageId; + nextLargerIndex = foundMessageIds.size() - nextLargerIndex - 1; + getMediaDataController().setCurrentMessage(nextLargerIndex); + num = nextLargerIndex; + } + } + } + long did = (Long) args[3]; if (messageId != 0) { scrollToMessageId(messageId, 0, true, did == dialog_id ? 0 : 1, true, 0); @@ -29097,7 +29139,7 @@ private void openSearchWithText(String text) { if (searchItem != null) { searchItem.setSearchFieldText(text, false); } - getMediaDataController().searchMessagesInChat(text, dialog_id, mergeDialogId, classGuid, 0, threadMessageId, searchingUserMessages, searchingChatMessages); + getMediaDataController().searchMessagesInChat(text, dialog_id, mergeDialogId, classGuid, 0, threadMessageId, searchingUserMessages, searchingChatMessages, true); } updatePinnedMessageView(true); } @@ -31670,7 +31712,7 @@ public void onSearchExpand() { public void onSearchPressed(EditText editText) { searchWas = true; updateSearchButtons(0, 0, -1); - getMediaDataController().searchMessagesInChat(editText.getText().toString(), dialog_id, mergeDialogId, classGuid, 0, threadMessageId, searchingUserMessages, searchingChatMessages); + getMediaDataController().searchMessagesInChat(editText.getText().toString(), dialog_id, mergeDialogId, classGuid, 0, threadMessageId, searchingUserMessages, searchingChatMessages, true); } @Override @@ -36329,4 +36371,8 @@ public ThanosEffect getChatThanosEffect() { } return chatListThanosEffect; } + + private int getFirstVisibleMessage() { + return MessageHelper.INSTANCE.getFirstVisibleMessage(chatLayoutManager, chatListView, chatAdapter, messages); + } } diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoChatSettingsActivity.java b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoChatSettingsActivity.java index ff796e3aa1..dc5afbbc74 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoChatSettingsActivity.java +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoChatSettingsActivity.java @@ -104,6 +104,7 @@ public class NekoChatSettingsActivity extends BaseNekoXSettingsActivity implemen private final AbstractConfigCell showPremiumStarInChatRow = cellGroup.appendCell(new ConfigCellTextCheck(NaConfig.INSTANCE.getShowPremiumStarInChat())); private final AbstractConfigCell showPremiumAvatarAnimationRow = cellGroup.appendCell(new ConfigCellTextCheck(NaConfig.INSTANCE.getShowPremiumAvatarAnimation())); private final AbstractConfigCell alwaysSaveChatOffsetRow = cellGroup.appendCell(new ConfigCellTextCheck(NaConfig.INSTANCE.getAlwaysSaveChatOffset())); + private final AbstractConfigCell searchInPlaceRow = cellGroup.appendCell(new ConfigCellTextCheck(NaConfig.INSTANCE.getSearchInPlace())); private final AbstractConfigCell autoInsertGIFCaptionRow = cellGroup.appendCell(new ConfigCellTextCheck(NaConfig.INSTANCE.getAutoInsertGIFCaption())); private final AbstractConfigCell defaultMonoLanguageRow = cellGroup.appendCell(new ConfigCellTextInput(null, NaConfig.INSTANCE.getDefaultMonoLanguage(), null, null, diff --git a/TMessagesProj/src/main/kotlin/xyz/nextalone/nagram/NaConfig.kt b/TMessagesProj/src/main/kotlin/xyz/nextalone/nagram/NaConfig.kt index 43f77e3aff..6595446aa0 100644 --- a/TMessagesProj/src/main/kotlin/xyz/nextalone/nagram/NaConfig.kt +++ b/TMessagesProj/src/main/kotlin/xyz/nextalone/nagram/NaConfig.kt @@ -454,6 +454,12 @@ object NaConfig { ConfigItem.configTypeString, "" ) + val searchInPlace = + addConfig( + "searchInPlace", + ConfigItem.configTypeBool, + false + ) private fun addConfig( k: String, diff --git a/TMessagesProj/src/main/kotlin/xyz/nextalone/nagram/helper/MessageHelper.kt b/TMessagesProj/src/main/kotlin/xyz/nextalone/nagram/helper/MessageHelper.kt index eafee6fa1d..1ed4fe59e8 100644 --- a/TMessagesProj/src/main/kotlin/xyz/nextalone/nagram/helper/MessageHelper.kt +++ b/TMessagesProj/src/main/kotlin/xyz/nextalone/nagram/helper/MessageHelper.kt @@ -5,9 +5,13 @@ import android.content.ClipboardManager import android.content.Context import android.text.TextUtils import androidx.core.content.FileProvider +import androidx.recyclerview.widget.GridLayoutManagerFixed import org.telegram.messenger.* import org.telegram.tgnet.TLRPC.* +import org.telegram.ui.Cells.ChatActionCell +import org.telegram.ui.Cells.ChatMessageCell import org.telegram.ui.ChatActivity +import org.telegram.ui.Components.RecyclerListView import xyz.nextalone.nagram.NaConfig import java.io.File @@ -217,4 +221,80 @@ object MessageHelper { } return message } + + fun getFirstVisibleMessage( + chatLayoutManager: GridLayoutManagerFixed, + chatListView: RecyclerListView, + chatAdapter: ChatActivity.ChatActivityAdapter, + messages: MutableList + ): Int { + var messageId = + 0 + val position: Int = + chatLayoutManager.findFirstVisibleItemPosition() + if (position != 0) { + var holder = + chatListView.findViewHolderForAdapterPosition( + position + ) + if (holder != null) { + var mid = + 0 + if (holder.itemView is ChatMessageCell) { + mid = + (holder.itemView as ChatMessageCell).messageObject.id + } else if (holder.itemView is ChatActionCell) { + mid = + (holder.itemView as ChatActionCell).messageObject.id + } + if (mid == 0) { + holder = + chatListView.findViewHolderForAdapterPosition( + position + 1 + ) + } + var ignore = + false + var count = + 0 + for (a in position - 1 downTo chatAdapter.messagesStartRow) { + val num: Int = + a - chatAdapter.messagesStartRow + if (num < 0 || num >= messages.size) { + continue + } + val messageObject: MessageObject = + messages.get( + num + ) + if (messageObject.id == 0) { + continue + } + if ((!messageObject.isOut || messageObject.messageOwner.from_scheduled) && messageObject.isUnread) { + ignore = + true + messageId = + 0 + } + if (count > 2) { + break + } + count++ + } + if (holder != null && !ignore) { + if (holder.itemView is ChatMessageCell) { + messageId = + (holder.itemView as ChatMessageCell).messageObject.id + } else if (holder.itemView is ChatActionCell) { + messageId = + (holder.itemView as ChatActionCell).messageObject.id + } else { + messageId = + 0 + } + } + } + } + return messageId + } } diff --git a/TMessagesProj/src/main/res/values-zh-rCN/strings_na.xml b/TMessagesProj/src/main/res/values-zh-rCN/strings_na.xml index 73f6b0325e..b4c74ac640 100644 --- a/TMessagesProj/src/main/res/values-zh-rCN/strings_na.xml +++ b/TMessagesProj/src/main/res/values-zh-rCN/strings_na.xml @@ -90,4 +90,5 @@ 偷偷看动态 隐藏文件夹中的\"全部取消静音\" 本地名称颜色 + 在当前位置中开始搜索 diff --git a/TMessagesProj/src/main/res/values/strings_na.xml b/TMessagesProj/src/main/res/values/strings_na.xml index 0086ba8bc8..47bfffd180 100644 --- a/TMessagesProj/src/main/res/values/strings_na.xml +++ b/TMessagesProj/src/main/res/values/strings_na.xml @@ -93,4 +93,5 @@ Disable Send Read Stories Hide filter mute all Use local quote color + Search in place