Skip to content

Commit

Permalink
Merge branch 'main' into folders
Browse files Browse the repository at this point in the history
  • Loading branch information
jplie committed Oct 23, 2023
2 parents 73f9098 + 5a8f1f5 commit 554ef6d
Show file tree
Hide file tree
Showing 18 changed files with 289 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -731,9 +731,9 @@ public boolean isMessageContentVisible (TdlibMessageViewer.Viewport viewport, Vi
}

@Override
public void onMessageViewed (TdlibMessageViewer.Viewport viewport, View view, TdApi.Message message, long flags, long viewId, boolean allowRequest) {
public boolean onMessageViewed (TdlibMessageViewer.Viewport viewport, View view, TdApi.Message message, long flags, long viewId, boolean allowRequest) {
if (inSpecialMode() || !allowRequest)
return;
return false;
MessageProvider provider = (MessageProvider) view;
TGMessage msg = provider.getMessage();
if (msg.markAsViewed() || msg.containsUnreadReactions()) {
Expand All @@ -744,7 +744,9 @@ public void onMessageViewed (TdlibMessageViewer.Viewport viewport, View view, Td
if (msg.containsUnreadReactions() && messageId > lastViewedReactionMessageId) {
lastViewedReactionMessageId = messageId;
}
return true;
}
return false;
}

@Override
Expand Down Expand Up @@ -1766,8 +1768,8 @@ private void replaceMessage (TGMessage msg, int index, long messageId, TdApi.Mes
}
}

private void replaceMessageContent (TGMessage msg, int index, long messageId, TdApi.MessageContent content) {
switch (msg.setMessageContent(messageId, content)) {
private void replaceMessageContent (TGMessage msg, int index, long chatId, long messageId, TdApi.MessageContent content) {
switch (msg.replaceMessageContent(chatId, messageId, content)) {
case TGMessage.MESSAGE_INVALIDATED: {
invalidateViewAt(index);
break;
Expand All @@ -1793,14 +1795,10 @@ private void updateMessageContent (long chatId, long messageId, TdApi.MessageCon
controller.onMessageChanged(chatId, messageId, content);
ArrayList<TGMessage> items = adapter.getItems();
if (!adapter.isEmpty() && items != null) {
int i = 0;
int index = 0;
for (TGMessage item : items) {
if (item.isDescendantOrSelf(messageId)) {
replaceMessageContent(item, i, messageId, content);
} else {
item.replaceReplyContent(chatId, messageId, content);
}
i++;
replaceMessageContent(item, index, chatId, messageId, content);
index++;
}
}
ThreadInfo messageThread = loader.getMessageThread();
Expand Down Expand Up @@ -1913,6 +1911,28 @@ public void updateMessageIsPinned (long messageId, boolean isPinned) {
}
}

private void handleMessageChange (TGMessage msg, int index, long messageId, @TGMessage.MessageChangeType int changeType) {
switch (changeType) {
case TGMessage.MESSAGE_INVALIDATED: {
invalidateViewAt(index);
break;
}
case TGMessage.MESSAGE_CHANGED: {
getAdapter().notifyItemChanged(index);
break;
}
case TGMessage.MESSAGE_NOT_CHANGED: {
// Nothing to do
break;
}
case TGMessage.MESSAGE_REPLACE_REQUIRED: {
TdApi.Message message = msg.getMessage(messageId);
replaceMessage(msg, index, messageId, message);
break;
}
}
}

public void updateMessagesDeleted (long chatId, long[] messageIds) {
controller.removeReply(messageIds);
controller.onMessagesDeleted(chatId, messageIds);
Expand All @@ -1926,14 +1946,17 @@ public void updateMessagesDeleted (long chatId, long[] messageIds) {
ThreadInfo messageThread = loader.getMessageThread();
long lastReadInboxMessageId = messageThread != null ? messageThread.getLastReadInboxMessageId() : chat.lastReadInboxMessageId;

int i = 0;
main: while (i < adapter.getMessageCount()) {
TGMessage item = adapter.getMessage(i);
int index = 0;
main: while (index < adapter.getMessageCount()) {
TGMessage item = adapter.getMessage(index);

for (long messageId : messageIds) {
switch (item.removeMessage(messageId)) {
case TGMessage.REMOVE_NOTHING: {
item.removeReply(chatId, messageId);
@TGMessage.MessageChangeType int changeType = item.removeMessagePreview(chatId, messageId);
if (changeType != TGMessage.MESSAGE_NOT_CHANGED) {
handleMessageChange(item, index, messageId, changeType);
}
break;
}
case TGMessage.REMOVE_COMBINATION: {
Expand All @@ -1951,7 +1974,7 @@ public void updateMessagesDeleted (long chatId, long[] messageIds) {
}
}
case TGMessage.REMOVE_COMPLETELY: {
TGMessage removed = adapter.removeItem(i);
TGMessage removed = adapter.removeItem(index);
if (controller.unselectMessage(messageId, removed)) {
selectedCount--;
unselectedSomeMessages = true;
Expand All @@ -1968,7 +1991,7 @@ public void updateMessagesDeleted (long chatId, long[] messageIds) {
}
}

i++;
index++;
}

if (unselectedSomeMessages) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public class ContentPreview {
public final @Nullable TdApi.FormattedText formattedText;
public final boolean isTranslatable;
public final boolean hideAuthor;
public @Nullable TdApi.Message relatedMessage;
private @Nullable MessageContentBuilder relatedMessageBuilder;

public ContentPreview (@Nullable Emoji emoji, @StringRes int placeholderTextRes) {
this(emoji, placeholderTextRes, (TdApi.FormattedText) null);
Expand Down Expand Up @@ -135,6 +137,41 @@ public ContentPreview (@Nullable Emoji emoji, int placeholderText, @Nullable TdA
this.parentEmoji = parentEmoji;
}

private interface MessageContentBuilder {
ContentPreview runBuilder (TdApi.Message message);
}

public boolean belongsToRelatedMessage (long chatId, long[] messageIds) {
return relatedMessage != null && relatedMessage.chatId == chatId && ArrayUtils.contains(messageIds, relatedMessage.id);
}

private ContentPreview setRelatedMessage (@NonNull TdApi.Message message, @NonNull MessageContentBuilder refresher) {
this.relatedMessage = message;
this.relatedMessageBuilder = refresher;
return this;
}

public boolean updateRelatedMessage (long chatId, long messageId, TdApi.MessageContent content, RefreshCallback callback) {
if (relatedMessage != null && relatedMessage.chatId == chatId && relatedMessage.id == messageId) {
relatedMessage.content = content;
if (callback != null) {
ContentPreview newPreview;
if (relatedMessageBuilder != null) {
newPreview = relatedMessageBuilder.runBuilder(relatedMessage);
} else {
newPreview = null;
}
if (newPreview != null) {
callback.onContentPreviewChanged(chatId, messageId, newPreview, this);
} else {
callback.onContentPreviewNotChanged(chatId, messageId, this);
}
}
return true;
}
return false;
}

@NonNull
public static ContentPreview getChatListPreview (Tdlib tdlib, long chatId, TdApi.Message message, boolean checkChatRestrictions) {
return getContentPreview(tdlib, chatId, message, true, true, checkChatRestrictions);
Expand Down Expand Up @@ -319,7 +356,15 @@ private static ContentPreview getContentPreview (Tdlib tdlib, long chatId, TdApi
return new ContentPreview(EMOJI_PIN, R.string.ChatContentPinned)
.setRefresher((oldPreview, callback) -> tdlib.getMessage(chatId, pinnedMessageId, remotePinnedMessage -> {
if (remotePinnedMessage != null) {
callback.onContentPreviewChanged(chatId, message.id, new ContentPreview(EMOJI_PIN, getContentPreview(tdlib, chatId, remotePinnedMessage, allowContent, isChatList, checkChatRestrictions)), oldPreview);
MessageContentBuilder builder = new MessageContentBuilder() {
@Override
public ContentPreview runBuilder (TdApi.Message message) {
return new ContentPreview(EMOJI_PIN, getContentPreview(tdlib, chatId, message, allowContent, isChatList, checkChatRestrictions))
.setRelatedMessage(message, this);
}
};
ContentPreview newPreview = builder.runBuilder(remotePinnedMessage);
callback.onContentPreviewChanged(chatId, message.id, newPreview, oldPreview);
} else {
callback.onContentPreviewNotChanged(chatId, message.id, oldPreview);
}
Expand All @@ -344,7 +389,16 @@ private static ContentPreview getContentPreview (Tdlib tdlib, long chatId, TdApi
if (remoteGameMessage != null && Td.isGame(remoteGameMessage.content)) {
String newGameTitle = TD.getGameName(((TdApi.MessageGame) remoteGameMessage.content).game, false);
if (!StringUtils.isEmpty(newGameTitle)) {
callback.onContentPreviewChanged(message.chatId, message.id, new ContentPreview(EMOJI_GAME, 0, Lang.plural(message.isOutgoing ? R.string.game_ActionYouScoredInGame : R.string.game_ActionScoredInGame, score.score, newGameTitle), true), oldPreview);
MessageContentBuilder builder = new MessageContentBuilder() {
@Override
public ContentPreview runBuilder (TdApi.Message updatedMessage) {
String newGameTitle = TD.getGameName(((TdApi.MessageGame) updatedMessage.content).game, false);
return new ContentPreview(EMOJI_GAME, 0, Lang.plural(message.isOutgoing ? R.string.game_ActionYouScoredInGame : R.string.game_ActionScoredInGame, score.score, newGameTitle), true)
.setRelatedMessage(updatedMessage, this);
}
};
ContentPreview newContent = builder.runBuilder(remoteGameMessage);
callback.onContentPreviewChanged(message.chatId, message.id, newContent, oldPreview);
return;
}
}
Expand Down
11 changes: 9 additions & 2 deletions app/src/main/java/org/thunderdog/challegram/data/TGChat.java
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,9 @@ public boolean updateMessageContent (long chatId, long messageId, TdApi.MessageC
return true;
}
}
if (currentPreview.updateRelatedMessage(chatId, messageId, newContent, this)) {
return true;
}
}
if (chat.lastMessage.id == messageId) {
chat.lastMessage.content = newContent;
Expand All @@ -555,7 +558,7 @@ public boolean updateMessageContent (long chatId, long messageId, TdApi.MessageC

public boolean updateMessagesDeleted (long chatId, long[] messageIds) {
if (chat.id == chatId && chat.lastMessage != null) {
if (ArrayUtils.indexOf(messageIds, chat.lastMessage.id) >= 0) {
if (ArrayUtils.contains(messageIds, chat.lastMessage.id)) {
chat.lastMessage = null;
setText();
return true;
Expand All @@ -566,7 +569,7 @@ public boolean updateMessagesDeleted (long chatId, long[] messageIds) {
if (album != null) {
boolean albumChanged = false;
for (int index = album.messages.size() - 1; index >= 0; index--) {
if (ArrayUtils.indexOf(messageIds, album.messages.get(index).id) >= 0) {
if (ArrayUtils.contains(messageIds, album.messages.get(index).id)) {
album.messages.remove(index);
albumChanged = true;
}
Expand All @@ -579,6 +582,10 @@ public boolean updateMessagesDeleted (long chatId, long[] messageIds) {
setContentPreview(ContentPreview.getAlbumPreview(tdlib, chat.lastMessage, album, true));
}
}
if (currentPreview.belongsToRelatedMessage(chatId, messageIds)) {
setText();
return true;
}
}
return true;
}
Expand Down
Loading

0 comments on commit 554ef6d

Please sign in to comment.