From bd4311b9ef1bb1e67fd2414aedfffd41a7fa834b Mon Sep 17 00:00:00 2001 From: manhlx Date: Tue, 2 Apr 2019 11:39:18 +0700 Subject: [PATCH 1/2] Add push notification when recevied ETH or token --- .../KNTransactionCoordinator.swift | 28 ++++++++++++++++++- .../Utilities/KNNotificationUtil.swift | 3 ++ .../Localization/en.lproj/Localizable.strings | 2 ++ .../Localization/ko.lproj/Localizable.strings | 2 ++ .../Localization/vi.lproj/Localizable.strings | 3 ++ .../zh-Hans.lproj/Localizable.strings | 3 ++ .../Transactions/Storage/Transaction.swift | 7 +++++ 7 files changed, 47 insertions(+), 1 deletion(-) diff --git a/KyberNetwork/KyberNetwork/Coordinators/TransactionCoordinator/KNTransactionCoordinator.swift b/KyberNetwork/KyberNetwork/Coordinators/TransactionCoordinator/KNTransactionCoordinator.swift index ccbb208101..8062c52e21 100644 --- a/KyberNetwork/KyberNetwork/Coordinators/TransactionCoordinator/KNTransactionCoordinator.swift +++ b/KyberNetwork/KyberNetwork/Coordinators/TransactionCoordinator/KNTransactionCoordinator.swift @@ -266,6 +266,7 @@ extension KNTransactionCoordinator { KNAppTracker.updateInternalTransactionLastBlockLoad(lastBlockLoaded, for: self.wallet.address) let eth = KNSupportedTokenStorage.shared.ethToken let transactions = data.map({ KNTokenTransaction(internalDict: $0, eth: eth).toTransaction() }) + self.handleReceiveEtherOrToken(transactions) self.transactionStorage.add(transactions) KNNotificationUtil.postNotification(for: kTokenTransactionListDidUpdateNotificationKey) print("---- Internal Token Transactions: Loaded \(transactions.count) transactions ----") @@ -304,6 +305,7 @@ extension KNTransactionCoordinator { func updateListTokenTransactions(_ transactions: [Transaction]) { if transactions.isEmpty { return } + self.handleReceiveEtherOrToken(transactions) self.transactionStorage.add(transactions) KNNotificationUtil.postNotification(for: kTokenTransactionListDidUpdateNotificationKey) var tokenObjects: [TokenObject] = [] @@ -317,6 +319,30 @@ extension KNTransactionCoordinator { KNNotificationUtil.postNotification(for: kTokenObjectListDidUpdateNotificationKey) } } + + fileprivate func handleReceiveEtherOrToken(_ transactions: [Transaction]) { + if transactions.isEmpty { return } + if KNAppTracker.transactionLoadState(for: wallet.address) != .done { return } + let receivedTxs = transactions.filter({ return $0.to.lowercased() == wallet.address.description.lowercased() && $0.state == .completed }).sorted(by: { return $0.date > $1.date }) + if let latestReceiveTx = receivedTxs.first { + let title = NSLocalizedString("received.token", value: "Received %@", comment: "") + let message = NSLocalizedString("successfully.received", value: "Successfully received %@ from %@", comment: "") + let txs = receivedTxs.filter({ return $0.id == latestReceiveTx.id }) + if txs.count > 1 { return } // swap transactions + if self.transactionStorage.get(forPrimaryKey: latestReceiveTx.compoundKey) != nil { return } + let address = "\(latestReceiveTx.from.prefix(12))...\(latestReceiveTx.from.suffix(10))" + + guard let symbol = latestReceiveTx.getTokenSymbol() else { return } + let amount = "\(latestReceiveTx.value) \(symbol)" + let userInfo = ["transaction_hash": "\(latestReceiveTx.id)"] + + KNNotificationUtil.localPushNotification( + title: String(format: title, symbol), + body: String(format: message, arguments: [amount, address]), + userInfo: userInfo + ) + } + } } // MARK: Pending transactions @@ -353,7 +379,7 @@ extension KNTransactionCoordinator { guard let `self` = self else { return } self.externalProvider.getTransactionByHash(transaction.id, completion: { [weak self] sessionError in guard let `self` = self else { return } - guard let trans = self.transactionStorage.get(forPrimaryKey: transaction.id) else { return } + guard let trans = self.transactionStorage.getKyberTransaction(forPrimaryKey: transaction.id) else { return } if trans.state != .pending { // Prevent the notification is called multiple time due to timer runs return diff --git a/KyberNetwork/KyberNetwork/Utilities/KNNotificationUtil.swift b/KyberNetwork/KyberNetwork/Utilities/KNNotificationUtil.swift index cfc6426fb6..1eb7903e83 100644 --- a/KyberNetwork/KyberNetwork/Utilities/KNNotificationUtil.swift +++ b/KyberNetwork/KyberNetwork/Utilities/KNNotificationUtil.swift @@ -41,6 +41,9 @@ let kIEOTxListUpdateNotificationKey = "kIEOTxListUpdateNotificationKey" let kUserWalletsListUpdatedNotificationKey = "kUserWalletsListUpdatedNotificationKey" +// Receive txs +let kNewReceivedTransactionKey = "kNewReceivedTransactionKey" + class KNNotificationUtil { static func postNotification(for key: String, object: Any? = nil, userInfo: [AnyHashable: Any]? = nil) { diff --git a/KyberNetwork/Localization/en.lproj/Localizable.strings b/KyberNetwork/Localization/en.lproj/Localizable.strings index 2ef79d3057..9834678284 100644 --- a/KyberNetwork/Localization/en.lproj/Localizable.strings +++ b/KyberNetwork/Localization/en.lproj/Localizable.strings @@ -447,6 +447,8 @@ "can.not.decode.data" = "Can not decode data"; "something.went.wrong.please.try.again" = "Something went wrong, please try again"; "please.send.your.request.to.support" = "Please send your request to support@kyber.network"; +"successfully.received" = "Successfully received %@ from %@"; +"received.token" = "Received %@"; // Promo code error messages "Promo code is invalid!" = "Promo code is invalid!"; diff --git a/KyberNetwork/Localization/ko.lproj/Localizable.strings b/KyberNetwork/Localization/ko.lproj/Localizable.strings index b060859bcd..73a57d6a69 100644 --- a/KyberNetwork/Localization/ko.lproj/Localizable.strings +++ b/KyberNetwork/Localization/ko.lproj/Localizable.strings @@ -409,6 +409,8 @@ "new" = "새로운"; "can.not.update.exchange.rate" = "환율을 업데이트할 수 없습니다."; +"successfully.received" = "%@ 에서 %@ 성공적으로 받았습니다."; +"received.token" = "받은 %@"; // MARK: Price alert feature "Cap reached" = "Cap reached"; diff --git a/KyberNetwork/Localization/vi.lproj/Localizable.strings b/KyberNetwork/Localization/vi.lproj/Localizable.strings index 99721b764c..4facade085 100644 --- a/KyberNetwork/Localization/vi.lproj/Localizable.strings +++ b/KyberNetwork/Localization/vi.lproj/Localizable.strings @@ -422,6 +422,9 @@ "something.went.wrong.please.try.again" = "Đã xảy ra lỗi, vui lòng thử lại"; "please.send.your.request.to.support" = "Vui lòng gửi yêu cầu của bạn đến support@kyber.network"; +"successfully.received" = "Thành công nhận được %@ từ %@"; +"received.token" = "Đã nhận %@"; + // MARK: Price alert feature "Cap reached" = "Đạt mức cho phép"; "You can only have maximum of 10 alerts" = "Bạn chỉ có thể có tối đa 10 thông báo"; diff --git a/KyberNetwork/Localization/zh-Hans.lproj/Localizable.strings b/KyberNetwork/Localization/zh-Hans.lproj/Localizable.strings index 59c8e6489d..ff2a63a5c0 100644 --- a/KyberNetwork/Localization/zh-Hans.lproj/Localizable.strings +++ b/KyberNetwork/Localization/zh-Hans.lproj/Localizable.strings @@ -402,6 +402,9 @@ "new" = "新"; "can.not.update.exchange.rate" = "无法更新汇率"; +"successfully.received" = "成功接收%@从%@"; +"received.token" = "收到%@"; + // MARK: Price alert feature "Cap reached" = "达到了限额"; "You can only have maximum of 10 alerts" = "您最多只能有10个提醒"; diff --git a/KyberNetwork/Transactions/Storage/Transaction.swift b/KyberNetwork/Transactions/Storage/Transaction.swift index 6fa9d54b03..430f8a43ee 100644 --- a/KyberNetwork/Transactions/Storage/Transaction.swift +++ b/KyberNetwork/Transactions/Storage/Transaction.swift @@ -125,4 +125,11 @@ extension Transaction { isDisabled: false ) } + + func getTokenSymbol() -> String? { + guard let localObject = self.localizedOperations.first, localObject.type == "transfer" else { + return nil + } + return localObject.symbol + } } From cdf25f142607fca283eca167c981e5bd0a587ae8 Mon Sep 17 00:00:00 2001 From: manhlx Date: Tue, 2 Apr 2019 15:09:58 +0700 Subject: [PATCH 2/2] Set interval time to remove swap trades --- .../KNTransactionCoordinator.swift | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/KyberNetwork/KyberNetwork/Coordinators/TransactionCoordinator/KNTransactionCoordinator.swift b/KyberNetwork/KyberNetwork/Coordinators/TransactionCoordinator/KNTransactionCoordinator.swift index 8062c52e21..c2d67ef2aa 100644 --- a/KyberNetwork/KyberNetwork/Coordinators/TransactionCoordinator/KNTransactionCoordinator.swift +++ b/KyberNetwork/KyberNetwork/Coordinators/TransactionCoordinator/KNTransactionCoordinator.swift @@ -40,6 +40,10 @@ class KNTransactionCoordinator { self.isLoadingEnabled = true self.startUpdatingCompletedTransactions() self.startUpdatingPendingTransactions() + + // remove some old txs + let oldKyberTxs = self.transactionStorage.kyberMinedTransactions.filter({ return Date().timeIntervalSince($0.date) >= 7.0 * 24.0 * 60.0 * 60.0 }) + self.transactionStorage.delete(oldKyberTxs) } func stop() { @@ -327,9 +331,8 @@ extension KNTransactionCoordinator { if let latestReceiveTx = receivedTxs.first { let title = NSLocalizedString("received.token", value: "Received %@", comment: "") let message = NSLocalizedString("successfully.received", value: "Successfully received %@ from %@", comment: "") - let txs = receivedTxs.filter({ return $0.id == latestReceiveTx.id }) - if txs.count > 1 { return } // swap transactions - if self.transactionStorage.get(forPrimaryKey: latestReceiveTx.compoundKey) != nil { return } + if self.transactionStorage.getKyberTransaction(forPrimaryKey: latestReceiveTx.id) != nil { return } // swap/transfer transaction + if self.transactionStorage.get(forPrimaryKey: latestReceiveTx.compoundKey) != nil { return } // already done transaction let address = "\(latestReceiveTx.from.prefix(12))...\(latestReceiveTx.from.suffix(10))" guard let symbol = latestReceiveTx.getTokenSymbol() else { return } @@ -365,9 +368,6 @@ extension KNTransactionCoordinator { @objc func shouldUpdatePendingTransaction(_ sender: Any?) { let objects = self.transactionStorage.kyberPendingTransactions - if objects.isEmpty { - self.transactionStorage.deleteAllKyberTransactions() - } objects.forEach { if self.isLoadingEnabled { self.updatePendingTransaction($0) } } @@ -426,7 +426,6 @@ extension KNTransactionCoordinator { object: newTx.id, userInfo: nil ) - self?.transactionStorage.delete([transaction]) completion(nil) case .failure(let error): completion(error)