Skip to content

Commit

Permalink
Add test account button (#302)
Browse files Browse the repository at this point in the history
* Add binance account button

* Method renamed
  • Loading branch information
flypaper0 authored and llbartekll committed Jul 5, 2022
1 parent 6dad7e4 commit 4507a82
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Example/ExampleApp/SessionDetails/SessionDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ private extension SessionDetailView {
HStack {
Text(chain)
Spacer()
Button("Add Account") { Task {
await viewModel.add(chain: chain)
}}
Button("Delete") { Task {
await viewModel.remove(field: .chain, for: chain)
}}
Expand Down
33 changes: 33 additions & 0 deletions Example/ExampleApp/SessionDetails/SessionDetailViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ final class SessionDetailViewModel: ObservableObject {
}
}

func add(chain: String) async {
let backup = namespaces

do {
addTestAccount(for: chain)

try await client.update(
topic: session.topic,
namespaces: namespaces
)
} catch {
namespaces = backup
print("[RESPONDER] Namespaces update failed with: \(error.localizedDescription)")
}
}

func ping() {
client.ping(topic: session.topic) { result in
switch result {
Expand All @@ -82,6 +98,17 @@ final class SessionDetailViewModel: ObservableObject {

private extension SessionDetailViewModel {

func addTestAccount(for chain: String) {
guard let viewModel = namespace(for: chain) else { return }

let account = Account("eip155:56:0xe5EeF1368781911d265fDB6946613dA61915a501")!

namespaces[chain] = SessionNamespace(
accounts: Set(viewModel.accounts.appending(account)),
namespace: viewModel.namespace
)
}

func removeAccounts(at offsets: IndexSet, chain: String) {
guard let viewModel = namespace(for: chain) else { return }

Expand Down Expand Up @@ -121,6 +148,12 @@ private extension Array {
array.remove(atOffsets: offsets)
return array
}

func appending(_ element: Element) -> Self {
var array = self
array.append(element)
return array
}
}

private extension SessionNamespace {
Expand Down

0 comments on commit 4507a82

Please sign in to comment.