-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
479 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
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
190 changes: 190 additions & 0 deletions
190
UnstoppableWallet/UnstoppableWallet/Modules/Coin/Rank/RankView.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,190 @@ | ||
import Kingfisher | ||
import MarketKit | ||
import SwiftUI | ||
|
||
struct RankView: View { | ||
@StateObject var viewModel: RankViewModel | ||
@StateObject var watchlistViewModel: WatchlistViewModel | ||
@Binding var isPresented: Bool | ||
|
||
@State private var presentedCoin: Coin? | ||
@State private var timePeriodSelectorPresented = false | ||
|
||
init(isPresented: Binding<Bool>, type: CoinRankModule.RankType) { | ||
_viewModel = StateObject(wrappedValue: RankViewModel(type: type)) | ||
_watchlistViewModel = StateObject(wrappedValue: WatchlistViewModel(page: type.statRankType)) | ||
_isPresented = isPresented | ||
} | ||
|
||
var body: some View { | ||
ThemeNavigationView { | ||
ThemeView { | ||
switch viewModel.state { | ||
case .loading: | ||
VStack(spacing: 0) { | ||
header() | ||
Spacer() | ||
ProgressView() | ||
Spacer() | ||
} | ||
case let .loaded(items): | ||
ThemeList(bottomSpacing: .margin16) { | ||
header() | ||
.listRowBackground(Color.clear) | ||
.listRowInsets(EdgeInsets()) | ||
.listRowSeparator(.hidden) | ||
|
||
list(items: items) | ||
} | ||
case .failed: | ||
VStack(spacing: 0) { | ||
header() | ||
|
||
SyncErrorView { | ||
viewModel.sync() | ||
} | ||
} | ||
} | ||
} | ||
.navigationBarTitleDisplayMode(.inline) | ||
.toolbar { | ||
ToolbarItem(placement: .navigationBarTrailing) { | ||
Button("button.close".localized) { | ||
isPresented = false | ||
} | ||
} | ||
} | ||
.sheet(item: $presentedCoin) { coin in | ||
CoinPageViewNew(coinUid: coin.uid).ignoresSafeArea() | ||
.onFirstAppear { stat(page: viewModel.type.statRankType, event: .openCoin(coinUid: coin.uid)) } | ||
} | ||
} | ||
} | ||
|
||
@ViewBuilder private func header() -> some View { | ||
VStack(spacing: 0) { | ||
HStack(spacing: .margin32) { | ||
VStack(spacing: .margin8) { | ||
Text(viewModel.type.title.localized).themeHeadline1() | ||
Text(viewModel.type.description.localized).themeSubhead2() | ||
} | ||
.padding(.vertical, .margin12) | ||
|
||
KFImage.url(URL(string: viewModel.type.imageUid.headerImageUrl)) | ||
.resizable() | ||
.frame(width: 76, height: 108) | ||
} | ||
.padding(.leading, .margin16) | ||
|
||
Rectangle() | ||
.fill(Color.themeSteel10) | ||
.frame(height: .heightOneDp) | ||
.frame(maxWidth: .infinity) | ||
} | ||
} | ||
|
||
@ViewBuilder private func listHeader(disabled: Bool = false) -> some View { | ||
ScrollView(.horizontal, showsIndicators: false) { | ||
HStack { | ||
Button(action: { | ||
viewModel.sortOrder.toggle() | ||
}) { | ||
Text(viewModel.type.sortingField.localized) | ||
} | ||
.buttonStyle(SecondaryButtonStyle(style: .default, rightAccessory: .custom(image: sortIcon()))) | ||
.disabled(disabled) | ||
|
||
if viewModel.timePeriods.count > 1 { | ||
Button(action: { | ||
timePeriodSelectorPresented = true | ||
}) { | ||
Text(viewModel.timePeriod.shortTitle) | ||
} | ||
.buttonStyle(SecondaryButtonStyle(style: .default, rightAccessory: .dropDown)) | ||
.disabled(disabled) | ||
} | ||
} | ||
.padding(.horizontal, .margin16) | ||
.padding(.vertical, .margin8) | ||
} | ||
.alert( | ||
isPresented: $timePeriodSelectorPresented, | ||
title: "market.time_period.title".localized, | ||
viewItems: viewModel.timePeriods.map { .init(text: $0.title, selected: viewModel.timePeriod == $0) }, | ||
onTap: { index in | ||
guard let index else { | ||
return | ||
} | ||
|
||
viewModel.timePeriod = viewModel.timePeriods[index] | ||
} | ||
) | ||
} | ||
|
||
@ViewBuilder private func list(items: [RankViewModel.Item]) -> some View { | ||
Section { | ||
ListForEach(items) { item in | ||
let coin = item.coin | ||
|
||
ClickableRow(action: { | ||
presentedCoin = item.coin | ||
}) { | ||
itemContent( | ||
index: item.index, | ||
coin: coin, | ||
value: item.value | ||
) | ||
} | ||
.watchlistSwipeActions(viewModel: watchlistViewModel, coinUid: coin.uid) | ||
} | ||
} header: { | ||
listHeader() | ||
.listRowInsets(EdgeInsets()) | ||
.background(Color.themeTyler) | ||
} | ||
} | ||
|
||
@ViewBuilder private func loadingList() -> some View { | ||
Section { | ||
ListForEach(Array(0 ... 10)) { index in | ||
ListRow { | ||
itemContent( | ||
index: 1, | ||
coin: nil, | ||
value: 12345.45 | ||
) | ||
.redacted() | ||
} | ||
} | ||
} header: { | ||
listHeader(disabled: true) | ||
.listRowInsets(EdgeInsets()) | ||
.background(Color.themeTyler) | ||
} | ||
} | ||
|
||
@ViewBuilder private func itemContent(index: Int, coin: Coin?, value: Decimal) -> some View { | ||
Text(index.description) | ||
.textCaptionSB() | ||
.frame(minWidth: 24, alignment: .center) | ||
|
||
CoinIconView(coin: coin) | ||
|
||
VStack(alignment: .leading, spacing: 1) { | ||
Text(coin?.code ?? "CODE").textBody() | ||
Text(coin?.name ?? "COIN NAME").textSubhead2() | ||
} | ||
|
||
Spacer() | ||
if let formatted = ValueFormatter.instance.formatShort(currency: viewModel.currency, value: value) { | ||
Text(formatted).textBody() | ||
} | ||
} | ||
|
||
private func sortIcon() -> Image { | ||
switch viewModel.sortOrder { | ||
case .asc: return Image("arrow_medium_2_up_20") | ||
case .desc: return Image("arrow_medium_2_down_20") | ||
} | ||
} | ||
} |
Oops, something went wrong.