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

swiftlint config and inital formatting pass #765

Merged
merged 1 commit into from
Jul 10, 2023
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
23 changes: 23 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
disabled_rules:
- cyclomatic_complexity
- identifier_name
- multiple_closures_with_trailing_closure
- nesting
- type_name
- unused_closure_parameter

excluded: # case-sensitive paths to ignore during linting. Takes precedence over `included`
- Pods
- "**/*.pb.swift" # exclude files with a wildcard

line_length: 300
Copy link
Collaborator

Choose a reason for hiding this comment

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

I doubt we have lines longer than 120 characters, but... if it was not enforced or checked, who knows.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added it so that there are no warnings (we have a few). The ruleset could be reduced and refined with future PRs (out of scope for this one IMHO).

type_body_length:
- 600

function_body_length:
- 150

file_length:
warning: 800

reporter: "xcode"
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import enum CoreBluetooth.CBManagerState

func encode(_ centralState: CBManagerState) -> Int32 {
switch (centralState) {
switch centralState {
case .unknown, .resetting:
return 0
case .unsupported:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ enum MaximumWriteValueLengthRetrieval: Int {
}

enum RequestConnectionPriorityFailure: Int {

case operationNotSupported = 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,3 @@ struct EventSink {
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class PluginController {
}
}
}
var messageQueue: [CharacteristicValueInfo] = [];
var messageQueue: [CharacteristicValueInfo] = []
var connectedDeviceSink: EventSink?
var characteristicValueUpdateSink: EventSink?

Expand Down Expand Up @@ -128,11 +128,11 @@ final class PluginController {
}
}
let sink = context.characteristicValueUpdateSink
if (sink != nil) {
if sink != nil {
sink!.add(.success(message))
} else {
// In case message arrives before sink is created
context.messageQueue.append(message);
context.messageQueue.append(message)
}

}
Expand Down Expand Up @@ -205,8 +205,7 @@ final class PluginController {
let servicesWithCharacteristicsToDiscover: ServicesWithCharacteristicsToDiscover
if args.hasServicesWithCharacteristicsToDiscover {
let items = args.servicesWithCharacteristicsToDiscover.items.reduce(
into: [ServiceID: [CharacteristicID]](),
{ dict, item in
into: [ServiceID: [CharacteristicID]](), { dict, item in
let serviceID = CBUUID(data: item.serviceID.data)
let characteristicIDs = item.characteristics.map { CBUUID(data: $0.data) }

Expand All @@ -221,7 +220,7 @@ final class PluginController {
let timeout = args.timeoutInMs > 0 ? TimeInterval(args.timeoutInMs) / 1000 : nil

completion(.success(nil))

if let sink = connectedDeviceSink {
let message = DeviceInfo.with {
$0.id = args.deviceID
Expand All @@ -231,7 +230,7 @@ final class PluginController {
} else {
print("Warning! No event channel set up to report a connection update")
}

do {
try central.connect(
to: deviceID,
Expand Down Expand Up @@ -296,10 +295,10 @@ final class PluginController {
Uuid.with { $0.data = characteristic.uuid.data }
}
$0.characteristics = (service.characteristics ?? []).map { characteristic in
DiscoveredCharacteristic.with{
$0.characteristicID = Uuid.with{$0.data = characteristic.uuid.data}
DiscoveredCharacteristic.with {
$0.characteristicID = Uuid.with {$0.data = characteristic.uuid.data}
if characteristic.service?.uuid.data != nil {
$0.serviceID = Uuid.with{$0.data = characteristic.service!.uuid.data}
$0.serviceID = Uuid.with {$0.data = characteristic.service!.uuid.data}
}
$0.isReadable = characteristic.properties.contains(.read)
$0.isWritableWithResponse = characteristic.properties.contains(.write)
Expand All @@ -308,7 +307,7 @@ final class PluginController {
$0.isIndicatable = characteristic.properties.contains(.indicate)
}
}

$0.includedServices = (service.includedServices ?? []).map(makeDiscoveredService)
}
}
Expand Down Expand Up @@ -463,20 +462,20 @@ final class PluginController {
completion(.success(result))
}
}

func writeCharacteristicWithoutResponse(name: String, args: WriteCharacteristicRequest, completion: @escaping PlatformMethodCompletionHandler) {
guard let central = central
else {
completion(.failure(PluginError.notInitialized.asFlutterError))
return
}

guard let characteristic = QualifiedCharacteristicIDFactory().make(from: args.characteristic)
else {
completion(.failure(PluginError.invalidMethodCall(method: name, details: "characteristic, service, and peripheral IDs are required").asFlutterError))
return
}

let result: WriteCharacteristicInfo
do {
try central.writeWithoutResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public class SwiftReactiveBlePlugin: NSObject, FlutterPlugin {
}),
AnyPlatformMethod(UnaryPlatformMethod(name: "negotiateMtuSize") { (name, context, args: NegotiateMtuRequest, completion) in
context.reportMaximumWriteValueLength(name: name, args: args, completion: completion)
}),
})
])

public func handle(_ call: FlutterMethodCall, result completion: @escaping FlutterResult) {
Expand Down
20 changes: 10 additions & 10 deletions packages/reactive_ble_mobile/ios/Classes/ReactiveBle/Central.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ final class Central {
discover: servicesWithCharacteristicsToDiscover,
completion: central.onServicesWithCharacteristicsInitialDiscovery
)
case .failedToConnect(_), .disconnected(_):
case .failedToConnect, .disconnected:
break
}
}
Expand Down Expand Up @@ -172,7 +172,7 @@ final class Central {
for peripheralID: PeripheralID,
discover servicesWithCharacteristicsToDiscover: ServicesWithCharacteristicsToDiscover,
completion: @escaping ServicesWithCharacteristicsDiscoveryHandler
) throws -> Void {
) throws {
let peripheral = try resolve(connected: peripheralID)

discoverServicesWithCharacteristics(
Expand All @@ -186,7 +186,7 @@ final class Central {
for peripheral: CBPeripheral,
discover servicesWithCharacteristicsToDiscover: ServicesWithCharacteristicsToDiscover,
completion: @escaping ServicesWithCharacteristicsDiscoveryHandler
) -> Void {
) {
servicesWithCharacteristicsDiscoveryRegistry.registerTask(
key: peripheral.identifier,
params: .init(servicesWithCharacteristicsToDiscover: servicesWithCharacteristicsToDiscover),
Expand Down Expand Up @@ -226,10 +226,10 @@ final class Central {

guard characteristic.properties.contains(.read)
else { throw Failure.notReadable(qualifiedCharacteristic) }

guard let peripheral = characteristic.service?.peripheral
else { throw Failure.peripheralIsUnknown(qualifiedCharacteristic.peripheralID) }

peripheral.readValue(for: characteristic)

}
Expand All @@ -251,16 +251,16 @@ final class Central {
completion(central, qualifiedCharacteristic, error)
}
)

guard let peripheral = characteristic.service?.peripheral
else{ throw Failure.peripheralIsUnknown(qualifiedCharacteristic.peripheralID) }
else { throw Failure.peripheralIsUnknown(qualifiedCharacteristic.peripheralID) }

characteristicWriteRegistry.updateTask(
key: qualifiedCharacteristic,
action: { $0.start(peripheral: peripheral) }
)
}

func writeWithoutResponse(
value: Data,
characteristic qualifiedCharacteristic: QualifiedCharacteristic
Expand All @@ -269,10 +269,10 @@ final class Central {

guard characteristic.properties.contains(.writeWithoutResponse)
else { throw Failure.notWritable(qualifiedCharacteristic) }

guard let response = characteristic.service?.peripheral?.writeValue(value, for: characteristic, type: .withoutResponse)
else { throw Failure.characteristicNotFound(qualifiedCharacteristic) }

return response
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class CentralManagerDelegate: NSObject, CBCentralManagerDelegate {
onStateChange(central.state)
}

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi: NSNumber) {
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi: NSNumber) {
onDiscovery(peripheral, advertisementData, rssi.intValue)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct CharacteristicNotifyTaskController: PeripheralTaskController {
func start(characteristic: CBCharacteristic) -> SubjectTask {
guard let peripheral = characteristic.service?.peripheral
else { return task.with(state: task.state.finished(CharacteristicNotifyError.unExpected)) }

peripheral.setNotifyValue(task.params.state.isOn, for: characteristic)
return task.with(state: task.state.processing(.applying))
}
Expand All @@ -26,7 +26,7 @@ struct CharacteristicNotifyTaskController: PeripheralTaskController {
return task.with(state: task.state.finished(error))
}

private enum CharacteristicNotifyError: Error{
case unExpected;
private enum CharacteristicNotifyError: Error {
case unExpected
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct ConnectTaskController: PeripheralTaskController {
case .processing(since: _, .connecting):
centralManager.cancelPeripheralConnection(peripheral)
return task.with(state: task.state.finished(.failedToConnect(error)))
case .finished(in: _, _):
case .finished:
assert(false)
return task
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
struct ConnectTaskSpec: PeripheralTaskSpec {

typealias Key = PeripheralID

struct Params {}

enum Stage {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ final class PeripheralTaskRegistry<Controller: PeripheralTaskController> {
tasks.update(record.with(task: updatedTask))
if case .pending = record.task.state,
case .processing = updatedTask.state,
let timeout = record.task.timeout
{
let timeout = record.task.timeout {
scheduleTaskTimeout(record.uniqueID, timeout)
}
}
Expand Down