Skip to content

Commit 8c217a0

Browse files
Merge pull request #103 from Adamant-im/feature/push_message_showing_fix
Fixed showing message in push notification
2 parents e5b2f16 + 96d4757 commit 8c217a0

File tree

11 files changed

+184
-123
lines changed

11 files changed

+184
-123
lines changed

Adamant.xcodeproj/project.pbxproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
55D1D851287B78FC00F94A4E /* SnapKit in Frameworks */ = {isa = PBXBuildFile; productRef = 55D1D850287B78FC00F94A4E /* SnapKit */; };
3636
55D1D855287B890300F94A4E /* AddressGeneratorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55D1D854287B890300F94A4E /* AddressGeneratorTests.swift */; };
3737
55E69E172868D7920025D82E /* CheckmarkView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55E69E162868D7920025D82E /* CheckmarkView.swift */; };
38+
55FBAAF528C54B230066E629 /* Nodes+Allowance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55FBAAF428C54B230066E629 /* Nodes+Allowance.swift */; };
39+
55FBAAF628C54B2F0066E629 /* Nodes+Allowance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55FBAAF428C54B230066E629 /* Nodes+Allowance.swift */; };
40+
55FBAAF728C54B2F0066E629 /* Nodes+Allowance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55FBAAF428C54B230066E629 /* Nodes+Allowance.swift */; };
41+
55FBAAF828C54B300066E629 /* Nodes+Allowance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55FBAAF428C54B230066E629 /* Nodes+Allowance.swift */; };
42+
55FBAAFB28C550920066E629 /* NodesAllowanceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55FBAAFA28C550920066E629 /* NodesAllowanceTests.swift */; };
3843
6403F5DB2272389800D58779 /* (null) in Sources */ = {isa = PBXBuildFile; };
3944
6403F5DE22723C6800D58779 /* DashMainnet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6403F5DD22723C6800D58779 /* DashMainnet.swift */; };
4045
6403F5E022723F6400D58779 /* DashWalletRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6403F5DF22723F6400D58779 /* DashWalletRouter.swift */; };
@@ -644,6 +649,8 @@
644649
557AC307287B1365004699D7 /* CheckmarkRowView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CheckmarkRowView.swift; sourceTree = "<group>"; };
645650
55D1D854287B890300F94A4E /* AddressGeneratorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddressGeneratorTests.swift; sourceTree = "<group>"; };
646651
55E69E162868D7920025D82E /* CheckmarkView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CheckmarkView.swift; sourceTree = "<group>"; };
652+
55FBAAF428C54B230066E629 /* Nodes+Allowance.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Nodes+Allowance.swift"; sourceTree = "<group>"; };
653+
55FBAAFA28C550920066E629 /* NodesAllowanceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NodesAllowanceTests.swift; sourceTree = "<group>"; };
647654
6403F5DD22723C6800D58779 /* DashMainnet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DashMainnet.swift; sourceTree = "<group>"; };
648655
6403F5DF22723F6400D58779 /* DashWalletRouter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DashWalletRouter.swift; sourceTree = "<group>"; };
649656
6403F5E122723F7500D58779 /* DashWallet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DashWallet.swift; sourceTree = "<group>"; };
@@ -1820,6 +1827,7 @@
18201827
553B1283281C6EE100FFF24C /* GCDUtilites.swift */,
18211828
5522A43F28B758530029B543 /* UIKitUtilites.swift */,
18221829
550698B528C00AED000E8A2B /* CollectionUtilites.swift */,
1830+
55FBAAF428C54B230066E629 /* Nodes+Allowance.swift */,
18231831
);
18241832
path = Helpers;
18251833
sourceTree = "<group>";
@@ -1975,6 +1983,7 @@
19751983
E95F85B6200A4D8F0070534A /* TestTools.swift */,
19761984
55D1D854287B890300F94A4E /* AddressGeneratorTests.swift */,
19771985
551F66E72895B3DA00DE5D69 /* AdamantHealthCheckServiceTests.swift */,
1986+
55FBAAFA28C550920066E629 /* NodesAllowanceTests.swift */,
19781987
E9EC344820066D4A00C0E546 /* Info.plist */,
19791988
);
19801989
path = AdamantTests;
@@ -2429,6 +2438,7 @@
24292438
E9079A91229DF1AC0022CA0D /* Date+adamant.swift in Sources */,
24302439
E9079A8C229DF19A0022CA0D /* AdamantAvatarService.swift in Sources */,
24312440
E9079A93229DF1CB0022CA0D /* ExtensionsApi.swift in Sources */,
2441+
55FBAAF828C54B300066E629 /* Nodes+Allowance.swift in Sources */,
24322442
E9D664C822A003B800733F8A /* WarningView.swift in Sources */,
24332443
E9079A96229DF1DA0022CA0D /* ChatAsset.swift in Sources */,
24342444
E9079A9A229DF1DA0022CA0D /* StateAsset.swift in Sources */,
@@ -2588,6 +2598,7 @@
25882598
E9E7CDB32002B9FB00DFC4DB /* LoginRoutes.swift in Sources */,
25892599
E9771DA022997D320099AAC7 /* AdamantUtilities.swift in Sources */,
25902600
E941CCDE20E7B70200C96220 /* WalletCollectionViewCell.swift in Sources */,
2601+
55FBAAF528C54B230066E629 /* Nodes+Allowance.swift in Sources */,
25912602
E9AA8BFA212C166600F9249F /* EthWalletService+Send.swift in Sources */,
25922603
6449BA68235CA0930033B936 /* ERC20WalletService.swift in Sources */,
25932604
644793C32166314A00FC4CF5 /* OnboardPage.swift in Sources */,
@@ -2784,6 +2795,7 @@
27842795
E957E161229C4BEA0019732A /* Crypto.swift in Sources */,
27852796
64E3D52624B258F800660641 /* ERC20Provider.swift in Sources */,
27862797
E957E16A229C53980019732A /* DogeProvider.swift in Sources */,
2798+
55FBAAF728C54B2F0066E629 /* Nodes+Allowance.swift in Sources */,
27872799
E957E15B229C42B20019732A /* KeychainStore.swift in Sources */,
27882800
E957E16F229C564B0019732A /* NotificationContent.swift in Sources */,
27892801
E957E169229C53980019732A /* AdamantProvider.swift in Sources */,
@@ -2850,6 +2862,7 @@
28502862
E9771D9622996FEB0099AAC7 /* StateType.swift in Sources */,
28512863
E9771D9522996FEB0099AAC7 /* StateAsset.swift in Sources */,
28522864
E9771D9222996FEB0099AAC7 /* TransactionAsset.swift in Sources */,
2865+
55FBAAF628C54B2F0066E629 /* Nodes+Allowance.swift in Sources */,
28532866
);
28542867
runOnlyForDeploymentPostprocessing = 0;
28552868
};
@@ -2863,6 +2876,7 @@
28632876
E94883E7203F07CD00F6E1B0 /* PassphraseValidation.swift in Sources */,
28642877
E95F85B7200A4D8F0070534A /* TestTools.swift in Sources */,
28652878
E95F85BC200A4E670070534A /* ParsingModelsTests.swift in Sources */,
2879+
55FBAAFB28C550920066E629 /* NodesAllowanceTests.swift in Sources */,
28662880
E96D64C02295C06400CA5587 /* JSModels.swift in Sources */,
28672881
E96D64BE2295C06400CA5587 /* JSAdamantCore.swift in Sources */,
28682882
E95F85752007E4790070534A /* HexAndBytesUtilitiesTest.swift in Sources */,

