Skip to content

Commit

Permalink
feat(sample): update sample apps for 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
goncalo-frade-iohk committed Mar 20, 2023
1 parent 5c94352 commit a88a9cc
Show file tree
Hide file tree
Showing 31 changed files with 1,711 additions and 27 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import SwiftUI

struct FuncionalitiesList: View {
let mainRouter = MainViewRouterImpl()
@State var presentWallet2 = false
var body: some View {
NavigationView {
NavigationStack {
List {
NavigationLink("Seed Funcionalities", destination: SeedFuncionalitiesView(model: .init()))
NavigationLink("DID Funcionalities", destination: DIDFuncionalitiesView(model: .init()))
Expand All @@ -12,7 +13,21 @@ struct FuncionalitiesList: View {
)
NavigationLink("Setup Prism Agent", destination: SetupPrismAgentView(viewModel: SetupPrismAgentViewModelImpl())
)
NavigationLink("Wallet Demo", destination: MainView(viewModel: MainViewModelImpl(router: mainRouter), router: mainRouter))
Button {
self.presentWallet2 = true
} label: {
Text("Wallet Demo 2.0")
}
// Button {
// self.presentWallet2 = true
// } label: {
// Text("Wallet Demo")
// }
// NavigationLink("Wallet Demo", destination: MainView(viewModel: MainViewModelImpl(router: mainRouter), router: mainRouter))
}
.buttonStyle(.plain)
.fullScreenCover(isPresented: $presentWallet2) {
Main2View(router: Main2RouterImpl())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import SwiftUI

protocol ConnectionsListViewModel: ObservableObject {
var connections: [ConnectionsViewState.Connection] { get }
var error: FancyToast? { get set }

func addConnection(invitation: String, alias: String)
}

struct ConnectionsListView<ViewModel: ConnectionsListViewModel>: View {
@StateObject var viewModel: ViewModel
@State var showAddConnection = false
@State var aliasInput = ""
@State var newConnectionInput = ""

var body: some View {
NavigationStack {
VStack {
if showAddConnection {
VStack(spacing: 16) {
TextField("Alias", text: $aliasInput)
TextField("DID or OOB", text: $newConnectionInput)
HStack(spacing: 8) {
Button {
viewModel.addConnection(invitation: newConnectionInput, alias: aliasInput)
showAddConnection = false
} label: {
Text("Add")
}
Button {
showAddConnection = false
} label: {
Text("Cancel")
}
}
}
}
List {
ForEach(viewModel.connections) { connection in
VStack(spacing: 8) {
if let alias = connection.alias {
Text(alias)
}
HStack {
Text("Host: ")
Text(connection.hostDID)
.lineLimit(1)
.truncationMode(.middle)
}

HStack {
Text("Recipient: ")
Text(connection.recipientDID)
.lineLimit(1)
.truncationMode(.middle)
}
}
}
}
}
.padding()
.toolbar(content: {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
showAddConnection = true
}, label: {
Image(systemName: "plus")
})
}
})
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import Combine
import Domain
import Foundation
import PrismAgent

final class ConnectionsListViewModelImpl: ConnectionsListViewModel {
@Published var connections = [ConnectionsViewState.Connection]()
@Published var error: FancyToast?

private let castor: Castor
private let pluto: Pluto
private let agent: PrismAgent

init(castor: Castor, pluto: Pluto, agent: PrismAgent) {
self.castor = castor
self.pluto = pluto
self.agent = agent

bind()
}

func bind() {
agent
.getAllDIDPairs()
.map {
$0.map {
ConnectionsViewState.Connection(
hostDID: $0.holder.string,
recipientDID: $0.other.string,
alias: $0.name
)
}
}
.replaceError(with: [])
.assign(to: &$connections)
}

func addConnection(invitation: String, alias: String) {
let castor = self.castor
let agent = self.agent
Task { [weak self] in
do {
if let did = try? castor.parseDID(str: invitation) {
let hostDID = try await agent.createNewPeerDID(
alias: alias.isEmpty ? nil : alias,
updateMediator: true
)
let connectionRequest = ConnectionRequest(
from: hostDID,
to: did,
thid: nil,
body: .init()
)
_ = try await agent.sendMessage(message: connectionRequest.makeMessage())
} else if let url = URL(string: invitation) {
let inv = try agent.parseOOBInvitation(url: url)
try await agent.acceptDIDCommInvitation(invitation: inv)
}
} catch let error as LocalizedError {
await MainActor.run { [weak self] in
self?.error = FancyToast(
type: .error,
title: error.localizedDescription,
message: error.errorDescription ?? ""
)
}
} catch {
await MainActor.run { [weak self] in
self?.error = FancyToast(
type: .error,
title: error.localizedDescription,
message: ""
)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
struct ConnectionsViewState {
struct Connection: Identifiable {
var id: String { hostDID + recipientDID }
let hostDID: String
let recipientDID: String
let alias: String?
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import SwiftUI

struct CredentialListRouterImpl: CredentialListRouter {
let container: DIContainer

func routeToCredentialDetail(id: String) -> some View {
Text("")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import SwiftUI

protocol CredentialListViewModel: ObservableObject {
var credentials: [CredentialListViewState.Credential] { get }
}

protocol CredentialListRouter {
associatedtype CredentialDetailV: View

func routeToCredentialDetail(id: String) -> CredentialDetailV
}

struct CredentialListView<
ViewModel: CredentialListViewModel,
Router: CredentialListRouter
>: View {
@StateObject var viewModel: ViewModel
let router: Router

var body: some View {
NavigationStack {
List(viewModel.credentials, id: \.id) { credential in
NavigationLink(value: credential) {
VStack(alignment: .leading) {
Text(credential.credentialType)
.font(.headline)
Text(credential.issuer)
.lineLimit(1)
.truncationMode(.middle)
.font(.subheadline)
.foregroundColor(.secondary)
Text(credential.issuanceDate)
.font(.subheadline)
.foregroundColor(.secondary)
if !credential.type.isEmpty {
Spacer()
HStack {
ForEach(credential.type, id: \.self) { type in
Text(type)
.font(.caption)
.padding(.horizontal, 8)
.padding(.vertical, 4)
.background(Color.blue.opacity(0.2))
.cornerRadius(4)
}
}
}
}
}
}
.navigationDestination(for: CredentialListViewState.Credential.self) {
router.routeToCredentialDetail(id: $0.id)
}
.navigationTitle("My Credentials")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import Combine
import Domain
import Foundation

final class CredentialListViewModelImpl: CredentialListViewModel {
@Published var credentials = [CredentialListViewState.Credential]()

private let pluto: Pluto

init(pluto: Pluto) {
self.pluto = pluto
bind()
}

private func bind() {
self.pluto
.getAllCredentials()
.map {
$0.map {
CredentialListViewState.Credential(
credentialType: getTypeString(type: $0.credentialType),
id: $0.id,
issuer: $0.issuer.string,
issuanceDate: $0.issuanceDate.formatted(),
context: Array($0.context),
type: Array($0.type)
)
}
}
.replaceError(with: [])
.assign(to: &$credentials)
}
}

private func getTypeString(type: CredentialType) -> String {
switch type {
case .jwt:
return "jwt"
case .w3c:
return "w3c"
default:
return "unknown"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
struct CredentialListViewState {
struct Credential: Hashable {
let credentialType: String
let id: String
let issuer: String
let issuanceDate: String
let context: [String]
let type: [String]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import SwiftUI

protocol DIDDetailViewModel: ObservableObject {
var state: DIDDetailViewState { get }
}

struct DIDDetailView: View {
var body: some View {
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
}
}

struct DIDDetailView_Previews: PreviewProvider {
static var previews: some View {
DIDDetailView()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Combine
import Domain
import Foundation
import PrismAgent

final class DIDDetailViewModelImpl: DIDDetailViewModel {
@Published var state: DIDDetailViewState = .init(
did: "",
alias: nil,
publicKeys: [],
services: [:]
)
private let pluto: Pluto
private let did: String

init(did: String, pluto: Pluto) {
self.did = did
self.pluto = pluto
}

// func bind() {
// let did = try! DID(string: did)
// switch did.method {
// case "prism":
// pluto
// .getPeerDIDInfo(did: <#T##DID#>)
// }
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
struct DIDDetailViewState {
let did: String
let alias: String?
let publicKeys: [String]
let services: [String: String]
}
Loading

0 comments on commit a88a9cc

Please sign in to comment.