Skip to content

Commit 4686e9c

Browse files
Merge pull request #98 from Adamant-im/feature/chats-screen-crash-fix
Fixed crash on first messages loading
2 parents c6682db + 84e9530 commit 4686e9c

File tree

7 files changed

+57
-19
lines changed

7 files changed

+57
-19
lines changed

Adamant.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
550066C7284D682D0044C0B1 /* AdamantHealthCheckService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 550066C6284D682D0044C0B1 /* AdamantHealthCheckService.swift */; };
2424
551F66E628959A5300DE5D69 /* LoadingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 551F66E528959A5200DE5D69 /* LoadingView.swift */; };
2525
551F66E82895B3DA00DE5D69 /* AdamantHealthCheckServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 551F66E72895B3DA00DE5D69 /* AdamantHealthCheckServiceTests.swift */; };
26+
5522A44028B758530029B543 /* UIKitUtilites.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5522A43F28B758530029B543 /* UIKitUtilites.swift */; };
2627
553B1284281C6EE100FFF24C /* GCDUtilites.swift in Sources */ = {isa = PBXBuildFile; fileRef = 553B1283281C6EE100FFF24C /* GCDUtilites.swift */; };
2728
5551CC8F28A8B75300B52AD0 /* ApiServiceStub.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5551CC8E28A8B75300B52AD0 /* ApiServiceStub.swift */; };
2829
5558A436282AAFCC0024DDD6 /* AdamantApi+Status.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5558A435282AAFCC0024DDD6 /* AdamantApi+Status.swift */; };
@@ -633,6 +634,7 @@
633634
550066C6284D682D0044C0B1 /* AdamantHealthCheckService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdamantHealthCheckService.swift; sourceTree = "<group>"; };
634635
551F66E528959A5200DE5D69 /* LoadingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoadingView.swift; sourceTree = "<group>"; };
635636
551F66E72895B3DA00DE5D69 /* AdamantHealthCheckServiceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdamantHealthCheckServiceTests.swift; sourceTree = "<group>"; };
637+
5522A43F28B758530029B543 /* UIKitUtilites.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIKitUtilites.swift; sourceTree = "<group>"; };
636638
553B1283281C6EE100FFF24C /* GCDUtilites.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GCDUtilites.swift; sourceTree = "<group>"; };
637639
5551CC8E28A8B75300B52AD0 /* ApiServiceStub.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApiServiceStub.swift; sourceTree = "<group>"; };
638640
5558A435282AAFCC0024DDD6 /* AdamantApi+Status.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AdamantApi+Status.swift"; sourceTree = "<group>"; };
@@ -1814,6 +1816,7 @@
18141816
E927171D20C04613002BB9A6 /* UIColor+hex.swift */,
18151817
E935C84C22AC06E500A8CA2F /* RichMessageTools.swift */,
18161818
553B1283281C6EE100FFF24C /* GCDUtilites.swift */,
1819+
5522A43F28B758530029B543 /* UIKitUtilites.swift */,
18171820
);
18181821
path = Helpers;
18191822
sourceTree = "<group>";
@@ -2447,6 +2450,7 @@
24472450
E908472A2196FEA80095825D /* RichMessageTransaction+CoreDataClass.swift in Sources */,
24482451
64A223D620F760BB005157CB /* Localization.swift in Sources */,
24492452
64E1C82F222E95F6006C4DA7 /* DogeWallet.swift in Sources */,
2453+
5522A44028B758530029B543 /* UIKitUtilites.swift in Sources */,
24502454
E9484B7D2285BAD9008E10F0 /* PrivateKeyGenerator.swift in Sources */,
24512455
E94E7B08205D4CB80042B639 /* SharedRoutes.swift in Sources */,
24522456
E9960B3621F5154300C840A8 /* DummyAccount+CoreDataProperties.swift in Sources */,

