-
Notifications
You must be signed in to change notification settings - Fork 58
Add fiat transactions history on buy/sell screen #1810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
DRadmir
wants to merge
1
commit into
main
Choose a base branch
from
1807-add-fiat-transactions-history-on-buy-sell-screen
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
50 changes: 50 additions & 0 deletions
50
Features/FiatConnect/Sources/Scenes/FiatTransactionDetailScene.swift
This file contains hidden or 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,50 @@ | ||
| // Copyright (c). Gem Wallet. All rights reserved. | ||
|
|
||
| import SwiftUI | ||
| import Components | ||
| import Style | ||
| import PrimitivesComponents | ||
|
|
||
| public struct FiatTransactionDetailScene: View { | ||
| @State private var model: FiatTransactionDetailViewModel | ||
|
|
||
| public init(model: FiatTransactionDetailViewModel) { | ||
| _model = State(initialValue: model) | ||
| } | ||
|
|
||
| public var body: some View { | ||
| List { | ||
| TransactionHeaderListItemView(model: model.headerModel) | ||
|
|
||
| ForEach(model.sections) { section in | ||
| Section { | ||
| ForEach(section.values) { item in | ||
| content(for: model.item(for: item)) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| .contentMargins([.top], .small, for: .scrollContent) | ||
| .listSectionSpacing(.compact) | ||
| .background(Colors.grayBackground) | ||
| .bindQuery(model.query) | ||
| .navigationTitle(model.title) | ||
| } | ||
|
|
||
| @ViewBuilder | ||
| private func content(for itemModel: FiatTransactionDetailItemModel) -> some View { | ||
| switch itemModel { | ||
| case let .listItem(model): | ||
| ListItemView(model: model) | ||
| case let .listImageItem(title, subtitle, image): | ||
| ListItemImageView(title: title, subtitle: subtitle, assetImage: image) | ||
| case let .explorer(url, text): | ||
| SafariNavigationLink(url: url) { | ||
| Text(text) | ||
| .tint(Colors.black) | ||
| } | ||
| case .empty: | ||
| EmptyView() | ||
| } | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletions
44
Features/FiatConnect/Sources/Scenes/FiatTransactionsScene.swift
This file contains hidden or 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,44 @@ | ||
| // Copyright (c). Gem Wallet. All rights reserved. | ||
|
|
||
| import SwiftUI | ||
| import Components | ||
| import Primitives | ||
| import Store | ||
| import Style | ||
| import PrimitivesComponents | ||
|
|
||
| public struct FiatTransactionsScene: View { | ||
| @State private var model: FiatTransactionsViewModel | ||
|
|
||
| public init(model: FiatTransactionsViewModel) { | ||
| _model = State(initialValue: model) | ||
| } | ||
|
|
||
| public var body: some View { | ||
| List { | ||
| Section { } header: { | ||
| AssetPreviewView(model: model.assetModel) | ||
| .frame(maxWidth: .infinity) | ||
| .padding(.bottom, .small) | ||
| } | ||
| .cleanListRow() | ||
| ForEach(model.transactions) { transaction in | ||
| NavigationLink(value: Scenes.FiatTransaction(walletId: model.walletId, asset: model.asset, transaction: transaction)) { | ||
| ListItemView(model: FiatTransactionViewModel(transaction: transaction).listItemModel) | ||
| } | ||
| } | ||
| .listRowInsets(.assetListRowInsets) | ||
| } | ||
| .overlay { | ||
| if model.transactions.isEmpty { | ||
| EmptyContentView(model: model.emptyContentModel) | ||
| .padding(.horizontal, .medium) | ||
| } | ||
| } | ||
| .contentMargins(.top, .scene.top, for: .scrollContent) | ||
| .listSectionSpacing(.compact) | ||
| .bindQuery(model.query) | ||
| .navigationTitle(model.title) | ||
| .task { await model.fetch() } | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
37
Features/FiatConnect/Sources/Types/FiatTransactionDetailSection.swift
This file contains hidden or 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,37 @@ | ||
| // Copyright (c). Gem Wallet. All rights reserved. | ||
|
|
||
| import Foundation | ||
| import Components | ||
| import PrimitivesComponents | ||
|
|
||
| public enum FiatTransactionDetailSectionType: String, Identifiable, Equatable { | ||
| case details | ||
| case explorer | ||
|
|
||
| public var id: String { rawValue } | ||
| } | ||
|
|
||
| public enum FiatTransactionDetailItem: Identifiable, Equatable, Sendable { | ||
| case status | ||
| case provider | ||
| case explorerLink | ||
|
|
||
| public var id: Self { self } | ||
| } | ||
|
|
||
| public enum FiatTransactionDetailItemModel { | ||
| case listItem(ListItemModel) | ||
| case listImageItem(title: String, subtitle: String, image: AssetImage) | ||
| case explorer(url: URL, text: String) | ||
| case empty | ||
| } | ||
|
|
||
| extension FiatTransactionDetailItemModel: ItemModelProvidable { | ||
| public var itemModel: FiatTransactionDetailItemModel { self } | ||
| } | ||
|
|
||
| public extension ListSection where T == FiatTransactionDetailItem { | ||
| init(type: FiatTransactionDetailSectionType, _ items: [FiatTransactionDetailItem]) { | ||
| self.init(type: type, values: items) | ||
| } | ||
| } |
This file contains hidden or 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
113 changes: 113 additions & 0 deletions
113
Features/FiatConnect/Sources/ViewModels/FiatTransactionDetailViewModel.swift
This file contains hidden or 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,113 @@ | ||
| // Copyright (c). Gem Wallet. All rights reserved. | ||
|
|
||
| import Components | ||
| import ExplorerService | ||
| import Formatters | ||
| import Foundation | ||
| import Localization | ||
| import Primitives | ||
| import PrimitivesComponents | ||
| import Store | ||
| import Style | ||
| import SwiftUI | ||
|
|
||
| @Observable | ||
| @MainActor | ||
| public final class FiatTransactionDetailViewModel { | ||
| let asset: Asset | ||
| private let explorerService: ExplorerService | ||
|
|
||
| public let query: ObservableQuery<FiatTransactionRequest> | ||
| var transaction: FiatTransaction { query.value } | ||
|
|
||
| public init( | ||
| transaction: FiatTransaction, | ||
| walletId: WalletId, | ||
| asset: Asset, | ||
| explorerService: ExplorerService = .standard | ||
| ) { | ||
| self.asset = asset | ||
| self.explorerService = explorerService | ||
| self.query = ObservableQuery(FiatTransactionRequest(walletId: walletId, transaction: transaction), initialValue: transaction) | ||
| } | ||
|
|
||
| var title: String { | ||
| switch transaction.transactionType { | ||
| case .buy: Localized.Wallet.buy | ||
| case .sell: Localized.Wallet.sell | ||
| } | ||
| } | ||
|
|
||
| var headerModel: TransactionHeaderItemModel { | ||
| let color: Color = switch transaction.status { | ||
| case .failed: Colors.gray | ||
| case .pending, .complete, .unknown: Colors.white | ||
| } | ||
| let amountText = AmountDisplay.currency( | ||
| value: transaction.fiatAmount, | ||
| currencyCode: transaction.fiatCurrency, | ||
| textStyle: TextStyle(font: .body, color: color, fontWeight: .medium), | ||
| showSign: false | ||
| ) | ||
| let display = FiatAmountDisplay( | ||
| amount: amountText, | ||
| assetImage: AssetIdViewModel(assetId: asset.id).assetImage | ||
| ) | ||
| return TransactionHeaderItemModel( | ||
| headerType: .amount(.fiat(display)), | ||
| showClearHeader: true | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - ListSectionProvideable | ||
|
|
||
| extension FiatTransactionDetailViewModel: ListSectionProvideable { | ||
| public var sections: [ListSection<FiatTransactionDetailItem>] { | ||
| var sections = [ListSection(type: .details, [.status, .provider])] | ||
| if let _ = explorerLink { | ||
| sections.append(ListSection(type: .explorer, [.explorerLink])) | ||
| } | ||
| return sections | ||
| } | ||
|
|
||
| public func itemModel(for item: FiatTransactionDetailItem) -> any ItemModelProvidable<FiatTransactionDetailItemModel> { | ||
| switch item { | ||
| case .status: statusModel | ||
| case .provider: providerModel | ||
| case .explorerLink: explorerModel | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Private | ||
|
|
||
| extension FiatTransactionDetailViewModel { | ||
| private var statusModel: FiatTransactionDetailItemModel { | ||
| let model = FiatTransactionStatusViewModel(status: transaction.status) | ||
| return .listItem(ListItemModel( | ||
| title: Localized.Transaction.status, | ||
| titleTagType: transaction.status == .pending ? .progressView() : .none, | ||
| subtitle: model.title, | ||
| subtitleStyle: TextStyle(font: .callout, color: model.color) | ||
| )) | ||
| } | ||
|
|
||
| private var providerModel: FiatTransactionDetailItemModel { | ||
| .listImageItem( | ||
| title: Localized.Common.provider, | ||
| subtitle: transaction.providerId.displayName, | ||
| image: .image(transaction.providerId.image) | ||
| ) | ||
| } | ||
|
|
||
| private var explorerLink: BlockExplorerLink? { | ||
| guard let hash = transaction.transactionHash else { return nil } | ||
| return explorerService.transactionUrl(chain: asset.chain, hash: hash) | ||
| } | ||
|
|
||
| private var explorerModel: FiatTransactionDetailItemModel { | ||
| guard let link = explorerLink else { return .empty } | ||
| return .explorer(url: link.url, text: Localized.Transaction.viewOn(link.name)) | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
Features/FiatConnect/Sources/ViewModels/FiatTransactionsViewModel.swift
This file contains hidden or 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,41 @@ | ||
| // Copyright (c). Gem Wallet. All rights reserved. | ||
|
|
||
| import Foundation | ||
| import FiatTransactionService | ||
| import Localization | ||
| import Primitives | ||
| import PrimitivesComponents | ||
| import Store | ||
|
|
||
| @Observable | ||
| @MainActor | ||
| public final class FiatTransactionsViewModel { | ||
| private let service: FiatTransactionService | ||
| let walletId: WalletId | ||
| let asset: Asset | ||
|
|
||
| public let query: ObservableQuery<FiatTransactionsRequest> | ||
| var transactions: [FiatTransaction] { query.value } | ||
| var assetModel: AssetViewModel { AssetViewModel(asset: asset) } | ||
|
|
||
| public init(walletId: WalletId, asset: Asset, service: FiatTransactionService) { | ||
| self.walletId = walletId | ||
| self.asset = asset | ||
| self.service = service | ||
| self.query = ObservableQuery(FiatTransactionsRequest(walletId: walletId, assetId: asset.id), initialValue: []) | ||
| } | ||
|
|
||
| var title: String { Localized.Activity.title } | ||
|
|
||
| var emptyContentModel: EmptyContentTypeViewModel { | ||
| EmptyContentTypeViewModel(type: .activity(isViewOnly: false)) | ||
| } | ||
|
|
||
| func fetch() async { | ||
| do { | ||
| try await service.update(walletId: walletId) | ||
| } catch { | ||
| debugLog("FiatTransactionsViewModel fetch error: \(error)") | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Silently catching and logging the error here means the user receives no feedback if fetching transactions fails. They will see an empty or stale list, which can be confusing. It's better to expose this error state to the view so it can display an appropriate error message. Consider adding a state property to the view model to track fetching errors.