-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate MarketAdvancedSearchResults module to SwiftUI
- Loading branch information
Showing
7 changed files
with
186 additions
and
147 deletions.
There are no files selected for viewing
38 changes: 12 additions & 26 deletions
38
UnstoppableWallet/UnstoppableWallet.xcodeproj/project.pbxproj
Large diffs are not rendered by default.
Oops, something went wrong.
121 changes: 121 additions & 0 deletions
121
...let/UnstoppableWallet/Modules/Market/AdvancedSearch/MarketAdvancedSearchResultsView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import Kingfisher | ||
import MarketKit | ||
import SwiftUI | ||
|
||
struct MarketAdvancedSearchResultsView: View { | ||
@StateObject var viewModel: MarketAdvancedSearchResultsViewModel | ||
@StateObject var watchlistViewModel: WatchlistViewModel | ||
@Binding var isParentPresented: Bool | ||
|
||
@State private var sortBySelectorPresented = false | ||
@State private var presentedFullCoin: FullCoin? | ||
|
||
init(marketInfos: [MarketInfo], timePeriod: HsTimePeriod, isParentPresented: Binding<Bool>) { | ||
_viewModel = StateObject(wrappedValue: MarketAdvancedSearchResultsViewModel(marketInfos: marketInfos, timePeriod: timePeriod)) | ||
_watchlistViewModel = StateObject(wrappedValue: WatchlistViewModel(page: .advancedSearchResults)) | ||
_isParentPresented = isParentPresented | ||
} | ||
|
||
var body: some View { | ||
ThemeView { | ||
VStack(spacing: 0) { | ||
header() | ||
|
||
ThemeList(viewModel.marketInfos, bottomSpacing: .margin16) { marketInfo in | ||
let coin = marketInfo.fullCoin.coin | ||
|
||
ClickableRow(action: { | ||
presentedFullCoin = marketInfo.fullCoin | ||
}) { | ||
itemContent( | ||
coin: coin, | ||
marketCap: marketInfo.marketCap, | ||
price: marketInfo.price.flatMap { ValueFormatter.instance.formatFull(currency: viewModel.currency, value: $0) } ?? "n/a".localized, | ||
rank: marketInfo.marketCapRank, | ||
diff: marketInfo.priceChangeValue(timePeriod: viewModel.timePeriod) | ||
) | ||
} | ||
.watchlistSwipeActions(viewModel: watchlistViewModel, coinUid: coin.uid) | ||
} | ||
} | ||
} | ||
.navigationTitle("market.advanced_search_results.title".localized) | ||
.navigationBarTitleDisplayMode(.inline) | ||
.toolbar { | ||
ToolbarItem(placement: .navigationBarTrailing) { | ||
Button("button.close".localized) { | ||
isParentPresented = false | ||
} | ||
} | ||
} | ||
.sheet(item: $presentedFullCoin) { fullCoin in | ||
CoinPageViewNew(coinUid: fullCoin.coin.uid).ignoresSafeArea() | ||
.onFirstAppear { stat(page: .advancedSearchResults, event: .openCoin(coinUid: fullCoin.coin.uid)) } | ||
} | ||
.alert( | ||
isPresented: $sortBySelectorPresented, | ||
title: "market.sort_by.title".localized, | ||
viewItems: viewModel.sortBys.map { .init(text: $0.title, selected: viewModel.sortBy == $0) }, | ||
onTap: { index in | ||
guard let index else { | ||
return | ||
} | ||
|
||
viewModel.sortBy = viewModel.sortBys[index] | ||
} | ||
) | ||
} | ||
|
||
@ViewBuilder private func header() -> some View { | ||
ScrollView(.horizontal, showsIndicators: false) { | ||
HStack { | ||
Button(action: { | ||
sortBySelectorPresented = true | ||
}) { | ||
Text(viewModel.sortBy.title) | ||
} | ||
.buttonStyle(SecondaryButtonStyle(style: .default, rightAccessory: .dropDown)) | ||
} | ||
.padding(.horizontal, .margin16) | ||
.padding(.vertical, .margin8) | ||
} | ||
.alert( | ||
isPresented: $sortBySelectorPresented, | ||
title: "market.sort_by.title".localized, | ||
viewItems: viewModel.sortBys.map { .init(text: $0.title, selected: viewModel.sortBy == $0) }, | ||
onTap: { index in | ||
guard let index else { | ||
return | ||
} | ||
|
||
viewModel.sortBy = viewModel.sortBys[index] | ||
} | ||
) | ||
} | ||
|
||
@ViewBuilder private func itemContent(coin: Coin?, marketCap: Decimal?, price: String, rank: Int?, diff: Decimal?) -> some View { | ||
CoinIconView(coin: coin) | ||
|
||
VStack(spacing: 1) { | ||
HStack(spacing: .margin8) { | ||
Text(coin?.code ?? "CODE").textBody() | ||
Spacer() | ||
Text(price).textBody() | ||
} | ||
|
||
HStack(spacing: .margin8) { | ||
HStack(spacing: .margin4) { | ||
if let rank { | ||
BadgeViewNew(text: "\(rank)") | ||
} | ||
|
||
if let marketCap, let formatted = ValueFormatter.instance.formatShort(currency: viewModel.currency, value: marketCap) { | ||
Text(formatted).textSubhead2() | ||
} | ||
} | ||
Spacer() | ||
DiffText(diff) | ||
} | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...nstoppableWallet/Modules/Market/AdvancedSearch/MarketAdvancedSearchResultsViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import Combine | ||
import Foundation | ||
import MarketKit | ||
|
||
class MarketAdvancedSearchResultsViewModel: ObservableObject { | ||
private let currencyManager = App.shared.currencyManager | ||
private var cancellables = Set<AnyCancellable>() | ||
|
||
private let internalMarketInfos: [MarketInfo] | ||
let timePeriod: HsTimePeriod | ||
|
||
@Published var marketInfos: [MarketInfo] = [] | ||
|
||
var sortBy: MarketModule.SortBy = .highestCap { | ||
didSet { | ||
syncState() | ||
} | ||
} | ||
|
||
init(marketInfos: [MarketInfo], timePeriod: HsTimePeriod) { | ||
internalMarketInfos = marketInfos | ||
self.timePeriod = timePeriod | ||
|
||
syncState() | ||
} | ||
|
||
private func syncState() { | ||
marketInfos = internalMarketInfos.sorted(sortBy: sortBy, timePeriod: timePeriod) | ||
} | ||
} | ||
|
||
extension MarketAdvancedSearchResultsViewModel { | ||
var currency: Currency { | ||
currencyManager.baseCurrency | ||
} | ||
|
||
var sortBys: [MarketModule.SortBy] { | ||
[.highestCap, .lowestCap, .gainers, .losers] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 0 additions & 15 deletions
15
...eWallet/Modules/Market/MarketAdvancedSearchResults/MarketAdvancedSearchResultModule.swift
This file was deleted.
Oops, something went wrong.
67 changes: 0 additions & 67 deletions
67
...Wallet/Modules/Market/MarketAdvancedSearchResults/MarketAdvancedSearchResultService.swift
This file was deleted.
Oops, something went wrong.
38 changes: 0 additions & 38 deletions
38
...Modules/Market/MarketAdvancedSearchResults/MarketAdvancedSearchResultViewController.swift
This file was deleted.
Oops, something went wrong.