Skip to content

Commit f85870f

Browse files
committed
Save chat position after reloading the app
1 parent f828fca commit f85870f

3 files changed

Lines changed: 29 additions & 10 deletions

File tree

Adamant/Services/DataProviders/AdamantChatsProvider.swift

Lines changed: 23 additions & 8 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.chatPositions = nil
326327
UserDefaultsManager.lastReceivedId = nil
327328

328329
// Set State
@@ -796,16 +797,30 @@ extension AdamantChatsProvider {
796797
return transaction
797798
}
798799

799-
@MainActor func removeChatPosition(for address: String) {
800-
chatPositon.removeValue(forKey: address)
800+
func removeChatPosition(for address: String) {
801+
var positions = UserDefaultsManager.chatPositions ?? [:]
802+
positions.removeValue(forKey: address)
803+
UserDefaultsManager.chatPositions = positions
801804
}
802-
803-
@MainActor func setChatPositon(for address: String, position: CGFloat?, collectionHeight: CGFloat?) {
804-
chatPositon[address] = (position, collectionHeight) as? (CGFloat, CGFloat)
805+
806+
func setChatPositon(for address: String, position: CGFloat?, collectionHeight: CGFloat?) {
807+
var positions = UserDefaultsManager.chatPositions ?? [:]
808+
if let position = position, let collectionHeight = collectionHeight {
809+
positions[address] = [Double(position), Double(collectionHeight)]
810+
} else {
811+
positions.removeValue(forKey: address)
812+
}
813+
UserDefaultsManager.chatPositions = positions
805814
}
806-
807-
@MainActor func getChatPositon(for address: String) -> (CGFloat, CGFloat)? {
808-
return chatPositon[address]
815+
816+
func getChatPositon(for address: String) -> (CGFloat, CGFloat)? {
817+
guard
818+
let values = UserDefaultsManager.chatPositions?[address],
819+
values.count == 2
820+
else {
821+
return nil
822+
}
823+
return (CGFloat(values[0]), CGFloat(values[1]))
809824
}
810825

811826
/// Logic:

CommonKit/Sources/CommonKit/Core/UserDefaultsKeys.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ public enum UserDefaultsKey: String {
1111
case leftSplitViewController
1212
case rightSplitViewController
1313
case lastReceivedId
14+
case chatPositions
1415
}

CommonKit/Sources/CommonKit/Core/UserDefaultsManager.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
public struct UserDefaultsManager {
88
@UserDefaultsStorage(.needsToShowNoActiveNodesAlert)
99
static var needsToShowNoActiveNodesAlert: Bool?
10-
10+
1111
@UserDefaultsStorage(.lastReceivedId)
1212
public static var lastReceivedId: [String]?
13-
13+
14+
@UserDefaultsStorage(.chatPositions)
15+
public static var chatPositions: [String: [Double]]?
16+
1417
public static func setInitialUserDefaults() {
1518
needsToShowNoActiveNodesAlert = true
1619
}

0 commit comments

Comments
 (0)