Skip to content

Commit 2aa497f

Browse files
committed
Live chat improvements
1 parent 10b18bc commit 2aa497f

File tree

4 files changed

+63
-16
lines changed

4 files changed

+63
-16
lines changed

app/src/main/java/com/fastcomments/ExampleLiveChatActivity.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,35 @@ class ExampleLiveChatActivity : AppCompatActivity() {
3030
urlId = "https://example.com/chat-room" // Use your URL ID or chat room identifier
3131
// Optional: Set additional configuration options
3232
pageTitle = "Example Chat Room"
33-
showLiveRightAway = true // Show new messages immediately
3433
}
35-
34+
LiveChatView.setupLiveChatConfig(config)
35+
3636
// Initialize the SDK. The below uses SimpleSSO for the demo. You probably want to use SecureSSO with a backend service.
37-
val userData = SimpleSSOUserData("Example User", "[email protected]", "https://staticm.fastcomments.com/1639362726066-DSC_0841.JPG");
37+
val userData = SimpleSSOUserData(
38+
"Example User",
39+
40+
"https://staticm.fastcomments.com/1639362726066-DSC_0841.JPG"
41+
);
3842
val sso = FastCommentsSSO(userData)
3943
config.sso = sso.prepareToSend()
4044
sdk = FastCommentsSDK().configure(config)
4145

4246
// Find the live chat view in the layout
4347
liveChatView = findViewById(R.id.liveChatView)
44-
48+
4549
// Set the SDK instance for the view
4650
liveChatView.setSDK(sdk)
47-
51+
4852
// Load the chat
4953
liveChatView.load()
5054
}
51-
55+
5256
override fun onResume() {
5357
super.onResume()
5458
// Refresh live events connection when returning to the app
5559
sdk.refreshLiveEvents()
5660
}
57-
61+
5862
override fun onDestroy() {
5963
super.onDestroy()
6064
// Clean up resources

libraries/sdk/src/main/java/com/fastcomments/sdk/CommentsTree.java

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.util.Log;
55

66
import com.fastcomments.model.PublicComment;
7+
import com.fastcomments.model.SortDirections;
78

89
import java.util.ArrayList;
910
import java.util.HashMap;
@@ -400,12 +401,28 @@ private void addForUser(String userId, RenderableComment renderableComment) {
400401
}
401402

402403
/**
403-
* Add a new comment to the tree (from live events)
404-
*
405-
* @param comment The comment to add
404+
* Backward-compatible method that defaults to NEWEST_FIRST sort direction
405+
*
406+
* @param comment The comment to add
406407
* @param displayNow Whether to show the comment immediately
407408
*/
408409
public void addComment(PublicComment comment, boolean displayNow) {
410+
// Default to NEWEST_FIRST for backward compatibility
411+
addComment(comment, displayNow, SortDirections.NF);
412+
}
413+
414+
private boolean isNewestFirst(SortDirections sortDirections) {
415+
return sortDirections == SortDirections.NF || sortDirections == SortDirections.MR;
416+
}
417+
418+
/**
419+
* Add a new comment to the tree (from live events) with the specified sort direction
420+
*
421+
* @param comment The comment to add
422+
* @param displayNow Whether to show the comment immediately
423+
* @param sortDirection The direction to sort comments (newest first or oldest first)
424+
*/
425+
public void addComment(PublicComment comment, boolean displayNow, SortDirections sortDirection) {
409426
if (comment == null || commentsById.containsKey(comment.getId())) {
410427
return;
411428
}
@@ -416,12 +433,28 @@ public void addComment(PublicComment comment, boolean displayNow) {
416433

417434
if (comment.getParentId() == null) {
418435
// This is a root comment
419-
allComments.add(0, renderableComment);
436+
// For NEWEST_FIRST, add at index 0 (top)
437+
// For OLDEST_FIRST, add at the end (bottom)
438+
boolean isNewestFirst = isNewestFirst(sortDirection);
439+
440+
if (isNewestFirst) {
441+
allComments.add(0, renderableComment);
442+
} else {
443+
allComments.add(renderableComment);
444+
}
420445

421446
if (displayNow) {
422-
// Show the comment right away at the top of the list
423-
visibleNodes.add(0, renderableComment);
424-
adapter.notifyItemInserted(0);
447+
int position;
448+
if (isNewestFirst) {
449+
// For newest first, add at the top
450+
position = 0;
451+
visibleNodes.add(0, renderableComment);
452+
} else {
453+
// For oldest first (like chat), add at the bottom
454+
position = visibleNodes.size();
455+
visibleNodes.add(renderableComment);
456+
}
457+
adapter.notifyItemInserted(position);
425458

426459
// Check for new user presence (optimized for single comment)
427460
checkAndRequestUserPresenceStatus(renderableComment);

libraries/sdk/src/main/java/com/fastcomments/sdk/FastCommentsSDK.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -852,8 +852,8 @@ private void handleNewComment(LiveEvent eventData) {
852852
}
853853

854854
public void addComment(PublicComment publicComment, boolean displayNow) {
855-
// Add to comments tree
856-
commentsTree.addComment(publicComment, displayNow);
855+
// Add to comments tree using configured sort direction
856+
commentsTree.addComment(publicComment, displayNow, config.defaultSortDirection);
857857

858858
// Increment the server comment count
859859
commentCountOnServer++;

libraries/sdk/src/main/java/com/fastcomments/sdk/LiveChatView.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
import android.view.animation.AnimationUtils;
1313
import android.widget.Button;
1414

15+
import com.fastcomments.core.CommentWidgetConfig;
1516
import com.fastcomments.core.VoteStyle;
1617
import com.fastcomments.model.APIEmptyResponse;
1718
import com.fastcomments.model.BlockSuccess;
1819
import com.fastcomments.model.PickFCommentApprovedOrCommentHTML;
1920
import com.fastcomments.model.PublicComment;
21+
import com.fastcomments.model.SortDirections;
2022
import com.google.android.material.floatingactionbutton.FloatingActionButton;
2123

2224
import android.widget.FrameLayout;
@@ -178,6 +180,12 @@ public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newStat
178180
initializeWithSDK();
179181
}
180182
}
183+
184+
public static void setupLiveChatConfig(CommentWidgetConfig config) {
185+
config.showLiveRightAway = true;
186+
config.defaultSortDirection = SortDirections.OF;
187+
config.maxReplyDepth = 0;
188+
}
181189

182190
/**
183191
* Set the SDK instance to use with this view (for use when inflating from XML)
@@ -825,6 +833,8 @@ public boolean onSuccess(PublicComment comment) {
825833
).show();
826834

827835
// Add the comment to the tree, display immediately
836+
// Note: For LiveChatView, comments will be added at the bottom because
837+
// we've set the sort direction to OLDEST_FIRST in setSDK()
828838
sdk.addComment(comment, true);
829839

830840
// Re-enable auto-scroll and scroll to the bottom to show the new comment

0 commit comments

Comments
 (0)