Adamant/ServiceProtocols/HealthCheckService.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,4 @@ protocol HealthCheckService: AnyObject {
1717
var delegate: HealthCheckDelegate? { get set }
1818

1919
func healthCheck()
20-
func getAllowedNodes(sortedBySpeedDescending: Bool, needWS: Bool) -> [Node]
2120
}

Adamant/Services/AdamantHealthCheckService.swift

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,53 +16,42 @@ final class AdamantHealthCheckService: HealthCheckService {
1616

1717
// MARK: - Properties
1818

19-
var nodes = [Node]() {
20-
didSet {
21-
resetRequests()
22-
}
23-
}
24-
25-
weak var delegate: HealthCheckDelegate?
19+
private var _nodes = [Node]()
2620
private var currentRequests = Set<DataRequest>()
2721
private let semaphore = DispatchSemaphore(value: 1)
2822

29-
// MARK: - Tools
23+
weak var delegate: HealthCheckDelegate?
3024

31-
func getAllowedNodes(sortedBySpeedDescending: Bool, needWS: Bool) -> [Node] {
32-
defer { semaphore.signal() }
33-
semaphore.wait()
34-
35-
var allowedNodes = nodes.filter {
36-
$0.connectionStatus == .allowed
37-
&& (!needWS || $0.status?.wsEnabled ?? false)
25+
var nodes: [Node] {
26+
get {
27+
defer { semaphore.signal() }
28+
semaphore.wait()
29+
return _nodes
3830
}
39-
40-
if allowedNodes.isEmpty && !needWS {
41-
allowedNodes = nodes.filter { $0.isEnabled }
31+
set {
32+
defer { semaphore.signal() }
33+
semaphore.wait()
34+
_nodes = newValue
4235
}
43-
44-
return sortedBySpeedDescending
45-
? allowedNodes.sorted {
46-
$0.status?.ping ?? .greatestFiniteMagnitude < $1.status?.ping ?? .greatestFiniteMagnitude
47-
}
48-
: allowedNodes.shuffled()
4936
}
5037

38+
// MARK: - Tools
39+
5140
func healthCheck() {
5241
defer { semaphore.signal() }
5342
semaphore.wait()
5443

5544
resetRequests()
5645
updateNodesAvailability()
5746

58-
nodes.filter { $0.isEnabled }.forEach { node in
47+
_nodes.filter { $0.isEnabled }.forEach { node in
5948
guard let request = updateNodeStatus(node: node) else { return }
6049
currentRequests.insert(request)
6150
}
6251
}
6352

6453
private func updateNodesAvailability() {
65-
let workingNodes = nodes.filter { $0.isWorking }
54+
let workingNodes = _nodes.filter { $0.isWorking }
6655

6756
let actualHeightsRange = getActualNodeHeightsRange(
6857
heights: workingNodes.compactMap { $0.status?.height }

Adamant/Services/AdamantNodesSource.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class AdamantNodesSource: NodesSource {
5858
object: nil,
5959
queue: nil
6060
) { [weak self] _ in
61-
self?.healthCheckService.healthCheck()
61+
self?.healthCheck()
6262
}
6363

6464
guard
@@ -84,14 +84,14 @@ class AdamantNodesSource: NodesSource {
8484
}
8585

8686
func getAllowedNodes(needWS: Bool) -> [Node] {
87-
healthCheckService.getAllowedNodes(
87+
healthCheckService.nodes.getAllowedNodes(
8888
sortedBySpeedDescending: preferTheFastestNode,
8989
needWS: needWS
9090
)
9191
}
9292

9393
func nodesUpdate() {
94-
healthCheckService.healthCheck()
94+
healthCheck()
9595
saveNodes()
9696
}
9797

@@ -154,6 +154,7 @@ class AdamantNodesSource: NodesSource {
154154
extension AdamantNodesSource: HealthCheckDelegate {
155155
func healthCheckUpdate() {
156156
sendNodesUpdateNotification()
157+
saveNodes()
157158
}
158159
}
159160

Adamant/Services/ApiService/AdamantApiService.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@ class AdamantApiService: ApiService {
102102
object: nil,
103103
queue: nil
104104
) { [weak self] _ in
105-
self?.semaphore.wait()
106105
self?.updateCurrentNodes()
107-
self?.semaphore.signal()
108106
}
109107
}
110108

@@ -286,7 +284,9 @@ class AdamantApiService: ApiService {
286284
}
287285

288286
private func updateCurrentNodes() {
287+
semaphore.wait()
289288
currentNodes = nodesSource?.getAllowedNodes(needWS: false) ?? []
289+
semaphore.signal()
290290
}
291291

292292
private func sendCurrentNodeUpdateNotification() {

Adamant/SharedViews/CheckmarkView.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ final class CheckmarkView: UIView {
6767
}
6868

6969
func setIsChecked(_ isChecked: Bool, animated: Bool) {
70+
guard self.isChecked != isChecked else { return }
71+
7072
self.isChecked = isChecked
7173
updateImage(animated: animated)
7274
}

AdamantShared/ExtensionsTools/ExtensionsApi.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,21 @@ class ExtensionsApi {
1515
let keychainStore: KeychainStore
1616

1717
private(set) lazy var nodes: [Node] = {
18+
let nodes: [Node]
1819
if let raw = keychainStore.get(nodesStoreKey), let data = raw.data(using: String.Encoding.utf8) {
1920
do {
20-
return try JSONDecoder().decode([Node].self, from: data)
21+
nodes = try JSONDecoder().decode([Node].self, from: data)
2122
} catch {
22-
return AdamantResources.nodes
23+
nodes = AdamantResources.nodes
2324
}
2425
} else {
25-
return AdamantResources.nodes
26+
nodes = AdamantResources.nodes
2627
}
28+
29+
return nodes.getAllowedNodes(sortedBySpeedDescending: true, needWS: false).reversed()
2730
}()
2831

29-
private var currentNode: Node? = nil
32+
private var currentNode: Node?
3033

3134
private func selectNewNode() {
3235
currentNode = nodes.popLast()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// Nodes+Allowance.swift
3+
// Adamant
4+
//
5+
// Created by Andrey on 05.09.2022.
6+
// Copyright © 2022 Adamant. All rights reserved.
7+
//
8+
9+
extension Collection where Element: Node {
10+
func getAllowedNodes(sortedBySpeedDescending: Bool, needWS: Bool) -> [Node] {
11+
var allowedNodes = filter {
12+
$0.connectionStatus == .allowed
13+
&& (!needWS || $0.status?.wsEnabled ?? false)
14+
}
15+
16+
if allowedNodes.isEmpty && !needWS {
17+
allowedNodes = filter { $0.isEnabled }
18+
}
19+
20+
return sortedBySpeedDescending
21+
? allowedNodes.sorted {
22+
$0.status?.ping ?? .greatestFiniteMagnitude < $1.status?.ping ?? .greatestFiniteMagnitude
23+
}
24+
: allowedNodes.shuffled()
25+
}
26+
}

AdamantShared/Helpers/UIKitUtilites.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import UIKit
1010

1111
extension UICollectionView {
1212
func scrollToLastItem(animated: Bool) {
13+
guard numberOfSections > .zero else { return }
14+
1315
let indexPath = IndexPath(
1416
row: numberOfItems(inSection: numberOfSections - 1) - 1,
1517
section: numberOfSections - 1

AdamantTests/AdamantHealthCheckServiceTests.swift

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -23,89 +23,6 @@ class AdamantHealthCheckServiceTests: XCTestCase {
2323
service = nil
2424
}
2525

26-
// MARK: - Allowed nodes tests without WS support
27-
28-
func testOneAllowedNode() {
29-
let node = makeTestNode(connectionStatus: .allowed)
30-
service.nodes = [node]
31-
32-
XCTAssertEqual([node], allowedNodes(ws: false))
33-
}
34-
35-
func testOneNodeWithoutConnectionStatusIsAllowed() {
36-
let node = makeTestNode()
37-
service.nodes = [node]
38-
39-
XCTAssertEqual([node], allowedNodes(ws: false))
40-
}
41-
42-
func testOneDisabledNodeIsNotAllowed() {
43-
let node = makeTestNode()
44-
node.isEnabled = false
45-
service.nodes = [node]
46-
47-
XCTAssert(allowedNodes(ws: false).isEmpty)
48-
}
49-
50-
func testOneOfflineNodeIsAllowed() {
51-
let node = makeTestNode(connectionStatus: .offline)
52-
service.nodes = [node]
53-
54-
XCTAssertEqual([node], allowedNodes(ws: false))
55-
}
56-
57-
func testManyAllowedNodesSortedBySpeedDescending() {
58-
let nodes: [Node] = (0 ..< 100).map { ping in
59-
let node = makeTestNode(connectionStatus: .allowed)
60-
node.status = .init(ping: TimeInterval(ping), wsEnabled: false, height: nil, version: nil)
61-
return node
62-
}
63-
64-
service.nodes = nodes.shuffled()
65-
66-
XCTAssertEqual(nodes, allowedNodes(ws: false))
67-
}
68-
69-
// MARK: - Allowed nodes tests with WS support
70-
71-
func testOneAllowedNodeWithoutWSIsNotAllowedWS() {
72-
let node = makeTestNode(connectionStatus: .allowed)
73-
node.status = .init(ping: .zero, wsEnabled: false, height: nil, version: nil)
74-
service.nodes = [node]
75-
76-
XCTAssert(allowedNodes(ws: true).isEmpty)
77-
}
78-
79-
func testOneAllowedWSNodeIsAllowedWS() {
80-
let node = makeTestNode(connectionStatus: .allowed)
81-
node.status = .init(ping: .zero, wsEnabled: true, height: nil, version: nil)
82-
service.nodes = [node]
83-
84-
XCTAssertEqual([node], allowedNodes(ws: true))
85-
}
86-
87-
func testOneWSNodeWithoutConnectionStatusIsNotAllowedWS() {
88-
let node = makeTestNode()
89-
node.status = .init(ping: .zero, wsEnabled: true, height: nil, version: nil)
90-
service.nodes = [node]
91-
92-
XCTAssert(allowedNodes(ws: true).isEmpty)
93-
}
94-
95-
func testManyAllowedNodesSortedBySpeedDescendingWS() {
96-
let nodes: [Node] = (0 ..< 100).map { ping in
97-
let node = makeTestNode(connectionStatus: .allowed)
98-
node.status = .init(ping: TimeInterval(ping), wsEnabled: true, height: nil, version: nil)
99-
return node
100-
}
101-
102-
service.nodes = nodes.shuffled()
103-
104-
XCTAssertEqual(nodes, allowedNodes(ws: true))
105-
}
106-
107-
// MARK: - Health check tests
108-
10926
func testOneNodeWithoutStatusIsSync() {
11027
let node = makeTestNode()
11128

@@ -197,10 +114,6 @@ class AdamantHealthCheckServiceTests: XCTestCase {
197114

198115
// MARK: - Helpers
199116

200-
private func allowedNodes(ws: Bool) -> [Node] {
201-
service.getAllowedNodes(sortedBySpeedDescending: true, needWS: ws)
202-
}
203-
204117
private func makeTestNode(connectionStatus: Node.ConnectionStatus = .synchronizing) -> Node {
205118
let node = Node(scheme: .default, host: "", port: nil)
206119
node.connectionStatus = connectionStatus

0 commit comments

Comments
 (0)