watch: fix snapshot sync and polish favourites UX
This commit is contained in:
@@ -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
|
||||||
|
|||||||
@@ -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 {
|
||||||
TabView(selection: $fuel) {
|
Group {
|
||||||
ForEach(WatchFuelType.allCases) { fuelCase in
|
if hasAnyFavourites {
|
||||||
fuelPage(for: fuelCase)
|
TabView(selection: $fuel) {
|
||||||
.tag(fuelCase)
|
ForEach(displayedFuels) { fuelCase in
|
||||||
|
fuelPage(for: fuelCase)
|
||||||
|
.tag(fuelCase)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.tabViewStyle(.verticalPage)
|
||||||
|
} else {
|
||||||
|
emptyStateView
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.tabViewStyle(.verticalPage)
|
|
||||||
.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,14 +358,8 @@ struct ContentView: View {
|
|||||||
Text("Favourites")
|
Text("Favourites")
|
||||||
.font(.headline)
|
.font(.headline)
|
||||||
|
|
||||||
if rows.isEmpty {
|
ForEach(rows) { row in
|
||||||
Text("No favourites for \(fuelCase.displayName.lowercased()) yet.")
|
favouriteRow(row, fuel: fuelCase)
|
||||||
.font(.caption)
|
|
||||||
.foregroundStyle(.secondary)
|
|
||||||
} else {
|
|
||||||
ForEach(rows) { row in
|
|
||||||
favouriteRow(row, fuel: fuelCase)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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,44 +406,76 @@ struct ContentView: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func cheapestCard(for fuel: WatchFuelType, favourite: WatchFavouriteEntry) -> some View {
|
private var emptyStateView: some View {
|
||||||
VStack(alignment: .leading, spacing: 6) {
|
ScrollView {
|
||||||
Text("Cheapest favourite")
|
VStack(alignment: .leading, spacing: 12) {
|
||||||
.font(.caption2)
|
HStack(alignment: .center, spacing: 8) {
|
||||||
.foregroundStyle(.secondary)
|
Image(systemName: "fuelpump.fill")
|
||||||
Text(favourite.station.name)
|
.foregroundStyle(Color(red: 48/255.0, green: 209/255.0, blue: 88/255.0))
|
||||||
.font(.headline)
|
Text("FuelBoard")
|
||||||
.lineLimit(2)
|
.font(.headline)
|
||||||
HStack(alignment: .firstTextBaseline) {
|
Spacer()
|
||||||
Text(favourite.station.prices[fuel].map { WatchFuelCache.priceText($0, style: priceStyle) } ?? "—")
|
}
|
||||||
.font(.title3)
|
|
||||||
.fontWeight(.bold)
|
statusRow
|
||||||
Spacer()
|
|
||||||
if let location = lastLocation {
|
Button {
|
||||||
Text(distanceUnit.format(km: favourite.station.distanceKM(to: location)))
|
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 they’ll appear here.")
|
||||||
.font(.caption)
|
.font(.caption)
|
||||||
.foregroundStyle(.secondary)
|
.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)
|
||||||
}
|
}
|
||||||
.padding(10)
|
|
||||||
.frame(maxWidth: .infinity, alignment: .leading)
|
|
||||||
.background(fuel.color.opacity(0.15), in: RoundedRectangle(cornerRadius: 14, style: .continuous))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private func emptyCheapestCard(for fuel: WatchFuelType) -> some View {
|
private func cheapestCard(for fuel: WatchFuelType, favourite: WatchFavouriteEntry) -> some View {
|
||||||
VStack(alignment: .leading, spacing: 6) {
|
Button {
|
||||||
Text("Cheapest favourite")
|
openMaps(latitude: favourite.station.lat, longitude: favourite.station.lng)
|
||||||
.font(.caption2)
|
} label: {
|
||||||
.foregroundStyle(.secondary)
|
VStack(alignment: .leading, spacing: 6) {
|
||||||
Text("No favourite saved")
|
Text("Cheapest favourite")
|
||||||
.font(.headline)
|
.font(.caption2)
|
||||||
Text("Add favourites on iPhone to see them here.")
|
.foregroundStyle(.secondary)
|
||||||
.font(.caption)
|
Text(favourite.station.name)
|
||||||
.foregroundStyle(.secondary)
|
.font(.headline)
|
||||||
|
.lineLimit(2)
|
||||||
|
HStack(alignment: .firstTextBaseline) {
|
||||||
|
Text(favourite.station.prices[fuel].map { WatchFuelCache.priceText($0, style: priceStyle) } ?? "—")
|
||||||
|
.font(.title3)
|
||||||
|
.fontWeight(.bold)
|
||||||
|
Spacer()
|
||||||
|
if let location = lastLocation {
|
||||||
|
Text(distanceUnit.format(km: favourite.station.distanceKM(to: location)))
|
||||||
|
.font(.caption)
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.padding(10)
|
||||||
|
.frame(maxWidth: .infinity, alignment: .leading)
|
||||||
|
.contentShape(RoundedRectangle(cornerRadius: 14, style: .continuous))
|
||||||
}
|
}
|
||||||
.padding(10)
|
.buttonStyle(.plain)
|
||||||
.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,34 +487,40 @@ struct ContentView: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func favouriteRow(_ favourite: WatchFavouriteRow, fuel: WatchFuelType) -> some View {
|
private func favouriteRow(_ favourite: WatchFavouriteRow, fuel: WatchFuelType) -> some View {
|
||||||
HStack(spacing: 8) {
|
Button {
|
||||||
VStack(alignment: .leading, spacing: 2) {
|
openMaps(latitude: favourite.latitude, longitude: favourite.longitude)
|
||||||
HStack(spacing: 4) {
|
} label: {
|
||||||
Text(favourite.name)
|
HStack(spacing: 8) {
|
||||||
.font(.caption)
|
VStack(alignment: .leading, spacing: 2) {
|
||||||
.fontWeight(favourite.isCheapest ? .semibold : .regular)
|
HStack(spacing: 4) {
|
||||||
.lineLimit(1)
|
Text(favourite.name)
|
||||||
if favourite.isCheapest {
|
.font(.caption)
|
||||||
Text("TOP")
|
.fontWeight(favourite.isCheapest ? .semibold : .regular)
|
||||||
.font(.system(size: 9, weight: .bold))
|
.lineLimit(1)
|
||||||
.padding(.horizontal, 4)
|
if favourite.isCheapest {
|
||||||
.padding(.vertical, 2)
|
Text("TOP")
|
||||||
.background(fuel.color.opacity(0.18), in: Capsule())
|
.font(.system(size: 9, weight: .bold))
|
||||||
|
.padding(.horizontal, 4)
|
||||||
|
.padding(.vertical, 2)
|
||||||
|
.background(fuel.color.opacity(0.18), in: Capsule())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let distanceText = favourite.distanceText {
|
||||||
|
Text(distanceText)
|
||||||
|
.font(.caption2)
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let distanceText = favourite.distanceText {
|
Spacer()
|
||||||
Text(distanceText)
|
Text(favourite.priceText)
|
||||||
.font(.caption2)
|
.font(.callout.monospacedDigit())
|
||||||
.foregroundStyle(.secondary)
|
.fontWeight(.semibold)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Spacer()
|
.padding(.horizontal, 10)
|
||||||
Text(favourite.priceText)
|
.padding(.vertical, 8)
|
||||||
.font(.callout.monospacedDigit())
|
.contentShape(RoundedRectangle(cornerRadius: 12, style: .continuous))
|
||||||
.fontWeight(.semibold)
|
|
||||||
}
|
}
|
||||||
.padding(.horizontal, 10)
|
.buttonStyle(.plain)
|
||||||
.padding(.vertical, 8)
|
|
||||||
.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")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user