Skip to content

Commit

Permalink
Add delegation as an extra for improv manager
Browse files Browse the repository at this point in the history
  • Loading branch information
bgoncal committed Jul 16, 2024
1 parent 1991cff commit 3b2aada
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Improv-iOS.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = "Improv-iOS"
spec.version = "0.0.1"
spec.version = "0.0.2"
spec.summary = "Easily detect and connect Improv devices to WiFi networks in iOS"
spec.description = "This library abstracts the bluetooth scanning for Improv devices and allow you to connect them to WiFi networks"
spec.author = "Improv"
Expand Down
25 changes: 25 additions & 0 deletions Improv-iOS/ImprovManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public protocol ImprovManagerProtocol: ObservableObject {
var foundDevices: [String : CBPeripheral] { get }
var scanInProgress: Bool { get }
var connectedDevice: CBPeripheral? { get }
var delegate: ImprovManagerDelegate? { get set }

func scan()
func stopScan()
Expand All @@ -19,6 +20,22 @@ public protocol ImprovManagerProtocol: ObservableObject {
func sendWifi(ssid: String, password: String)
}

public protocol ImprovManagerDelegate: AnyObject {
func didUpdateBluetoohState(_ state: CBManagerState)

func didUpdateFoundDevices(devices: [String : CBPeripheral])

func didConnect(peripheral: CBPeripheral)

func didDisconnect(peripheral: CBPeripheral)

func didUpdateDeviceState(_ state: DeviceState?)

func didUpdateErrorState(_ state: ErrorState?)

func didReceiveResult(_ result: [String]?)
}

public final class ImprovManager: NSObject, ImprovManagerProtocol {
enum State {
case idle
Expand All @@ -31,6 +48,7 @@ public final class ImprovManager: NSObject, ImprovManagerProtocol {
)

private var bluetoothManager: BluetoothManagerProtocol
weak public var delegate: ImprovManagerDelegate?

@Published public private(set) var bluetoothState: CBManagerState = .unknown
@Published public private(set) var errorState: ErrorState?
Expand Down Expand Up @@ -83,29 +101,36 @@ public final class ImprovManager: NSObject, ImprovManagerProtocol {
extension ImprovManager: BluetoothManagerDelegate {
func didUpdateBluetoohState(_ state: CBManagerState) {
bluetoothState = state
delegate?.didUpdateBluetoohState(state)
}

func didFindNewDevice(peripheral: CBPeripheral) {
foundDevices[peripheral.identifier.uuidString] = peripheral
delegate?.didUpdateFoundDevices(devices: foundDevices)
}

func didConnect(peripheral: CBPeripheral) {
connectedDevice = peripheral
delegate?.didConnect(peripheral: peripheral)
}

func didDisconnect(peripheral: CBPeripheral) {
connectedDevice = nil
delegate?.didDisconnect(peripheral: peripheral)
}

func didUpdateDeviceState(_ state: DeviceState?) {
deviceState = state
delegate?.didUpdateDeviceState(state)
}

func didUpdateErrorState(_ state: ErrorState?) {
errorState = state
delegate?.didUpdateErrorState(state)
}

func didReceiveResult(_ result: [String]?) {
lastResult = result
delegate?.didReceiveResult(result)
}
}

0 comments on commit 3b2aada

Please sign in to comment.