@@ -53,6 +53,16 @@ struct TransactionCell: View {
53
53
struct TransactionView : View {
54
54
var wallet : WalletContract
55
55
var assetId : String = CitadelVault . embedded. nativeAsset. id
56
+ var asset : Asset {
57
+ CitadelVault . embedded. assets [ assetId] !
58
+ }
59
+
60
+ private struct AddressBalance : Identifiable {
61
+ public var id : String { address }
62
+ public let address : String
63
+ public var amount : Double
64
+ public var utxo : [ ( OutPoint , Double ) ]
65
+ }
56
66
57
67
private var assetIdModified : String ? {
58
68
assetId == CitadelVault . embedded. nativeAsset. id ? nil : assetId
@@ -65,35 +75,86 @@ struct TransactionView: View {
65
75
}
66
76
return wallet. operations. filter { $0. assetId == assetIdModified }
67
77
}
78
+ private var addressBalances : [ AddressBalance ] {
79
+ Array ( ( wallet. balance ( of: assetId) ? . unspentAllocations. reduce ( into: [ String: AddressBalance] ( ) ) { ( balances: inout [ String : AddressBalance ] , allocation) in
80
+ guard let address = allocation. address else { return }
81
+ var balance = balances [ address] ?? AddressBalance ( address: address, amount: 0 , utxo: [ ] )
82
+ balance. amount += allocation. amount
83
+ balance. utxo. append ( ( allocation. outpoint, allocation. amount) )
84
+ balances [ address] = balance
85
+ } ?? [ : ] ) . values)
86
+ }
68
87
88
+ enum SelectedTab : Hashable {
89
+ case history, balance
90
+
91
+ var title : String {
92
+ switch self {
93
+ case . balance: return " Balance "
94
+ case . history: return " History "
95
+ }
96
+ }
97
+ }
98
+
99
+ @State private var selectedTab : SelectedTab = . history
69
100
@State private var scannedInvoice : Invoice ? = nil
70
101
@State private var scannedString : String = " "
71
102
@State var presentedSheet : PresentedSheet ?
72
- private let placement : ToolbarItemPlacement = {
73
- #if os(iOS)
74
- return ToolbarItemPlacement . navigationBarLeading
75
- #else
76
- return ToolbarItemPlacement . primaryAction
77
- #endif
78
- } ( )
79
103
80
104
var body : some View {
81
- List ( operations) { transaction in
82
- TransactionCell ( transaction: transaction)
105
+ List {
106
+ if selectedTab == . history {
107
+ ForEach ( operations) { transaction in
108
+ TransactionCell ( transaction: transaction)
109
+ }
110
+ } else {
111
+ ForEach ( addressBalances) { balance in
112
+ Section ( header: Text ( " Address " ) ) {
113
+ Copyable ( text: balance. address) {
114
+ BechBrief ( text: balance. address)
115
+ }
116
+ DetailsCell ( title: " Balance: " , details: " \( balance. amount) \( asset. ticker) " , clipboardCopy: true )
117
+ ForEach ( balance. utxo, id: \. 0 ) { ( outpoint, amount) in
118
+ HStack ( alignment: . center) {
119
+ VStack ( alignment: . leading) {
120
+ Text ( outpoint. txid)
121
+ . font ( . subheadline)
122
+ . truncationMode ( . middle)
123
+ . lineLimit ( 1 )
124
+ HStack ( alignment: . lastTextBaseline) {
125
+ Text ( " Output number: " ) . font ( . caption) . foregroundColor ( . secondary)
126
+ Text ( " \( outpoint. vout) " ) . font ( . subheadline)
127
+ }
128
+ }
129
+ Spacer ( )
130
+ Text ( " \( amount) \( asset. ticker) " ) . font ( . subheadline)
131
+ } . padding ( . leading)
132
+ }
133
+ }
134
+ }
135
+ }
83
136
}
84
- . navigationTitle ( " History " )
137
+ . listStyle ( GroupedListStyle ( ) )
138
+ . navigationTitle ( selectedTab. title)
85
139
. toolbar {
86
- ToolbarItemGroup ( placement: placement ) {
140
+ ToolbarItem ( placement: . confirmationAction ) {
87
141
Button ( " Receive " ) { presentedSheet = . invoice( wallet, assetId) }
88
142
. background ( Color . accentColor)
89
143
. foregroundColor ( . white)
90
144
. cornerRadius ( 13 )
91
- Spacer ( )
145
+ }
146
+ ToolbarItem ( placement: . cancellationAction) {
92
147
Button ( " Send " ) { presentedSheet = . scan( " invoice " , . invoice) }
93
148
. background ( Color . accentColor)
94
149
. foregroundColor ( . white)
95
150
. cornerRadius ( 13 )
96
151
}
152
+ ToolbarItem ( placement: . principal) {
153
+ Picker ( selection: $selectedTab, label: EmptyView ( ) ) {
154
+ Text ( " History " ) . tag ( SelectedTab . history)
155
+ Text ( " Balance " ) . tag ( SelectedTab . balance)
156
+ } . pickerStyle ( SegmentedPickerStyle ( ) )
157
+ }
97
158
}
98
159
. sheet ( item: $presentedSheet) { item in
99
160
switch item {
0 commit comments