Skip to content

Commit

Permalink
install SwiftFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
Gurrium committed Apr 5, 2022
1 parent ce1de69 commit eba6d96
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 50 deletions.
2 changes: 2 additions & 0 deletions .swiftformat
@@ -0,0 +1,2 @@
--ranges no-space
--patternlet inline
16 changes: 16 additions & 0 deletions BuildTools/Package.resolved
@@ -0,0 +1,16 @@
{
"object": {
"pins": [
{
"package": "SwiftFormat",
"repositoryURL": "https://github.com/nicklockwood/SwiftFormat",
"state": {
"branch": null,
"revision": "16e7dd37937af0f9adf7d8cfb35e97146ce1875f",
"version": "0.49.7"
}
}
]
},
"version": 1
}
11 changes: 11 additions & 0 deletions BuildTools/Package.swift
@@ -0,0 +1,11 @@
// swift-tools-version:5.1
import PackageDescription

let package = Package(
name: "BuildTools",
platforms: [.macOS(.v10_11)],
dependencies: [
.package(url: "https://github.com/nicklockwood/SwiftFormat", from: "0.49.0"),
],
targets: [.target(name: "BuildTools", path: "")]
)
21 changes: 21 additions & 0 deletions Komusou.xcodeproj/project.pbxproj
Expand Up @@ -181,6 +181,7 @@
isa = PBXNativeTarget;
buildConfigurationList = E3178D7627D379730000DB0F /* Build configuration list for PBXNativeTarget "Komusou" */;
buildPhases = (
E3578A0927FC6BE2001C7397 /* ShellScript */,
E3178D4E27D379710000DB0F /* Sources */,
E3178D4F27D379710000DB0F /* Frameworks */,
E3178D5027D379710000DB0F /* Resources */,
Expand Down Expand Up @@ -302,6 +303,26 @@
};
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
E3578A0927FC6BE2001C7397 /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
);
outputFileListPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "SDKROOT=(xcrun --sdk macosx --show-sdk-path)\n#swift package update #Uncomment this line temporarily to update the version used to the latest matching your BuildTools/Package.swift file\nswift run -c release --package-path BuildTools swiftformat \"$SRCROOT\" --swiftversion 5.0 --config .swiftformat\n";
};
/* End PBXShellScriptBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
E3178D4E27D379710000DB0F /* Sources */ = {
isa = PBXSourcesBuildPhase;
Expand Down
16 changes: 9 additions & 7 deletions Komusou/BluetoothCadenceSensor.swift
Expand Up @@ -5,8 +5,8 @@
// Created by gurrium on 2022/03/17.
//

import Foundation
import CoreBluetooth
import Foundation

final class BluetoothCadenceSensor: NSObject, CadenceSensor {
// CadenceSensor
Expand All @@ -23,13 +23,15 @@ final class BluetoothCadenceSensor: NSObject, CadenceSensor {
}
}
}

private var connectedPeripheral: CBPeripheral?
// speed measurement
private var cadence: Double = 0 {
didSet {
delegate?.onCadenceUpdate(cadence)
}
}

private var previousCrankEventTime: UInt16?
private var previousCumulativeCrankRevolutions: UInt16?
private var cadenceMeasurementPauseCounter = 0 {
Expand All @@ -52,34 +54,34 @@ extension BluetoothCadenceSensor: CBCentralManagerDelegate {
isBluetoothEnabled = central.state == .poweredOn
}

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
func centralManager(_: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData _: [String: Any], rssi _: NSNumber) {
// ここで参照を保持しないと破棄される
connectedPeripheral = peripheral

centralManager.connect(peripheral, options: nil)
}

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
func centralManager(_: CBCentralManager, didConnect peripheral: CBPeripheral) {
peripheral.delegate = self
peripheral.discoverServices([.cyclingSpeedAndCadence])
}
}

