From aead0cd0555a2ffa8891837a8e6b055feaa7d06e Mon Sep 17 00:00:00 2001 From: Tran Giang Long Date: Mon, 29 Jan 2024 17:58:31 +0700 Subject: [PATCH] feat(swap): improve ui and sorting --- .../Service/ChooseSwapTokenService.swift | 14 ++++++--- .../Subviews/ChooseSwapTokenItemView.swift | 30 ++++++++++++++----- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/p2p_wallet/Scenes/Main/Swap/ChooseSwapItem/Service/ChooseSwapTokenService.swift b/p2p_wallet/Scenes/Main/Swap/ChooseSwapItem/Service/ChooseSwapTokenService.swift index 4665d00e9c..8676e2526a 100644 --- a/p2p_wallet/Scenes/Main/Swap/ChooseSwapItem/Service/ChooseSwapTokenService.swift +++ b/p2p_wallet/Scenes/Main/Swap/ChooseSwapItem/Service/ChooseSwapTokenService.swift @@ -52,11 +52,17 @@ final class ChooseSwapTokenService: ChooseItemService { func sortFiltered(by keyword: String, items: [ChooseItemListSection]) -> [ChooseItemListSection] { let sections = items.map { section in guard var tokens = section.items as? [SwapToken] else { return section } - tokens = tokens.sorted(by: { lhs, _ in + tokens = tokens.sorted(by: { lhs, rhs in // Put 'start' matches in the beginning of array, 'contains' after - lhs.token.name.lowercased().starts(with: keyword.lowercased()) || - lhs.token.symbol.lowercased().starts(with: keyword.lowercased()) || - lhs.token.mintAddress.lowercased().starts(with: keyword.lowercased()) + if !lhs.isNonStrict, rhs.isNonStrict { + return true + } else if lhs.isNonStrict, !rhs.isNonStrict { + return false + } else { + return lhs.token.name.lowercased().starts(with: keyword.lowercased()) || + lhs.token.symbol.lowercased().starts(with: keyword.lowercased()) || + lhs.token.mintAddress.lowercased().starts(with: keyword.lowercased()) + } }) if let index = tokens.firstIndex(where: { $0.token.name.lowercased().elementsEqual(keyword.lowercased()) || diff --git a/p2p_wallet/Scenes/Main/Swap/ChooseSwapItem/Subviews/ChooseSwapTokenItemView.swift b/p2p_wallet/Scenes/Main/Swap/ChooseSwapItem/Subviews/ChooseSwapTokenItemView.swift index 327a37b5d5..26b48a3b81 100644 --- a/p2p_wallet/Scenes/Main/Swap/ChooseSwapItem/Subviews/ChooseSwapTokenItemView.swift +++ b/p2p_wallet/Scenes/Main/Swap/ChooseSwapItem/Subviews/ChooseSwapTokenItemView.swift @@ -16,12 +16,21 @@ struct ChooseSwapTokenItemView: View { self.token = token self.chosen = chosen self.fromToken = fromToken + + let formattedMint = RecipientFormatter.format(destination: token.mintAddress) + if fromToken { - subtitle = token.userWallet?.amount?.tokenAmountFormattedString( + let amount = token.userWallet?.amount?.tokenAmountFormattedString( symbol: token.token.symbol, maximumFractionDigits: Int(token.token.decimals) - ) ?? token.token.symbol + ) + + if let amount { + subtitle = "\(amount) • \(formattedMint)" + } else { + subtitle = "\(token.token.symbol) • \(formattedMint))" + } } else { - subtitle = token.token.symbol + subtitle = "\(token.token.symbol) • \(formattedMint))" } } @@ -35,10 +44,17 @@ struct ChooseSwapTokenItemView: View { .cornerRadius(radius: 48 / 2, corners: .allCorners) VStack(alignment: .leading, spacing: 4) { - Text(token.token.name) - .font(uiFont: .font(of: .text3, weight: .bold)) - .foregroundColor(Color(.night)) - .lineLimit(1) + HStack { + Text(token.token.name) + .font(uiFont: .font(of: .text3, weight: .bold)) + .foregroundColor(Color(.night)) + .lineLimit(1) + + Text(token.isNonStrict ? " ⚠" : "") + .font(uiFont: .font(of: .text3, weight: .bold)) + .foregroundColor(Color(.night)) + .lineLimit(1) + } Text(subtitle) .apply(style: .label1) .foregroundColor(Color(.mountain))