Skip to content

Commit e017cbf

Browse files
committed
first commit
1 parent 54e2516 commit e017cbf

File tree

15 files changed

+469
-19
lines changed

15 files changed

+469
-19
lines changed
Loading

Onout Watch App/Assets.xcassets/AppIcon.appiconset/Contents.json renamed to Onout Watch App/Assets.xcassets/WatchAppIcon.appiconset/Contents.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"images" : [
33
{
4+
"filename" : "1024.png",
45
"idiom" : "universal",
56
"platform" : "watchos",
67
"size" : "1024x1024"

Onout Watch App/Balance.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// Balance.swift
3+
// Onout Watch App
4+
//
5+
// Created by Никита Иванов on 03.10.2023.
6+
//
7+
8+
import Foundation
9+
10+
struct Balance: Codable {
11+
let totalUsd: Double
12+
13+
enum CodingKeys: String, CodingKey {
14+
case totalUsd = "total_usd"
15+
}
16+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//
2+
// BalanceViewModel.swift
3+
// Onout Watch App
4+
//
5+
// Created by Никита Иванов on 03.10.2023.
6+
//
7+
8+
import Foundation
9+
10+
11+
class BalanceViewModel: ObservableObject {
12+
@Published var balance = 0.0
13+
private var wallet = ""
14+
15+
init() {
16+
self.wallet = UserDefaults.standard.string(forKey: "wallet") ?? "0x873351e707257C28eC6fAB1ADbc850480f6e0633"
17+
self.balance = UserDefaults.standard.double(forKey: "balance")
18+
fetchBalance()
19+
}
20+
21+
func updateWallet(wallet: String) {
22+
UserDefaults.standard.set(wallet, forKey: "wallet")
23+
self.wallet = wallet
24+
fetchBalance()
25+
}
26+
27+
28+
func fetchBalance() {
29+
guard let url = URL(string: "https://dashapi.onout.org/debank?address=\(self.wallet)&app=itracker") else {
30+
return
31+
}
32+
33+
URLSession.shared.dataTask(with: url) { data, response, error in
34+
guard let data = data else {
35+
return
36+
}
37+
38+
do {
39+
let json = try JSONSerialization.jsonObject(with: data, options: [])
40+
if let dict = json as? [String: Any], let totalUsd = dict["total_usd"] as? Double {
41+
DispatchQueue.main.async {
42+
self.balance = totalUsd
43+
UserDefaults.standard.set(totalUsd, forKey: "balance")
44+
45+
46+
}
47+
}
48+
} catch {
49+
print(error.localizedDescription)
50+
}
51+
}.resume()
52+
}
53+
54+
func fetchBalance(completion: @escaping (Double) -> Void) {
55+
guard let url = URL(string: "https://dashapi.onout.org/debank?address=\(self.wallet)&app=itracker") else {
56+
return
57+
}
58+
59+
URLSession.shared.dataTask(with: url) { data, response, error in
60+
guard let data = data else {
61+
return
62+
}
63+
64+
do {
65+
let json = try JSONSerialization.jsonObject(with: data, options: [])
66+
if let dict = json as? [String: Any], let totalUsd = dict["total_usd"] as? Double {
67+
DispatchQueue.main.async {
68+
self.balance = totalUsd
69+
UserDefaults.standard.set(totalUsd, forKey: "balance")
70+
completion(totalUsd)
71+
}
72+
}
73+
} catch {
74+
print(error.localizedDescription)
75+
}
76+
}.resume()
77+
}
78+
}

Onout Watch App/ContentView.swift

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,32 @@
88
import SwiftUI
99

1010
struct ContentView: View {
11+
@StateObject private var viewModel = BalanceViewModel()
12+
13+
@State private var inputString = ""
14+
@State private var showingInputDialog = false
15+
1116
var body: some View {
1217
VStack {
13-
Image(systemName: "globe")
14-
.imageScale(.large)
15-
.foregroundStyle(.tint)
16-
Text("Hello, world!")
18+
Text("Balance:")
19+
.font(.headline)
20+
.padding()
21+
Text("$\(Int(viewModel.balance))")
22+
.font(.title)
23+
.padding()
24+
25+
TextField("Enter new wallet", text: $inputString, onCommit: {
26+
viewModel.updateWallet(wallet: inputString)
27+
28+
})
29+
.textFieldStyle(.plain)
30+
31+
.padding()
1732
}
18-
.padding()
1933
}
2034
}
2135

36+
2237
#Preview {
2338
ContentView()
2439
}

Onout Watch App/Info.plist

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>NSExtension</key>
6+
<dict>
7+
<key>NSExtensionPointIdentifier</key>
8+
<string>com.apple.widgetkit-extension</string>
9+
</dict>
10+
</dict>
11+
</plist>

0 commit comments

Comments
 (0)