extension BluetoothCadenceSensor: CBPeripheralDelegate {
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices _: Error?) {
guard let service = peripheral.services?.first(where: { $0.uuid == .cyclingSpeedAndCadence }) else { return }

peripheral.discoverCharacteristics([.cscMeasurement], for: service)
}

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
guard let characteristic = service.characteristics?.first(where: { $0.uuid == .cscMeasurement}),
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error _: Error?) {
guard let characteristic = service.characteristics?.first(where: { $0.uuid == .cscMeasurement }),
characteristic.properties.contains(.notify) else { return }

peripheral.setNotifyValue(true, for: characteristic)
}

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
func peripheral(_: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error _: Error?) {
guard let data = characteristic.value else { return }

let value = [UInt8](data)
Expand Down
18 changes: 10 additions & 8 deletions Komusou/BluetoothSpeedSensor.swift
Expand Up @@ -5,8 +5,8 @@
// Created by gurrium on 2022/03/16.
//

import Foundation
import CoreBluetooth
import Foundation

final class BluetoothSpeedSensor: NSObject, SpeedSensor {
// SpeedSensor
Expand All @@ -23,13 +23,15 @@ final class BluetoothSpeedSensor: NSObject, SpeedSensor {
}
}
}

private var connectedPeripheral: CBPeripheral?
// speed measurement
private var speed: Double = 0 {
didSet {
delegate?.onSpeedUpdate(speed)
}
}

private var previousWheelEventTime: UInt16?
private var previousCumulativeWheelRevolutions: UInt32?
private var speedMeasurementPauseCounter = 0 {
Expand All @@ -52,34 +54,34 @@ extension BluetoothSpeedSensor: CBCentralManagerDelegate {
isBluetoothEnabled = central.state == .poweredOn
}

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
func centralManager(_: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData _: [String: Any], rssi _: NSNumber) {
// ここで参照を保持しないと破棄される
connectedPeripheral = peripheral

centralManager.connect(peripheral, options: nil)
}

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
func centralManager(_: CBCentralManager, didConnect peripheral: CBPeripheral) {
peripheral.delegate = self
peripheral.discoverServices([.cyclingSpeedAndCadence])
}
}

