55// Created by Qiwei Li on 6/8/22.
66//
77
8+ import BigInt
89import SwiftUI
910import web3
10- import BigInt
1111
1212struct PasswordList : View {
1313 @EnvironmentObject var userAccountModel : UserAccountModel
@@ -16,14 +16,10 @@ struct PasswordList: View {
1616 @EnvironmentObject var transactionModel : TransactionModel
1717 @EnvironmentObject var passwordListModel : PasswordListModel
1818
19-
20-
2119 var body : some View {
22- Group {
23- NavigationLink ( destination: PasswordForm ( ) , tag: 1 , selection: $passwordListModel. selection) {
24-
25- }
26- List {
20+ Group {
21+ NavigationLink ( destination: PasswordForm ( ) , tag: 1 , selection: $passwordListModel. selection) { }
22+ List {
2723 Section ( header: Text ( " Blockchain info " ) ) {
2824 InfoCard ( title: " BlockNumber " , subtitle: " \( passwordListModel. blockNumber) " , color: . orange, unit: " blocks " , icon: . boltBatteryblock)
2925
@@ -32,32 +28,29 @@ struct PasswordList: View {
3228 InfoCard ( title: " Password Count " , subtitle: " \( passwordListModel. passwordSize) " , color: . orange, unit: " " , icon: . boltBatteryblock)
3329 }
3430
35-
3631 Section ( header: Text ( " Passwords " ) ) {
37- ForEach ( passwordListModel. passwords) { password in
32+ ForEach ( passwordListModel. passwords) { password in
3833 NavigationLink ( destination: PasswordForm ( editMode: false , password: password) ) {
3934 PasswordRow ( password: password)
4035 }
4136 }
42- . onDelete {
37+ . onDelete {
4338 indexSet in
44- Task {
39+ Task {
4540 await delete ( at: indexSet)
4641 }
4742 }
48-
4943 }
50-
5144 }
5245 . listStyle ( InsetGroupedListStyle ( ) )
53- . toolbar {
54- ToolbarItem ( placement: . navigationBarLeading) {
55- Button ( action: { userAccountModel. resetAccount ( ) } ) {
46+ . toolbar {
47+ ToolbarItem ( placement: . navigationBarLeading) {
48+ Button ( action: { userAccountModel. resetAccount ( ) } ) {
5649 Image ( systemSymbol: . externaldriveFill)
5750 }
5851 }
59- ToolbarItem ( placement: . navigationBarTrailing) {
60- Button ( action: { passwordListModel. selection = 1 } ) {
52+ ToolbarItem ( placement: . navigationBarTrailing) {
53+ Button ( action: { passwordListModel. selection = 1 } ) {
6154 Image ( systemSymbol: . plus)
6255 }
6356 }
@@ -68,18 +61,18 @@ struct PasswordList: View {
6861 print ( " BlockNumber: \( passwordListModel. blockNumber) " )
6962 await fetchBlockchainData ( isRefresh: true )
7063 }
71- . onReceive ( userAccountModel. $userAccount) { _ in
64+ . onReceive ( userAccountModel. $userAccount) { _ in
7265 Task {
7366 await fetchBlockchainData ( )
7467 }
7568 }
76- . onReceive ( transactionModel. $transactionReceipt) {
69+ . onReceive ( transactionModel. $transactionReceipt) {
7770 _ in
7871 Task {
7972 await fetchBlockchainData ( )
8073 }
8174 }
82- . alert ( isPresented: $passwordListModel. hasError) {
75+ . alert ( isPresented: $passwordListModel. hasError) {
8376 Alert ( title: Text ( " Error " ) , message: Text ( passwordListModel. error!) , dismissButton: . default( Text ( " ok " ) ) )
8477 }
8578 . task {
@@ -92,7 +85,7 @@ struct PasswordList: View {
9285
9386 func delete( at offsets: IndexSet ) async {
9487 let index = offsets. first
95- if let cryptoPass = cryptoPassModel. client{
88+ if let cryptoPass = cryptoPassModel. client {
9689 if let index = index {
9790 let transaction = try ! await cryptoPass. prepareDeleteTransaction ( at: BigUInt ( index) )
9891 transactionModel. showConfirmation ( transaction: transaction)
@@ -105,17 +98,17 @@ struct PasswordList: View {
10598 */
10699 private func fetchBlockchainData( isRefresh: Bool = false ) async {
107100 passwordListModel. update ( )
108- withAnimation {
109- Task {
110- do {
111- if let userAccount = userAccountModel. userAccount{
101+ withAnimation {
102+ Task {
103+ do {
104+ if let userAccount = userAccountModel. userAccount {
112105 passwordListModel. blockNumber = try await ethereumModel. ethereumClient. eth_blockNumber ( )
113- passwordListModel. accountBalance = BigInt ( try await ethereumModel. ethereumClient. eth_getBalance ( address: userAccount. address, block: EthereumBlock . Latest) )
106+ passwordListModel. accountBalance = try BigInt ( await ethereumModel. ethereumClient. eth_getBalance ( address: userAccount. address, block: EthereumBlock . Latest) )
114107
115- if let cryptoPass = cryptoPassModel. client{
108+ if let cryptoPass = cryptoPassModel. client {
116109 passwordListModel. passwordSize = try await cryptoPass. getSecretSize ( )
117- if passwordListModel. passwordSize > 0 {
118- passwordListModel. passwords = try await cryptoPass. getSecretsInRange ( start: 0 , end: passwordListModel. passwordSize) . map {
110+ if passwordListModel. passwordSize > 0 {
111+ passwordListModel. passwords = try await cryptoPass. getSecretsInRange ( start: 0 , end: passwordListModel. passwordSize) . map {
119112 password in
120113 CommonPassword . from ( password: password) !
121114 }
@@ -125,7 +118,7 @@ struct PasswordList: View {
125118 }
126119 }
127120
128- } catch {
121+ } catch {
129122 print ( error. localizedDescription)
130123 passwordListModel. hasError = true
131124 passwordListModel. error = error. localizedDescription
0 commit comments