forked from Echo-Mirror-Butler/echomirror-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentView.swift
More file actions
91 lines (82 loc) · 2.99 KB
/
Copy pathContentView.swift
File metadata and controls
91 lines (82 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import EchoMirrorSDK
import SwiftUI
struct ContentView: View {
@State private var apiKey = "demo"
@State private var userId = "user-1"
@State private var moodScore = 7.0
@State private var note = ""
@State private var publicKey = ""
@State private var moodMessage = "No mood logged yet"
@State private var balanceMessage = "Enter a Stellar public key"
@State private var isWorking = false
var body: some View {
NavigationStack {
Form {
Section("Configuration") {
TextField("API key", text: $apiKey)
.textInputAutocapitalization(.never)
TextField("User ID", text: $userId)
.textInputAutocapitalization(.never)
}
Section("Mood") {
Stepper(value: $moodScore, in: 1...10, step: 1) {
Text("Score: \(Int(moodScore))")
}
TextField("Note", text: $note)
Button("Log mood") {
Task { await logMood() }
}
.disabled(isWorking)
Text(moodMessage)
.font(.footnote)
.foregroundStyle(.secondary)
}
Section("Stellar") {
TextField("Public key", text: $publicKey)
.textInputAutocapitalization(.never)
.font(.system(.body, design: .monospaced))
Button("Fetch balance") {
Task { await fetchBalance() }
}
.disabled(isWorking || publicKey.isEmpty)
Text(balanceMessage)
.font(.footnote)
.foregroundStyle(.secondary)
}
}
.navigationTitle("EchoMirror")
}
}
private func makeSDK() throws -> EchoMirror {
try EchoMirror(config: EchoMirrorConfig(apiKey: apiKey, network: .testnet))
}
@MainActor
private func logMood() async {
isWorking = true
defer { isWorking = false }
do {
let sdk = try makeSDK()
let entry = try await sdk.mood.logMood(
userId: userId,
score: UInt8(moodScore),
note: note.isEmpty ? nil : note,
tags: ["swiftui"]
)
moodMessage = "Logged score \(entry.score) for \(entry.userId)"
} catch {
moodMessage = error.localizedDescription
}
}
@MainActor
private func fetchBalance() async {
isWorking = true
defer { isWorking = false }
do {
let sdk = try makeSDK()
let balance = try await sdk.stellar.getBalance(publicKey: publicKey)
balanceMessage = "\(balance.xlm) XLM, \(balance.echo) ECHO"
} catch {
balanceMessage = error.localizedDescription
}
}
}