SPB Git

Repository Public License

BatteryView.swift 5.8 KB 101 lines swift

1// ORIX Secure2// File: App/Sources/Features/Battery/BatteryView.swift3// Description: Battery and performance advisor: the readings iOS provides, guidance grounded in them, user-assisted checks in4//              Settings, and a plain list of what no app can do (no "RAM boost").5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import ORIXDesign9import ORIXSecureEngines10import ORIXSecureModel11import SwiftUI1213struct BatteryView: View {14    @Environment(AppModel.self) private var model1516    var body: some View {17        let device = model.device18        let advice = model.diagnosticsEngine.advice(device)19        ScreenScroll {20            VStack(alignment: .leading, spacing: ORIX.Space.md) {21                HStack(alignment: .center, spacing: ORIX.Space.lg) {22                    gauge(device)23                    VStack(alignment: .leading, spacing: 4) {24                        if let level = device.batteryLevel.value {25                            Text(verbatim: ORIXFormat.percent(level)).font(.system(.largeTitle, design: .rounded, weight: .bold)).foregroundStyle(ORIX.Palette.textPrimary)26                            Text("iOS reports the level in 5 % steps").font(.orixCaption).foregroundStyle(ORIX.Palette.textTertiary)27                        } else {28                            Text("Battery level not available").font(.orixHeadline).foregroundStyle(ORIX.Palette.textPrimary)29                            Text(verbatim: device.batteryLevel.unavailability?.detail ?? "").font(.orixCaption).foregroundStyle(ORIX.Palette.textSecondary)30                        }31                        if let state = device.batteryState.value { Text(verbatim: state.title).font(.orixCallout).foregroundStyle(ORIX.Palette.textSecondary) }32                    }33                }34                .accessibilityElement(children: .combine)35                Divider()36                ReadingRow(label: String(localized: "Low Power Mode"), value: .measured(device.lowPowerMode ? String(localized: "On") : String(localized: "Off")))37                ReadingRow(label: String(localized: "Thermal state"), value: .measured(device.thermal.title), note: device.thermal.explanation)38                ReadingRow(label: String(localized: "Active processor cores"), value: .measured("\(device.activeProcessorCount) / \(device.processorCount)"))39                ReadingRow(label: String(localized: "Memory used by ORIX Secure"), value: device.appMemoryFootprint.map { ORIXFormat.bytes($0) },40                           note: String(localized: "This app only. Other apps' memory cannot be read."))41            }42            .orixCard()43            .accessibilityElement(children: .combine)44            .accessibilityIdentifier("battery.readings")45            section("From the readings", advice.filter { $0.kind == .observation }, empty: "Nothing in the current readings calls for action.")46            section("Only you can check", advice.filter { $0.kind == .userCheck }, empty: nil)47            section("Good to know", advice.filter { $0.kind == .guidance }, empty: nil)48            ORIXSectionHeader("What ORIX Secure does not offer")49            VStack(alignment: .leading, spacing: ORIX.Space.sm) {50                ForEach(DeviceDiagnosticsEngine.notOffered, id: \.self) { line in51                    Label { Text(verbatim: line) } icon: { Image(systemName: "xmark.circle") }.font(.orixCallout).foregroundStyle(ORIX.Palette.textSecondary)52                }53            }54            .frame(maxWidth: .infinity, alignment: .leading)55            .orixCard()56            .accessibilityElement(children: .combine)57            .accessibilityIdentifier("battery.notOffered")58        }59        .navigationTitle("Battery & Performance")60        .toolbar { ToolbarItem(placement: .topBarTrailing) { Button { model.refresh() } label: { Label("Refresh", systemImage: "arrow.clockwise") } } }61        .accessibilityIdentifier("battery")62    }6364    private func gauge(_ device: DeviceSnapshot) -> some View {65        ZStack {66            Circle().stroke(ORIX.Palette.surfaceRaised, lineWidth: 12)67            if let level = device.batteryLevel.value {68                Circle().trim(from: 0, to: level)69                    .stroke(level <= 0.2 ? ORIX.Palette.warning : ORIX.Palette.success, style: StrokeStyle(lineWidth: 12, lineCap: .round))70                    .rotationEffect(.degrees(-90))71            }72            Image(systemName: device.batteryState.value == .charging ? "bolt.fill" : (device.batteryLevel.isMeasured ? "battery.75percent" : "minus"))73                .font(.title2).foregroundStyle(ORIX.Palette.textSecondary)74        }75        .frame(width: 92, height: 92)76        .accessibilityHidden(true)77    }7879    @ViewBuilder private func section(_ title: LocalizedStringKey, _ items: [Advice], empty: LocalizedStringKey?) -> some View {80        if !items.isEmpty || empty != nil {81            ORIXSectionHeader(title)82            if items.isEmpty, let empty {83                Text(empty).font(.orixCallout).foregroundStyle(ORIX.Palette.textSecondary).frame(maxWidth: .infinity, alignment: .leading).orixCard()84            }85            ForEach(items) { item in86                VStack(alignment: .leading, spacing: ORIX.Space.xs) {87                    Text(verbatim: item.title).font(.orixHeadline).foregroundStyle(ORIX.Palette.textPrimary)88                    Text(verbatim: item.detail).font(.orixCallout).foregroundStyle(ORIX.Palette.textSecondary)89                    ForEach(item.basis) { basis in90                        Text(verbatim: "\(basis.label): \(basis.value)").font(.orixMono).foregroundStyle(ORIX.Palette.textTertiary)91                    }92                }93                .frame(maxWidth: .infinity, alignment: .leading)94                .orixCard()95                .accessibilityElement(children: .combine)96                .accessibilityIdentifier("advice." + item.id)97            }98        }99    }100}101
Files main
Menu

Theme