Skip to content

Commit

Permalink
feat: search in place
Browse files Browse the repository at this point in the history
Co-authored-by: Next Alone <12210746+NextAlone@users.noreply.github.com>
  • Loading branch information
omg-xtao and NextAlone committed Jan 1, 2024
1 parent e30199d commit 22e427f
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
Expand All @@ -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;
Expand All @@ -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]) {
Expand Down
50 changes: 48 additions & 2 deletions TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<MessageObject> foundMessageObjects = getMediaDataController().getFoundMessageObjects();
List<Integer> 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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -36329,4 +36371,8 @@ public ThanosEffect getChatThanosEffect() {
}
return chatListThanosEffect;
}

private int getFirstVisibleMessage() {
return MessageHelper.INSTANCE.getFirstVisibleMessage(chatLayoutManager, chatListView, chatAdapter, messages);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,12 @@ object NaConfig {
ConfigItem.configTypeString,
""
)
val searchInPlace =
addConfig(
"searchInPlace",
ConfigItem.configTypeBool,
false
)

private fun addConfig(
k: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -217,4 +221,80 @@ object MessageHelper {
}
return message
}

fun getFirstVisibleMessage(
chatLayoutManager: GridLayoutManagerFixed,
chatListView: RecyclerListView,
chatAdapter: ChatActivity.ChatActivityAdapter,
messages: MutableList<MessageObject>
): 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
}
}
1 change: 1 addition & 0 deletions TMessagesProj/src/main/res/values-zh-rCN/strings_na.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,5 @@
<string name="DisableSendReadStories">偷偷看动态</string>
<string name="HideFilterMuteAll">隐藏文件夹中的\"全部取消静音\"</string>
<string name="UseLocalQuoteColor">本地名称颜色</string>
<string name="searchInPlace">在当前位置中开始搜索</string>
</resources>
1 change: 1 addition & 0 deletions TMessagesProj/src/main/res/values/strings_na.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,5 @@
<string name="DisableSendReadStories">Disable Send Read Stories</string>
<string name="HideFilterMuteAll">Hide filter mute all</string>
<string name="UseLocalQuoteColor">Use local quote color</string>
<string name="searchInPlace">Search in place</string>
</resources>

0 comments on commit 22e427f

Please sign in to comment.