Skip to content

Commit f49103f

Browse files
authored
Merge pull request #650 from Adamant-im/trello.com/c/QCnKo03A
Added a time correction. Presenting an error on message send failure. Refactoring
2 parents e82ead9 + ac5ce06 commit f49103f

File tree

5 files changed

+11
-57
lines changed

5 files changed

+11
-57
lines changed

Adamant/Modules/Chat/ViewModel/ChatViewModel.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1424,7 +1424,9 @@ private extension ChatViewModel {
14241424
dialog.send(.freeTokenAlert)
14251425
return
14261426
}
1427-
case .accountNotFound, .accountNotInitiated, .dependencyError, .internalError, .networkError, .notLogged, .requestCancelled, .serverError, .transactionNotFound, .invalidTransactionStatus, .none:
1427+
case .serverError:
1428+
dialog.send(.richError(error))
1429+
case .accountNotFound, .accountNotInitiated, .dependencyError, .internalError, .networkError, .notLogged, .requestCancelled, .transactionNotFound, .invalidTransactionStatus, .none:
14281430
break
14291431
}
14301432
}

Adamant/Modules/Wallets/Adamant/AdmWalletService+DynamicConstants.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ extension AdmWalletService {
1818
crucialServiceUpdateInterval: 30,
1919
onScreenServiceUpdateInterval: 10
2020
)
21+
22+
static var adamantTimestampCorrection: TimeInterval {
23+
0.5
24+
}
2125

2226
static var newPendingInterval: Int {
2327
4000

Adamant/ServiceProtocols/DataProviders/ChatsProvider.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,6 @@ protocol ChatsProvider: DataProvider, Actor {
257257
// MARK: - Unconfirmed Transaction
258258
func addUnconfirmed(transactionId: UInt64, managedObjectId: NSManagedObjectID)
259259

260-
// MARK: - Fake messages
261-
func fakeSent(message: AdamantMessage, recipientId: String, date: Date, status: MessageStatus, showsChatroom: Bool, completion: @escaping (ChatsProviderResultWithTransaction) -> Void)
262-
263260
func fakeReceived(
264261
message: AdamantMessage,
265262
senderId: String,
@@ -269,8 +266,6 @@ protocol ChatsProvider: DataProvider, Actor {
269266
showsChatroom: Bool
270267
) async throws -> ChatTransaction
271268

272-
func fakeUpdate(status: MessageStatus, forTransactionId id: String, completion: @escaping (ChatsProviderResult) -> Void)
273-
274269
// MARK: - Search
275270
func getMessages(containing text: String, in chatroom: Chatroom?) -> [MessageTransaction]?
276271
func isTransactionUnique(_ transaction: RichMessageTransaction) -> Bool

Adamant/Services/DataProviders/AdamantChatsProvider+fakeMessages.swift

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -128,42 +128,6 @@ extension AdamantChatsProvider {
128128
}
129129
}
130130

131-
func fakeUpdate(status: MessageStatus, forTransactionId id: String, completion: @escaping (ChatsProviderResult) -> Void) {
132-
// MARK: 1. Get transaction
133-
let request = NSFetchRequest<MessageTransaction>(entityName: MessageTransaction.entityName)
134-
request.predicate = NSPredicate(format: "transactionId == %@", id)
135-
request.fetchLimit = 1
136-
137-
guard let transaction = (try? stack.container.viewContext.fetch(request))?.first else {
138-
completion(.failure(.transactionNotFound(id: id)))
139-
return
140-
}
141-
142-
// MARK: 2. Update transaction in private context
143-
let privateContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
144-
privateContext.parent = stack.container.viewContext
145-
146-
if let trs = privateContext.object(with: transaction.objectID) as? MessageTransaction {
147-
trs.date = Date() as NSDate
148-
trs.status = status.rawValue
149-
} else {
150-
completion(.failure(.internalError(AdamantError(message: "CoreData changed"))))
151-
return
152-
}
153-
154-
// MARK: 3. Save changes
155-
if privateContext.hasChanges {
156-
do {
157-
try privateContext.save()
158-
completion(.success)
159-
} catch {
160-
completion(.failure(.internalError(error)))
161-
}
162-
} else {
163-
completion(.success)
164-
}
165-
}
166-
167131
// MARK: - Logic
168132

169133
private func fakeSent(text: String, loggedAddress: String, recipient: CoreDataAccount, date: Date, status: MessageStatus, markdown: Bool, showsChatroom: Bool, completion: @escaping (ChatsProviderResultWithTransaction) -> Void) {

Adamant/Services/DataProviders/AdamantChatsProvider.swift

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -306,20 +306,6 @@ extension AdamantChatsProvider {
306306
securedStore.remove(StoreKey.chatProvider.receivedLastHeight)
307307
securedStore.remove(StoreKey.chatProvider.readedLastHeight)
308308

309-
// Drop CoreData
310-
// let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
311-
// context.parent = stack.container.viewContext
312-
//
313-
// let trs = NSFetchRequest<ChatTransaction>(entityName: "ChatTransaction")
314-
//
315-
// if let results = try? context.fetch(trs) {
316-
// for obj in results {
317-
// context.delete(obj)
318-
// }
319-
//
320-
// try! context.save()
321-
// }
322-
323309
// Set State
324310
setState(.empty, previous: prevState, notify: notify)
325311
}
@@ -1115,7 +1101,10 @@ extension AdamantChatsProvider {
11151101
}
11161102

11171103
// MARK: 3. Prepare transaction
1118-
1104+
if var correctedDate = transaction.date as? Date {
1105+
correctedDate -= AdmWalletService.adamantTimestampCorrection
1106+
transaction.date = correctedDate as NSDate
1107+
}
11191108
transaction.statusEnum = MessageStatus.pending
11201109
transaction.partner = context.object(with: recipientAccount.objectID) as? BaseAccount
11211110

0 commit comments

Comments
 (0)