@@ -264,8 +264,8 @@ public class MXThreadingService: NSObject {
264264 }
265265
266266 private func thread( forRootEvent rootEvent: MXEvent , session: MXSession ) -> MXThreadModel {
267- let notificationCount : UInt
268- let highlightCount : UInt
267+ var notificationCount : UInt = 0
268+ var highlightCount : UInt = 0
269269 if let store = session. store {
270270 notificationCount = store. localUnreadEventCount ( rootEvent. roomId,
271271 threadId: rootEvent. eventId,
@@ -274,9 +274,10 @@ public class MXThreadingService: NSObject {
274274 threadId: rootEvent. eventId,
275275 withTypeIn: session. unreadEventTypes)
276276 highlightCount = UInt ( newEvents. filter { $0. shouldBeHighlighted ( inSession: session) } . count)
277- } else {
278- notificationCount = 0
279- highlightCount = 0
277+ }
278+ if let localThread = thread ( withId: rootEvent. eventId) {
279+ notificationCount = max ( notificationCount, localThread. notificationCount)
280+ highlightCount = max ( highlightCount, localThread. highlightCount)
280281 }
281282 let thread = MXThreadModel ( withRootEvent: rootEvent,
282283 notificationCount: notificationCount,
@@ -303,14 +304,17 @@ public class MXThreadingService: NSObject {
303304 completion ? ( handled)
304305 } else {
305306 // create the thread for the first time
306- var thread : MXThread = MXThread ( withSession: session, identifier: threadId, roomId: event. roomId)
307+ let thread = MXThread ( withSession: session, identifier: threadId, roomId: event. roomId)
308+ self . saveThread ( thread)
309+ self . notifyDidCreateThread ( thread, direction: direction)
310+ self . notifyDidUpdateThreads ( )
307311 let dispatchGroup = DispatchGroup ( )
308312 // try to find the root event in the session store
309313 dispatchGroup. enter ( )
310314 session. event ( withEventId: threadId, inRoom: event. roomId) { response in
311315 switch response {
312316 case . success( let rootEvent) :
313- thread = MXThread ( withSession : session , rootEvent : rootEvent )
317+ thread. addEvent ( rootEvent , direction : direction )
314318 case . failure( let error) :
315319 MXLog . error ( " [MXThreadingService] handleInThreadEvent: root event not found: \( error) " )
316320 }
@@ -319,8 +323,6 @@ public class MXThreadingService: NSObject {
319323
320324 dispatchGroup. notify ( queue: . main) {
321325 let handled = thread. addEvent ( event, direction: direction)
322- self . saveThread ( thread)
323- self . notifyDidCreateThread ( thread, direction: direction)
324326 self . notifyDidUpdateThreads ( )
325327 completion ? ( handled)
326328 }
0 commit comments