Make linter happy

This commit is contained in:
Albert Vaca Cintora
2025-03-01 22:54:22 +01:00
parent 438556bc40
commit 1f107c2312
13 changed files with 92 additions and 97 deletions

View File

@@ -22,7 +22,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
if self.needMenuUpdate == true {
if safe {
safe = false
self.menu?.items.removeAll(where: { !self.allowedMenus.contains($0.title) })
self.menu?.items.removeAll { !self.allowedMenus.contains($0.title) }
safe = true
}
self.needMenuUpdate = false

View File

@@ -34,50 +34,42 @@ public class InternalBattery {
public var temperature: Double?
public var charge: Double? {
get {
if let current = self.currentCapacity,
let max = self.maxCapacity {
return (Double(current) / Double(max)) * 100.0
}
return nil
if let current = self.currentCapacity,
let max = self.maxCapacity {
return (Double(current) / Double(max)) * 100.0
}
return nil
}
public var health: Double? {
get {
if let design = self.designCapacity,
let current = self.maxCapacity {
return (Double(current) / Double(design)) * 100.0
}
return nil
if let design = self.designCapacity,
let current = self.maxCapacity {
return (Double(current) / Double(design)) * 100.0
}
return nil
}
public var timeLeft: String {
get {
if let isCharging = self.isCharging {
if let minutes = isCharging ? self.timeToFull : self.timeToEmpty {
if minutes <= 0 {
return "-"
}
return String(format: "%.2d:%.2d", minutes / 60, minutes % 60)
if let isCharging = self.isCharging {
if let minutes = isCharging ? self.timeToFull : self.timeToEmpty {
if minutes <= 0 {
return "-"
}
return String(format: "%.2d:%.2d", minutes / 60, minutes % 60)
}
return "-"
}
return "-"
}
public var timeRemaining: Int? {
get {
if let isCharging = self.isCharging {
return isCharging ? self.timeToFull : self.timeToEmpty
}
return nil
if let isCharging = self.isCharging {
return isCharging ? self.timeToFull : self.timeToEmpty
}
return nil
}
}

View File

@@ -20,17 +20,15 @@ public class InternalFinder {
public init() { }
public var batteryPresent: Bool {
get {
if !self.internalChecked {
let snapshot = IOPSCopyPowerSourcesInfo().takeRetainedValue()
let sources = IOPSCopyPowerSourcesList(snapshot).takeRetainedValue() as Array
self.hasInternalBattery = sources.count > 0
self.internalChecked = true
}
if !self.internalChecked {
let snapshot = IOPSCopyPowerSourcesInfo().takeRetainedValue()
let sources = IOPSCopyPowerSourcesList(snapshot).takeRetainedValue() as Array
return self.hasInternalBattery
self.hasInternalBattery = !sources.isEmpty
self.internalChecked = true
}
return self.hasInternalBattery
}
fileprivate func open() {
@@ -64,9 +62,9 @@ public class InternalFinder {
let snapshot = IOPSCopyPowerSourcesInfo().takeRetainedValue()
let sources = IOPSCopyPowerSourcesList(snapshot).takeRetainedValue() as Array
for ps in sources {
for source in sources {
// Fetch the information for a given power source out of our snapshot
let info = IOPSGetPowerSourceDescription(snapshot, ps).takeUnretainedValue() as! Dictionary<String, Any>
let info = IOPSGetPowerSourceDescription(snapshot, source).takeUnretainedValue() as! [String: Any]
// Pull out the name and capacity
battery.name = info[kIOPSNameKey] as? String

View File

@@ -36,8 +36,8 @@ class BatteryInfo {
stopNotificationSource()
}
notificationSource = IOPSNotificationCreateRunLoopSource({ _ in
BatteryInfo.shared.observers.forEach { (_, value) in
value.observer?.batteryInfo(didChange: BatteryInfo.shared)
Self.shared.observers.forEach { _, value in
value.observer?.batteryInfo(didChange: Self.shared)
}
}, nil).takeRetainedValue() as CFRunLoopSource
CFRunLoopAddSource(CFRunLoopGetCurrent(), notificationSource, .defaultMode)
@@ -48,14 +48,14 @@ class BatteryInfo {
}
func addObserver(_ observer: ObserverProtocol) {
if observers.count == 0 {
if observers.isEmpty {
startNotificationSource()
}
observers[ObjectIdentifier(observer)] = Observation(observer: observer)
}
func removeObserver(_ observer: ObserverProtocol) {
observers.removeValue(forKey: ObjectIdentifier(observer))
if observers.count == 0 {
if observers.isEmpty {
stopNotificationSource()
}
}
@@ -64,13 +64,13 @@ class BatteryInfo {
}
class BatteryObserver: BatteryInfo.ObserverProtocol {
var batteryInfoClosure: (_ info: BatteryInfo) -> ()
var batteryInfoClosure: (_ info: BatteryInfo) -> Void
func batteryInfo(didChange info: BatteryInfo) {
self.batteryInfoClosure(info)
}
init(_ callback: @escaping (_ info: BatteryInfo) -> ()) {
init(_ callback: @escaping (_ info: BatteryInfo) -> Void) {
self.batteryInfoClosure = callback
BatteryInfo.shared.addObserver(self)
}

View File

@@ -36,7 +36,7 @@ class NotificationManager: ObservableObject {
}
func post(title: String, body: String, userInfo: [AnyHashable: Any] = [:], categoryIdentifier: String = "NORMAL", interruptionLevel: UNNotificationInterruptionLevel = .timeSensitive) {
let prepared = NotificationManager.prepareRequest(title: title, body: body, userInfo: userInfo, categoryIdentifier: categoryIdentifier, interruptionLevel: interruptionLevel)
let prepared = Self.prepareRequest(title: title, body: body, userInfo: userInfo, categoryIdentifier: categoryIdentifier, interruptionLevel: interruptionLevel)
inAppNotificationManager.addNotification(request: prepared.request, remover: prepared.remover)
UNUserNotificationCenter.current().add(prepared.request)
}

View File

@@ -201,7 +201,7 @@ struct DevicesDetailView: View {
Text("Battery Level")
} icon: {
Image(systemName: battery.statusSFSymbolName)
.foregroundColor(battery.statusColor ?? .primary)
.foregroundColor(battery.statusColor)
}
Spacer()
Text("\(percent: battery.remoteChargeLevel)")

View File

@@ -46,7 +46,7 @@ struct DevicesView: View {
VStack {
devicesList
.refreshable {
await refreshDiscovery()
refreshDiscovery()
}
.sheet(isPresented: $isDeviceDiscoveryHelpPresented) {
DeviceDiscoveryHelp()

View File

@@ -123,19 +123,21 @@ struct DeviceItemView: View {
withAnimation {
self.backgroundColor = getBackgroundColor(newValue)
}
}.onTapGesture {
}
.onTapGesture {
parent?.clickedDeviceId = self.deviceId
}.onDrop(of: [.fileURL], isTargeted: nil) { providers in
}
.onDrop(of: [.fileURL], isTargeted: nil) { providers in
// Ref: https://stackoverflow.com/questions/60831260/swiftui-drag-and-drop-files
if isPluginAvailable(.share) {
var droppedFileURLs: [URL] = []
providers.forEach { provider in
provider.loadDataRepresentation(forTypeIdentifier: "public.file-url", completionHandler: { (data, error) in
provider.loadDataRepresentation(forTypeIdentifier: "public.file-url") { data, _ in
if let data = data, let path = NSString(data: data, encoding: 4), let url = URL(string: path as String) {
droppedFileURLs.append(url)
print("File drppped: ", url)
}
})
}
}
while droppedFileURLs.count != providers.count {
continue // block thread until all providers are proceeded
@@ -151,7 +153,8 @@ struct DeviceItemView: View {
}
return false
}
}.contextMenu {
}
.contextMenu {
if parent?.clickedDeviceId == self.deviceId {
if self.connState == .connected || self.connState == .saved {
Button("Unpair") {
@@ -191,19 +194,21 @@ struct DeviceItemView: View {
}
}
}
}.mediaImporter(isPresented: $showingPhotosPicker, allowedMediaTypes: .all, allowsMultipleSelection: true) { result in
}
.mediaImporter(isPresented: $showingPhotosPicker, allowedMediaTypes: .all, allowsMultipleSelection: true) { result in
if case .success(let chosenMediaURLs) = result, !chosenMediaURLs.isEmpty {
(backgroundService._devices[self.deviceId]!._plugins[.share] as! Share).prepAndInitFileSend(fileURLs: chosenMediaURLs)
} else {
print("Media Picker Result: \(result)")
}
}.fileImporter(isPresented: $showingFilePicker, allowedContentTypes: allUTTypes, allowsMultipleSelection: true) { result in
}
.fileImporter(isPresented: $showingFilePicker, allowedContentTypes: allUTTypes, allowsMultipleSelection: true) { result in
do {
chosenFileURLs = try result.get()
} catch {
print("Document Picker Error")
}
if (chosenFileURLs.count > 0) {
if (!chosenFileURLs.isEmpty) {
(backgroundService._devices[self.deviceId]!._plugins[.share] as! Share).prepAndInitFileSend(fileURLs: chosenFileURLs)
}
}

View File

@@ -74,17 +74,17 @@ struct DevicesView: View {
return []
case .demo:
return [
DeviceItemView(deviceId: "1", parent: self, deviceName: .constant("My iPhone"), icon: DevicesView.getIconFromDeviceType(.phone), connState: .connected, mockBatteryLevel: 67),
DeviceItemView(deviceId: "2", parent: self, deviceName: .constant("My iMac"), icon: DevicesView.getIconFromDeviceType(.desktop), connState: .connected),
DeviceItemView(deviceId: "3", parent: self, deviceName: .constant("My MacBook"), icon: DevicesView.getIconFromDeviceType(.laptop), connState: .saved),
DeviceItemView(deviceId: "4", parent: self, deviceName: .constant("My iPad"), icon: DevicesView.getIconFromDeviceType(.tablet), connState: .visible),
DeviceItemView(deviceId: "5", parent:self, deviceName: .constant("My Apple TV"), icon: DevicesView.getIconFromDeviceType(.appletv), connState: .visible),
DeviceItemView(deviceId: "6", deviceName: .constant("Unknown device"), icon: DevicesView.getIconFromDeviceType(.unknown), connState: .visible)
DeviceItemView(deviceId: "1", parent: self, deviceName: .constant("My iPhone"), icon: Self.getIconFromDeviceType(.phone), connState: .connected, mockBatteryLevel: 67),
DeviceItemView(deviceId: "2", parent: self, deviceName: .constant("My iMac"), icon: Self.getIconFromDeviceType(.desktop), connState: .connected),
DeviceItemView(deviceId: "3", parent: self, deviceName: .constant("My MacBook"), icon: Self.getIconFromDeviceType(.laptop), connState: .saved),
DeviceItemView(deviceId: "4", parent: self, deviceName: .constant("My iPad"), icon: Self.getIconFromDeviceType(.tablet), connState: .visible),
DeviceItemView(deviceId: "5", parent: self, deviceName: .constant("My Apple TV"), icon: Self.getIconFromDeviceType(.appletv), connState: .visible),
DeviceItemView(deviceId: "6", deviceName: .constant("Unknown device"), icon: Self.getIconFromDeviceType(.unknown), connState: .visible),
]
case .hundred:
var deviceIcons = [DeviceItemView]()
for demoDeviceId in 1...100 {
deviceIcons.append(DeviceItemView(deviceId: String(demoDeviceId), parent: self, deviceName: .constant(String(demoDeviceId)), icon: DevicesView.getIconFromDeviceType(.phone), connState: .saved))
deviceIcons.append(DeviceItemView(deviceId: String(demoDeviceId), parent: self, deviceName: .constant(String(demoDeviceId)), icon: Self.getIconFromDeviceType(.phone), connState: .saved))
}
return deviceIcons
case .normal:
@@ -94,7 +94,7 @@ struct DevicesView: View {
deviceId: key,
parent: self,
deviceName: .constant(viewModel.connectedDevices[key] ?? "Unknown device"),
icon: DevicesView.getIconFromDeviceType(backgroundService._devices[key]?._deviceInfo.type ?? .unknown),
icon: Self.getIconFromDeviceType(backgroundService._devices[key]?._deviceInfo.type ?? .unknown),
connState: .connected
))
}
@@ -103,7 +103,7 @@ struct DevicesView: View {
deviceId: key,
parent: self,
deviceName: .constant(viewModel.savedDevices[key] ?? "Unknown device"),
icon: DevicesView.getIconFromDeviceType(backgroundService._devices[key]?._deviceInfo.type ?? .unknown),
icon: Self.getIconFromDeviceType(backgroundService._devices[key]?._deviceInfo.type ?? .unknown),
connState: .saved
))
}
@@ -112,7 +112,7 @@ struct DevicesView: View {
deviceId: key,
parent: self,
deviceName: .constant(viewModel.visibleDevices[key] ?? "Unknown device"),
icon: DevicesView.getIconFromDeviceType(backgroundService._devices[key]?._deviceInfo.type ?? .unknown),
icon: Self.getIconFromDeviceType(backgroundService._devices[key]?._deviceInfo.type ?? .unknown),
connState: .visible
))
}

View File

@@ -9,18 +9,18 @@
import SwiftUI
//struct EditButtonStyle: ButtonStyle {
// func makeBody(configuration: Configuration) -> some View {
// HStack {
// configuration.label
// .padding(.horizontal, 4)
// }
// .background(.gray)
// .foregroundColor(.white)
// .border(.black)
// .clipShape(Rectangle())
// }
//}
// struct EditButtonStyle: ButtonStyle {
// func makeBody(configuration: Configuration) -> some View {
// HStack {
// configuration.label
// .padding(.horizontal, 4)
// }
// .background(.gray)
// .foregroundColor(.white)
// .border(.black)
// .clipShape(Rectangle())
// }
// }
struct PeerSettingsView: View {
@Binding var directIPs: [String]
@@ -90,8 +90,7 @@ struct PeerSettingsView: View {
}
if colorScheme == .light {
mainFrame
}
else {
} else {
mainFrame.colorInvert()
}
HStack {

View File

@@ -33,7 +33,7 @@ struct MainView: View {
func helpButton(_ action: @escaping () -> Void) -> some View {
// ref: https://blog.urtti.com/creating-a-macos-help-button-in-swiftui
Button(action: action, label: {
Button(action: action) {
ZStack {
Circle()
.strokeBorder(Color(NSColor.controlShadowColor), lineWidth: 0.5)
@@ -42,7 +42,7 @@ struct MainView: View {
.frame(width: 20, height: 20)
Text("?").font(.system(size: 15, weight: .medium ))
}
})
}
.buttonStyle(PlainButtonStyle())
}
@@ -72,19 +72,19 @@ struct MainView: View {
}
}
}
.refreshable(action: {
.refreshable {
refreshDiscoveryAndList()
})
}
.onAppear {
self.disabledSingletonConflict = MainView.mainViewSingleton != nil
self.disabledSingletonConflict = Self.mainViewSingleton != nil
if !self.disabledSingletonConflict {
MainView.mainViewSingleton = self
Self.mainViewSingleton = self
}
}
.onReceive(NotificationCenter.default.publisher(for: .didReceivePairRequestNotification, object: nil)
.receive(on: RunLoop.main)) { notification in
onPairRequest(fromDeviceWithID: notification.userInfo?["deviceID"] as? String)
}
}
.onReceive(NotificationCenter.default.publisher(for: .pairRequestTimedOutNotification, object: nil)
.receive(on: RunLoop.main)) { notification in
onPairTimeout(toDeviceWithID: notification.userInfo?["deviceID"] as? String)
@@ -104,9 +104,9 @@ struct MainView: View {
.onReceive(NotificationCenter.default.publisher(for: .didReceiveFindMyPhoneRequestNotification, object: nil)
.receive(on: RunLoop.main)) { _ in
showFindMyPhoneAlert()
MainView.updateFindMyPhoneTimer(isRunning: true) // TODO: alert sound does not work
Self.updateFindMyPhoneTimer(isRunning: true) // TODO: alert sound does not work
}
.onReceive(MainView.findMyPhoneTimer) { _ in
.onReceive(Self.findMyPhoneTimer) { _ in
SystemSound.calendarAlert.play()
}
} else {
@@ -159,13 +159,13 @@ struct MainView: View {
static func updateFindMyPhoneTimer(isRunning: Bool) {
if isRunning {
MainView.findMyPhoneTimer = Deferred {
Self.findMyPhoneTimer = Deferred {
Just(Date())
}
.append(Timer.publish(every: 4, on: .main, in: .common).autoconnect())
.eraseToAnyPublisher()
} else {
MainView.findMyPhoneTimer = Empty<Date, Never>().eraseToAnyPublisher()
Self.findMyPhoneTimer = Empty<Date, Never>().eraseToAnyPublisher()
}
}

View File

@@ -82,7 +82,9 @@ struct NotificationView: View {
}
selectedRequest = inAppNotificationManager.requests[selectedNotificationIndex]
selectedRemover = { inAppNotificationManager.removeNotification(at: selectedNotificationIndex) }
let selectedCategoryActions = NotificationManager.categories.first(where: { $0.identifier == selectedRequest!.content.categoryIdentifier })!.actions
let selectedCategoryActions = NotificationManager.categories
.first { $0.identifier == selectedRequest!.content.categoryIdentifier }!
.actions
backgroundColor = switch selectedRequest!.content.categoryIdentifier {
case "PAIR_REQUEST": .yellow
case "NORMAL": .secondary
@@ -163,9 +165,9 @@ struct NotificationView: View {
.onAppear {
updateSelection()
}
.onChange(of: inAppNotificationManager.requests, perform: { _ in
.onChange(of: inAppNotificationManager.requests) { _ in
updateSelection()
})
}
}
.padding(4)
}

View File

@@ -12,7 +12,6 @@
// Created by Lucas Wang on 2021-06-17.
//
#if !os(macOS)
import SwiftUI