Skip to content

Commit

Permalink
#1 completed Tron Scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
ctkqiang committed Feb 9, 2023
1 parent aa0cf3e commit 04d9c76
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 46 deletions.
15 changes: 14 additions & 1 deletion Cryato/Controllers/TransactionsHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
8 changes: 6 additions & 2 deletions Cryato/Models/Transactions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]?
}
99 changes: 56 additions & 43 deletions Cryato/Views/ScannerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 {
Expand All @@ -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")
Expand All @@ -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 = ""
}

}
}
}
Expand Down

0 comments on commit 04d9c76

Please sign in to comment.