extension BluetoothSpeedSensor: CBPeripheralDelegate {
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices _: Error?) {
guard let service = peripheral.services?.first(where: { $0.uuid == .cyclingSpeedAndCadence }) else { return }

peripheral.discoverCharacteristics([.cscMeasurement], for: service)
}

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
guard let characteristic = service.characteristics?.first(where: { $0.uuid == .cscMeasurement}),
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error _: Error?) {
guard let characteristic = service.characteristics?.first(where: { $0.uuid == .cscMeasurement }),
characteristic.properties.contains(.notify) else { return }

peripheral.setNotifyValue(true, for: characteristic)
}

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
func peripheral(_: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error _: Error?) {
guard let data = characteristic.value else { return }

let value = [UInt8](data)
Expand Down
4 changes: 2 additions & 2 deletions Komusou/CBUUID+AssignedNumbers.swift
Expand Up @@ -8,6 +8,6 @@
import CoreBluetooth

extension CBUUID {
static var cyclingSpeedAndCadence: Self { Self.init(string: "1816") }
static var cscMeasurement: Self { Self.init(string: "2a5b") }
static var cyclingSpeedAndCadence: Self { Self(string: "1816") }
static var cscMeasurement: Self { Self(string: "2a5b") }
}
6 changes: 4 additions & 2 deletions Komusou/ControlPanelView.swift
Expand Up @@ -17,6 +17,7 @@ final class ControlPanelView: UIView {

return formatter
}()

private static let cadenceFormatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .none
Expand All @@ -26,10 +27,11 @@ final class ControlPanelView: UIView {

return formatter
}()

// @IBOutlet weak var speedPanel: UIView!
@IBOutlet weak var speedLabel: UILabel!
@IBOutlet var speedLabel: UILabel!
// @IBOutlet weak var cadencePanel: UIView!
@IBOutlet weak var cadenceLabel: UILabel!
@IBOutlet var cadenceLabel: UILabel!

override func awakeFromNib() {
super.awakeFromNib()
Expand Down
21 changes: 11 additions & 10 deletions Komusou/Settings/SensorSettingView.swift
Expand Up @@ -5,9 +5,9 @@
// Created by gurrium on 2022/03/25.
//

import SwiftUI
import CoreBluetooth
import Combine
import CoreBluetooth
import SwiftUI

struct SensorSettingView: View {
@ObservedObject
Expand Down Expand Up @@ -70,7 +70,7 @@ final class SensorSettingViewState: ObservableObject {
BluetoothManager.shared.connectToSpeedSensor(uuid: uuid).sink { [unowned self] result in
switch result {
case .failure:
self?.didError = true
self.didError = true
case .finished:
break
}
Expand Down Expand Up @@ -156,7 +156,7 @@ final class BluetoothManager: NSObject {
typealias ConnectingWithPeripheralFuture = Future<Void, ConnectingWithPeripheralError>

static let shared = BluetoothManager()
static private let kSpeedSensorKey = "speed_sensor_key"
private static let kSpeedSensorKey = "speed_sensor_key"

@Published
private(set) var discoveredPeripherals = [UUID: CBPeripheral]()
Expand All @@ -182,11 +182,13 @@ final class BluetoothManager: NSObject {
}
}
}

private var _connectedSpeedSensorUUID: UUID?
private var connectingSpeedSensorUUID: UUID?
private var isBluetoothEnabled: Bool {
centralManager.state == .poweredOn
}

private var speedSensorPromise: ConnectingWithPeripheralFuture.Promise?
private var cancellables = Set<AnyCancellable>()

Expand Down Expand Up @@ -241,13 +243,13 @@ final class BluetoothManager: NSObject {
}

extension BluetoothManager: CBCentralManagerDelegate {
func centralManagerDidUpdateState(_ central: CBCentralManager) {}
func centralManagerDidUpdateState(_: CBCentralManager) {}

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
func centralManager(_: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData _: [String: Any], rssi _: NSNumber) {
discoveredPeripherals[peripheral.identifier] = peripheral
}

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
func centralManager(_: CBCentralManager, didConnect peripheral: CBPeripheral) {
switch peripheral.identifier {
case connectingSpeedSensorUUID:
connectingSpeedSensorUUID = nil
Expand All @@ -258,7 +260,7 @@ extension BluetoothManager: CBCentralManagerDelegate {
}
}

func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
func centralManager(_: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error _: Error?) {
switch peripheral.identifier {
case connectingSpeedSensorUUID:
connectingSpeedSensorUUID = nil
Expand All @@ -269,5 +271,4 @@ extension BluetoothManager: CBCentralManagerDelegate {
}
}

extension BluetoothManager: CBPeripheralDelegate {
}
extension BluetoothManager: CBPeripheralDelegate {}
9 changes: 5 additions & 4 deletions Komusou/Settings/TireSettingView.swift
Expand Up @@ -5,8 +5,8 @@
// Created by gurrium on 2022/03/09.
//

import SwiftUI
import Combine
import SwiftUI

struct TireSettingView: View {
@Binding var tireSize: TireSize
Expand All @@ -15,7 +15,7 @@ struct TireSettingView: View {
@FocusState var isCustomTireSizeStringFieldFocused: Bool

init(tireSize: Binding<TireSize>) {
self._tireSize = tireSize
_tireSize = tireSize

if case .custom(let circumference) = tireSize.wrappedValue {
customTireSizeString = String(circumference)
Expand All @@ -25,7 +25,7 @@ struct TireSettingView: View {
isTireCustomSize = false
}
}

var body: some View {
List {
Section {
Expand All @@ -43,7 +43,8 @@ struct TireSettingView: View {
.focused($isCustomTireSizeStringFieldFocused)
.onChange(of: customTireSizeString) { [old = customTireSizeString] new in
if let size = Int(new),
new.count <= TireSize.significantDigits {
new.count <= TireSize.significantDigits
{
// 整数として解釈できる文字列
tireSize = .custom(size)
} else if new.isEmpty {
Expand Down

0 comments on commit eba6d96

Please sign in to comment.