Sources/OrgstarMobile/ClockScreens.swift
129 lines · 5333 bytes
1#if os(iOS)
2import OrgApp
3import OrgCore
4import SwiftUI
5
6/// The running clock over the tab bar: the entry and time so far, and clocking out.
7struct ClockAccessory: View {
8 let running: ClockModel.Running
9 let perform: (String) -> Void
10
11 var body: some View {
12 Menu {
13 Button("Clock Out", systemImage: "stop.circle") { perform("app.clock.out") }
14 Button("Cancel Clock", systemImage: "xmark.circle") { perform("app.clock.cancel") }
15 Button("Go to Clocked Entry", systemImage: "arrow.right.circle") { perform("app.clock.goto") }
16 Button("Clock Report", systemImage: "list.bullet.rectangle") { perform("app.clock.report") }
17 } label: {
18 HStack {
19 Image(systemName: "stopwatch")
20 Text(running.heading).lineLimit(1)
21 Spacer()
22 TimelineView(.periodic(from: .now, by: 30)) { context in
23 Text(Self.elapsed(running, now: context.date)).monospacedDigit()
24 }
25 }
26 .padding(.horizontal)
27 }
28 .accessibilityLabel("Clocked in on \(running.heading)")
29 }
30
31 /// Elapsed time of the running clock, `H:MM`.
32 static func elapsed(_ running: ClockModel.Running, now: Date) -> String {
33 let minutes = max(0, Int(now.timeIntervalSince(running.startedAt) / 60))
34 return String(format: "%d:%02d", minutes / 60, minutes % 60)
35 }
36}
37
38extension View {
39 /// The running clock over the tab bar, while there is one.
40 @ViewBuilder
41 func clockAccessory(_ running: ClockModel.Running?, perform: @escaping (String) -> Void) -> some View {
42 if #available(iOS 26.1, *) {
43 tabViewBottomAccessory(isEnabled: running != nil) {
44 if let running { ClockAccessory(running: running, perform: perform) }
45 }
46 } else {
47 safeAreaInset(edge: .bottom) {
48 if let running { ClockAccessory(running: running, perform: perform).padding(.vertical, 8).background(.bar) }
49 }
50 }
51 }
52}
53
54/// Time per day and heading, as `tr/generate-report` writes it, for chosen files and dates.
55struct ClockReportScreen: View {
56 let workspace: WorkspaceModel
57 @State private var start = Calendar.current.date(byAdding: .day, value: -6, to: Date()) ?? Date()
58 @State private var end = Date()
59 @State private var limit = false
60 @State private var selected: Set<String> = []
61 @State private var report = ""
62 @Environment(\.dismiss) private var dismiss
63
64 private var candidates: [FileEntry] { workspace.files.filter { $0.kind == .org } }
65
66 var body: some View {
67 NavigationStack {
68 Form {
69 Section("Files") {
70 ForEach(candidates) { file in
71 Toggle(file.relativePath, isOn: Binding {
72 selected.contains(file.path)
73 } set: { on in
74 if on { selected.insert(file.path) } else { selected.remove(file.path) }
75 })
76 }
77 }
78 Section {
79 Toggle("Limit dates", isOn: $limit)
80 if limit {
81 DatePicker("From", selection: $start, displayedComponents: .date)
82 DatePicker("To", selection: $end, displayedComponents: .date)
83 }
84 }
85 Section("Report") {
86 ScrollView(.horizontal) {
87 Text(report.isEmpty ? "Choose files with clocked time." : report)
88 .font(.footnote.monospaced())
89 .textSelection(.enabled)
90 .fixedSize()
91 }
92 }
93 }
94 .navigationTitle("Clock Report")
95 .navigationBarTitleDisplayMode(.inline)
96 .toolbar {
97 ToolbarItem(placement: .cancellationAction) { Button("Close") { dismiss() } }
98 ToolbarItem(placement: .primaryAction) {
99 ShareLink(item: report, preview: SharePreview("Time report")).disabled(report.isEmpty)
100 }
101 }
102 }
103 .onAppear {
104 if selected.isEmpty {
105 selected = Set(candidates.filter { (try? String(contentsOfFile: $0.path, encoding: .utf8))?.contains("CLOCK: [") ?? false }.map(\.path))
106 }
107 compute()
108 }
109 .onChange(of: selected) { compute() }
110 .onChange(of: start) { compute() }
111 .onChange(of: end) { compute() }
112 .onChange(of: limit) { compute() }
113 }
114
115 private func day(_ date: Date) -> String {
116 let c = Calendar.current.dateComponents([.year, .month, .day], from: date)
117 return String(format: "%04d-%02d-%02d", c.year!, c.month!, c.day!)
118 }
119
120 private func compute() {
121 var entries: [ClockReport.Entry] = []
122 for path in selected.sorted() {
123 guard let text = try? String(contentsOfFile: path, encoding: .utf8) else { continue }
124 entries += ClockReport.entries(text, settings: workspace.settings.org)
125 }
126 report = selected.isEmpty ? "" : ClockReport.timeReport(entries, start: limit ? day(start) : nil, end: limit ? day(end) : nil)
127 }
128}
129#endif