watch: fix snapshot sync and polish favourites UX

This commit is contained in:
FuelBoard Contributor
2026-08-31 07:48:20 +01:00
parent 486b8cff2e
commit 7a84364238
3 changed files with 132 additions and 76 deletions
+5 -3
View File
@@ -46,9 +46,11 @@ final class WatchSyncManager: NSObject, WCSessionDelegate {
if let favourites = try? JSONEncoder().encode(FuelStore.loadFavourites()) { if let favourites = try? JSONEncoder().encode(FuelStore.loadFavourites()) {
context[FuelStore.favouritesKey] = favourites context[FuelStore.favouritesKey] = favourites
} }
if let stations = try? JSONEncoder().encode(FuelStore.loadStations()) { // Do NOT send the full station dump to watchOS via WatchConnectivity.
context[FuelStore.stationsKey] = stations // The UK dataset is megabytes large and exceeds application-context
} // payload limits, which prevents any snapshot from arriving. The
// favourites payload already carries station snapshots + prices, so the
// watch can render favourites without the full list.
context[FuelStore.fuelKey] = FuelStore.loadSelectedFuel().rawValue context[FuelStore.fuelKey] = FuelStore.loadSelectedFuel().rawValue
context[FuelStore.distanceUnitKey] = FuelStore.loadDistanceUnit().rawValue context[FuelStore.distanceUnitKey] = FuelStore.loadDistanceUnit().rawValue
+82 -25
View File
@@ -1,5 +1,6 @@
import SwiftUI import SwiftUI
import Foundation import Foundation
import WatchKit
private enum WatchFuelType: String, CaseIterable, Identifiable, Codable { private enum WatchFuelType: String, CaseIterable, Identifiable, Codable {
case e10 case e10
@@ -102,6 +103,8 @@ private struct WatchFavouriteRow: Identifiable {
let priceText: String let priceText: String
let distanceText: String? let distanceText: String?
let isCheapest: Bool let isCheapest: Bool
let latitude: Double
let longitude: Double
} }
private enum WatchRefreshState { private enum WatchRefreshState {
@@ -283,14 +286,30 @@ struct ContentView: View {
@State private var distanceUnit: WatchDistanceUnit = WatchFuelCache.loadDistanceUnit() @State private var distanceUnit: WatchDistanceUnit = WatchFuelCache.loadDistanceUnit()
@State private var priceStyle: WatchPriceStyle = WatchFuelCache.loadPriceStyle() @State private var priceStyle: WatchPriceStyle = WatchFuelCache.loadPriceStyle()
private var displayedFuels: [WatchFuelType] {
let fuels = Set(favourites.map(\.fuel))
let sorted = fuels.sorted { $0.sortRank < $1.sortRank }
return sorted.isEmpty ? [fuel] : sorted
}
private var hasAnyFavourites: Bool {
!favourites.isEmpty
}
var body: some View { var body: some View {
Group {
if hasAnyFavourites {
TabView(selection: $fuel) { TabView(selection: $fuel) {
ForEach(WatchFuelType.allCases) { fuelCase in ForEach(displayedFuels) { fuelCase in
fuelPage(for: fuelCase) fuelPage(for: fuelCase)
.tag(fuelCase) .tag(fuelCase)
} }
} }
.tabViewStyle(.verticalPage) .tabViewStyle(.verticalPage)
} else {
emptyStateView
}
}
.onAppear { .onAppear {
reloadFromCache() reloadFromCache()
WatchSyncManager.shared.requestSnapshot() WatchSyncManager.shared.requestSnapshot()
@@ -301,6 +320,7 @@ struct ContentView: View {
.onChange(of: scenePhase) { _, newPhase in .onChange(of: scenePhase) { _, newPhase in
if newPhase == .active { if newPhase == .active {
reloadFromCache() reloadFromCache()
WatchSyncManager.shared.requestSnapshot()
} }
} }
} }
@@ -317,8 +337,6 @@ struct ContentView: View {
if let cheapest { if let cheapest {
cheapestCard(for: fuelCase, favourite: cheapest) cheapestCard(for: fuelCase, favourite: cheapest)
} else {
emptyCheapestCard(for: fuelCase)
} }
statusRow statusRow
@@ -340,17 +358,11 @@ struct ContentView: View {
Text("Favourites") Text("Favourites")
.font(.headline) .font(.headline)
if rows.isEmpty {
Text("No favourites for \(fuelCase.displayName.lowercased()) yet.")
.font(.caption)
.foregroundStyle(.secondary)
} else {
ForEach(rows) { row in ForEach(rows) { row in
favouriteRow(row, fuel: fuelCase) favouriteRow(row, fuel: fuelCase)
} }
} }
} }
}
.padding(.horizontal, 10) .padding(.horizontal, 10)
.padding(.vertical, 12) .padding(.vertical, 12)
} }
@@ -372,7 +384,9 @@ struct ContentView: View {
name: favourite.station.name, name: favourite.station.name,
priceText: priceText, priceText: priceText,
distanceText: distanceText, distanceText: distanceText,
isCheapest: favourite.id == cheapest?.id isCheapest: favourite.id == cheapest?.id,
latitude: favourite.station.lat,
longitude: favourite.station.lng
) )
} }
} }
@@ -392,7 +406,52 @@ struct ContentView: View {
} }
} }
private var emptyStateView: some View {
ScrollView {
VStack(alignment: .leading, spacing: 12) {
HStack(alignment: .center, spacing: 8) {
Image(systemName: "fuelpump.fill")
.foregroundStyle(Color(red: 48/255.0, green: 209/255.0, blue: 88/255.0))
Text("FuelBoard")
.font(.headline)
Spacer()
}
statusRow
Button {
runCheckNow()
} label: {
HStack {
Image(systemName: refreshState.icon)
Text("Check now")
.fontWeight(.semibold)
}
.frame(maxWidth: .infinity)
}
.buttonStyle(.borderedProminent)
.tint(Color(red: 48/255.0, green: 209/255.0, blue: 88/255.0))
VStack(alignment: .leading, spacing: 6) {
Text("No favourites yet")
.font(.headline)
Text("Add favourites on iPhone and theyll appear here.")
.font(.caption)
.foregroundStyle(.secondary)
}
.padding(10)
.frame(maxWidth: .infinity, alignment: .leading)
.background(Color.white.opacity(0.06), in: RoundedRectangle(cornerRadius: 14, style: .continuous))
}
.padding(.horizontal, 10)
.padding(.vertical, 12)
}
}
private func cheapestCard(for fuel: WatchFuelType, favourite: WatchFavouriteEntry) -> some View { private func cheapestCard(for fuel: WatchFuelType, favourite: WatchFavouriteEntry) -> some View {
Button {
openMaps(latitude: favourite.station.lat, longitude: favourite.station.lng)
} label: {
VStack(alignment: .leading, spacing: 6) { VStack(alignment: .leading, spacing: 6) {
Text("Cheapest favourite") Text("Cheapest favourite")
.font(.caption2) .font(.caption2)
@@ -414,22 +473,9 @@ struct ContentView: View {
} }
.padding(10) .padding(10)
.frame(maxWidth: .infinity, alignment: .leading) .frame(maxWidth: .infinity, alignment: .leading)
.background(fuel.color.opacity(0.15), in: RoundedRectangle(cornerRadius: 14, style: .continuous)) .contentShape(RoundedRectangle(cornerRadius: 14, style: .continuous))
} }
.buttonStyle(.plain)
private func emptyCheapestCard(for fuel: WatchFuelType) -> some View {
VStack(alignment: .leading, spacing: 6) {
Text("Cheapest favourite")
.font(.caption2)
.foregroundStyle(.secondary)
Text("No favourite saved")
.font(.headline)
Text("Add favourites on iPhone to see them here.")
.font(.caption)
.foregroundStyle(.secondary)
}
.padding(10)
.frame(maxWidth: .infinity, alignment: .leading)
.background(fuel.color.opacity(0.15), in: RoundedRectangle(cornerRadius: 14, style: .continuous)) .background(fuel.color.opacity(0.15), in: RoundedRectangle(cornerRadius: 14, style: .continuous))
} }
@@ -441,6 +487,9 @@ struct ContentView: View {
} }
private func favouriteRow(_ favourite: WatchFavouriteRow, fuel: WatchFuelType) -> some View { private func favouriteRow(_ favourite: WatchFavouriteRow, fuel: WatchFuelType) -> some View {
Button {
openMaps(latitude: favourite.latitude, longitude: favourite.longitude)
} label: {
HStack(spacing: 8) { HStack(spacing: 8) {
VStack(alignment: .leading, spacing: 2) { VStack(alignment: .leading, spacing: 2) {
HStack(spacing: 4) { HStack(spacing: 4) {
@@ -469,6 +518,9 @@ struct ContentView: View {
} }
.padding(.horizontal, 10) .padding(.horizontal, 10)
.padding(.vertical, 8) .padding(.vertical, 8)
.contentShape(RoundedRectangle(cornerRadius: 12, style: .continuous))
}
.buttonStyle(.plain)
.background(Color.white.opacity(0.06), in: RoundedRectangle(cornerRadius: 12, style: .continuous)) .background(Color.white.opacity(0.06), in: RoundedRectangle(cornerRadius: 12, style: .continuous))
} }
@@ -526,6 +578,11 @@ struct ContentView: View {
formatter.unitsStyle = .short formatter.unitsStyle = .short
return formatter.localizedString(for: date, relativeTo: Date()) return formatter.localizedString(for: date, relativeTo: Date())
} }
private func openMaps(latitude: Double, longitude: Double) {
guard let url = URL(string: "maps://?daddr=\(latitude),\(longitude)&t=d") else { return }
WKExtension.shared().openSystemURL(url)
}
} }
#Preview { #Preview {
@@ -59,9 +59,6 @@ final class WatchSyncManager: NSObject, WCSessionDelegate {
if let favourites = payload["fuelboard.favourites"] as? Data { if let favourites = payload["fuelboard.favourites"] as? Data {
defaults.set(favourites, forKey: "fuelboard.favourites") defaults.set(favourites, forKey: "fuelboard.favourites")
} }
if let stations = payload["fuelboard.stations"] as? Data {
defaults.set(stations, forKey: "fuelboard.stations")
}
if let fuel = payload["fuelboard.selectedFuel"] as? String { if let fuel = payload["fuelboard.selectedFuel"] as? String {
defaults.set(fuel, forKey: "fuelboard.selectedFuel") defaults.set(fuel, forKey: "fuelboard.selectedFuel")
} }