open-merou

Open Mérou, a free iOS app for the Oceanic+ underwater iPhone housing
git clone https://git.schurger.org/open-merou.git
Log | Files | Refs | README | LICENSE

HousingView.swift (4603B)


      1 // SPDX-License-Identifier: GPL-2.0-only
      2 import SwiftUI
      3 
      4 /// Diagnostics screen of the prototype: everything the housing sends, live.
      5 struct HousingView: View {
      6     let housing: HousingClient
      7     @Environment(\.dismiss) private var dismiss
      8 
      9     var body: some View {
     10         NavigationStack {
     11             List {
     12                 Section {
     13                     Text(housing.status.label)
     14                     if let event = housing.readings.lastButton {
     15                         HStack {
     16                             Text("Last button")
     17                             Spacer()
     18                             Text(event.button.label + (event.isLong ? " (long)" : ""))
     19                                 .font(.title2.bold())
     20                         }
     21                     }
     22                 }
     23 
     24                 Section("Vacuum") {
     25                     VacuumView(vacuum: housing.vacuum)
     26                 }
     27 
     28                 Section("Housing") {
     29                     row("Internal pressure", housing.readings.internalPressurePa.map { hPa($0) })
     30                     row("External pressure (assumed)", housing.readings.externalPressurePa.map { hPa($0) })
     31                     row("Internal temperature", housing.readings.temperatureC.map { String(format: "%.2f °C", $0) })
     32                     row("Battery", housing.readings.batteryPercent.map { "\($0) %" })
     33                     row("Pump", housing.readings.pumpState.map(pumpLabel))
     34                     row("1628 (alarm?)", housing.readings.alarmRaw.map { String(format: "%02X", $0) })
     35                 }
     36 
     37                 if housing.pumpRequested {
     38                     Section {
     39                         Button("Stop the pump", role: .destructive) { housing.stopPump(reason: String(localized: "button")) }
     40                     }
     41                 }
     42 
     43                 if housing.simulated {
     44                     Section { Button("Leave the simulation") { housing.stopSimulation() } }
     45                 } else if housing.released {
     46                     Section { Button("Reconnect") { housing.reconnect() } }
     47                 } else if housing.isConnected {
     48                     Section {
     49                         Button("Disconnect from the housing", role: .destructive) { housing.release() }
     50                     } footer: {
     51                         Text("Once disconnected, hold the right button of the housing for 2 seconds to turn it off.")
     52                     }
     53                 }
     54 
     55                 if !housing.deviceInfo.isEmpty {
     56                     Section("Device") {
     57                         // Sorted by displayed label, as the rows always were.
     58                         ForEach(housing.deviceInfo.keys.sorted { $0.label < $1.label }, id: \.self) { field in
     59                             LabeledContent(field.label, value: housing.deviceInfo[field] ?? "—")
     60                         }
     61                     }
     62                 }
     63 
     64                 let files = DevLog.files
     65                 if !files.isEmpty {
     66                     Section("Development mode logs") {
     67                         ForEach(files, id: \.self) { file in
     68                             ShareLink(item: file) { Text(file.lastPathComponent).font(.footnote) }
     69                         }
     70                     }
     71                 }
     72 
     73                 Section("Log") {
     74                     ForEach(housing.log) { line in
     75                         HStack(alignment: .firstTextBaseline) {
     76                             Text(line.date, format: .dateTime.hour().minute().second())
     77                                 .foregroundStyle(.secondary)
     78                                 .monospacedDigit()
     79                             Text(line.text)
     80                         }
     81                         .font(.footnote)
     82                     }
     83                 }
     84             }
     85             .navigationTitle("Open Mérou")
     86             .navigationBarTitleDisplayMode(.inline)
     87             .toolbar {
     88                 ToolbarItem(placement: .cancellationAction) {
     89                     Button { dismiss() } label: { Label("Back", systemImage: "chevron.left").labelStyle(.titleAndIcon) }
     90                 }
     91             }
     92         }
     93     }
     94 
     95     private func row(_ title: LocalizedStringKey, _ value: String?) -> some View {
     96         LabeledContent(title, value: value ?? "—")
     97     }
     98 
     99     private func hPa(_ pa: Double) -> String {
    100         String(format: "%.1f hPa", pa / 100)
    101     }
    102 
    103     private func pumpLabel(_ state: PumpState) -> String {
    104         switch state {
    105         case .running: String(localized: "running")
    106         case .stopped: String(localized: "stopped")
    107         case .other(let byte): String(localized: "unknown (\(String(format: "%02X", byte)))")
    108         }
    109     }
    110 }