Skip to content

Commit 7d39a1e

Browse files
authored
Keep the title and zero message count when a saved chat becomes empty (#133)
* fix(ChatUI): preserve empty Saved Messages chat title and keep chat open * fix(ChatUI): distinguish saved chat loading and deletion * fix(ChatUI): use consistent saved-message count labels * fix(ChatUI): close explicitly deleted saved chats across account windows
1 parent 4e534e1 commit 7d39a1e

6 files changed

Lines changed: 546 additions & 13 deletions

File tree

���packages/ChatUI/Sources/ChatUI/ChatController.swift‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3912,13 +3912,8 @@ open class ChatController<Context: AccountContextProtocol>: TelegramGenericViewC
39123912
let countViewKey: PostboxViewKey = .historyTagSummaryView(tag: MessageTags(), peerId: peerId, threadId: savedMessagesPeerId.toInt64(), namespace: Namespaces.Message.Cloud, customTag: nil)
39133913
answersCount = context.account.postbox.combinedView(keys: [basicPeerKey, countViewKey])
39143914
|> map { views -> Int32? in
3915-
3916-
var messageCount = 0
3917-
if let summaryView = views.views[countViewKey] as? MessageHistoryTagSummaryView, let count = summaryView.count {
3918-
messageCount += Int(count)
3919-
}
3920-
3921-
return Int32(messageCount)
3915+
let count = (views.views[countViewKey] as? MessageHistoryTagSummaryView)?.count
3916+
return mode.isSavedMessagesThread ? count : count ?? 0
39223917
}
39233918

39243919
} else if isThread {
@@ -11367,6 +11362,9 @@ open class ChatController<Context: AccountContextProtocol>: TelegramGenericViewC
1136711362
return takeTableItem?(msgId)
1136811363
})
1136911364
super.init(context)
11365+
if mode.isSavedMessagesThread {
11366+
SavedChatControllerRegistry.shared.add(self)
11367+
}
1137011368

1137111369
self.bar = .init(height: 0)
1137211370
self.insets = NSEdgeInsets()
@@ -11802,7 +11800,7 @@ open class ChatController<Context: AccountContextProtocol>: TelegramGenericViewC
1180211800
}
1180311801

1180411802
if let peer = value.mainPeer, let oldPeer = oldValue.mainPeer {
11805-
if TelegramUtilsHooks.shared.peerIsForum(peer) && !TelegramUtilsHooks.shared.peerIsForum(oldPeer) || (oldValue.threadInfo != nil && value.threadInfo == nil && !peer.displayForumAsTabs(splitState: context.layout)), !peer.isMonoForum {
11803+
if TelegramUtilsHooks.shared.peerIsForum(peer) && !TelegramUtilsHooks.shared.peerIsForum(oldPeer) || (!self.mode.isSavedMessagesThread && oldValue.threadInfo != nil && value.threadInfo == nil && !peer.displayForumAsTabs(splitState: context.layout)), !peer.isMonoForum {
1180611804
if (self.navigationController?.stackCount ?? 0) > 1 {
1180711805
self.navigationController?.removeImmediately(self)
1180811806
} else if self.mode == .preview {

‎packages/ChatUI/Sources/ChatUI/ChatHeaderBubblesView.swift‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -903,10 +903,9 @@ final class ChatHeaderBubblesView: View, InteractionContentViewProtocol {
903903
result = result.withUpdatedTitle(presentation.threadInfo?.info.title ?? "").withUpdatedStatus(strings().peerInfoTopicStatusIn(peer.displayTitle))
904904
}
905905
case .savedMessages:
906-
if let count = counters.replies, count > 0 {
907-
result = result.withUpdatedTitle(peer.displayTitle).withUpdatedStatus(strings().chatTitleTopicOther(Int(count)))
908-
} else {
909-
result = result.withUpdatedTitle(presentation.threadInfo?.info.title ?? "").withUpdatedStatus(strings().peerInfoTopicStatusIn(peer.displayTitle))
906+
result = result.withUpdatedTitle(peer.displayTitle).withUpdatedStatus("")
907+
if let count = counters.replies {
908+
result = result.withUpdatedStatus(strings().sharedMediaSavedCountOther(Int(count)))
910909
}
911910
if PeerId(data.threadId) == chatInteraction.interactionContext.peerId {
912911
result = result.withUpdatedTitle(strings().peerMyNotes)

‎packages/ChatUI/Sources/ChatUI/LeaveChatActions.swift‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,14 +377,19 @@ private func showStandardLeaveDialog(context: any AccountContextProtocol, peer:
377377

378378
let verify = verifyAlertSignal(for: context.window, information: text, ok: okTitle ?? strings().alertOK, option: thridTitle, optionIsSelected: false, completeWhenDismissed: true) |> filter { $0 != nil }
379379

380-
return combineLatest(verify, context.globalPeerHandler.get() |> take(1)) |> map { result, location -> Bool in
380+
return combineLatest(verify, context.globalPeerHandler.get() |> take(1)) |> deliverOnMainQueue |> map { result, location -> Bool in
381381

382382
if let threadId = threadId {
383383
_ = context.engine.peers.removeForumChannelThread(id: peerId, threadId: threadId).start()
384384
} else {
385385
scheduleChatRemoval(context: context, peer: peer, peerId: peerId, deleteGloballyIfPossible: result == .thrid || forceRemoveGlobally, blockBot: peer.isBot && result == .thrid)
386386
}
387387

388+
if let threadId = threadId, peerId == context.peerId {
389+
SavedChatControllerRegistry.shared.remove(accountId: context.account.id, peerId: peerId, threadId: threadId)
390+
return true
391+
}
392+
388393
switch location {
389394
case let .peer(id):
390395
if id == peerId {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import Cocoa
2+
import Postbox
3+
import TelegramCore
4+
import TelegramContext
5+
import TGUIKit
6+
7+
final class SavedChatControllerRegistry {
8+
static let shared = SavedChatControllerRegistry()
9+
private let controllers = NSHashTable<ViewController>.weakObjects()
10+
11+
func add(_ controller: ViewController) {
12+
controllers.add(controller)
13+
}
14+
15+
func remove(accountId: AccountRecordId, peerId: PeerId, threadId: Int64) {
16+
let matches = controllers.allObjects.filter { controller in
17+
guard let controller = controller as? ChatControllerKind else { return false }
18+
let interaction = controller.chatInteraction
19+
return interaction.interactionContext.account.id == accountId
20+
&& interaction.mode.isSavedMessagesThread
21+
&& interaction.chatLocation.peerId == peerId
22+
&& interaction.chatLocation.threadId == threadId
23+
}
24+
for controller in matches {
25+
guard let navigation = controller.navigationController else { continue }
26+
if navigation.empty === controller {
27+
navigation.window?.performClose(nil)
28+
} else {
29+
navigation.removeImmediately(controller)
30+
}
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)