Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / apple / FindFamily / FindFamilyiOS / UI / HistoryBar.swift
HistoryBar.swift114 lines · 4.3 KiBRaw
| 1 | import SwiftUI |
| 2 | |
| 3 | /// Vertical slider + ± buttons + date picker for browsing a user's historical locations. |
| 4 | struct HistoryBar: View { |
| 5 | let userid: Int64 |
| 6 | @Binding var isShowingPresent: Bool |
| 7 | @Binding var historicalPosition: Coord? |
| 8 | |
| 9 | @State private var pickedDate: Date = Date() |
| 10 | @State private var pickedSecondsOfDay: Float = Float(Self.currentSecondsOfDay()) |
| 11 | @State private var showDatePicker = false |
| 12 | |
| 13 | static func currentSecondsOfDay() -> Int { |
| 14 | let comps = Calendar.current.dateComponents([.hour, .minute, .second], from: Date()) |
| 15 | return (comps.hour ?? 0) * 3600 + (comps.minute ?? 0) * 60 + (comps.second ?? 0) |
| 16 | } |
| 17 | |
| 18 | private var simulatedTimestamp: Date { |
| 19 | let startOfDay = Calendar.current.startOfDay(for: pickedDate) |
| 20 | return startOfDay.addingTimeInterval(TimeInterval(pickedSecondsOfDay)) |
| 21 | } |
| 22 | |
| 23 | private var maxSeconds: Float { |
| 24 | let today = Calendar.current.isDateInToday(pickedDate) |
| 25 | return today ? Float(Self.currentSecondsOfDay()) : 86399.9 |
| 26 | } |
| 27 | |
| 28 | private func bump(_ delta: Float) { |
| 29 | let v = min(maxSeconds, max(0, pickedSecondsOfDay + delta)) |
| 30 | pickedSecondsOfDay = v |
| 31 | } |
| 32 | |
| 33 | private func updateHistoricalPosition() { |
| 34 | let locs = Database.shared.locationsFor(userid: userid) |
| 35 | guard !locs.isEmpty else { return } |
| 36 | let target = simulatedTimestamp |
| 37 | let closest = locs.min { abs($0.timestamp.timeIntervalSince(target)) < abs($1.timestamp.timeIntervalSince(target)) } |
| 38 | historicalPosition = closest?.coord |
| 39 | } |
| 40 | |
| 41 | var body: some View { |
| 42 | VStack(spacing: 4) { |
| 43 | if !isShowingPresent { |
| 44 | // Vertical slider (rotated horizontal slider). |
| 45 | GeometryReader { geo in |
| 46 | Slider(value: $pickedSecondsOfDay, in: 0...86400) |
| 47 | .rotationEffect(.degrees(-90)) |
| 48 | .frame(width: geo.size.height, height: geo.size.width) |
| 49 | .position(x: geo.size.width / 2, y: geo.size.height / 2) |
| 50 | .onChange(of: pickedSecondsOfDay) { _, _ in updateHistoricalPosition() } |
| 51 | } |
| 52 | .frame(maxHeight: .infinity) |
| 53 | |
| 54 | HStack { |
| 55 | Button(Strings.historyRewindLarge) { bump(-5 * 60) } |
| 56 | Spacer() |
| 57 | Button(Strings.historyForwardLarge) { bump(5 * 60) } |
| 58 | } |
| 59 | HStack { |
| 60 | Button(Strings.historyRewindMedium) { bump(-60) } |
| 61 | Spacer() |
| 62 | Button(Strings.historyForwardMedium) { bump(60) } |
| 63 | } |
| 64 | HStack { |
| 65 | Button(Strings.historyRewindSmall) { bump(-10) } |
| 66 | Spacer() |
| 67 | Button(Strings.historyForwardSmall) { bump(10) } |
| 68 | } |
| 69 | .font(.caption) |
| 70 | |
| 71 | Text(TimeFormatting.amPmTimeSeconds.string(from: simulatedTimestamp)) |
| 72 | .font(.caption2) |
| 73 | |
| 74 | Button(TimeFormatting.dayMonth.string(from: pickedDate)) { |
| 75 | showDatePicker = true |
| 76 | } |
| 77 | .font(.caption2) |
| 78 | } |
| 79 | |
| 80 | Button(isShowingPresent ? Strings.historyButton : Strings.hideButton) { |
| 81 | isShowingPresent.toggle() |
| 82 | if !isShowingPresent { |
| 83 | pickedDate = Date() |
| 84 | pickedSecondsOfDay = Float(Self.currentSecondsOfDay()) |
| 85 | updateHistoricalPosition() |
| 86 | } else { |
| 87 | historicalPosition = nil |
| 88 | } |
| 89 | } |
| 90 | .font(.caption2) |
| 91 | .frame(maxWidth: .infinity) |
| 92 | } |
| 93 | .padding(6) |
| 94 | .background(RoundedRectangle(cornerRadius: 10).fill(.ultraThinMaterial)) |
| 95 | .sheet(isPresented: $showDatePicker) { |
| 96 | VStack { |
| 97 | DatePicker( |
| 98 | "Pick Date", |
| 99 | selection: $pickedDate, |
| 100 | in: ...Date(), |
| 101 | displayedComponents: .date |
| 102 | ) |
| 103 | .datePickerStyle(.graphical) |
| 104 | Button(Strings.okButton) { |
| 105 | showDatePicker = false |
| 106 | updateHistoricalPosition() |
| 107 | } |
| 108 | .buttonStyle(.borderedProminent) |
| 109 | .padding() |
| 110 | } |
| 111 | .padding() |
| 112 | } |
| 113 | } |
| 114 | } |