-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from netguru/Add-handler-for-connection-cancel…
…led-manually Add handler for peripheral connection cancelled
- Loading branch information
Showing
12 changed files
with
199 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
xcuserdata | ||
profile | ||
build | ||
.build | ||
output | ||
DerivedData | ||
*.mode1v3 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Change Log | ||
All notable changes to this project will be documented in this file. | ||
|
||
## [1.0.6] - 2022-04-08 | ||
|
||
### Added | ||
|
||
- added public `peripheralConnectionCancelledHandler(_:)` setable property to `BluetoothConnection` class. It is called when disconnecting a peripheral using `disconnect(_:)` is completed | ||
|
||
### Changed | ||
|
||
- refactored `.filter(_:).first` to `first(where:)` for optimisation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// Sequence.swift | ||
// Bluetooth | ||
// | ||
// Created by Filip Zieliński on 08/04/2022. | ||
// Copyright © 2022 Netguru. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import CoreBluetooth | ||
|
||
internal extension Sequence { | ||
|
||
/// Returns the first element of the sequence where given element property is identical (`===`) to given object instance. | ||
/// - Parameters: | ||
/// - propertyKeyPath: a key path to an element property. | ||
/// - instance: an object instance to compare against. | ||
/// - Returns: The first element of the sequence where given element property is identical (`===`) to given object instance, or `nil` if there is no such element. | ||
func first<Object: AnyObject>(where propertyKeyPath: KeyPath<Element, Object>, isIdenticalTo instance: Object) -> Element? { | ||
first { $0[keyPath: propertyKeyPath] === instance } | ||
} | ||
|
||
/// Returns the first element of the sequence where given element optional property is identical (`===`) to given object instance. | ||
/// - Parameters: | ||
/// - propertyKeyPath: a key path to an element property. | ||
/// - instance: an object instance to compare against. | ||
/// - Returns: The first element of the sequence where given element optional property is identical (`===`) to given object instance, or `nil` if there is no such element. | ||
func first<Object: AnyObject>(where propertyKeyPath: KeyPath<Element, Object?>, isIdenticalTo instance: Object) -> Element? { | ||
first { $0[keyPath: propertyKeyPath] === instance } | ||
} | ||
} | ||
|
||
internal extension Sequence where Element == Peripheral<Connectable> { | ||
|
||
/// Convenience method returning the first peripheral of the sequence with `peripheral` property identical (`===`) to given `CBPeripheral` instance. | ||
/// - Parameter cbPeripheral: a `CBPeripheral` instance. | ||
/// - Returns: The first peripheral of the sequence with `peripheral` property identical (`===`) to given `CBPeripheral` instance, or `nil` if there is no such element. | ||
func first(withIdentical cbPeripheral: CBPeripheral) -> Element? { | ||
first(where: \.peripheral, isIdenticalTo: cbPeripheral) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// | ||
// SequenceExtensionTests.swift | ||
// Bluetooth | ||
// | ||
// Created by Filip Zieliński on 08/04/2022. | ||
// Copyright © 2022 Netguru. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
import CoreBluetooth | ||
@testable import BlueSwift | ||
|
||
final class SequenceExtensionTests: XCTestCase { | ||
|
||
private let fixtureReference = SimpleClass() | ||
private let fixtureOptionalReference = SimpleClass() | ||
private lazy var fixtureTestClass = TestClass(someReference: fixtureReference, someOptionalReference: fixtureOptionalReference) | ||
|
||
func testFirstSequenceElement_withIdenticalProperty() { | ||
// given: | ||
var sut: [TestClass] = [ | ||
TestClass(someReference: .init(), someOptionalReference: nil), | ||
TestClass(someReference: .init(), someOptionalReference: .init()), | ||
fixtureTestClass, | ||
TestClass(someReference: .init(), someOptionalReference: .init()), | ||
] | ||
|
||
// then: | ||
testFindingElement_withMatchingIdenticalProperty(sut: sut) | ||
|
||
// given | ||
sut = [ | ||
fixtureTestClass, | ||
TestClass(someReference: .init(), someOptionalReference: .init()), | ||
TestClass(someReference: .init(), someOptionalReference: .init()), | ||
TestClass(someReference: .init(), someOptionalReference: nil) | ||
] | ||
|
||
// then: | ||
testFindingElement_withMatchingIdenticalProperty(sut: sut) | ||
|
||
// given | ||
sut = [ | ||
TestClass(someReference: .init(), someOptionalReference: .init()), | ||
TestClass(someReference: .init(), someOptionalReference: nil), | ||
TestClass(someReference: .init(), someOptionalReference: .init()), | ||
fixtureTestClass | ||
] | ||
|
||
// then: | ||
testFindingElement_withMatchingIdenticalProperty(sut: sut) | ||
} | ||
} | ||
|
||
private extension SequenceExtensionTests { | ||
|
||
func testFindingElement_withMatchingIdenticalProperty(sut: [TestClass]) { | ||
XCTAssert(sut.first(where: \.someReference, isIdenticalTo: fixtureReference) === fixtureTestClass, "Should find object with matching identical (`===`) property") | ||
XCTAssert(sut.first(where: \.someOptionalReference, isIdenticalTo: fixtureOptionalReference) === fixtureTestClass, "Should find object with matching identical (`===`) property") | ||
XCTAssertNil(sut.first(where: \.someReference, isIdenticalTo: fixtureOptionalReference), "Should not find any object with matching identical (`===`) property") | ||
XCTAssertNil(sut.first(where: \.someOptionalReference, isIdenticalTo: fixtureReference), "Should not find any object with matching identical (`===`) property") | ||
XCTAssertNil(sut.first(where: \.someReference, isIdenticalTo: .init()), "Should not find any object with matching identical (`===`) property") | ||
XCTAssertNil(sut.first(where: \.someOptionalReference, isIdenticalTo: .init()), "Should not find any object with matching identical (`===`) property") | ||
} | ||
} | ||
|
||
private final class TestClass: Identifiable { | ||
|
||
let someReference: SimpleClass | ||
let someOptionalReference: SimpleClass? | ||
|
||
init(someReference: SimpleClass, someOptionalReference: SimpleClass?) { | ||
self.someReference = someReference | ||
self.someOptionalReference = someOptionalReference | ||
} | ||
} | ||
|
||
private final class SimpleClass {} |
Oops, something went wrong.