Adamant/ServiceProtocols/DataProviders/ChatsProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ protocol ChatsProvider: DataProvider {
190190
func getChatroomsController() -> NSFetchedResultsController<Chatroom>
191191
func getChatController(for chatroom: Chatroom) -> NSFetchedResultsController<ChatTransaction>
192192
func getChatRooms(offset: Int?, completion: (() ->())?)
193-
func getChatMessages(with addressRecipient: String, offset: Int?, completion: ((Int) ->())?)
193+
func getChatMessages(with addressRecipient: String, offset: Int?, completion: (() -> Void)?)
194194

195195
/// Unread messages controller. Sections by chatroom.
196196
func getUnreadMessagesController() -> NSFetchedResultsController<ChatTransaction>

Adamant/Services/ApiService/AdamantApiService.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class AdamantApiService: ApiService {
6464
// MARK: - Properties
6565

6666
private var _lastRequestTimeDelta: TimeInterval?
67-
private var lastRequestTimeDeltaSemaphore: DispatchSemaphore = DispatchSemaphore(value: 1)
67+
private var semaphore: DispatchSemaphore = DispatchSemaphore(value: 1)
6868

6969
private(set) var currentNodes: [Node] = [] {
7070
didSet {
@@ -75,15 +75,15 @@ class AdamantApiService: ApiService {
7575

7676
private(set) var lastRequestTimeDelta: TimeInterval? {
7777
get {
78-
defer { lastRequestTimeDeltaSemaphore.signal() }
79-
lastRequestTimeDeltaSemaphore.wait()
78+
defer { semaphore.signal() }
79+
semaphore.wait()
8080

8181
return _lastRequestTimeDelta
8282
}
8383
set {
84-
lastRequestTimeDeltaSemaphore.wait()
84+
semaphore.wait()
8585
_lastRequestTimeDelta = newValue
86-
lastRequestTimeDeltaSemaphore.signal()
86+
semaphore.signal()
8787
}
8888
}
8989

@@ -102,7 +102,9 @@ class AdamantApiService: ApiService {
102102
object: nil,
103103
queue: nil
104104
) { [weak self] _ in
105+
self?.semaphore.wait()
105106
self?.updateCurrentNodes()
107+
self?.semaphore.signal()
106108
}
107109
}
108110

Adamant/Services/DataProviders/AdamantChatsProvider.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,10 @@ extension AdamantChatsProvider {
324324
}
325325
}
326326

327-
func getChatMessages(with addressRecipient: String, offset: Int?, completion: ((Int) ->())?) {
327+
func getChatMessages(with addressRecipient: String, offset: Int?, completion: (() -> Void)?) {
328328
guard let address = accountService.account?.address,
329329
let privateKey = accountService.keypair?.privateKey else {
330-
completion?(0)
330+
completion?()
331331
return
332332
}
333333

@@ -344,7 +344,7 @@ extension AdamantChatsProvider {
344344
self?.chatLoadedMessages[addressRecipient] = loadedCount + (chatroom?.messages?.count ?? 0)
345345
guard let transactions = chatroom?.messages,
346346
transactions.count > 0 else {
347-
completion?(0)
347+
completion?()
348348
return
349349
}
350350
self?.process(messageTransactions: transactions,
@@ -353,7 +353,7 @@ extension AdamantChatsProvider {
353353
context: privateContext,
354354
contextMutatingSemaphore: cms,
355355
completion: {
356-
completion?(transactions.count)
356+
completion?()
357357
})
358358
}
359359
}

Adamant/Services/SocketService/AdamantSocketService.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class AdamantSocketService: SocketService {
4848
private var socket: SocketIOClient?
4949
private var currentAddress: String?
5050
private var currentHandler: ((ApiServiceResult<Transaction>) -> Void)?
51+
private let semaphore = DispatchSemaphore(value: 1)
5152

5253
let defaultResponseDispatchQueue = DispatchQueue(
5354
label: "com.adamant.response-queue",
@@ -67,7 +68,10 @@ class AdamantSocketService: SocketService {
6768
// MARK: - Tools
6869

6970
func connect(address: String, handler: @escaping (ApiServiceResult<Transaction>) -> Void) {
70-
disconnect()
71+
semaphore.wait()
72+
defer { semaphore.signal() }
73+
74+
_disconnect()
7175
currentAddress = address
7276
currentHandler = handler
7377

@@ -91,6 +95,12 @@ class AdamantSocketService: SocketService {
9195
}
9296

9397
func disconnect() {
98+
semaphore.wait()
99+
_disconnect()
100+
semaphore.signal()
101+
}
102+
103+
private func _disconnect() {
94104
socket?.disconnect()
95105
socket = nil
96106
manager = nil

Adamant/Stories/Chats/ChatViewController.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,15 +1135,11 @@ extension ChatViewController {
11351135

11361136
setBusyIndicator(state: true)
11371137

1138-
chatsProvider.getChatMessages(with: address, offset: 0) { [weak self] count in
1138+
chatsProvider.getChatMessages(with: address, offset: 0) { [weak self] in
11391139
DispatchQueue.main.async {
11401140
self?.messagesCollectionView.reloadDataAndKeepOffset()
1141-
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
1142-
if count > 0 {
1143-
self?.messagesCollectionView.scrollToItem(at: IndexPath(row: 0, section: count - 1), at: .top, animated: false)
1144-
}
1145-
self?.setBusyIndicator(state: false)
1146-
}
1141+
self?.messagesCollectionView.scrollToLastItem(animated: false)
1142+
self?.setBusyIndicator(state: false)
11471143
}
11481144
}
11491145
}
@@ -1161,7 +1157,7 @@ extension ChatViewController {
11611157
if address == AdamantContacts.adamantWelcomeWallet.name { return }
11621158
isBusy = true
11631159
let offset = chatsProvider.chatLoadedMessages[address] ?? 0
1164-
chatsProvider.getChatMessages(with: address, offset: offset) { [weak self] _count in
1160+
chatsProvider.getChatMessages(with: address, offset: offset) { [weak self] in
11651161
DispatchQueue.main.async {
11661162
self?.messagesCollectionView.reloadDataAndKeepOffset()
11671163
self?.isBusy = false
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// UIKitUtilites.swift
3+
// Adamant
4+
//
5+
// Created by Andrey on 25.08.2022.
6+
// Copyright © 2022 Adamant. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
extension UICollectionView {
12+
func scrollToLastItem(animated: Bool) {
13+
let indexPath = IndexPath(
14+
row: numberOfItems(inSection: numberOfSections - 1) - 1,
15+
section: numberOfSections - 1
16+
)
17+
18+
guard hasItemAtIndexPath(indexPath) else { return }
19+
scrollToItem(at: indexPath, at: .bottom, animated: animated)
20+
}
21+
22+
func hasItemAtIndexPath(_ indexPath: IndexPath) -> Bool {
23+
indexPath.section < numberOfSections
24+
&& indexPath.row < numberOfItems(inSection: indexPath.section)
25+
}
26+
}

0 commit comments

Comments
 (0)