Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Chat] Sample app (demo) #320

Merged
merged 4 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions Example/ExampleApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
A5629AE22876CC6E00094373 /* InviteListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5629ADD2876CC6E00094373 /* InviteListView.swift */; };
A5629AE42876E6D200094373 /* ThreadViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5629AE32876E6D200094373 /* ThreadViewModel.swift */; };
A5629AE828772A0100094373 /* InviteViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5629AE728772A0100094373 /* InviteViewModel.swift */; };
A5629AEA2877F2D600094373 /* WalletConnectChat in Frameworks */ = {isa = PBXBuildFile; productRef = A5629AE92877F2D600094373 /* WalletConnectChat */; };
A5629AED2877F6A600094373 /* ChatFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5629AEC2877F6A600094373 /* ChatFactory.swift */; };
A5629AF02877F73000094373 /* SocketFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5629AEF2877F73000094373 /* SocketFactory.swift */; };
A5629AF22877F75100094373 /* Starscream in Frameworks */ = {isa = PBXBuildFile; productRef = A5629AF12877F75100094373 /* Starscream */; };
A578FA322873036400AA7720 /* InputView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A578FA312873036400AA7720 /* InputView.swift */; };
A578FA35287304A300AA7720 /* Color.swift in Sources */ = {isa = PBXBuildFile; fileRef = A578FA34287304A300AA7720 /* Color.swift */; };
A578FA372873D8EE00AA7720 /* UIColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = A578FA362873D8EE00AA7720 /* UIColor.swift */; };
Expand Down Expand Up @@ -201,6 +205,8 @@
A5629ADD2876CC6E00094373 /* InviteListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InviteListView.swift; sourceTree = "<group>"; };
A5629AE32876E6D200094373 /* ThreadViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThreadViewModel.swift; sourceTree = "<group>"; };
A5629AE728772A0100094373 /* InviteViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InviteViewModel.swift; sourceTree = "<group>"; };
A5629AEC2877F6A600094373 /* ChatFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatFactory.swift; sourceTree = "<group>"; };
A5629AEF2877F73000094373 /* SocketFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SocketFactory.swift; sourceTree = "<group>"; };
A578FA312873036400AA7720 /* InputView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InputView.swift; sourceTree = "<group>"; };
A578FA34287304A300AA7720 /* Color.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Color.swift; sourceTree = "<group>"; };
A578FA362873D8EE00AA7720 /* UIColor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIColor.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -283,6 +289,8 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
A5629AEA2877F2D600094373 /* WalletConnectChat in Frameworks */,
A5629AF22877F75100094373 /* Starscream in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -472,7 +480,8 @@
A5629AA42876A19D00094373 /* DomainLayer */ = {
isa = PBXGroup;
children = (
A5629AA82876A23100094373 /* ChatService.swift */,
A5629AEE2877F72B00094373 /* SocketFactory */,
A5629AEB2877F69C00094373 /* Chat */,
);
path = DomainLayer;
sourceTree = "<group>";
Expand Down Expand Up @@ -531,6 +540,23 @@
path = Models;
sourceTree = "<group>";
};
A5629AEB2877F69C00094373 /* Chat */ = {
isa = PBXGroup;
children = (
A5629AA82876A23100094373 /* ChatService.swift */,
A5629AEC2877F6A600094373 /* ChatFactory.swift */,
);
path = Chat;
sourceTree = "<group>";
};
A5629AEE2877F72B00094373 /* SocketFactory */ = {
isa = PBXGroup;
children = (
A5629AEF2877F73000094373 /* SocketFactory.swift */,
);
path = SocketFactory;
sourceTree = "<group>";
};
A578FA332873049400AA7720 /* Style */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -848,6 +874,10 @@
dependencies = (
);
name = Showcase;
packageProductDependencies = (
A5629AE92877F2D600094373 /* WalletConnectChat */,
A5629AF12877F75100094373 /* Starscream */,
);
productName = Showcase;
productReference = A58E7CE828729F550082D443 /* Showcase.app */;
productType = "com.apple.product-type.application";
Expand Down Expand Up @@ -1080,6 +1110,8 @@
A5629AE22876CC6E00094373 /* InviteListView.swift in Sources */,
A578FA3D2874002400AA7720 /* View.swift in Sources */,
A5629AD72876CC5700094373 /* InviteView.swift in Sources */,
A5629AF02877F73000094373 /* SocketFactory.swift in Sources */,
A5629AED2877F6A600094373 /* ChatFactory.swift in Sources */,
A58E7D1D2872A57B0082D443 /* Configurator.swift in Sources */,
A58E7D482872EF610082D443 /* MessageViewModel.swift in Sources */,
A5629AD32876CC5700094373 /* InviteModule.swift in Sources */,
Expand Down Expand Up @@ -1421,7 +1453,7 @@
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.walletconnect.Showcase;
PRODUCT_BUNDLE_IDENTIFIER = com.walletconnect.showcase;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
Expand Down Expand Up @@ -1450,7 +1482,7 @@
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.walletconnect.Showcase;
PRODUCT_BUNDLE_IDENTIFIER = com.walletconnect.showcase;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
Expand Down Expand Up @@ -1629,6 +1661,15 @@
package = 8449439F278EC49700CC26BB /* XCRemoteSwiftPackageReference "Web3" */;
productName = Web3;
};
A5629AE92877F2D600094373 /* WalletConnectChat */ = {
isa = XCSwiftPackageProductDependency;
productName = WalletConnectChat;
};
A5629AF12877F75100094373 /* Starscream */ = {
isa = XCSwiftPackageProductDependency;
package = A5D85224286333D500DAF5C3 /* XCRemoteSwiftPackageReference "Starscream" */;
productName = Starscream;
};
A5D85225286333D500DAF5C3 /* Starscream */ = {
isa = XCSwiftPackageProductDependency;
package = A5D85224286333D500DAF5C3 /* XCRemoteSwiftPackageReference "Starscream" */;
Expand Down
16 changes: 8 additions & 8 deletions Example/IntegrationTests/Chat/ChatTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import WalletConnectRelay
import Combine

final class ChatTests: XCTestCase {
var invitee: Chat!
var inviter: Chat!
var invitee: ChatClient!
var inviter: ChatClient!
var registry: KeyValueRegistry!
private var publishers = [AnyCancellable]()

Expand Down Expand Up @@ -37,13 +37,13 @@ final class ChatTests: XCTestCase {
return
}

func makeClient(prefix: String) -> Chat {
func makeClient(prefix: String) -> ChatClient {
let logger = ConsoleLogger(suffix: prefix, loggingLevel: .debug)
let relayHost = "relay.walletconnect.com"
let projectId = "8ba9ee138960775e5231b70cc5ef1c3a"
let keychain = KeychainStorageMock()
let relayClient = RelayClient(relayHost: relayHost, projectId: projectId, keychainStorage: keychain, socketFactory: SocketFactory(), logger: logger)
return Chat(registry: registry, relayClient: relayClient, kms: KeyManagementService(keychain: keychain), logger: logger, keyValueStorage: RuntimeKeyValueStorage())
return ChatClient(registry: registry, relayClient: relayClient, kms: KeyManagementService(keychain: keychain), logger: logger, keyValueStorage: RuntimeKeyValueStorage())
}

func testInvite() async {
Expand All @@ -69,8 +69,8 @@ final class ChatTests: XCTestCase {
//
// try! await inviter.invite(publicKey: pubKey, peerAccount: inviteeAccount, openingMessage: "opening message", account: inviterAccount)
//
// invitee.invitePublisher.sink { [unowned self] inviteEnvelope in
// Task {try! await invitee.accept(inviteId: inviteEnvelope.pubKey)}
// invitee.invitePublisher.sink { [unowned self] invite in
// Task {try! await invitee.accept(inviteId: invite.pubKey)}
// }.store(in: &publishers)
//
// invitee.newThreadPublisher.sink { _ in
Expand All @@ -94,8 +94,8 @@ final class ChatTests: XCTestCase {
// let pubKey = try! await invitee.register(account: inviteeAccount)
// try! await inviter.invite(publicKey: pubKey, peerAccount: inviteeAccount, openingMessage: "opening message", account: inviterAccount)
//
// invitee.invitePublisher.sink { [unowned self] inviteEnvelope in
// Task {try! await invitee.accept(inviteId: inviteEnvelope.pubKey)}
// invitee.invitePublisher.sink { [unowned self] invite in
// Task {try! await invitee.accept(inviteId: invite.pubKey)}
// }.store(in: &publishers)
//
// invitee.newThreadPublisher.sink { [unowned self] thread in
Expand Down
3 changes: 2 additions & 1 deletion Example/Showcase/Classes/ApplicationLayer/Application.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Foundation
import Chat

final class Application {

let chatService: ChatService = {
return ChatService()
return ChatService(client: ChatFactory.create())
}()
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
import Combine

struct ApplicationConfigurator: Configurator {

private var publishers = Set<AnyCancellable>()

private let app: Application

init(app: Application) {
self.app = app
}

func configure() {
registerAccount()

ChatListModule.create(app: app)
.wrapToNavigationController()
.present()
}
}

private extension ApplicationConfigurator {

func registerAccount() {
Task(priority: .high) {
for await status in app.chatService.connectionPublisher {
guard status == .connected else {
fatalError("Not Connected")
}

print("Socket connected")

try! await app.chatService.register(account: ChatService.selfAccount)
}
}
}
}
22 changes: 22 additions & 0 deletions Example/Showcase/Classes/DomainLayer/Chat/ChatFactory.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Foundation
import Chat
import WalletConnectKMS
import WalletConnectRelay

class ChatFactory {

static func create() -> ChatClient {
let relayHost = "relay.walletconnect.com"
let projectId = "8ba9ee138960775e5231b70cc5ef1c3a"
let keychain = KeychainStorage(serviceIdentifier: "com.walletconnect.showcase")
let client = HTTPClient(host: "159.65.123.131")
let registry = KeyserverRegistryProvider(client: client)
let relayClient = RelayClient(relayHost: relayHost, projectId: projectId, keychainStorage: keychain, socketFactory: SocketFactory())
return ChatClient(
registry: registry,
relayClient: relayClient,
kms: KeyManagementService(keychain: keychain),
keyValueStorage: UserDefaults.standard
)
}
}
70 changes: 70 additions & 0 deletions Example/Showcase/Classes/DomainLayer/Chat/ChatService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import Foundation
import Combine
import Chat
import WalletConnectUtils
import WalletConnectRelay

typealias Stream<T> = AsyncPublisher<AnyPublisher<T, Never>>

final class ChatService {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are going to remove this class... correct?


static let selfAccount = Account("eip155:1:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb")!

private let client: ChatClient

init(client: ChatClient) {
self.client = client
}

var connectionPublisher: Stream<SocketConnectionStatus> {
return client.socketConnectionStatusPublisher.values
}

var messagePublisher: Stream<Message> {
return client.messagePublisher.values
}

var threadPublisher: Stream<Chat.Thread> {
return client.newThreadPublisher.values
}

var invitePublisher: Stream<Invite> {
return client.invitePublisher.values
}

func getMessages(thread: Chat.Thread) async -> [Chat.Message] {
await client.getMessages(topic: thread.topic)
}

func getThreads() async -> [Chat.Thread] {
await client.getThreads()
}

func getInvites(account: Account) async -> [Chat.Invite] {
client.getInvites(account: account)
}

func sendMessage(topic: String, message: String) async throws {
try await client.message(topic: topic, message: message)
}

func accept(invite: Invite) async throws {
try await client.accept(inviteId: invite.pubKey)
}

func reject(invite: Invite) async throws {
try await client.reject(inviteId: invite.pubKey)
}

func invite(peerPubkey publicKey: String, peerAccount: Account, message: String, selfAccount: Account) async throws {
try await client.invite(publicKey: publicKey, peerAccount: peerAccount, openingMessage: message, account: selfAccount)
}

func register(account: Account) async throws {
_ = try await client.register(account: account)
}

func resolve(account: Account) async throws -> String {
return try await client.resolve(account: account)
}
}
69 changes: 0 additions & 69 deletions Example/Showcase/Classes/DomainLayer/ChatService.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Foundation
import Starscream
import WalletConnectRelay

extension WebSocket: WebSocketConnecting { }

struct SocketFactory: WebSocketFactory {
func create(with url: URL) -> WebSocketConnecting {
return WebSocket(url: url)
}
}
Loading