Skip to content

Commit 2aa72c4

Browse files
committed
Merge branch 'trello.com/c/aZtAfZ44' into release/3.11.0
2 parents 9315df9 + a03d6c6 commit 2aa72c4

File tree

8 files changed

+69
-41
lines changed

8 files changed

+69
-41
lines changed

Adamant/Helpers/UserDefaultsManager.swift

Lines changed: 0 additions & 17 deletions
This file was deleted.

Adamant/Models/CoreData/Chatroom+CoreDataClass.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ public class Chatroom: NSManagedObject, @unchecked Sendable {
1616

1717
func markAsReaded() {
1818
hasUnreadMessages = false
19-
19+
2020
if let trs = transactions as? Set<ChatTransaction> {
21-
trs.filter { $0.isUnread }.forEach { $0.isUnread = false }
21+
trs.filter { $0.isUnread }.forEach {
22+
$0.isUnread = false
23+
}
2224
}
2325
}
2426

@@ -29,8 +31,11 @@ public class Chatroom: NSManagedObject, @unchecked Sendable {
2931
return
3032
}
3133
message.isUnread = false
32-
33-
message.richMessageTransactions?.forEach { $0.isUnread = false }
34+
35+
36+
message.richMessageTransactions?.forEach {
37+
$0.isUnread = false
38+
}
3439

3540
if let context = message.managedObjectContext, context.hasChanges {
3641
try? context.save()

Adamant/Modules/Chat/ViewModel/ChatViewModel.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -556,14 +556,13 @@ final class ChatViewModel: NSObject {
556556
recipient: partnerAddress
557557
)
558558
}
559+
if let chatroom {
560+
await chatsProvider.markMessageAsRead(chatroom: chatroom, message: messageId)
561+
}
559562

560563
await waitForMessage(withId: messageId)
561564
scrollToId = messageId
562-
563565
dialog.send(.progress(false))
564-
if let chatroom {
565-
await chatsProvider.markMessageAsRead(chatroom: chatroom, message: messageId)
566-
}
567566
} catch {
568567
print(error)
569568
dialog.send(.progress(false))

Adamant/Services/DataProviders/AdamantChatsProvider.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ extension AdamantChatsProvider {
323323
SecureStore.remove(StoreKey.chatProvider.receivedLastHeight)
324324
SecureStore.remove(StoreKey.chatProvider.readedLastHeight)
325325
SecureStore.remove(StoreKey.chatProvider.markedChatsAsUnread)
326+
UserDefaultsManager.lastReceivedId = nil
326327

327328
// Set State
328329
setState(.empty, previous: prevState, notify: notify)
@@ -1922,6 +1923,9 @@ extension AdamantChatsProvider {
19221923
if unreadTransactions.count == 0 {
19231924
unreadTransactions = newMessageTransactions.filter { $0.height == 0 }
19241925
}
1926+
unreadTransactions.forEach {
1927+
UserDefaultsManager.addLastReceivedId($0.transactionId)
1928+
}
19251929
let chatrooms = Dictionary(grouping: unreadTransactions, by: ({ (t: ChatTransaction) -> Chatroom in t.chatroom! }))
19261930
for (chatroom, trs) in chatrooms {
19271931
if let address = chatroom.partner?.address {

CommonKit/Sources/CommonKit/Core/UserDefaultsKeys.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ public enum UserDefaultsKey: String {
1010

1111
case leftSplitViewController
1212
case rightSplitViewController
13+
case lastReceivedId
1314
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// UserDefaultsManager.swift
3+
// CommonKit
4+
//
5+
// Created by Владимир Клевцов on 2. 5. 2025..
6+
//
7+
public struct UserDefaultsManager {
8+
@UserDefaultsStorage(.needsToShowNoActiveNodesAlert)
9+
static var needsToShowNoActiveNodesAlert: Bool?
10+
11+
@UserDefaultsStorage(.lastReceivedId)
12+
public static var lastReceivedId: [String]?
13+
14+
public static func setInitialUserDefaults() {
15+
needsToShowNoActiveNodesAlert = true
16+
}
17+
}
18+
19+
public extension UserDefaultsManager {
20+
static func addLastReceivedId(_ id: String) {
21+
var ids = lastReceivedId ?? []
22+
ids.removeAll { $0 == id }
23+
ids.insert(id, at: 0)
24+
if ids.count > 100 {
25+
ids = Array(ids.prefix(100))
26+
}
27+
lastReceivedId = ids
28+
}
29+
}

CommonKit/Sources/CommonKit/Core/UserDefaultsWrapper.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import Foundation
99

1010
@propertyWrapper
1111
public struct UserDefaultsStorage<T> {
12-
private let defaults = UserDefaults.standard
12+
//user group.adamant.adamant-messenger to share UserDefaults Data to NotificationService and another app targets
13+
private let defaults = UserDefaults(suiteName: "group.adamant.adamant-messenger") ?? UserDefaults.standard
1314
private let key: String
1415

1516
public var wrappedValue: T? {

NotificationServiceExtension/NotificationService.swift

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ class NotificationService: UNNotificationServiceExtension {
4343
// MARK: - Hanlder
4444
var contentHandler: ((UNNotificationContent) -> Void)?
4545
var bestAttemptContent: UNMutableNotificationContent?
46+
var shouldIgnoreNotification = false
47+
4648

4749
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
50+
shouldIgnoreNotification = false
4851
AdamantUtilities.consoleLog(
4952
"Push notification received",
5053
request.content.userInfo.debugDescription,
@@ -79,6 +82,12 @@ class NotificationService: UNNotificationServiceExtension {
7982
contentHandler(bestAttemptContent)
8083
return
8184
}
85+
let lastReadIds = UserDefaultsManager.lastReceivedId
86+
87+
if let lastReadIds,
88+
lastReadIds.contains(String(transaction.id)) {
89+
shouldIgnoreNotification = true
90+
}
8291

8392
// MARK: 3. Working on transaction
8493
let partnerAddress: String
@@ -107,9 +116,7 @@ class NotificationService: UNNotificationServiceExtension {
107116
partnerName = partnerAddress.checkAndReplaceSystemWallets()
108117
bestAttemptContent.userInfo[AdamantNotificationUserInfoKeys.partnerNoDislpayNameKey] = AdamantNotificationUserInfoKeys.partnerNoDisplayNameValue
109118
}
110-
111-
var shouldIgnoreNotification = false
112-
119+
113120
var isReaction = false
114121

115122
// MARK: 5. Content
@@ -313,11 +320,6 @@ class NotificationService: UNNotificationServiceExtension {
313320
break
314321
}
315322

316-
guard !shouldIgnoreNotification else {
317-
contentHandler(UNNotificationContent())
318-
return
319-
}
320-
321323
bestAttemptContent.sound = getSound(
322324
SecureStore: SecureStore,
323325
isReaction: isReaction
@@ -330,21 +332,25 @@ class NotificationService: UNNotificationServiceExtension {
330332
if let data = try? JSONEncoder().encode(transaction), let transactionRaw = String(data: data, encoding: .utf8) {
331333
bestAttemptContent.userInfo[AdamantNotificationUserInfoKeys.transaction] = transactionRaw
332334
}
333-
bestAttemptContent.userInfo[AdamantNotificationUserInfoKeys.decodedMessage] = decodedMessage
334335

335-
// MARK: 8 Set current budge
336-
let storedBadgeString: String? = SecureStore.get(StoreKey.notificationsService.customBadgeNumber)
337-
let currentBadge = (Int(storedBadgeString ?? "0") ?? 0) + 1
338-
bestAttemptContent.badge = NSNumber(value: currentBadge)
339-
SecureStore.set(String(currentBadge), for: StoreKey.notificationsService.customBadgeNumber)
336+
// MARK: 8 Set current budge and Message
337+
var badgeValue = (Int(SecureStore.get(StoreKey.notificationsService.customBadgeNumber) ?? "0") ?? 0)
338+
if !shouldIgnoreNotification {
339+
badgeValue += 1
340+
bestAttemptContent.userInfo[AdamantNotificationUserInfoKeys.decodedMessage] = decodedMessage
341+
}
342+
343+
bestAttemptContent.badge = NSNumber(value: badgeValue)
344+
SecureStore.set(String(badgeValue), for: StoreKey.notificationsService.customBadgeNumber)
340345

341346
contentHandler(bestAttemptContent)
342347
}
343348

344349
override func serviceExtensionTimeWillExpire() {
345350
// Called just before the extension will be terminated by the system.
346351
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
347-
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
352+
if let contentHandler = contentHandler,
353+
let bestAttemptContent = bestAttemptContent {
348354
contentHandler(bestAttemptContent)
349355
}
350356
}

0 commit comments

Comments
 (0)