- Test-alert buttons moved out of the Alerts section into a Debug section. - Debug section only renders when the debug flag is on; completely hidden otherwise (no user-facing toggle). - Hidden toggle: tap Settings → About → Version five times to flip the flag (keychain + app-group storage, survives reinstall). - About section added with version/build number. - 35 tests pass.
259 lines
11 KiB
Swift
259 lines
11 KiB
Swift
import SwiftUI
|
|
import StoreKit
|
|
import UserNotifications
|
|
import WidgetKit
|
|
|
|
/// Settings tab — distance units, onboarding replay, a tip jar, and an About
|
|
/// section. Test-notification buttons live in a Debug section that is hidden
|
|
/// unless the developer debug flag is on (tap the About → Version row five
|
|
/// times to toggle it — no user-facing switch). The first test button uses
|
|
/// REAL data (the actual cheapest station for the monitored fuel within the
|
|
/// radius, exactly like a live alert) via `onTestAlert`; the second fires
|
|
/// with no criteria at all.
|
|
struct SettingsView: View {
|
|
@Binding var distanceUnit: DistanceUnit
|
|
/// The fuel + radius currently configured for alerts (mirrors the Alerts
|
|
/// tab) so the test notification matches what real alerts will say.
|
|
var alertsFuel: FuelType = .e10
|
|
var alertsRadiusKM: Double = 3.0
|
|
/// Result line from the last real-data test alert (what it picked).
|
|
var testAlertResult: String?
|
|
/// Drives the real-data test — ContentView routes this to the live
|
|
/// ProximityMonitor so the test uses its actual stations/fuel/radius.
|
|
var onTestAlert: () -> Void = {}
|
|
/// Drives the plain test (no criteria) — routed to the live monitor so it
|
|
/// can carry a real station's name, price and coordinates.
|
|
var onPlainTestAlert: () -> Void = {}
|
|
var onShowOnboarding: () -> Void = {}
|
|
|
|
@StateObject private var tipStore = TipStore()
|
|
@State private var showTipAlert = false
|
|
@State private var tipAlertMessage = ""
|
|
@State private var testAlertMessage: String?
|
|
/// Hidden developer flag — the Debug section only appears when on. Toggled
|
|
/// by tapping the About → Version row five times (NOT a user-facing switch).
|
|
@State private var debugMode: Bool = FuelStore.loadDebugMode()
|
|
@State private var versionTapCount = 0
|
|
@State private var lastVersionTap = Date.distantPast
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
List {
|
|
Section {
|
|
Picker("Distance", selection: $distanceUnit) {
|
|
ForEach(DistanceUnit.allCases) { unit in
|
|
Text(unit.displayName).tag(unit)
|
|
}
|
|
}
|
|
.pickerStyle(.segmented)
|
|
.onChange(of: distanceUnit) { _, newValue in
|
|
FuelStore.saveDistanceUnit(newValue)
|
|
WidgetCenter.shared.reloadAllTimelines()
|
|
}
|
|
} header: {
|
|
Text("Units")
|
|
} footer: {
|
|
Text("Distances and search radii across the app, widget and alerts are shown in this unit.")
|
|
}
|
|
|
|
Section {
|
|
Button {
|
|
onShowOnboarding()
|
|
} label: {
|
|
Label("Show introduction", systemImage: "sparkles")
|
|
}
|
|
} footer: {
|
|
Text("Replay the welcome screen, including the location and notification permission prompts.")
|
|
}
|
|
|
|
if debugMode {
|
|
Section {
|
|
Button {
|
|
sendTestAlert()
|
|
} label: {
|
|
Label("Test alert notification (real data)", systemImage: "bell.badge.fill")
|
|
}
|
|
if let testAlertMessage {
|
|
Text(testAlertMessage)
|
|
.font(.footnote)
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
if let testAlertResult {
|
|
Label(testAlertResult, systemImage: "checkmark.circle.fill")
|
|
.font(.footnote)
|
|
.foregroundStyle(.green)
|
|
}
|
|
Button {
|
|
onPlainTestAlert()
|
|
} label: {
|
|
Label("Send plain test notification", systemImage: "bell.slash.fill")
|
|
}
|
|
} header: {
|
|
Text("Debug")
|
|
} footer: {
|
|
Text("The first button applies the real criteria — cheapest \(alertsFuel.displayName.lowercased()) station within \(distanceUnit.format(alertsRadiusKM)) of you — and sends the actual station's name, price and coordinates. The second fires with no criteria at all (nearest seller), still carrying a real station, just to verify a notification appears and tapping it opens directions.")
|
|
}
|
|
}
|
|
|
|
Section {
|
|
Button {
|
|
Task { await tipStore.purchase() }
|
|
} label: {
|
|
HStack {
|
|
Label("Leave a tip", systemImage: "heart.fill")
|
|
.foregroundStyle(.pink)
|
|
Spacer()
|
|
Text(tipStore.displayPrice)
|
|
.foregroundStyle(.secondary)
|
|
.monospacedDigit()
|
|
}
|
|
}
|
|
.disabled(tipStore.purchaseInProgress)
|
|
} header: {
|
|
Text("Support FuelBoard")
|
|
} footer: {
|
|
Text("A small tip helps keep the data relay and app development going. Thank you!")
|
|
}
|
|
|
|
Section {
|
|
HStack {
|
|
Text("Version")
|
|
Spacer()
|
|
Text(appVersion)
|
|
.foregroundStyle(.secondary)
|
|
.monospacedDigit()
|
|
.contentShape(Rectangle())
|
|
.onTapGesture { handleVersionTap() }
|
|
}
|
|
} header: {
|
|
Text("About")
|
|
} footer: {
|
|
Text("FuelBoard \(appVersion)")
|
|
}
|
|
|
|
if let message = tipStore.message {
|
|
Section {
|
|
Text(message)
|
|
.font(.footnote)
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
}
|
|
}
|
|
.navigationTitle("Settings")
|
|
.onAppear {
|
|
Task { await tipStore.load() }
|
|
}
|
|
}
|
|
}
|
|
|
|
private var appVersion: String {
|
|
let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "1.0"
|
|
let build = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String ?? "1"
|
|
return "\(version) (\(build))"
|
|
}
|
|
|
|
/// Hidden debug-mode toggle: five taps on the Version row flips the flag.
|
|
/// Nothing in the UI advertises this — the Debug section simply appears or
|
|
/// disappears. Deliberately NOT a visible switch so end users never see it.
|
|
private func handleVersionTap() {
|
|
let now = Date()
|
|
if now.timeIntervalSince(lastVersionTap) > 0.6 {
|
|
versionTapCount = 0
|
|
}
|
|
lastVersionTap = now
|
|
versionTapCount += 1
|
|
if versionTapCount >= 5 {
|
|
versionTapCount = 0
|
|
debugMode.toggle()
|
|
FuelStore.saveDebugMode(debugMode)
|
|
}
|
|
}
|
|
|
|
private func sendTestAlert() {
|
|
UNUserNotificationCenter.current().getNotificationSettings { settings in
|
|
Task { @MainActor in
|
|
switch settings.authorizationStatus {
|
|
case .authorized, .provisional, .ephemeral:
|
|
onTestAlert()
|
|
case .denied:
|
|
testAlertMessage = "Notifications are turned off for FuelBoard. Enable them in Settings → Notifications → FuelBoard, then try again."
|
|
default:
|
|
// First time — ask, then fire if granted.
|
|
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, _ in
|
|
Task { @MainActor in
|
|
if granted {
|
|
onTestAlert()
|
|
} else {
|
|
testAlertMessage = "Notifications weren't allowed, so no test alert was sent."
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Loads the £4.99 tip product and drives its purchase.
|
|
@MainActor
|
|
final class TipStore: ObservableObject {
|
|
/// Product ID for the £4.99 tip (App Store Connect — consumable).
|
|
static let productID = "com.apt.fuelboard.tip499"
|
|
|
|
@Published private(set) var product: Product?
|
|
@Published private(set) var purchaseInProgress = false
|
|
@Published private(set) var message: String?
|
|
|
|
var displayPrice: String {
|
|
product?.displayPrice ?? "£4.99"
|
|
}
|
|
|
|
func load() async {
|
|
// Refreshes product state on every visit so a newly-approved product
|
|
// (or a restored transaction) is picked up.
|
|
do {
|
|
let products = try await Product.products(for: [Self.productID])
|
|
product = products.first
|
|
} catch {
|
|
// No product yet (sideloaded build) — the button still shows the
|
|
// intended price and reports the purchase attempt gracefully.
|
|
product = nil
|
|
}
|
|
}
|
|
|
|
func purchase() async {
|
|
guard !purchaseInProgress else { return }
|
|
purchaseInProgress = true
|
|
defer { purchaseInProgress = false }
|
|
|
|
// If the product hasn't loaded (e.g. not configured in App Store
|
|
// Connect yet), still allow the attempt so the user sees a clear
|
|
// outcome rather than a dead button.
|
|
guard let product else {
|
|
message = "The tip isn't available in this build yet — check back after an App Store release."
|
|
return
|
|
}
|
|
|
|
do {
|
|
let result = try await product.purchase()
|
|
switch result {
|
|
case .success(let verification):
|
|
switch verification {
|
|
case .verified:
|
|
message = "Thank you! Your tip has been received. ⛽"
|
|
case .unverified:
|
|
message = "The purchase couldn't be verified. Please try again."
|
|
}
|
|
case .userCancelled:
|
|
message = nil // silent — the user just closed the sheet
|
|
case .pending:
|
|
message = "Your tip is pending approval. It'll finish automatically."
|
|
@unknown default:
|
|
message = nil
|
|
}
|
|
} catch {
|
|
message = "The tip couldn't be completed: \(error.localizedDescription)"
|
|
}
|
|
}
|
|
}
|