@@ -34,13 +34,28 @@ class AdamantChatsProvider: ChatsProvider {
34
34
// MARK: Lifecycle
35
35
init ( ) {
36
36
NotificationCenter . default. addObserver ( forName: Notification . Name. adamantUserLoggedIn, object: nil , queue: nil ) { [ weak self] notification in
37
- self ? . update ( )
37
+ guard let store = self ? . securedStore else {
38
+ return
39
+ }
40
+
41
+ guard let loggedAddress = notification. userInfo ? [ AdamantUserInfoKey . AccountService. loggedAccountAddress] as? String else {
42
+ store. remove ( StoreKey . chatProvider. address)
43
+ store. remove ( StoreKey . chatProvider. receivedLastHeight)
44
+ store. remove ( StoreKey . chatProvider. readedLastHeight)
45
+ return
46
+ }
38
47
39
- if let address = notification. userInfo ? [ AdamantUserInfoKey . AccountService. loggedAccountAddress] as? String {
40
- self ? . securedStore. set ( address, for: StoreKey . chatProvider. address)
48
+ if let savedAddress = self ? . securedStore. get ( StoreKey . chatProvider. address) , savedAddress == loggedAddress {
49
+ if let raw = store. get ( StoreKey . chatProvider. readedLastHeight) , let h = Int64 ( raw) {
50
+ self ? . readedLastHeight = h
51
+ }
41
52
} else {
42
- print ( " Can't get logged address. " )
53
+ store. remove ( StoreKey . chatProvider. receivedLastHeight)
54
+ store. remove ( StoreKey . chatProvider. readedLastHeight)
55
+ store. set ( loggedAddress, for: StoreKey . chatProvider. address)
43
56
}
57
+
58
+ self ? . update ( )
44
59
}
45
60
46
61
NotificationCenter . default. addObserver ( forName: Notification . Name. adamantUserLoggedOut, object: nil , queue: nil ) { [ weak self] _ in
@@ -100,8 +115,11 @@ extension AdamantChatsProvider {
100
115
private func reset( notify: Bool ) {
101
116
let prevState = self . state
102
117
setState ( . updating, previous: prevState, notify: false ) // Block update calls
118
+
103
119
receivedLastHeight = nil
104
120
readedLastHeight = nil
121
+ securedStore. remove ( StoreKey . chatProvider. receivedLastHeight)
122
+ securedStore. remove ( StoreKey . chatProvider. readedLastHeight)
105
123
106
124
let chatrooms = NSFetchRequest < Chatroom > ( entityName: Chatroom . entityName)
107
125
let context = NSManagedObjectContext ( concurrencyType: . privateQueueConcurrencyType)
@@ -170,10 +188,20 @@ extension AdamantChatsProvider {
170
188
userInfo: [ AdamantUserInfoKey . ChatProvider. lastMessageHeight: h] )
171
189
}
172
190
173
- self ? . readedLastHeight = self ? . receivedLastHeight
191
+ if let h = self ? . receivedLastHeight {
192
+ self ? . readedLastHeight = h
193
+ } else {
194
+ self ? . readedLastHeight = 0
195
+ }
174
196
175
- if let h = self ? . receivedLastHeight, let store = self ? . securedStore {
176
- store. set ( String ( h) , for: StoreKey . chatProvider. receivedLastHeight)
197
+ if let store = self ? . securedStore {
198
+ if let h = self ? . receivedLastHeight {
199
+ store. set ( String ( h) , for: StoreKey . chatProvider. receivedLastHeight)
200
+ }
201
+
202
+ if let h = self ? . readedLastHeight, h > 0 {
203
+ store. set ( String ( h) , for: StoreKey . chatProvider. readedLastHeight)
204
+ }
177
205
}
178
206
}
179
207
}
@@ -344,38 +372,26 @@ extension AdamantChatsProvider {
344
372
345
373
// MARK: - Getting messages
346
374
extension AdamantChatsProvider {
347
- func getChatroomsController( ) -> NSFetchedResultsController < Chatroom > ? {
375
+ func getChatroomsController( ) -> NSFetchedResultsController < Chatroom > {
348
376
let request : NSFetchRequest < Chatroom > = NSFetchRequest ( entityName: Chatroom . entityName)
349
377
request. sortDescriptors = [ NSSortDescriptor ( key: " updatedAt " , ascending: false ) ]
350
378
request. predicate = NSPredicate ( format: " partner!=nil " )
351
379
let controller = NSFetchedResultsController ( fetchRequest: request, managedObjectContext: stack. container. viewContext, sectionNameKeyPath: nil , cacheName: nil )
352
380
353
- do {
354
- try controller. performFetch ( )
355
- return controller
356
- } catch {
357
- print ( " Error fetching request: \( error) " )
358
- return nil
359
- }
381
+ return controller
360
382
}
361
383
362
- func getChatController( for chatroom: Chatroom ) -> NSFetchedResultsController < ChatTransaction > ? {
384
+ func getChatController( for chatroom: Chatroom ) -> NSFetchedResultsController < ChatTransaction > {
363
385
guard let context = chatroom. managedObjectContext else {
364
- return nil
386
+ fatalError ( )
365
387
}
366
388
367
389
let request : NSFetchRequest < ChatTransaction > = NSFetchRequest ( entityName: ChatTransaction . entityName)
368
390
request. predicate = NSPredicate ( format: " chatroom = %@ " , chatroom)
369
391
request. sortDescriptors = [ NSSortDescriptor ( key: " date " , ascending: true ) ]
370
392
let controller = NSFetchedResultsController ( fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: nil , cacheName: nil )
371
393
372
- do {
373
- try controller. performFetch ( )
374
- return controller
375
- } catch {
376
- print ( " Error fetching request: \( error) " )
377
- return nil
378
- }
394
+ return controller
379
395
}
380
396
381
397
func chatroomWith( _ account: CoreDataAccount ) -> Chatroom {
@@ -400,21 +416,14 @@ extension AdamantChatsProvider {
400
416
return chatroom
401
417
}
402
418
403
- func getUnreadMessagesController( ) -> NSFetchedResultsController < ChatTransaction > ? {
419
+ func getUnreadMessagesController( ) -> NSFetchedResultsController < ChatTransaction > {
404
420
let request = NSFetchRequest < ChatTransaction > ( entityName: ChatTransaction . entityName)
405
421
request. predicate = NSPredicate ( format: " isUnread == true " )
406
422
request. sortDescriptors = [ NSSortDescriptor . init ( key: " date " , ascending: false ) ]
407
423
408
424
let controller = NSFetchedResultsController ( fetchRequest: request, managedObjectContext: stack. container. viewContext, sectionNameKeyPath: " chatroom.partner.address " , cacheName: nil )
409
- controller. section
410
425
411
- do {
412
- try controller. performFetch ( )
413
- return controller
414
- } catch {
415
- print ( " Error fetching unread messages: \( error) " )
416
- return nil
417
- }
426
+ return controller
418
427
}
419
428
}
420
429
@@ -633,12 +642,6 @@ extension AdamantChatsProvider {
633
642
chatroom. hasUnreadMessages = true
634
643
trs. forEach ( { $0. isUnread = true } )
635
644
}
636
- } else {
637
- let msgs = Dictionary ( grouping: newChatTransactions, by: ( { ( t: ChatTransaction ) -> Chatroom in t. chatroom!} ) )
638
- for (chatroom, trs) in msgs {
639
- chatroom. hasUnreadMessages = true
640
- trs. forEach ( { $0. isUnread = true } )
641
- }
642
645
}
643
646
644
647
0 commit comments