From 04d9c767ded300181e93f3b07744ddb0c5921675 Mon Sep 17 00:00:00 2001 From: John Melody Me Date: Fri, 10 Feb 2023 04:11:02 +0800 Subject: [PATCH] #1 completed Tron Scanner --- Cryato/Controllers/TransactionsHelper.swift | 15 +++- Cryato/Models/Transactions.swift | 8 +- Cryato/Views/ScannerView.swift | 99 ++++++++++++--------- 3 files changed, 76 insertions(+), 46 deletions(-) diff --git a/Cryato/Controllers/TransactionsHelper.swift b/Cryato/Controllers/TransactionsHelper.swift index d468022..97f646c 100644 --- a/Cryato/Controllers/TransactionsHelper.swift +++ b/Cryato/Controllers/TransactionsHelper.swift @@ -8,5 +8,18 @@ import Foundation class TransactionsHelper : ObservableObject { - + public static func loadTransactions(WalletID :String, completion: @escaping (Transactions) -> ()) { + guard let url = URL(string: "https://api.trongrid.io/v1/accounts/\(WalletID)/transactions/trc20") else { + print("The url is not working...") + return + } + + URLSession.shared.dataTask(with: url) { data, response, error in + let result = try! JSONDecoder().decode(Transactions.self, from: data!) + + DispatchQueue.main.async { + completion(result) + } + }.resume() + } } diff --git a/Cryato/Models/Transactions.swift b/Cryato/Models/Transactions.swift index eb2e262..a2f0b2a 100644 --- a/Cryato/Models/Transactions.swift +++ b/Cryato/Models/Transactions.swift @@ -7,11 +7,15 @@ import Foundation -struct Transactions : Decodable { - public var id :String +struct Transaction : Codable, Identifiable { + public var id :String? public var transaction_id :String public var block_timestamp :Int64 public var from :String public var to :String public var value :String } + +struct Transactions : Codable { + public var data :[Transaction]? +} diff --git a/Cryato/Views/ScannerView.swift b/Cryato/Views/ScannerView.swift index 2f7359c..fbbd164 100644 --- a/Cryato/Views/ScannerView.swift +++ b/Cryato/Views/ScannerView.swift @@ -6,11 +6,14 @@ // import SwiftUI +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif struct ScannerView: View { @Environment(\.colorScheme) private var colorScheme - @State private var transactions :[Transactions] = [Transactions]() + @State private var transaction :[Transaction] = [Transaction]() @State private var walletId :String = "" private var validator :FormValidator = FormValidator() @@ -20,36 +23,14 @@ struct ScannerView: View { return } - private func getTransactions() -> Void { - guard let url = URL( - string: "https://api.trongrid.io/v1/accounts/\(self.walletId)/transactions/trc20" - ) else { - print("Url Malfunction or Invalid") - return - } + @available(iOS, deprecated: 6.0, obsoleted: 7.0, message: "No longer in need") + private func parseData(_ json: Data) -> Void { + let decoder = JSONDecoder() - let request = URLRequest(url: url) + if (try? decoder.decode(Transactions.self, from: json)) != nil { + // self.transaction = listOfTransactions.data + } - URLSession.shared.dataTask(with: request) { data, response, error in - if let data = data { - let decoder = JSONDecoder() - - decoder.dateDecodingStrategy = .iso8601 - - if let decodedResponse = try? - decoder.decode([Transactions].self, from: data) { - DispatchQueue.main.async { - self.transactions = decodedResponse - } - - return - } - - } - - print ("Fetch url failed") - - }.resume() } var body: some View { @@ -64,11 +45,17 @@ struct ScannerView: View { try! self.validate(input, 0x0) } - Button("Search") { - print(self.walletId) + Button(action: { + /** Return API to Listview */ - self.getTransactions() - }.foregroundColor(.green) + TransactionsHelper.loadTransactions(WalletID: self.walletId) { (result) in + self.transaction = result.data ?? [] + } + }) { + Image(systemName: "magnifyingglass") + .foregroundColor(.black) + .frame(height:20) + } } } .navigationBarTitle("Scanner") @@ -77,20 +64,46 @@ struct ScannerView: View { .refreshable { self.walletId = "" } .frame(maxHeight: 60) - List { - Section { - ForEach(0..<5) { i in - Section { - Text("A new row \(i)") - } - } + List(self.transaction) { data in + + VStack(alignment: .leading) { + Text("Transaction ID: \(data.transaction_id)") + .listRowSeparator(.hidden) + .font(Font.system(size: 12)) + + Text("Transaction FROM: \(data.from)") + .listRowSeparator(.hidden) + .font(Font.system(size: 12)) + + Text("Transaction TO: \(data.to)") + .listRowSeparator(.hidden) + .font(Font.system(size: 12)) + + Text("Transaction VALUE: \(data.value)") + .listRowSeparator(.hidden) + .font(Font.system(size: 12)) + + Text("BLOCK TIMESTAMP: \(data.block_timestamp)") + .listRowSeparator(.hidden) + .font(Font.system(size: 12)) } + .padding() + .overlay(RoundedRectangle(cornerRadius: 5) + .stroke(.gray.opacity(0.3), lineWidth: 1)) } + .onAppear { + /** Remove Underline */ + UITableView.appearance().separatorStyle = .none + } + .listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)) + .listRowSeparator(.hidden) .background(.gray.opacity(0.0)) - .onAppear(perform: self.getTransactions) - .listStyle(.insetGrouped) + .listStyle(.plain) + } + .scrollContentBackground(.hidden) + .refreshable { + self.walletId = "" } - } } }