Skip to content

Commit

Permalink
feat: search in place
Browse files Browse the repository at this point in the history
Signed-off-by: Next Alone <12210746+NextAlone@users.noreply.github.com>
  • Loading branch information
NextAlone committed Dec 31, 2023
1 parent e9217eb commit c425343
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import android.text.style.CharacterStyle;
import android.text.style.URLSpan;
import android.text.util.Linkify;
import android.util.Log;
import android.util.Pair;
import android.util.SparseArray;

Expand Down Expand Up @@ -3385,9 +3384,13 @@ public void clearFoundMessageObjects() {
public boolean isMessageFound(int messageId, boolean mergeDialog) {
return searchResultMessagesMap[mergeDialog ? 1 : 0].indexOfKey(messageId) >= 0;
}

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 @@ -3398,9 +3401,21 @@ public void jumpToSearchedMessage(int guid, int index) {
MessageObject messageObject = searchResultMessages.get(lastReturnedNum);
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 @@ -3409,8 +3424,12 @@ public void loadMoreSearchMessages() {
lastReturnedNum = temp;
loadingMoreSearchMessages = true;
}

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 Down Expand Up @@ -3609,7 +3628,9 @@ private void searchMessagesInChat(String query, long dialogId, long mergeDialogI
lastReturnedNum = searchResultMessages.size() - 1;
}
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);
}
}
if (queryWithDialogFinal == dialogId && messagesSearchEndReached[0] && mergeDialogId != 0 && !messagesSearchEndReached[1]) {
Expand Down
52 changes: 49 additions & 3 deletions TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@
import xyz.nextalone.nnngram.config.ConfigManager;
import xyz.nextalone.nnngram.config.DialogConfig;
import xyz.nextalone.nnngram.config.ForwardContext;
import xyz.nextalone.nnngram.helpers.MessageHelper;
import xyz.nextalone.nnngram.helpers.QrHelper;
import xyz.nextalone.nnngram.helpers.TranslateHelper;
import xyz.nextalone.nnngram.translate.LanguageDetectorTimeout;
Expand Down Expand Up @@ -19167,13 +19168,54 @@ public void didReceivedNotification(int id, int account, final Object... args) {
boolean jumpToMessage = (Boolean) args[6];
if (jumpToMessage) {
int messageId = (Integer) args[1];
int mask = (Integer) args[2];
int num = (Integer) args[4];
boolean firstSearch = args.length > 7 && (boolean) args[7];
if (Config.searchInPlace && firstSearch) {
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;
}
}
} else {
}
long did = (Long) args[3];
if (messageId != 0) {
scrollToMessageId(messageId, 0, true, did == dialog_id ? 0 : 1, true, 0);
} else {
updateVisibleRows();
}
updateSearchButtons((Integer) args[2], (Integer) args[4], (Integer) args[5]);
updateSearchButtons(mask, num, (Integer) args[5]);
if (searchItem != null) {
searchItem.setShowSearchProgress(false);
}
Expand Down Expand Up @@ -28656,7 +28698,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 @@ -31224,7 +31266,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 @@ -35131,4 +35173,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 @@ -131,6 +131,7 @@ public class ChatSettingActivity extends BaseActivity {
private int mergeMessageRow;
private int filterZalgoRow;
private int hideKeyboardWhenScrollingRow;
private int searchInPlaceRow;
private int chat2Row;

private int markdownRow;
Expand Down Expand Up @@ -427,6 +428,11 @@ protected void onItemClick(View view, int position, float x, float y) {
if (view instanceof TextCheckCell) {
((TextCheckCell) view).setChecked(Config.hideKeyboardWhenScrolling);
}
} else if (position == searchInPlaceRow) {
Config.toggleSearchInPlace();
if (view instanceof TextCheckCell) {
((TextCheckCell) view).setChecked(Config.searchInPlace);
}
}
}

Expand Down Expand Up @@ -488,6 +494,7 @@ protected void updateRows() {
mergeMessageRow = addRow("mergeMessage");
filterZalgoRow = addRow("filterZalgo");
hideKeyboardWhenScrollingRow = addRow("hideKeyboardWhenScrolling");
searchInPlaceRow = addRow("searchInPlace");
chat2Row = addRow();
markdownRow = addRow();
markdownDisableRow = addRow("markdownDisabled");
Expand Down Expand Up @@ -650,6 +657,8 @@ public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, boole
textCell.setTextAndCheck(LocaleController.getString("filterZalgo", R.string.filterZalgo), Config.filterZalgo, true);
} else if (position == hideKeyboardWhenScrollingRow) {
textCell.setTextAndCheck(LocaleController.getString("hideKeyboardWhenScrolling", R.string.hideKeyboardWhenScrolling), Config.hideKeyboardWhenScrolling, true);
} else if (position == searchInPlaceRow) {
textCell.setTextAndCheck(LocaleController.getString("searchInPlace", R.string.searchInPlace), Config.searchInPlace, true);
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package xyz.nextalone.nnngram.helpers

import androidx.recyclerview.widget.GridLayoutManagerFixed
import org.telegram.messenger.MessageObject
import org.telegram.ui.Cells.ChatActionCell
import org.telegram.ui.Cells.ChatMessageCell
import org.telegram.ui.ChatActivity
import org.telegram.ui.Components.RecyclerListView

object MessageHelper {

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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ object Defines {
@BooleanConfig const val hideSendAsButton = "hideSendAsButton"
@BooleanConfig const val hideFilterMuteAll = "hideFilterMuteAll"
@BooleanConfig const val hideKeyboardWhenScrolling = "hideKeyboardWhenScrolling"
@BooleanConfig const val searchInPlace = "searchInPlace"

// Drawer List
@BooleanConfig(true) const val showNewGroup = "showNewGroup"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@
<string name="hideSendAsButton">隐藏发送者按钮</string>
<string name="hideFilterMuteAll">隱藏文件夾中的\"全部取消靜音\"</string>
<string name="hideKeyboardWhenScrolling">滑動時隱藏鍵盤</string>
<string name="searchInPlace">在当前位置中开始搜索</string>

<string name="Quote">引用</string>
</resources>
1 change: 1 addition & 0 deletions TMessagesProj/src/main/res/values-zh/strings_nullgram.xml
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@
<string name="hideSendAsButton">隐藏发送者按钮</string>
<string name="hideFilterMuteAll">隐藏文件夹中的\"全部取消静音\"</string>
<string name="hideKeyboardWhenScrolling">滑动时隐藏键盘</string>
<string name="searchInPlace">从当前位置中开始搜索</string>

<string name="Quote">引用</string>
<string name="QuoteTo">引用到...</string>
Expand Down
1 change: 1 addition & 0 deletions TMessagesProj/src/main/res/values/strings_nullgram.xml
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,5 @@
<string name="hideSendAsButton">Hide send as button</string>
<string name="hideFilterMuteAll">Hide filter mute all</string>
<string name="hideKeyboardWhenScrolling">Hide keyboard when scrolling</string>
<string name="searchInPlace">Search in place</string>
</resources>

0 comments on commit c425343

Please sign in to comment.