Skip to content

Commit

Permalink
fix asset details
Browse files Browse the repository at this point in the history
  • Loading branch information
lynx56 committed Mar 14, 2024
1 parent fec656e commit 331c0c6
Show file tree
Hide file tree
Showing 25 changed files with 227 additions and 198 deletions.
1 change: 1 addition & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ excluded:
- R.generated.swift
- novawalletTests
- novawalletIntegrationTests
- NovaPushNotificationServiceExtension/R.generated.swift
identifier_name:
excluded:
- id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ final class TransferHandler: CommonHandler, PushNotificationHandler {
let callStore = CancellableCallStore()
let chainId: ChainModel.Id
let payload: NotificationTransferPayload
let type: TransferType
let type: PushNotification.TransferType

init(
chainId: ChainModel.Id,
payload: NotificationTransferPayload,
type: TransferType,
type: PushNotification.TransferType,
operationQueue: OperationQueue
) {
self.chainId = chainId
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Foundation

extension TransferType {
extension PushNotification.TransferType {
func title(locale: Locale, walletName: String?) -> String {
let walletString = walletName.flatMap { "[\($0)]" } ?? ""
let title: String
switch self {
case .income:
title = localizedString(LocalizationKeys.Transfer.incomeTitle, locale: locale)
title = R.string.localizable.pushNotificationReceiveTokensTitle(preferredLanguages: locale.rLanguages)
case .outcome:
title = localizedString(LocalizationKeys.Transfer.outcomeTitle, locale: locale)
title = R.string.localizable.pushNotificationSentTokensTitle(preferredLanguages: locale.rLanguages)
}

return [title, walletString].joined(with: .space)
Expand All @@ -24,23 +24,27 @@ extension TransferType {
let priceString = price.map { "(\($0))" } ?? ""
switch self {
case .income:
return localizedString(
LocalizationKeys.Transfer.incomeSubtitle,
with: [amount, priceString, chainName],
locale: locale
return R.string.localizable.pushNotificationReceiveTokensSubtitle(
amount,
priceString,
chainName,
preferredLanguages: locale.rLanguages
)
case .outcome:
if let address = address {
return localizedString(
LocalizationKeys.Transfer.outcomeSubtitle,
with: [amount, priceString, address, chainName],
locale: locale
return R.string.localizable.pushNotificationSentTokensSubtitle(
amount,
priceString,
address,
chainName,
preferredLanguages: locale.rLanguages
)
} else {
return localizedString(
LocalizationKeys.Transfer.outcomeWOAddressSubtitle,
with: [amount, priceString, chainName],
locale: locale
return R.string.localizable.pushNotificationSentTokensWoAddressSubtitle(
amount,
priceString,
chainName,
preferredLanguages: locale.rLanguages
)
}
}
Expand Down
51 changes: 34 additions & 17 deletions novawallet.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion novawallet/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
fetchCompletionHandler(.noData)
return
}
PushHandlingService.shared.handle(userInfo: userInfo) { success in
PushNotificationHandlingService.shared.handle(userInfo: userInfo) { success in
if success {
fetchCompletionHandler(.newData)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ final class AssetDetailsNotificationMessageHandler {
}

private func handle(
for chainId: ChainModel.Id,
assetId: String?,
address: AccountAddress?,
completion: @escaping (Result<ChainAssetId, AssetDetailsHandlingError>) -> Void
parameters: ResolvedParameters,
completion: @escaping (Result<ChainAsset, AssetDetailsHandlingError>) -> Void
) {
guard let address = address else {
guard let address = parameters.address else {
completion(.failure(AssetDetailsHandlingError.invalidAddress))
return
}
Expand All @@ -56,7 +54,7 @@ final class AssetDetailsNotificationMessageHandler {
let chains: [ChainModel] = changes.allChangedItems()

guard let chainModel = chains.first(where: {
Web3Alert.createRemoteChainId(from: $0.chainId) == chainId
Web3Alert.createRemoteChainId(from: $0.chainId) == parameters.chainId
}) else {
return
}
Expand All @@ -65,7 +63,7 @@ final class AssetDetailsNotificationMessageHandler {

self.handle(
chain: chainModel,
assetId: assetId,
assetId: parameters.assetId,
address: address,
completion: completion
)
Expand All @@ -76,7 +74,7 @@ final class AssetDetailsNotificationMessageHandler {
chain: ChainModel,
assetId: String?,
address: AccountAddress,
completion: @escaping (Result<ChainAssetId, AssetDetailsHandlingError>) -> Void
completion: @escaping (Result<ChainAsset, AssetDetailsHandlingError>) -> Void
) {
guard let asset = mapAssetId(assetId, chain: chain) else {
completion(.failure(AssetDetailsHandlingError.invalidAssetId))
Expand All @@ -93,7 +91,7 @@ final class AssetDetailsNotificationMessageHandler {
let wallet = Self.targetWallet(
address: address,
chainId: chain.chainId,
wallets: wallets,
pushNotificationWallets: wallets,
metaAccounts: metaAccounts
)

Expand All @@ -112,31 +110,39 @@ final class AssetDetailsNotificationMessageHandler {
inOperationQueue: operationQueue,
backingCallIn: callbackStore,
runningCallbackIn: workingQueue
) { result in
) { [weak self] result in
switch result {
case let .success(result):
guard let wallet = result else {
completion(.failure(AssetDetailsHandlingError.unknownWallet))
return
}

self.select(wallet: wallet) { error in
if let error = error {
completion(.failure(AssetDetailsHandlingError.select(error)))
} else {
completion(.success(ChainAssetId(
chainId: chain.chainId,
assetId: asset.assetId
)))
}
}

self?.trySelect(
wallet: result,
chainAsset: .init(chain: chain, asset: asset),
completion: completion
)
case let .failure(error):
completion(.failure(AssetDetailsHandlingError.select(error)))
}
}
}

private func trySelect(
wallet: MetaAccountModel?,
chainAsset: ChainAsset,
completion: @escaping (Result<ChainAsset, AssetDetailsHandlingError>) -> Void
) {
guard let wallet = wallet else {
completion(.failure(AssetDetailsHandlingError.unknownWallet))
return
}

select(wallet: wallet) { error in
if let error = error {
completion(.failure(AssetDetailsHandlingError.select(error)))
} else {
completion(.success(chainAsset))
}
}
}

private func mapAssetId(_ assetId: String?, chain: ChainModel) -> AssetModel? {
if assetId == nil {
return chain.utilityAsset()
Expand All @@ -148,10 +154,10 @@ final class AssetDetailsNotificationMessageHandler {
private static func targetWallet(
address: AccountAddress,
chainId: ChainModel.Id,
wallets: [Web3Alert.LocalWallet],
pushNotificationWallets: [Web3Alert.LocalWallet],
metaAccounts: [MetaAccountModel]
) -> MetaAccountModel? {
guard let targetWallet = wallets.first(where: {
guard let targetWallet = pushNotificationWallets.first(where: {
if let specificAddress = $0.model.chainSpecific[chainId] {
return specificAddress == address
} else {
Expand All @@ -176,45 +182,46 @@ final class AssetDetailsNotificationMessageHandler {
}
}
}
}

extension AssetDetailsNotificationMessageHandler: NotificationMessageHandlerProtocol {
func handle(message: NotificationMessage, completion: @escaping (Result<PushHandlingScreen, Error>) -> Void) {
let targetAddress: AccountAddress?
let targetChainId: ChainModel.Id
let targetAssetId: String?
private func handleParsedMessage(
_ parameters: ResolvedParameters,
completion: @escaping (Result<PushNotification.OpenScreen, Error>) -> Void
) {
handle(parameters: parameters) {
switch $0 {
case let .success(chainAsset):
completion(.success(.historyDetails(chainAsset)))
case let .failure(error):
completion(.failure(error))
}
}
}
}

extension AssetDetailsNotificationMessageHandler: PushNotificationMessageHandlingProtocol {
func handle(
message: NotificationMessage,
completion: @escaping (Result<PushNotification.OpenScreen, Error>) -> Void
) {
switch message {
case let .stakingReward(chainId, payload):
targetChainId = chainId
targetAddress = payload.recipient
targetAssetId = nil
let resolvedParameters = ResolvedParameters(chainId: chainId, assetId: nil, address: payload.recipient)
handleParsedMessage(resolvedParameters, completion: completion)
case let .transfer(type, chainId, payload):
switch type {
case .income:
targetAddress = payload.recipient
targetAssetId = payload.assetId
case .outcome:
targetAddress = payload.sender
targetAssetId = payload.assetId
}
targetChainId = chainId
let address = type == .income ? payload.recipient : payload.sender
let resolvedParameters = ResolvedParameters(chainId: chainId, assetId: payload.assetId, address: address)
handleParsedMessage(resolvedParameters, completion: completion)
default:
completion(.failure(AssetDetailsHandlingError.internalError))
completion(.failure(AssetDetailsHandlingError.unsupportedMessage))
return
}
}
}

handle(
for: targetChainId,
assetId: targetAssetId,
address: targetAddress
) {
switch $0 {
case let .success(chainAssetId):
completion(.success(.historyDetails(chainAssetId)))
case let .failure(error):
completion(.failure(error))
}
}
extension AssetDetailsNotificationMessageHandler {
struct ResolvedParameters {
let chainId: ChainModel.Id
let assetId: String?
let address: AccountAddress?
}
}
2 changes: 1 addition & 1 deletion novawallet/Common/PushHandling/AssetDetails/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ enum AssetDetailsHandlingError: Error {
case select(Error)
case unknownWallet
case invalidAddress
case internalError
case unsupportedMessage
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
typealias GovernanceNotificationMessageHandler = OpenGovernanceUrlParsingService

extension GovernanceNotificationMessageHandler: NotificationMessageHandlerProtocol {
func handle(message: NotificationMessage, completion: @escaping (Result<PushHandlingScreen, Error>) -> Void) {
extension GovernanceNotificationMessageHandler: PushNotificationMessageHandlingProtocol {
func handle(
message: NotificationMessage,
completion: @escaping (Result<PushNotification.OpenScreen, Error>) -> Void
) {
switch message {
case let .newReferendum(chainId, payload):
handle(chainId: chainId, referendumIndex: payload.referendumId, completion: completion)
Expand All @@ -15,7 +18,7 @@ extension GovernanceNotificationMessageHandler: NotificationMessageHandlerProtoc
private func handle(
chainId: ChainModel.Id,
referendumIndex: Referenda.ReferendumIndex,
completion: @escaping (Result<PushHandlingScreen, Error>) -> Void
completion: @escaping (Result<PushNotification.OpenScreen, Error>) -> Void
) {
let chainClosure: (ChainModel) -> Bool = {
Web3Alert.createRemoteChainId(from: $0.chainId) == chainId
Expand Down
19 changes: 0 additions & 19 deletions novawallet/Common/PushHandling/PushHandlingService.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
protocol PushNotificationHandlingServiceProtocol: AnyObject {
func handle(userInfo: [AnyHashable: Any], completion: @escaping (Bool) -> Void)
}

final class PushNotificationHandlingService {
static let shared = PushNotificationHandlingService()

private(set) var service: PushNotificationOpenScreenFacadeProtocol?

func setup(service: PushNotificationOpenScreenFacadeProtocol) {
self.service = service
}
}

extension PushNotificationHandlingService: PushNotificationHandlingServiceProtocol {
func handle(userInfo: [AnyHashable: Any], completion: @escaping (Bool) -> Void) {
service?.handle(userInfo: userInfo, completion: completion)
}
}
Loading

0 comments on commit 331c0c6

Please sign in to comment.