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

Tidy up the mock / live choice code in example app #127

Merged
merged 1 commit into from
Nov 19, 2024
Merged
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
60 changes: 32 additions & 28 deletions Example/AblyChatExample/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,34 @@ import Ably
import AblyChat
import SwiftUI

private enum Environment: Equatable {
// Set ``current`` to `.live` if you wish to connect to actual instances of the Chat client in either Prod or Sandbox environments. Setting the mode to `.mock` will use the `MockChatClient`, and therefore simulate all features of the Chat app.
static let current: Self = .mock

case mock
/// - Parameters:
/// - key: Your Ably API key.
/// - clientId: A string that identifies this client.
case live(key: String, clientId: String)

func createChatClient() -> ChatClient {
switch self {
case .mock:
return MockChatClient(
realtime: MockRealtime.create(),
maratal marked this conversation as resolved.
Show resolved Hide resolved
clientOptions: ClientOptions()
)
case let .live(key: key, clientId: clientId):
let realtimeOptions = ARTClientOptions()
realtimeOptions.key = key
realtimeOptions.clientId = clientId
let realtime = ARTRealtime(options: realtimeOptions)

return DefaultChatClient(realtime: realtime, clientOptions: .init())
}
}
}

@MainActor
struct ContentView: View {
#if os(macOS)
Expand All @@ -15,20 +43,7 @@ struct ContentView: View {
// Can be replaced with your own room ID
private let roomID = "DemoRoomID"

// Set mode to `.live` if you wish to connect to actual instances of the Chat client in either Prod or Sandbox environments. Setting the mode to `.mock` will use the `MockChatClient`, and therefore simulate all features of the Chat app.
private let mode = Environment.mock
private enum Environment {
case mock
case live
}

@State private var mockChatClient = MockChatClient(
realtime: MockRealtime.create(),
clientOptions: ClientOptions()
)

private let liveRealtime: ARTRealtime
@State private var liveChatClient: DefaultChatClient
@State private var chatClient = Environment.current.createChatClient()

@State private var title = "Room"
@State private var messages = [BasicListItem]()
Expand All @@ -38,19 +53,8 @@ struct ContentView: View {
@State private var occupancyInfo = "Connections: 0"
@State private var statusInfo = ""

// You only need to set `options.key` and `options.clientId` if your mode is set to `.live`. Otherwise, you can ignore this.
init() {
let options = ARTClientOptions()
options.key = ""
options.clientId = ""
liveRealtime = ARTRealtime(options: options)

_liveChatClient = State(initialValue: DefaultChatClient(realtime: liveRealtime, clientOptions: .init()))
}

private func room() async throws -> Room {
let chosenChatClient: ChatClient = (mode == .mock) ? mockChatClient : liveChatClient
return try await chosenChatClient.rooms.get(
try await chatClient.rooms.get(
roomID: roomID,
options: .init(
presence: .init(),
Expand Down Expand Up @@ -137,8 +141,8 @@ struct ContentView: View {
.tryTask { try await showPresence() }
.tryTask { try await showOccupancy() }
.tryTask {
// NOTE: As we implement more features, move them out of the `if mode == .mock` block and into the main block just above.
if mode == .mock {
// NOTE: As we implement more features, move them out of the `if Environment.current == .mock` block and into the main block just above.
if Environment.current == .mock {
try await showTypings()
try await showRoomStatus()
}
Expand Down