Skip to content

Commit

Permalink
Merge branch 'release/2.11.1' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
bigearsenal committed Feb 2, 2024
2 parents e0207e3 + b3039dc commit 9975a30
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/ashleymills/Reachability.swift.git",
"state" : {
"revision" : "c01bbdf2d633cf049ae1ed1a68a2020a8bda32e2",
"version" : "5.1.0"
"revision" : "c01127cb51f591045696128effe43c16840d08bf",
"version" : "5.2.0"
}
},
{
Expand Down
78 changes: 50 additions & 28 deletions p2p_wallet/Scenes/Main/Common/ChooseItem/ChooseItemViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ final class ChooseItemViewModel: BaseViewModel, ObservableObject {

@Injected private var notifications: NotificationService

var searchingTask: Task<Void, Error>?

init(service: ChooseItemService, chosenToken: any ChooseItemSearchableItem) {
self.chosenToken = chosenToken
self.service = service
Expand All @@ -33,6 +35,9 @@ private extension ChooseItemViewModel {
func bind() {
service.state
.receive(on: DispatchQueue.main)
.removeDuplicates { lhs, rhs in
lhs.status == rhs.status && lhs.value.count == rhs.value.count
}
.sink { [weak self] state in
guard let self else { return }
switch state.status {
Expand All @@ -46,7 +51,7 @@ private extension ChooseItemViewModel {
self.allItems = self.service.sort(items: dataWithoutChosen)

if !self.isSearchGoing {
self.sections = self.allItems
self.sections = self.allItems.filter { $0.items.count < 100 }
}
}

Expand All @@ -66,35 +71,52 @@ private extension ChooseItemViewModel {
.store(in: &subscriptions)

$searchText
.debounce(for: .seconds(0.5), scheduler: RunLoop.main)
.subscribe(on: DispatchQueue.global(qos: .userInitiated))
.map { [weak self] (input: String) -> (String, [ChooseItemListSection]) in
if input.isEmpty {
return (input, [])
} else {
guard let self else { return (input, []) }
// Do not split up sections if there is a keyword
let searchedItems = self.allItems
.flatMap(\.items)
.filter { $0.matches(keyword: input.lowercased()) }
let result = self.service.sortFiltered(
by: input.lowercased(),
items: [ChooseItemListSection(items: searchedItems)]
)

return (input, result)
}
.debounce(for: .seconds(1), scheduler: RunLoop.main)
.sink { [weak self] value in
self?.searchingTask(keyword: value)
}
.receive(on: RunLoop.main)
.sinkAsync(receiveValue: { [weak self] value, result in
guard let self else { return }
self.isSearchGoing = !value.isEmpty
if value.isEmpty {
self.sections = self.allItems
} else {
.store(in: &subscriptions)
}

func searchingTask(keyword value: String) {
if searchingTask != nil {
searchingTask?.cancel()
}

searchingTask = Task {
try Task.checkCancellation()
self.isSearchGoing = !value.isEmpty

if value.isEmpty {
try Task.checkCancellation()

await MainActor.run {
self.sections = self.allItems.filter { $0.items.count < 100 }
}
} else {
// Do not split up sections if there is a keyword
var searchedItems: [any ChooseItemSearchableItem] = []

for section in self.allItems {
for item in section.items {
if item.matches(keyword: value.lowercased()) {
try Task.checkCancellation()
searchedItems.append(item)
}
}
}

try Task.checkCancellation()
let result = self.service.sortFiltered(
by: value.lowercased(),
items: [ChooseItemListSection(items: searchedItems)]
)

try Task.checkCancellation()
await MainActor.run {
self.sections = result
}
})
.store(in: &subscriptions)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
}
}

Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ configFiles:

settings:
base:
MARKETING_VERSION: 2.11.0
MARKETING_VERSION: 2.11.1
configs:
Debug:
VALIDATE_PRODUCT: false
Expand Down

0 comments on commit 9975a30

Please sign in to comment.