Skip to content

Commit

Permalink
feat: pick some changes from https://github.com/i-mobility/RxBluetoot…
Browse files Browse the repository at this point in the history
…hKit fork
  • Loading branch information
ghislainfrs committed Dec 6, 2023
1 parent 9356cea commit c541879
Show file tree
Hide file tree
Showing 20 changed files with 127 additions and 211 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ Carthage/Build
.idea
Podfile.lock
.DS_Store
.build/
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 7.0.1
- Cherry pick some commits from https://github.com/i-mobility/RxBluetoothKit (no longer maintained)

# 7.0.0
- Updated RxSwift version to 6.5.0 with SPM
- Add compatibility with Xcode 15
Expand Down
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "ReactiveX/RxSwift" ~> 5.1
github "ReactiveX/RxSwift" ~> 6.5
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "ReactiveX/RxSwift" "5.1.1"
github "ReactiveX/RxSwift" "6.6.0"
24 changes: 11 additions & 13 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
{
"object": {
"pins": [
{
"package": "RxSwift",
"repositoryURL": "https://github.com/ReactiveX/RxSwift.git",
"state": {
"branch": null,
"revision": "b4307ba0b6425c0ba4178e138799946c3da594f8",
"version": "6.5.0"
}
"pins" : [
{
"identity" : "rxswift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/ReactiveX/RxSwift.git",
"state" : {
"revision" : "9dcaa4b333db437b0fbfaf453fad29069044a8b4",
"version" : "6.6.0"
}
]
},
"version": 1
}
],
"version" : 2
}
4 changes: 2 additions & 2 deletions RxBluetoothKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "RxBluetoothKit"
s.version = "7.0.0"
s.version = "7.0.1"
s.summary = "Bluetooth library for RxSwift"

s.description = <<-DESC
Expand All @@ -24,5 +24,5 @@ Pod::Spec.new do |s|
s.source_files = 'Source/*.swift'
s.osx.exclude_files = 'Source/RestoredState.swift', 'Source/CentralManager+RestoredState.swift', 'Source/CentralManagerRestoredState.swift'
s.frameworks = 'CoreBluetooth'
s.dependency 'RxSwift', '~> 5.1'
s.dependency 'RxSwift', '~> 6.5'
end
10 changes: 10 additions & 0 deletions Source/BluetoothError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public enum BluetoothError: Error {
// L2CAP
case openingL2CAPChannelFailed(Peripheral, Error?)
case publishingL2CAPChannelFailed(CBL2CAPPSM, Error?)

case peripheralDestroyed
case serviceDestroyed

// Unknown
case unknownWriteType
}
Expand Down Expand Up @@ -120,6 +124,12 @@ extension BluetoothError: CustomStringConvertible {
return "Opening L2CAP channel error has occured: \(err?.localizedDescription ?? "-")"
case let .publishingL2CAPChannelFailed(_, err):
return "Publishing L2CAP channel error has occured: \(err?.localizedDescription ?? "-")"

case .peripheralDestroyed:
return "Peripheral was already deallocated"
case .serviceDestroyed:
return "Service was already deallocated"

// Unknown
case .unknownWriteType:
return "Unknown write type"
Expand Down
8 changes: 6 additions & 2 deletions Source/CentralManagerRestoredState.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Foundation
import CoreBluetooth
import Foundation

/// It should be deleted when `RestoredState` will be deleted
protocol CentralManagerRestoredStateType {
Expand Down Expand Up @@ -62,7 +62,11 @@ public struct CentralManagerRestoredState: CentralManagerRestoredStateType {

return cbServices.compactMap({ service in
if let peripheral = service.peripheral {
return Service(peripheral: centralManager.retrievePeripheral(for: peripheral), service: service)
return Service(
peripheral: centralManager
.retrievePeripheral(for: peripheral),
service: service
)
}
return nil
})
Expand Down
14 changes: 8 additions & 6 deletions Source/Characteristic.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import CoreBluetooth
import Foundation
import RxSwift
import CoreBluetooth

/// Characteristic is a class implementing ReactiveX which wraps CoreBluetooth functions related to interaction with [CBCharacteristic](https://developer.apple.com/library/ios/documentation/CoreBluetooth/Reference/CBCharacteristic_Class/)
public class Characteristic {
Expand Down Expand Up @@ -40,12 +40,14 @@ public class Characteristic {
self.service = service
}

convenience init(characteristic: CBCharacteristic, peripheral: Peripheral) {
guard let cbService = characteristic.service else {
fatalError()
convenience init(characteristic: CBCharacteristic, peripheral: Peripheral) throws {
guard let service = characteristic.service else {
throw BluetoothError.serviceDestroyed
}
let service = Service(peripheral: peripheral, service: cbService)
self.init(characteristic: characteristic, service: service)
self.init(
characteristic: characteristic,
service: Service(peripheral: peripheral, service: service)
)
}

/// Function that triggers descriptors discovery for characteristic.
Expand Down
22 changes: 15 additions & 7 deletions Source/Descriptor.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Foundation
import CoreBluetooth
import Foundation
import RxSwift

/// Descriptor is a class implementing ReactiveX which wraps CoreBluetooth functions related to interaction with
Expand Down Expand Up @@ -28,13 +28,21 @@ public class Descriptor {
self.characteristic = characteristic
}

convenience init(descriptor: CBDescriptor, peripheral: Peripheral) {
guard let cbCharacteristic = descriptor.characteristic, let cbService = cbCharacteristic.service else {
fatalError()
convenience init(descriptor: CBDescriptor, peripheral: Peripheral) throws {
let characteristic: CBCharacteristic? = descriptor.characteristic
let service: CBService? = characteristic?.service

guard let characteristic, let service else {
throw BluetoothError.serviceDestroyed
}
let service = Service(peripheral: peripheral, service: cbService)
let characteristic = Characteristic(characteristic: cbCharacteristic, service: service)
self.init(descriptor: descriptor, characteristic: characteristic)

self.init(
descriptor: descriptor,
characteristic: Characteristic(
characteristic: characteristic,
service: Service(peripheral: peripheral, service: service)
)
)
}

/// Function that allow to observe writes that happened for descriptor.
Expand Down
10 changes: 5 additions & 5 deletions Source/Peripheral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ public class Peripheral {
.filter { characteristic != nil ? ($0.0 == characteristic!.characteristic) : true }
.map { [weak self] (cbCharacteristic, error) -> Characteristic in
guard let strongSelf = self else { throw BluetoothError.destroyed }
let characteristic = characteristic ?? Characteristic(characteristic: cbCharacteristic, peripheral: strongSelf)
let characteristic = try characteristic ?? Characteristic(characteristic: cbCharacteristic, peripheral: strongSelf)
if let error = error {
throw BluetoothError.characteristicWriteFailed(characteristic, error)
}
Expand Down Expand Up @@ -498,7 +498,7 @@ public class Peripheral {
.filter { characteristic != nil ? ($0.0 == characteristic!.characteristic) : true }
.map { [weak self] (cbCharacteristic, error) -> Characteristic in
guard let strongSelf = self else { throw BluetoothError.destroyed }
let characteristic = characteristic ?? Characteristic(characteristic: cbCharacteristic, peripheral: strongSelf)
let characteristic = try characteristic ?? Characteristic(characteristic: cbCharacteristic, peripheral: strongSelf)
if let error = error {
throw BluetoothError.characteristicReadFailed(characteristic, error)
}
Expand Down Expand Up @@ -568,7 +568,7 @@ public class Peripheral {
.filter { $0.0 == characteristic.characteristic }
.map { [weak self] (cbCharacteristic, error) -> Characteristic in
guard let strongSelf = self else { throw BluetoothError.destroyed }
let characteristic = Characteristic(characteristic: cbCharacteristic, peripheral: strongSelf)
let characteristic = try Characteristic(characteristic: cbCharacteristic, peripheral: strongSelf)
if let error = error {
throw BluetoothError.characteristicSetNotifyValueFailed(characteristic, error)
}
Expand Down Expand Up @@ -637,7 +637,7 @@ public class Peripheral {
.filter { descriptor != nil ? ($0.0 == descriptor!.descriptor) : true }
.map { [weak self] (cbDescriptor, error) -> Descriptor in
guard let strongSelf = self else { throw BluetoothError.destroyed }
let descriptor = descriptor ?? Descriptor(descriptor: cbDescriptor, peripheral: strongSelf)
let descriptor = try descriptor ?? Descriptor(descriptor: cbDescriptor, peripheral: strongSelf)
if let error = error {
throw BluetoothError.descriptorWriteFailed(descriptor, error)
}
Expand Down Expand Up @@ -666,7 +666,7 @@ public class Peripheral {
.filter { descriptor != nil ? ($0.0 == descriptor!.descriptor) : true }
.map { [weak self] (cbDescriptor, error) -> Descriptor in
guard let strongSelf = self else { throw BluetoothError.destroyed }
let descriptor = descriptor ?? Descriptor(descriptor: cbDescriptor, peripheral: strongSelf)
let descriptor = try descriptor ?? Descriptor(descriptor: cbDescriptor, peripheral: strongSelf)
if let error = error {
throw BluetoothError.descriptorReadFailed(descriptor, error)
}
Expand Down
4 changes: 2 additions & 2 deletions Templates/Mock.swifttemplate
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ import RxSwift
static func changeTypeNameToTestable(_ typeName: String) -> String {
let regexGroup = classNamesToTestable.reduce("", { "\($0)\($1)|" }).dropLast()
let regex = try! NSRegularExpression(pattern: "\\b(\(regexGroup))\\b", options: NSRegularExpression.Options.caseInsensitive)
let range = NSMakeRange(0, typeName.characters.count)
let range = NSMakeRange(0, typeName.count)
return regex.stringByReplacingMatches(in: typeName, options: [], range: range, withTemplate: "_$0")
}

static func changeTypeNameToMock(_ typeName: String) -> String {
let regexGroup = namesToMock.reduce("", { "\($0)\($1)|" }).dropLast()
let regex = try! NSRegularExpression(pattern: "\\b(\(regexGroup))\\b", options: NSRegularExpression.Options.caseInsensitive)
let range = NSMakeRange(0, typeName.characters.count)
let range = NSMakeRange(0, typeName.count)
return regex.stringByReplacingMatches(in: typeName, options: [], range: range, withTemplate: "$0Mock")
}

Expand Down
Loading

0 comments on commit c541879

Please sign in to comment.