Skip to content

Commit

Permalink
Add conversion picker
Browse files Browse the repository at this point in the history
ayn committed Oct 9, 2020
1 parent 339b243 commit b2bac67
Showing 6 changed files with 51 additions and 14 deletions.
17 changes: 14 additions & 3 deletions PurpleMenu/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -94,9 +94,20 @@ class AppDelegate: NSObject, NSApplicationDelegate {
let humidity = Float(humidityStr)
else { return }

debugPrint("id = \(self.sensorViewModel.sensorId), pm25 = \(pm25), pm25Cf1 = \(pm25Cf1), RH = \(humidity)")

let aqi = self.pmToEPA(paCf1: (pm25Cf1 + pm25Cf1B) * 0.5, humidity: humidity)
debugPrint("id = \(self.sensorViewModel.sensorId), conversion = \(UserDefaults.standard.conversion), pm25 = \(pm25), pm25Cf1 = \(pm25Cf1), RH = \(humidity)")

let aqi: Int

switch Conversion(rawValue: UserDefaults.standard.conversion) ?? Conversion.epa {
case .none:
aqi = self.pmToAQI(pm25)
case .epa:
aqi = self.pmToEPA(paCf1: (pm25Cf1 + pm25Cf1B) * 0.5, humidity: humidity)
case .aqandu:
aqi = self.pmToAQandU(pm: pm25)
case .lrapa:
aqi = self.pmToLRAPA(paCf1: pm25Cf1)
}

DispatchQueue.main.async {
self.statusBarItem.button?.title = "aqi=\(aqi)"
7 changes: 7 additions & 0 deletions PurpleMenu/ContentView.swift
Original file line number Diff line number Diff line change
@@ -23,6 +23,13 @@ struct ContentView: View {
.frame(width: 80)
}
.frame(width: g.size.width / 2, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
Picker("Conversion", selection: $sensor.conversion) {
ForEach(Conversion.allCases) { conversion in
Text(conversion.rawValue)
}
}
.frame(width: 180)
.pickerStyle(PopUpButtonPickerStyle())
HStack {
Button("Update") {
sensor.update()
9 changes: 9 additions & 0 deletions PurpleMenu/Sensor.swift
Original file line number Diff line number Diff line change
@@ -7,6 +7,15 @@

import Foundation

enum Conversion: String, CaseIterable, Identifiable {
case none = "None", epa = "US EPA", aqandu = "AQandU", lrapa = "LRAPA"

var id: String { self.rawValue }
var description: String {
return self.rawValue
}
}

struct Sensor: Codable {
let mapVersion : String?
let baseVersion : String?
14 changes: 9 additions & 5 deletions PurpleMenu/SensorViewModel.swift
Original file line number Diff line number Diff line change
@@ -9,13 +9,17 @@ import Foundation
import Combine

final class SensorViewModel: ObservableObject {
static let userDefaultsKey = "SensorId"

@Published var sensorId = UserDefaults.standard.sensorId
@Published var conversion = UserDefaults.standard.conversion {
didSet {
self.update()
}
}

func update() {
if !sensorId.isEmpty {
UserDefaults.standard.sensorId = sensorId
}
guard !sensorId.isEmpty else { return }

UserDefaults.standard.sensorId = sensorId
UserDefaults.standard.conversion = conversion
}
}
9 changes: 9 additions & 0 deletions PurpleMenu/UserDefaultsExtension.swift
Original file line number Diff line number Diff line change
@@ -16,4 +16,13 @@ extension UserDefaults {
set(newValue, forKey: "sensor_id")
}
}

@objc var conversion: String {
get {
return string(forKey: "conversion") ?? Conversion.epa.rawValue
}
set {
set(newValue, forKey: "conversion")
}
}
}
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# PurpleMenu
I wrote this to display the nearest Purple Air AQI with US EPA conversion in my macOS menu bar, tested in Catalina and Big Sur.
I wrote this to display my nearest Purple Air AQI with US EPA conversion in my macOS menu bar, tested in Catalina and Big Sur.

For now you can change the `sensorId` [here](https://github.com/ayn/PurpleMenu/blob/master/PurpleMenu/AppDelegate.swift#L67) to monitor your preferred Purple Air sensor.
There's a basic UI to change sensor to any public Purple Air outdoor sensor, and choose between US EPA, AQandU, LRAPA, and no conversion.

## TODOS

* Settings UI
* choose which conversion (or none) to use
To get a sensor ID, go to [Purple Air Map](http://www.purpleair.com/map?mylocation) in your browser, click/tap on a sensor on the map, look at the url in your browser's location bar, and find the `select=` parameter.

0 comments on commit b2bac67

Please sign in to comment.