cheogram-aapl
created pr with
cheogram-aapl/jump-to-unread.1
commented
lgtm
cmds
checkout latest patchset:
ssh pr.pico.sh pull cheogram-aapl:jump-to-unread | git am -3checkout specific patchset revision:
ssh pr.pico.sh pull cheogram-aapl:jump-to-unread [rev] | git am -3add changes to patch request:
git format-patch main --stdout | ssh pr.pico.sh cheogram-aapl:jump-to-unreadadd comment to patch request:
ssh pr.pico.sh comment cheogram-aapl:jump-to-unread "lgtm!"
Patchset
cheogram-aapl/jump-to-unread.1
Update to the latest ExyteChat (fork)
Stephen Paul Weber
→ Open at the first unread message and mark read only what is visible
2026-11-16T20:04:24ZStephen Paul Weber
Use canReplace
2026-11-16T20:05:22ZStephen Paul Weber
Add temporary XYZZY probes around newer-page fetches
2026-11-16T20:05:22ZStephen Paul Weber
2026-11-16T20:05:22Z
Open at the first unread message and mark read only what is visible
Stephen Paul Weber
2026-11-16T20:05:22ZDiff stat
2 files changed, 167 insertions(+), 40 deletions(-)
Semantic diff summary
0 added,
0 modified,
0 signature changed,
0 removed
across 0 analyzed files
(2 files skipped: unsupported file type)
+151
-24
App/App.swift
#
| ... | ... | @@ -291,13 +291,24 @@ class ChatViewModel { | |
| 291 | 291 | var messageData: [String: Borogove.ChatMessage] = [:] | |
| 292 | 292 | var messages: [Message] = [] | |
| 293 | 293 | var chats: ChatsModel | |
| 294 | + | var scrollToParams: ScrollToParams? = nil | |
| 295 | + | var isCaughtUpToNewest = true | |
| 296 | + | private var readBoundaryServerId: String? = nil | |
| 297 | + | private var markReadTask: Task<Void, Never>? = nil | |
| 298 | + | private var loadingChatId: String? = nil | |
| 294 | 299 | var chat: Borogove.Chat? { | |
| 295 | 300 | didSet { | |
| 296 | 301 | if oldValue?.chatId == chat?.chatId { return } | |
| 297 | 302 | messages = [] | |
| 298 | 303 | messageData = [:] | |
| 304 | + | scrollToParams = nil | |
| 305 | + | isCaughtUpToNewest = true | |
| 306 | + | readBoundaryServerId = nil | |
| 307 | + | markReadTask?.cancel() | |
| 308 | + | markReadTask = nil | |
| 309 | + | loadingChatId = nil | |
| 299 | 310 | if chat != nil { | |
| 300 | - | loadMessages() | |
| 311 | + | Task { await loadMessages() } | |
| 301 | 312 | } | |
| 302 | 313 | } | |
| 303 | 314 | } |
| ... | ... | @@ -380,7 +391,10 @@ class ChatViewModel { | |
| 380 | 391 | return Message( | |
| 381 | 392 | id: useId, | |
| 382 | 393 | user: await user(chat, message.senderId, message.senderMemberStub()), | |
| 383 | - | status: Self.mapStatus(message.status), | |
| 394 | + | status: message.serverId != nil | |
| 395 | + | && message.serverId == readBoundaryServerId | |
| 396 | + | && message.isIncoming() | |
| 397 | + | ? .readBy([]) : Self.mapStatus(message.status), | |
| 384 | 398 | createdAt: parseDate(date: message.timeSent)!, | |
| 385 | 399 | attributedText: styledText, | |
| 386 | 400 | attachments: otherAttachments.map { |
| ... | ... | @@ -420,31 +434,143 @@ class ChatViewModel { | |
| 420 | 434 | } | |
| 421 | 435 | } | |
| 422 | 436 | ||
| 423 | - | func loadMessages() { | |
| 424 | - | loadMessagesBefore(message: nil) | |
| 425 | - | } | |
| 437 | + | func loadMessages() async { | |
| 438 | + | guard let chat else { return } | |
| 439 | + | let chatId = chat.chatId | |
| 440 | + | guard loadingChatId != chatId else { return } | |
| 441 | + | loadingChatId = chatId | |
| 442 | + | defer { | |
| 443 | + | if loadingChatId == chatId { loadingChatId = nil } | |
| 444 | + | } | |
| 445 | + | ||
| 446 | + | let readUpTo = await chat.readUpTo() | |
| 447 | + | let marker: Borogove.ChatMessage? | |
| 448 | + | let around: [Borogove.ChatMessage] | |
| 449 | + | if let readUpTo, chat.unreadCount() > 0 { | |
| 450 | + | readBoundaryServerId = readUpTo.serverId | |
| 451 | + | marker = readUpTo | |
| 452 | + | around = await chat.getMessagesAround(around: readUpTo) | |
| 453 | + | } else { | |
| 454 | + | readBoundaryServerId = nil | |
| 455 | + | marker = nil | |
| 456 | + | around = await chat.getMessagesBefore(before: nil) | |
| 457 | + | } | |
| 426 | 458 | ||
| 427 | - | func loadMessagesBefore(message: Message?) { | |
| 428 | - | Task { | |
| 429 | - | let idAtStart = chat?.chatId | |
| 430 | - | let messages = await chat?.getMessagesBefore( | |
| 431 | - | before: message == nil ? nil : messageData[message!.id] | |
| 459 | + | let built = await buildMessages(chat, around) | |
| 460 | + | guard self.chat?.chatId == chatId else { return } | |
| 461 | + | messages = built | |
| 462 | + | refreshIsCaughtUpToNewest(in: chat) | |
| 463 | + | ||
| 464 | + | if let marker, let index = around.firstIndex(where: { $0.serverId == marker.serverId }), index + 1 < around.count { | |
| 465 | + | scrollToParams = ScrollToParams( | |
| 466 | + | messageID: built[index+1].id, | |
| 467 | + | position: .middle | |
| 432 | 468 | ) | |
| 433 | - | if let chat = self.chat { | |
| 434 | - | var toPrepend: [Message] = [] | |
| 469 | + | } else { | |
| 470 | + | scrollToParams = nil | |
| 471 | + | } | |
| 472 | + | } | |
| 435 | 473 | ||
| 436 | - | for message in (messages ?? []).reversed() { | |
| 437 | - | if self.messageIndex(for: message) == nil { | |
| 438 | - | toPrepend.insert(await self.mkMessage(chat, message), at: 0) | |
| 439 | - | } | |
| 440 | - | } | |
| 441 | - | await MainActor.run { | |
| 442 | - | if self.chat?.chatId == idAtStart { | |
| 443 | - | self.messages.insert(contentsOf: toPrepend, at: 0) | |
| 444 | - | } | |
| 445 | - | } | |
| 474 | + | private func buildMessages( | |
| 475 | + | _ chat: Borogove.Chat, | |
| 476 | + | _ chatMessages: [Borogove.ChatMessage], | |
| 477 | + | onlyNew: Bool = false | |
| 478 | + | ) async -> [Message] { | |
| 479 | + | var built: [Message] = [] | |
| 480 | + | for message in chatMessages { | |
| 481 | + | if let index = messageIndex(for: message) { | |
| 482 | + | if !onlyNew { built.append(messages[index]) } | |
| 483 | + | } else { | |
| 484 | + | built.append(await mkMessage(chat, message)) | |
| 485 | + | } | |
| 486 | + | } | |
| 487 | + | return built | |
| 488 | + | } | |
| 489 | + | ||
| 490 | + | private func isNewest( | |
| 491 | + | _ message: Borogove.ChatMessage, | |
| 492 | + | in chat: Borogove.Chat | |
| 493 | + | ) -> Bool { | |
| 494 | + | guard let last = chat.lastMessage else { return true } | |
| 495 | + | if let serverID = message.serverId, let lastServerID = last.serverId { | |
| 496 | + | return serverID == lastServerID | |
| 497 | + | } | |
| 498 | + | if let localID = message.localId, let lastLocalID = last.localId { | |
| 499 | + | return localID == lastLocalID | |
| 500 | + | } | |
| 501 | + | return false | |
| 502 | + | } | |
| 503 | + | ||
| 504 | + | func markRead(upTo message: Message) { | |
| 505 | + | guard let chat, messageData[message.id] != nil else { return } | |
| 506 | + | // Debounce | |
| 507 | + | markReadTask?.cancel() | |
| 508 | + | markReadTask = Task { [weak self] in | |
| 509 | + | try? await Task.sleep(for: .milliseconds(300)) | |
| 510 | + | guard !Task.isCancelled, let self, | |
| 511 | + | self.chat?.chatId == chat.chatId, | |
| 512 | + | let chatMessage = self.messageData[message.id] | |
| 513 | + | else { | |
| 514 | + | return | |
| 446 | 515 | } | |
| 516 | + | chat.markReadUpTo(message: chatMessage) | |
| 517 | + | } | |
| 518 | + | } | |
| 519 | + | ||
| 520 | + | func loadMessagesBefore(message: Message?) async { | |
| 521 | + | guard let chat else { return } | |
| 522 | + | let fetched = await chat.getMessagesBefore( | |
| 523 | + | before: message.flatMap { messageData[$0.id] } | |
| 524 | + | ) | |
| 525 | + | await insertPage(fetched, older: true, of: chat) | |
| 526 | + | } | |
| 527 | + | ||
| 528 | + | func loadMessagesAfter(message: Message?) async { | |
| 529 | + | guard let chat, let message, let after = messageData[message.id] else { | |
| 530 | + | return | |
| 531 | + | } | |
| 532 | + | let fetched = await chat.getMessagesAfter(after: after) | |
| 533 | + | await insertPage(fetched, older: false, of: chat) | |
| 534 | + | } | |
| 535 | + | ||
| 536 | + | private func insertPage( | |
| 537 | + | _ fetched: [Borogove.ChatMessage], | |
| 538 | + | older: Bool, | |
| 539 | + | of chat: Borogove.Chat | |
| 540 | + | ) async { | |
| 541 | + | let chatId = chat.chatId | |
| 542 | + | let toInsert = await buildMessages(chat, fetched, onlyNew: true) | |
| 543 | + | guard self.chat?.chatId == chatId else { return } | |
| 544 | + | if older { | |
| 545 | + | messages.insert(contentsOf: toInsert, at: 0) | |
| 546 | + | } else { | |
| 547 | + | messages.append(contentsOf: toInsert) | |
| 548 | + | refreshIsCaughtUpToNewest(in: chat, exhausted: fetched.isEmpty) | |
| 549 | + | } | |
| 550 | + | } | |
| 551 | + | ||
| 552 | + | private func refreshIsCaughtUpToNewest( | |
| 553 | + | in chat: Borogove.Chat, | |
| 554 | + | exhausted: Bool = false | |
| 555 | + | ) { | |
| 556 | + | isCaughtUpToNewest = exhausted | |
| 557 | + | || messages.last | |
| 558 | + | .flatMap { messageData[$0.id] } | |
| 559 | + | .map { isNewest($0, in: chat) } ?? true | |
| 560 | + | } | |
| 561 | + | ||
| 562 | + | func jumpToNewest() async { | |
| 563 | + | guard let chat else { return } | |
| 564 | + | let chatId = chat.chatId | |
| 565 | + | var previousLastID: String? = nil | |
| 566 | + | while !isCaughtUpToNewest, let last = messages.last, | |
| 567 | + | last.id != previousLastID | |
| 568 | + | { | |
| 569 | + | previousLastID = last.id | |
| 570 | + | await loadMessagesAfter(message: last) | |
| 571 | + | guard self.chat?.chatId == chatId else { return } | |
| 447 | 572 | } | |
| 573 | + | scrollToParams = ScrollToParams(.newestMessage) | |
| 448 | 574 | } | |
| 449 | 575 | } | |
| 450 | 576 |
| ... | ... | @@ -529,7 +655,8 @@ class ChatsModel { | |
| 529 | 655 | #endif | |
| 530 | 656 | let _ = client.addStatusOnlineListener { | |
| 531 | 657 | Task { @MainActor in | |
| 532 | - | self.selectedChat?.loadMessages() | |
| 658 | + | self.selectedChat?.messages = [] | |
| 659 | + | await self.selectedChat?.loadMessages() | |
| 533 | 660 | self.online = true | |
| 534 | 661 | self.isLoading = false | |
| 535 | 662 | } |
| ... | ... | @@ -608,7 +735,7 @@ class ChatsModel { | |
| 608 | 735 | if let index = self.selectedChat!.messageIndex(for: message) { | |
| 609 | 736 | let msg = await self.selectedChat!.mkMessage(chat, message, existingId: self.selectedChat!.messages[index].id) | |
| 610 | 737 | self.selectedChat!.messages[index] = msg | |
| 611 | - | } else { | |
| 738 | + | } else if self.selectedChat!.isCaughtUpToNewest { | |
| 612 | 739 | let msg = await self.selectedChat!.mkMessage(chat, message) | |
| 613 | 740 | self.selectedChat!.messages.append(msg) | |
| 614 | 741 | } |
+16
-16
App/ChatDetailView.swift
#
| ... | ... | @@ -79,7 +79,7 @@ struct ChatDetailView: View { | |
| 79 | 79 | @State var moderationMessage: ChatMessage? = nil | |
| 80 | 80 | @ScaledMetric(relativeTo: .largeTitle) var profileAvatarSize: CGFloat = 160 | |
| 81 | 81 | ||
| 82 | - | private var chatList: some View { | |
| 82 | + | var body: some View { | |
| 83 | 83 | ChatView(messages: viewModel.messages) { toSend in | |
| 84 | 84 | Task { | |
| 85 | 85 | let builder = |
| ... | ... | @@ -271,8 +274,18 @@ struct ChatDetailView: View { | |
| 271 | 274 | } | |
| 272 | 275 | } | |
| 273 | 276 | .linkPreviewsDisabled() | |
| 277 | + | .scrollTo(viewModel.scrollToParams) | |
| 278 | + | .showLastReadIndicator(true) | |
| 279 | + | .onWillDisplayCell { message in | |
| 280 | + | viewModel.markRead(upTo: message) | |
| 281 | + | } | |
| 282 | + | .enableLoadMoreNewerMessages( | |
| 283 | + | hasMoreToLoad: !viewModel.isCaughtUpToNewest | |
| 284 | + | ) { | |
| 285 | + | await viewModel.loadMessagesAfter(message: viewModel.messages.last) | |
| 286 | + | } | |
| 274 | 287 | .enableLoadMoreOlderMessages { | |
| 275 | - | viewModel.loadMessagesBefore(message: viewModel.messages.first) | |
| 288 | + | await viewModel.loadMessagesBefore(message: viewModel.messages.first) | |
| 276 | 289 | }.setRecorderSettings( | |
| 277 | 290 | RecorderSettings( | |
| 278 | 291 | audioFormatID: kAudioFormatOpus, |
| ... | ... | @@ -399,19 +412,6 @@ struct ChatDetailView: View { | |
| 399 | 412 | } | |
| 400 | 413 | moderationMessage = nil | |
| 401 | 414 | } | |
| 402 | - | } | |
| 403 | - | } | |
| 404 | - | ||
| 405 | - | var body: some View { | |
| 406 | - | chatList | |
| 407 | - | .onChange(of: viewModel.chat?.chatId) { _, chatId in | |
| 408 | - | currentThread = nil | |
| 409 | - | }.onChange(of: viewModel.messages) { _, _ in | |
| 410 | - | if let lastM = viewModel.messages.last, | |
| 411 | - | let last = viewModel.messageData[lastM.id] | |
| 412 | - | { | |
| 413 | - | viewModel.chat?.markReadUpTo(message: last) | |
| 414 | - | } | |
| 415 | - | } | |
| 415 | + | }.id(viewModel.chat?.chatId) | |
| 416 | 416 | } | |
| 417 | 417 | } |