Graphley / app · graphley.ai / Apps / iPhone / PadApp.swift
| 1 | import SwiftUI |
| 2 | import AnalyticsKit |
| 3 | import AnalyticsUI |
| 4 | |
| 5 | // iPad: the Mac's layout for reading. Our own sidebar rows on the left, one screen on the right, |
| 6 | // the same Tide cards, tiles and charts as the Mac (design/DESIGN.md). Everything here is |
| 7 | // read-only; the Mac refreshes the numbers and shares them through iCloud. |
| 8 | |
| 9 | enum PadItem: Hashable { case overview, apps, dashboard(String), site(String), settings } |
| 10 | |
| 11 | struct PadRootView: View { |
| 12 | @Environment(PhoneModel.self) private var model |
| 13 | @State private var columns: NavigationSplitViewVisibility = .all |
| 14 | |
| 15 | var body: some View { |
| 16 | NavigationSplitView(columnVisibility: $columns) { |
| 17 | PadSidebar() |
| 18 | .navigationSplitViewColumnWidth(min: 260, ideal: 290, max: 340) |
| 19 | .toolbar(.hidden, for: .navigationBar) |
| 20 | } detail: { |
| 21 | PadDetail() |
| 22 | } |
| 23 | .navigationSplitViewStyle(.balanced) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | // MARK: Sidebar |
| 28 | |
| 29 | struct PadSidebar: View { |
| 30 | @Environment(PhoneModel.self) private var model |
| 31 | |
| 32 | var body: some View { |
| 33 | VStack(alignment: .leading, spacing: 0) { |
| 34 | HStack(spacing: 10) { |
| 35 | TideMark(size: 30) |
| 36 | Text("Graphley").font(Tide.display(22)).tracking(-0.3).foregroundStyle(Tide.ink) |
| 37 | } |
| 38 | .padding(.horizontal, 22).padding(.top, 20).padding(.bottom, 6) |
| 39 | .accessibilityElement(children: .combine) |
| 40 | |
| 41 | ScrollView { |
| 42 | VStack(alignment: .leading, spacing: 2) { |
| 43 | PadSectionLabel("Portfolio") |
| 44 | PadSidebarRow("Overview", icon: "chart.xyaxis.line", item: .overview) |
| 45 | PadSidebarRow("Apps", icon: "arrow.down.app", item: .apps) |
| 46 | ForEach(model.dashboards) { d in |
| 47 | PadSidebarRow(d.name, icon: "rectangle.3.group", item: .dashboard(d.id)) |
| 48 | } |
| 49 | if !model.sites.isEmpty { |
| 50 | PadSectionLabel("Sites") |
| 51 | // Same order as the Mac's sidebar, which the user sets by dragging there. |
| 52 | ForEach(model.sites) { site in |
| 53 | PadSidebarRow(site.name, dot: Tide.color(for: site), |
| 54 | count: model.total(.users, [site.id], days: model.range), item: .site(site.id)) |
| 55 | } |
| 56 | } |
| 57 | PadSectionLabel("Graphley") |
| 58 | PadSidebarRow("Settings", icon: "gearshape", item: .settings) |
| 59 | } |
| 60 | .padding(.horizontal, 12).padding(.bottom, 12) |
| 61 | } |
| 62 | PadSyncStatus() |
| 63 | } |
| 64 | .background(Tide.ground) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | struct PadSectionLabel: View { |
| 69 | let text: String |
| 70 | init(_ text: String) { self.text = text } |
| 71 | var body: some View { |
| 72 | Text(text.uppercased()).font(Tide.sectionLabel).tracking(0.8).foregroundStyle(Tide.muted) |
| 73 | .padding(.leading, 12).padding(.top, 18).padding(.bottom, 6) |
| 74 | .accessibilityAddTraits(.isHeader) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | struct PadSidebarRow: View { |
| 79 | let title: String, icon: String?, dot: Color?, count: Double?, item: PadItem |
| 80 | @Environment(PhoneModel.self) private var model |
| 81 | @Environment(\.colorScheme) private var scheme |
| 82 | @State private var hover = false |
| 83 | |
| 84 | init(_ title: String, icon: String? = nil, dot: Color? = nil, count: Double? = nil, item: PadItem) { |
| 85 | self.title = title; self.icon = icon; self.dot = dot; self.count = count; self.item = item |
| 86 | } |
| 87 | |
| 88 | var body: some View { |
| 89 | let selected = model.padSelection == item |
| 90 | Button { model.padSelection = item } label: { |
| 91 | HStack(spacing: 10) { |
| 92 | if let icon { |
| 93 | Image(systemName: icon).font(.system(size: 16, weight: .medium)).frame(width: 22) |
| 94 | .foregroundStyle(selected ? Tide.accentInk : Tide.muted) |
| 95 | } else if let dot { |
| 96 | // On the selected row the dot turns light, so a teal site stays visible on the teal fill. |
| 97 | Circle().fill(selected ? Tide.accentInk : dot).frame(width: 9, height: 9).frame(width: 22) |
| 98 | } |
| 99 | Text(title).font(Tide.sans(16, .medium)).lineLimit(1).truncationMode(.tail) |
| 100 | .foregroundStyle(selected ? Tide.accentInk : Tide.ink) |
| 101 | Spacer(minLength: 4) |
| 102 | if let count { |
| 103 | Text(Tide.number(count)).font(Tide.figure(13)) |
| 104 | .foregroundStyle(selected ? Tide.accentInk.opacity(0.75) : Tide.muted) |
| 105 | } |
| 106 | } |
| 107 | .padding(.horizontal, 12) |
| 108 | .frame(height: 44) |
| 109 | .background(selected ? Tide.fill : hover ? (scheme == .dark ? Tide.surface2 : Tide.sunk) : .clear, in: RoundedRectangle(cornerRadius: 10)) |
| 110 | .contentShape(RoundedRectangle(cornerRadius: 10)) |
| 111 | } |
| 112 | .buttonStyle(.plain) |
| 113 | .onHover { hover = $0 } |
| 114 | .accessibilityAddTraits(selected ? .isSelected : []) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /// Bottom of the sidebar: when the Mac last shared numbers, with a button to check iCloud again. |
| 119 | struct PadSyncStatus: View { |
| 120 | @Environment(PhoneModel.self) private var model |
| 121 | var body: some View { |
| 122 | HStack(spacing: 10) { |
| 123 | Circle().fill(dotColor).frame(width: 7, height: 7) |
| 124 | VStack(alignment: .leading, spacing: 2) { |
| 125 | Text(headline).font(Tide.sans(13, .medium)).foregroundStyle(Tide.ink).lineLimit(1) |
| 126 | Text(detail).font(Tide.caption).foregroundStyle(Tide.muted).lineLimit(2) |
| 127 | } |
| 128 | Spacer(minLength: 4) |
| 129 | Button { Task { await model.load() } } label: { |
| 130 | if model.loading { ProgressView() } else { Label("Check iCloud", systemImage: "arrow.clockwise") } |
| 131 | } |
| 132 | .buttonStyle(.tideIcon) |
| 133 | .disabled(model.loading) |
| 134 | .accessibilityLabel("Check iCloud for new numbers") |
| 135 | } |
| 136 | .padding(.leading, 18).padding(.trailing, 10).padding(.vertical, 12) |
| 137 | .overlay(alignment: .top) { Rectangle().fill(Tide.line).frame(height: 1) } |
| 138 | } |
| 139 | |
| 140 | var stale: Bool { model.snapshot.map { Date.now.timeIntervalSince($0.refreshedAt) > 36 * 3600 } ?? false } |
| 141 | var dotColor: Color { model.error != nil ? Tide.down : stale || model.snapshot == nil ? Tide.attention : Tide.up } |
| 142 | var headline: String { model.snapshot == nil ? "Waiting for your Mac" : "Synced from your Mac" } |
| 143 | var detail: String { |
| 144 | if model.snapshot == nil, let e = model.cloud.error { return e } |
| 145 | guard let s = model.snapshot else { return "Through iCloud" } |
| 146 | return "Updated \(s.refreshedAt.formatted(.relative(presentation: .named)))" |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // MARK: Detail |
| 151 | |
| 152 | struct PadDetail: View { |
| 153 | @Environment(PhoneModel.self) private var model |
| 154 | |
| 155 | var body: some View { |
| 156 | NavigationStack { |
| 157 | TourScrollView("pad") { |
| 158 | VStack(alignment: .leading, spacing: 16) { |
| 159 | if model.snapshot != nil, let e = model.error { Banner(text: e, kind: .error) } |
| 160 | if let s = model.snapshot, Date.now.timeIntervalSince(s.refreshedAt) > 36 * 3600 { |
| 161 | Banner(text: "These numbers are from \(s.refreshedAt.formatted(.relative(presentation: .named))). Open Graphley on your Mac to refresh.", kind: .attention) |
| 162 | } |
| 163 | if model.snapshot == nil && model.padSelection != .settings { PadWaiting() } else { screen } |
| 164 | } |
| 165 | .padding(.horizontal, 32).padding(.top, 8).padding(.bottom, 32) |
| 166 | .frame(maxWidth: 1180, alignment: .leading) |
| 167 | .frame(maxWidth: .infinity, alignment: .leading) |
| 168 | } |
| 169 | .id(model.padSelection) |
| 170 | .refreshable { await model.load() } |
| 171 | .background(Tide.surface) |
| 172 | .toolbarBackground(.hidden, for: .navigationBar) |
| 173 | .navigationBarTitleDisplayMode(.inline) |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | @ViewBuilder var screen: some View { |
| 178 | switch model.padSelection { |
| 179 | case .overview: PadOverview() |
| 180 | case .apps: PadAppStore() |
| 181 | case .dashboard(let id): PadDashboard(dashboardID: id) |
| 182 | case .site(let id): PadSite(siteID: id) |
| 183 | case .settings: SyncSettingsContent() |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | struct PadWaiting: View { |
| 189 | @Environment(PhoneModel.self) private var model |
| 190 | var body: some View { |
| 191 | ConnectMacGuide { model.padSelection = .settings } |
| 192 | .frame(maxWidth: .infinity).padding(.top, 48) |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /// The 7 / 30 / 90 day pill, bound to the iPad's range. |
| 197 | struct PadRange: View { |
| 198 | @Environment(PhoneModel.self) private var model |
| 199 | var body: some View { |
| 200 | @Bindable var model = model |
| 201 | RangeSelector(selection: $model.range) |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /// Lays out side by side when there is room, stacked otherwise (portrait, split view). |
| 206 | struct PadPair<First: View, Second: View>: View { |
| 207 | let wide: Bool, secondWidth: CGFloat |
| 208 | let first: First, second: Second |
| 209 | init(wide: Bool, secondWidth: CGFloat = 340, @ViewBuilder first: () -> First, @ViewBuilder second: () -> Second) { |
| 210 | self.wide = wide; self.secondWidth = secondWidth; self.first = first(); self.second = second() |
| 211 | } |
| 212 | var body: some View { |
| 213 | if wide { |
| 214 | HStack(alignment: .top, spacing: 16) { first.layoutPriority(1); second.frame(width: secondWidth) } |
| 215 | } else { |
| 216 | VStack(alignment: .leading, spacing: 16) { first; second } |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /// Measures the screen's width so cards can sit side by side in landscape. |
| 222 | struct WidthReader: ViewModifier { |
| 223 | @Binding var width: CGFloat |
| 224 | func body(content: Content) -> some View { |
| 225 | content.onGeometryChange(for: CGFloat.self) { $0.size.width } action: { width = $0 } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // MARK: Overview |
| 230 | |
| 231 | struct PadOverview: View { |
| 232 | @Environment(PhoneModel.self) private var model |
| 233 | @State private var width: CGFloat = 900 |
| 234 | |
| 235 | var body: some View { |
| 236 | let n = model.range |
| 237 | VStack(alignment: .leading, spacing: 16) { |
| 238 | ScreenHeading("Overview", subtitle: subtitle) { PadRange() } |
| 239 | LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: 12), count: width > 760 ? tiles.count : 2), spacing: 12) { |
| 240 | ForEach(tiles, id: \.0) { m, label in |
| 241 | KPITile(label, value: model.total(m, days: n), previous: model.total(m, days: n, offset: n), footer: "vs previous \(n) days") |
| 242 | } |
| 243 | } |
| 244 | TideCard("Daily visitors by site", subtitle: "Stacked. Touch a day to read it.") { |
| 245 | StackedChart(days: model.days, series: stackedSeries).frame(height: 340) |
| 246 | } |
| 247 | PadPair(wide: width > 860 && !model.apps.isEmpty) { |
| 248 | TideCard("Every site", subtitle: "Last \(n) days vs the \(n) before. Tap a site to open it.") { PadSiteTable() } |
| 249 | } second: { |
| 250 | if !model.apps.isEmpty { |
| 251 | TideCard("Every app", subtitle: "App Store and Mac website downloads, last \(n) days. Tap an app to open Apps.") { |
| 252 | Button { model.padSelection = .apps } label: { |
| 253 | BarList(model.apps.map { ($0.shortName, model.appTotal($0, days: n)) }.sorted { $0.1 > $1.1 }) |
| 254 | } |
| 255 | .buttonStyle(.plain) |
| 256 | .accessibilityHint("Opens Apps") |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | .modifier(WidthReader(width: $width)) |
| 262 | } |
| 263 | |
| 264 | var subtitle: String { |
| 265 | let sites = model.sites.count, apps = model.apps.count |
| 266 | var s = "\(sites) \(sites == 1 ? "site" : "sites") and \(apps) \(apps == 1 ? "app" : "apps")" |
| 267 | if let last = model.points.map(\.day).max(), let date = PadDay.date(last) { |
| 268 | s += " · data through \(date.formatted(.dateTime.month(.abbreviated).day()))" |
| 269 | } |
| 270 | return s |
| 271 | } |
| 272 | |
| 273 | var tiles: [(Metric, String)] { |
| 274 | var t: [(Metric, String)] = [(.users, "Visitors"), (.sessions, "Sessions"), (.pageViews, "Page views")] |
| 275 | if !model.apps.isEmpty { t.append((.appDownloads, "App Store downloads")) } |
| 276 | return t |
| 277 | } |
| 278 | |
| 279 | /// Top five sites plus one "Other sites" band, so colors never repeat. |
| 280 | var stackedSeries: [StackedChart.Series] { |
| 281 | let n = model.range |
| 282 | let ranked = model.sites.sorted { model.total(.users, [$0.id], days: n) > model.total(.users, [$1.id], days: n) } |
| 283 | var series = ranked.prefix(5).map { StackedChart.Series(id: $0.id, name: $0.name, color: Tide.color(for: $0), values: model.series(.users, [$0.id], days: n)) } |
| 284 | let rest = ranked.dropFirst(5).map(\.id) |
| 285 | if !rest.isEmpty { |
| 286 | series.append(.init(id: "other", name: "Other sites", color: Tide.other, values: model.series(.users, Set(rest), days: n))) |
| 287 | } |
| 288 | return series |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | struct PadSiteTable: View { |
| 293 | @Environment(PhoneModel.self) private var model |
| 294 | var body: some View { |
| 295 | let n = model.range |
| 296 | VStack(spacing: 0) { |
| 297 | HStack(spacing: 12) { |
| 298 | Text("Site").frame(maxWidth: .infinity, alignment: .leading) |
| 299 | Text("Trend").frame(width: 80, alignment: .leading) |
| 300 | Text("Visitors").frame(width: 72, alignment: .trailing) |
| 301 | Text("Change").frame(width: 60, alignment: .trailing) |
| 302 | Text("Page views").frame(width: 84, alignment: .trailing) |
| 303 | } |
| 304 | .font(Tide.caption).foregroundStyle(Tide.muted) |
| 305 | .padding(.horizontal, 10).padding(.bottom, 8) |
| 306 | RowDivider() |
| 307 | ForEach(model.sites.sorted { model.total(.users, [$0.id], days: n) > model.total(.users, [$1.id], days: n) }) { s in |
| 308 | PadSiteRow(site: s) |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | struct PadSiteRow: View { |
| 315 | let site: Site |
| 316 | @Environment(PhoneModel.self) private var model |
| 317 | @State private var hover = false |
| 318 | var body: some View { |
| 319 | let n = model.range |
| 320 | let cur = model.total(.users, [site.id], days: n) |
| 321 | Button { model.padSelection = .site(site.id) } label: { |
| 322 | HStack(spacing: 12) { |
| 323 | HStack(spacing: 9) { |
| 324 | Circle().fill(Tide.color(for: site)).frame(width: 9, height: 9) |
| 325 | Text(site.name).font(Tide.sans(15, .medium)).foregroundStyle(Tide.ink).lineLimit(1) |
| 326 | } |
| 327 | .frame(maxWidth: .infinity, alignment: .leading) |
| 328 | Sparkline(model.series(.users, [site.id], days: n), color: Tide.color(for: site)) |
| 329 | Text(Tide.number(cur)).font(Tide.figure(14)).foregroundStyle(Tide.ink).frame(width: 72, alignment: .trailing) |
| 330 | DeltaLabel(current: cur, previous: model.total(.users, [site.id], days: n, offset: n)).frame(width: 60, alignment: .trailing) |
| 331 | Text(Tide.number(model.total(.pageViews, [site.id], days: n))).font(Tide.figure(14)).foregroundStyle(Tide.ink2).frame(width: 84, alignment: .trailing) |
| 332 | } |
| 333 | .padding(.horizontal, 10).frame(height: 46) |
| 334 | .background(hover ? Tide.sunk : .clear, in: RoundedRectangle(cornerRadius: 8)) |
| 335 | .contentShape(Rectangle()) |
| 336 | } |
| 337 | .buttonStyle(.plain) |
| 338 | .onHover { hover = $0 } |
| 339 | .accessibilityLabel("\(site.name), \(Tide.number(cur)) visitors") |
| 340 | .accessibilityHint("Opens the site") |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | // MARK: Site |
| 345 | |
| 346 | struct PadSite: View { |
| 347 | @Environment(PhoneModel.self) private var model |
| 348 | @Environment(\.openURL) private var openURL |
| 349 | let siteID: String |
| 350 | @State private var metric: Metric = .users |
| 351 | @State private var width: CGFloat = 900 |
| 352 | |
| 353 | static let eventNames = ["mac_installer_download": "Mac downloads", "download_click_mac": "Mac downloads", |
| 354 | "app_store_click": "App Store clicks", "download_click_ios": "App Store clicks", |
| 355 | "source_click": "Source clicks", "source_download": "Source downloads", |
| 356 | "newsletter_click": "Newsletter clicks", "book_call": "Calls booked", "generate_lead": "Leads"] |
| 357 | |
| 358 | var body: some View { |
| 359 | if let site = model.sites.first(where: { $0.id == siteID }) { |
| 360 | let n = model.range |
| 361 | let pages = model.points.filter { $0.subject == siteID && ($0.breakdown?.hasPrefix("page:") ?? false) } |
| 362 | .sorted { $0.value > $1.value }.prefix(8).map { (String($0.breakdown!.dropFirst(5)), $0.value) } |
| 363 | VStack(alignment: .leading, spacing: 16) { |
| 364 | ScreenHeading(site.name, subtitle: site.url?.host() ?? "Google Analytics 4") { |
| 365 | HStack(spacing: 6) { |
| 366 | if let url = site.url { |
| 367 | Button { openURL(url) } label: { Label("Open \(url.host() ?? site.name)", systemImage: "arrow.up.right.square") } |
| 368 | .buttonStyle(.tideIcon) |
| 369 | .accessibilityLabel("Open \(url.host() ?? site.name)") |
| 370 | } |
| 371 | PadRange() |
| 372 | } |
| 373 | } |
| 374 | LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: 12), count: width > 760 ? 4 : 2), spacing: 12) { |
| 375 | ForEach([Metric.users, .newUsers, .sessions, .pageViews], id: \.self) { m in |
| 376 | Button { withAnimation(.easeOut(duration: 0.15)) { metric = m } } label: { |
| 377 | KPITile(m.title, value: model.total(m, [siteID], days: n), previous: model.total(m, [siteID], days: n, offset: n), |
| 378 | footer: "vs previous \(n) days", selected: metric == m) |
| 379 | } |
| 380 | .buttonStyle(.plain) |
| 381 | } |
| 382 | } |
| 383 | PadPair(wide: width > 860) { |
| 384 | TideCard("\(metric.title) per day", subtitle: "Tap a number above to switch.") { |
| 385 | let values = model.series(metric, [siteID], days: n) |
| 386 | TrendChart(days: model.days, values: values, color: Tide.color(for: site), label: metric.title).frame(height: 360) |
| 387 | if values.allSatisfy({ $0 == 0 }) { |
| 388 | Text("No \(metric.title.lowercased()) in the last \(n) days.").font(Tide.secondary).foregroundStyle(Tide.muted) |
| 389 | } |
| 390 | } |
| 391 | } second: { |
| 392 | TideCard("Top pages", subtitle: "Page views, last 30 days") { |
| 393 | if pages.isEmpty { Text("No page views recorded yet.").font(Tide.secondary).foregroundStyle(Tide.muted) } |
| 394 | else { BarList(Array(pages)) } |
| 395 | } |
| 396 | } |
| 397 | let events = keyEvents(n) |
| 398 | if !events.isEmpty { |
| 399 | TideCard("Key events", subtitle: "Last \(n) days") { BarList(events) } |
| 400 | } |
| 401 | } |
| 402 | .modifier(WidthReader(width: $width)) |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | func keyEvents(_ n: Int) -> [(String, Double)] { |
| 407 | let days = Set(DayKey.lastDays(n)) |
| 408 | var totals: [String: Double] = [:] |
| 409 | for p in model.points where p.subject == siteID && p.metric == .keyEvent && days.contains(p.day) { |
| 410 | guard let e = p.event else { continue } |
| 411 | totals[Self.eventNames[e] ?? e, default: 0] += p.value |
| 412 | } |
| 413 | return totals.filter { $0.value > 0 }.sorted { $0.value > $1.value }.map { ($0.key, $0.value) } |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | // MARK: App Store |
| 418 | |
| 419 | struct PadAppStore: View { |
| 420 | @Environment(PhoneModel.self) private var model |
| 421 | @State private var width: CGFloat = 900 |
| 422 | |
| 423 | static func platformName(_ raw: String) -> String { |
| 424 | let p = raw.hasPrefix("platform:") ? String(raw.dropFirst(9)) : raw |
| 425 | switch p.lowercased() { |
| 426 | case "iphone": return "iPhone" |
| 427 | case "ipad": return "iPad" |
| 428 | case "desktop", "mac": return "Mac" |
| 429 | case "apple vision", "vision", "apple vision pro": return "Apple Vision Pro" |
| 430 | case "apple tv", "appletv": return "Apple TV" |
| 431 | default: return p |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | var body: some View { |
| 436 | let n = model.range |
| 437 | let platforms = Set(model.points.filter { $0.metric == .appDownloads }.compactMap(\.breakdown)).sorted() |
| 438 | VStack(alignment: .leading, spacing: 16) { |
| 439 | ScreenHeading("Apps", subtitle: "App Store downloads from App Store Connect, and Mac installers from each app's website.") { PadRange() } |
| 440 | if model.apps.isEmpty { |
| 441 | TideCard { |
| 442 | EmptyState("No App Store numbers yet", message: "Add your App Store Connect key in Graphley on your Mac. Downloads appear here after the next refresh.") |
| 443 | } |
| 444 | } else { |
| 445 | LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: 12), count: min(model.apps.count, width > 760 ? 4 : 2)), spacing: 12) { |
| 446 | ForEach(model.apps) { app in |
| 447 | KPITile(app.shortName, value: model.appTotal(app, days: n), |
| 448 | previous: model.appTotal(app, days: n, offset: n), footer: "vs previous \(n) days") |
| 449 | } |
| 450 | } |
| 451 | TideCard("Downloads per day by platform", subtitle: "All apps") { |
| 452 | StackedChart(days: model.days, series: platforms.enumerated().map { i, p in |
| 453 | let byDay = Dictionary(grouping: model.points.filter { $0.metric == .appDownloads && $0.breakdown == p }, by: \.day).mapValues { $0.reduce(0) { $0 + $1.value } } |
| 454 | return .init(id: p, name: Self.platformName(p), color: Tide.series[i % Tide.series.count], values: model.days.map { byDay[$0] ?? 0 }) |
| 455 | }, bars: true).frame(height: 380) |
| 456 | } |
| 457 | TideCard("Every app", subtitle: "Last \(n) days") { |
| 458 | Grid(alignment: .leading, horizontalSpacing: 20, verticalSpacing: 12) { |
| 459 | GridRow { |
| 460 | Text("App"); Text("App Store").gridColumnAlignment(.trailing); Text("Mac (website)").gridColumnAlignment(.trailing) |
| 461 | Text("Redownloads").gridColumnAlignment(.trailing); Text("Rating").gridColumnAlignment(.trailing) |
| 462 | } |
| 463 | .font(Tide.caption).foregroundStyle(Tide.muted) |
| 464 | ForEach(model.apps) { app in |
| 465 | let store = app.hasAppStoreListing ? model.total(.appDownloads, [app.id], days: n) : nil |
| 466 | GridRow { |
| 467 | VStack(alignment: .leading, spacing: 2) { |
| 468 | Text(app.shortName).font(Tide.sans(15, .medium)).foregroundStyle(Tide.ink) |
| 469 | Text((app.platforms ?? []).joined(separator: " · ")).font(Tide.caption).foregroundStyle(Tide.muted).lineLimit(1) |
| 470 | } |
| 471 | .frame(maxWidth: .infinity, alignment: .leading) |
| 472 | Text(store.map(Tide.number) ?? "–").font(Tide.figure(14)).foregroundStyle(Tide.ink) |
| 473 | Text(website(app, n).map(Tide.number) ?? "–").font(Tide.figure(14)).foregroundStyle(Tide.ink) |
| 474 | Text(app.hasAppStoreListing ? Tide.number(model.total(.appRedownloads, [app.id], days: n)) : "–").font(Tide.figure(14)).foregroundStyle(Tide.ink2) |
| 475 | Text(rating(app.id)).font(Tide.figure(14)).foregroundStyle(Tide.ink2) |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | .modifier(WidthReader(width: $width)) |
| 483 | } |
| 484 | |
| 485 | /// Mac installer downloads from the app's website, or nil when it has no website download. |
| 486 | func website(_ app: StoreApp, _ n: Int) -> Double? { |
| 487 | model.points.websiteDownloads(app, days: model.days)?.reduce(0, +) |
| 488 | } |
| 489 | |
| 490 | func rating(_ id: String) -> String { |
| 491 | guard let r = model.points.filter({ $0.subject == id && $0.metric == .rating && $0.breakdown == nil }).max(by: { $0.day < $1.day }), r.value > 0 else { return "–" } |
| 492 | return r.value.formatted(.number.precision(.fractionLength(1))) + " ★" |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | // MARK: Dashboards |
| 497 | |
| 498 | struct PadDashboard: View { |
| 499 | @Environment(PhoneModel.self) private var model |
| 500 | let dashboardID: String |
| 501 | @State private var width: CGFloat = 900 |
| 502 | |
| 503 | var body: some View { |
| 504 | if let dash = model.dashboards.first(where: { $0.id == dashboardID }) { |
| 505 | VStack(alignment: .leading, spacing: 16) { |
| 506 | ScreenHeading(dash.name, subtitle: "Change widgets from your agent on the Mac.") |
| 507 | if dash.widgets.isEmpty { |
| 508 | TideCard { EmptyState("This dashboard is empty", message: "Ask your agent on the Mac to add widgets, for example “add a chart of my top site's visitors to my dashboard”.") } |
| 509 | } |
| 510 | DashboardBoard(data: model.portfolio, dashboard: dash, compact: width < 700) |
| 511 | } |
| 512 | .modifier(WidthReader(width: $width)) |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | enum PadDay { |
| 518 | nonisolated(unsafe) static let formatter: ISO8601DateFormatter = { let f = ISO8601DateFormatter(); f.formatOptions = [.withFullDate]; f.timeZone = .current; return f }() |
| 519 | static func date(_ s: String) -> Date? { formatter.date(from: s) } |
| 520 | } |