Skip to content

Commit

Permalink
Fixed handling reactions for messages that are currently visible on s…
Browse files Browse the repository at this point in the history
…creen
  • Loading branch information
vkryl committed Oct 23, 2023
1 parent f70c55f commit 5a8f1f5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 41 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
28 changes: 8 additions & 20 deletions app/src/main/java/org/thunderdog/challegram/data/TGMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -4471,17 +4471,6 @@ public final boolean containsUnreadReactions () {
return msg.unreadReactions != null && msg.unreadReactions.length > 0;
}

public final void readReactions () {
synchronized (this) {
if (combinedMessages != null) {
for (TdApi.Message message : combinedMessages) {
message.unreadReactions = new TdApi.UnreadReaction[0];
}
}
msg.unreadReactions = new TdApi.UnreadReaction[0];
}
}

public final boolean containsUnreadMention () {
synchronized (this) {
if (combinedMessages != null) {
Expand Down Expand Up @@ -4952,16 +4941,15 @@ public boolean markAsViewed () {
}
result = true;
}
if (containsUnreadReactions()) {
if (!BitwiseUtils.hasFlag(flags, FLAG_IGNORE_REACTIONS_VIEW)) {
highlightUnreadReactions();
highlight(true);
tdlib.ui().postDelayed(() -> {
flags = BitwiseUtils.setFlag(flags, FLAG_IGNORE_REACTIONS_VIEW, false);
}, 500L);
tdlib().ui().post(this::readReactions);
}
if (containsUnreadReactions() && !BitwiseUtils.hasFlag(flags, FLAG_IGNORE_REACTIONS_VIEW)) {
flags |= FLAG_IGNORE_REACTIONS_VIEW;

highlightUnreadReactions();
highlight(true);
tdlib.ui().postDelayed(() -> {
flags = BitwiseUtils.setFlag(flags, FLAG_IGNORE_REACTIONS_VIEW, false);
}, 500L);

result = true;
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public long[] getMessageIds (boolean onlyRecentlyViewed, boolean updateRecentSta
/**
* @return True, if flags were changed or message wasn't previously visible
*/
public boolean addVisibleMessage (TdApi.Object rawMessage, @Flags long flags, long viewId) {
public boolean addVisibleMessage (TdApi.Object rawMessage, @Flags long flags, long viewId, boolean forceMarkAsRecent) {
VisibleMessage visibleMessage;
boolean isSponsored = rawMessage.getConstructor() == TdApi.SponsoredMessage.CONSTRUCTOR;
if (isSponsored) {
Expand All @@ -235,7 +235,7 @@ public boolean addVisibleMessage (TdApi.Object rawMessage, @Flags long flags, lo
}
if (visibleMessage != null) {
visibleMessage.viewId = viewId;
if (visibleMessage.flags != flags || visibleMessage.visibility.isHidden()) {
if (visibleMessage.flags != flags || visibleMessage.visibility.isHidden() || forceMarkAsRecent) {
long oldFlags = visibleMessage.flags;
visibleMessage.flags = flags;
visibleMessage.isRecentlyViewed = true;
Expand Down Expand Up @@ -512,28 +512,28 @@ private boolean replaceMessage (TdApi.Message message, long oldMessageId, @Nulla
}
VisibleMessage visibleMessage = visibleChat.removeVisibleMessage(message.chatId, oldMessageId);
if (visibleMessage != null) {
return addVisibleMessage(message, visibleMessage.flags, visibleMessage.viewId);
return addVisibleMessage(message, visibleMessage.flags, visibleMessage.viewId, false);
}
return false;
}

public boolean addVisibleMessage (TdApi.Message message, @Flags long flags, long viewId) {
return addVisibleMessageImpl(message.chatId, message, flags, viewId);
public boolean addVisibleMessage (TdApi.Message message, @Flags long flags, long viewId, boolean forceMarkAsRecent) {
return addVisibleMessageImpl(message.chatId, message, flags, viewId, forceMarkAsRecent);
}

public boolean addVisibleMessage (long chatId, TdApi.SponsoredMessage sponsoredMessage, @Flags long flags, long viewId) {
return addVisibleMessageImpl(chatId, sponsoredMessage, flags, viewId);
public boolean addVisibleMessage (long chatId, TdApi.SponsoredMessage sponsoredMessage, @Flags long flags, long viewId, boolean forceMarkAsRecent) {
return addVisibleMessageImpl(chatId, sponsoredMessage, flags, viewId, forceMarkAsRecent);
}

private boolean addVisibleMessageImpl (long chatId, TdApi.Object rawMessage, @Flags long flags, long viewId) {
private boolean addVisibleMessageImpl (long chatId, TdApi.Object rawMessage, @Flags long flags, long viewId, boolean forceMarkAsRecent) {
if (isDestroyed()) {
return false;
}
VisibleChat visibleChat = find(chatId);
if (visibleChat == null) {
visibleChat = new VisibleChat(this, chatId);
}
if (visibleChat.addVisibleMessage(rawMessage, flags, viewId)) {
if (visibleChat.addVisibleMessage(rawMessage, flags, viewId, forceMarkAsRecent)) {
if (visibleChat.visibility.markAsVisible()) {
trackChat(visibleChat, true);
}
Expand Down
18 changes: 8 additions & 10 deletions app/src/main/java/org/thunderdog/challegram/telegram/TdlibUi.java
Original file line number Diff line number Diff line change
Expand Up @@ -6801,7 +6801,7 @@ default long getVisibleChatId () {
}

public interface MessageViewCallback {
void onMessageViewed (TdlibMessageViewer.Viewport viewport, View view, TdApi.Message message, @TdlibMessageViewer.Flags long flags, long viewId, boolean allowRequest);
boolean onMessageViewed (TdlibMessageViewer.Viewport viewport, View view, TdApi.Message message, @TdlibMessageViewer.Flags long flags, long viewId, boolean allowRequest);
default boolean needForceRead (TdlibMessageViewer.Viewport viewport) {
return false;
}
Expand Down Expand Up @@ -6848,7 +6848,7 @@ public Runnable attachViewportToRecyclerView (TdlibMessageViewer.Viewport viewpo
if (provider.isSponsoredMessage()) {
TdApi.SponsoredMessage sponsoredMessage = provider.getVisibleSponsoredMessage();
long chatId = provider.getVisibleChatId();
if (sponsoredMessage != null && viewport.addVisibleMessage(chatId, sponsoredMessage, flags, viewId)) {
if (sponsoredMessage != null && viewport.addVisibleMessage(chatId, sponsoredMessage, flags, viewId, false)) {
if (callback != null) {
callback.onSponsoredMessageViewed(viewport, view, sponsoredMessage, flags, viewId, allowViewRequest);
}
Expand All @@ -6858,21 +6858,19 @@ public Runnable attachViewportToRecyclerView (TdlibMessageViewer.Viewport viewpo
List<TdApi.Message> mediaGroup = provider.getVisibleMediaGroup();
if (mediaGroup != null) {
for (TdApi.Message message : mediaGroup) {
if (viewport.addVisibleMessage(message, flags, viewId)) {
if (callback != null) {
callback.onMessageViewed(viewport, view, message, flags, viewId, allowViewRequest);
}
boolean forceMarkAsRecent = callback != null && callback.onMessageViewed(viewport, view, message, flags, viewId, allowViewRequest);
if (viewport.addVisibleMessage(message, flags, viewId, forceMarkAsRecent)) {
viewedMessageCount++;
}
}
}
} else {
TdApi.Message message = provider.getVisibleMessage();
if (message != null && viewport.addVisibleMessage(message, flags, viewId)) {
if (callback != null) {
callback.onMessageViewed(viewport, view, message, flags, viewId, allowViewRequest);
if (message != null) {
boolean forceMarkAsRecent = callback != null && callback.onMessageViewed(viewport, view, message, flags, viewId, allowViewRequest);
if (viewport.addVisibleMessage(message, flags, viewId, forceMarkAsRecent)) {
viewedMessageCount++;
}
viewedMessageCount++;
}
}
}
Expand Down

0 comments on commit 5a8f1f5

Please sign in to comment.