Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Resources/pl.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,7 @@
"not done" = "do zrobienia";
"stopped" = "zatrzymano";
"round %lld of %lld" = "runda %lld z %lld";
"+5 min" = "+5 min";
"five more minutes" = "jeszcze pięć minut";
"skip break" = "pomiń przerwę";
"wait for me after breaks" = "czekaj na mnie po przerwie";
23 changes: 23 additions & 0 deletions Sources/FocusCore/SessionClock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public struct SessionClock: Sendable {
private var studiedBefore: TimeInterval = 0
/// Where a session ended with stop was, so its summary can take it back.
private var stopped: (phase: Phase, remaining: TimeInterval)?
/// After a break the next study round waits, paused, for the user instead of starting by itself.
public var waitAfterBreak = false

public init() {}

Expand Down Expand Up @@ -118,6 +120,23 @@ public struct SessionClock: Sendable {
self.stopped = nil
}

/// Ends the break now and starts the next study round.
public mutating func skipBreak(at now: Date) {
tick(at: now)
guard case .rest(let round) = phase else { return }
pausedRemaining = nil
enter(.study(round: round + 1), at: now)
}

/// Makes the running phase longer, paused or not.
public mutating func extend(by seconds: TimeInterval, at now: Date) {
tick(at: now)
guard phase.isRunning else { return }
phaseDuration += seconds
if let pausedRemaining { self.pausedRemaining = pausedRemaining + seconds }
else { phaseEnd = phaseEnd?.addingTimeInterval(seconds) }
}

public mutating func pause(at now: Date) {
tick(at: now) // catch up on phases that ended while asleep
guard phase.isRunning, !isPaused else { return }
Expand Down Expand Up @@ -153,6 +172,10 @@ public struct SessionClock: Sendable {
enter(.rest(round: round), at: time)
case .rest(let round):
enter(.study(round: round + 1), at: time)
if waitAfterBreak { // the round stays at its full length until resumed
pausedRemaining = phaseDuration
phaseEnd = nil
}
case .idle, .done:
phaseEnd = nil
}
Expand Down
20 changes: 20 additions & 0 deletions Sources/FocusGuard/AppModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ final class AppModel: ObservableObject {
}
}
@Published var soundsOn: Bool { didSet { save() } }
/// After a break the next round waits for a click or space instead of starting by itself.
@Published var waitForMe: Bool { didSet { save(); clock.waitAfterBreak = waitForMe } }
/// Draws the whole panel 25 % larger, for reading the hand-written text more easily.
@Published var largePanel: Bool { didSet { save() } }
@Published var launchAtLogin: Bool {
Expand Down Expand Up @@ -121,11 +123,13 @@ final class AppModel: ObservableObject {
showPreview = d.object(forKey: "showPreview") as? Bool ?? false
useCamera = d.object(forKey: "useCamera") as? Bool ?? true
soundsOn = d.object(forKey: "soundsOn") as? Bool ?? true
waitForMe = d.object(forKey: "waitForMe") as? Bool ?? false
largePanel = d.object(forKey: "largePanel") as? Bool ?? false
seenHint = d.object(forKey: "seenHint") as? Bool ?? false
cameraExplained = d.object(forKey: "cameraExplained") as? Bool ?? false
todos = d.data(forKey: "todos").flatMap { try? JSONDecoder().decode([Todo].self, from: $0) } ?? []
launchAtLogin = [.enabled, .requiresApproval].contains(SMAppService.mainApp.status)
clock.waitAfterBreak = waitForMe

spotify.objectWillChange.sink { [weak self] in self?.objectWillChange.send() }.store(in: &bag)
cameraService.onSignals = { [weak self] signals, t in
Expand Down Expand Up @@ -250,6 +254,21 @@ final class AppModel: ObservableObject {
phaseChanged()
}

/// "skip" on the break card: the next study round starts now.
func skipBreak() {
tick()
clock.skipBreak(at: monoNow)
pausedBySleep = false
phaseStart = Date()
phaseChanged()
}

/// "+5 min" on the study and break cards.
func extend() {
tick()
clock.extend(by: 5 * 60, at: monoNow)
}

private func pauseBecauseAway() {
guard clock.phase.isRunning, !clock.isPaused else { return }
togglePause()
Expand Down Expand Up @@ -438,6 +457,7 @@ final class AppModel: ObservableObject {
d.set(showPreview, forKey: "showPreview")
d.set(useCamera, forKey: "useCamera")
d.set(soundsOn, forKey: "soundsOn")
d.set(waitForMe, forKey: "waitForMe")
d.set(largePanel, forKey: "largePanel")
d.set(seenHint, forKey: "seenHint")
d.set(cameraExplained, forKey: "cameraExplained")
Expand Down
33 changes: 30 additions & 3 deletions Sources/FocusGuard/Card.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ private struct CardContent: View {
}
}
RuledRow(height: 22, seed: 2) {
if model.showsFocus { Sparkline(values: model.history.suffix(40) + [model.focus]).frame(height: 16) }
HStack(spacing: 0) { // the time row is full with the focus figure, so the extra minutes go here
if model.showsFocus { Sparkline(values: model.history.suffix(40) + [model.focus]).frame(height: 16) }
Spacer(minLength: 8)
MoreTime()
}
}
RuledRow(height: 18, seed: 3) {
if let nudge = model.nudgeText {
Expand All @@ -134,11 +138,23 @@ private struct CardContent: View {
PhaseHeader(title: model.clock.isLongBreak ? "long break \(round) of \(model.clock.config.rounds)"
: "break \(round) of \(model.clock.config.rounds)")
}
RuledRow(height: 34, seed: 1) { Num(text: model.remainingText, color: Ink.ballpoint.opacity(model.clock.isPaused ? 0.5 : 1)) }
RuledRow(height: 34, seed: 1) {
HStack(alignment: .lastTextBaseline, spacing: 0) {
Num(text: model.remainingText, color: Ink.ballpoint.opacity(model.clock.isPaused ? 0.5 : 1))
Spacer(minLength: 4)
MoreTime()
}
}
RuledRow(height: 20, seed: 2) {
Text(model.clock.isPaused ? pausedLine : String(localized: "stretch, drink some water")).font(Ink.hand(12))
}
RuledRow(height: 18, seed: 3) { Text("camera is off").font(Ink.hand(11)).opacity(0.75) }
RuledRow(height: 18, seed: 3) {
HStack(spacing: 0) {
Text("camera is off").font(Ink.hand(11)).opacity(0.75)
Spacer(minLength: 4)
WordLink(title: "skip break", size: 12, action: model.skipBreak).padding(.trailing, 4)
}
}
case "why":
RuledRow(height: 18) { Text("about the camera").font(Ink.hand(12)) }
Text("FocusGuard looks at you through the camera only while you study. Nothing is saved or sent anywhere.")
Expand Down Expand Up @@ -229,6 +245,17 @@ private struct PhaseHeader: View {
}
}

/// "+5 min" right after the time: the running phase gets five more minutes.
private struct MoreTime: View {
@EnvironmentObject var model: AppModel

var body: some View {
WordLink(title: "+5 min", size: 12, action: model.extend)
.accessibilityLabel(Text("five more minutes"))
.padding(.trailing, 4)
}
}

private struct StatusLine: View {
@EnvironmentObject var model: AppModel

Expand Down
1 change: 1 addition & 0 deletions Sources/FocusGuard/Sticky.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ struct SettingsNote: View {
BoxChoice(title: "camera preview", checked: model.showPreview, size: 12) { model.showPreview.toggle() }
}
BoxChoice(title: "sounds", checked: model.soundsOn, size: 12) { model.soundsOn.toggle() }
BoxChoice(title: "wait for me after breaks", checked: model.waitForMe, size: 12) { model.waitForMe.toggle() }
BoxChoice(title: "bigger panel", checked: model.largePanel, size: 12) { model.largePanel.toggle() }
BoxChoice(title: "launch at login", checked: model.launchAtLogin, size: 12) { model.launchAtLogin.toggle() }
if model.loginApproval {
Expand Down
48 changes: 48 additions & 0 deletions Sources/focus-check/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,51 @@ func undoStopCarriesOn() {
check(clock.phase == .idle && clock.studied(at: t0 + 40 * 60) == 0)
}

func skipBreakStartsNextRound() {
var clock = SessionClock()
let t0 = Date(timeIntervalSinceReferenceDate: 0)
clock.start(SessionConfig(studyMinutes: 25, breakMinutes: 5, rounds: 4), at: t0)
clock.skipBreak(at: t0 + 60) // only breaks can be skipped
check(clock.phase == .study(round: 1))
clock.tick(at: t0 + 26 * 60)
clock.pause(at: t0 + 26 * 60)
clock.skipBreak(at: t0 + 27 * 60)
check(clock.phase == .study(round: 2) && !clock.isPaused)
check(clock.remaining(at: t0 + 27 * 60) == 25 * 60)
check(clock.studied(at: t0 + 30 * 60) == 28 * 60)
}

func extendAddsFiveMinutes() {
var clock = SessionClock()
let t0 = Date(timeIntervalSinceReferenceDate: 0)
clock.start(SessionConfig(studyMinutes: 25, breakMinutes: 5, rounds: 4), at: t0)
clock.extend(by: 5 * 60, at: t0 + 20 * 60)
check(clock.remaining(at: t0 + 20 * 60) == 10 * 60)
clock.tick(at: t0 + 26 * 60)
check(clock.phase == .study(round: 1))
clock.pause(at: t0 + 26 * 60)
clock.extend(by: 5 * 60, at: t0 + 40 * 60) // paused: stays paused, with 5 more minutes
check(clock.isPaused && clock.remaining(at: t0 + 50 * 60) == 9 * 60)
clock.resume(at: t0 + 50 * 60)
clock.tick(at: t0 + 59 * 60)
check(clock.phase == .rest(round: 1) && clock.studied(at: t0 + 59 * 60) == 35 * 60)
clock.extend(by: 5 * 60, at: t0 + 60 * 60)
check(clock.remaining(at: t0 + 60 * 60) == 9 * 60 && !clock.isLongBreak)
}

func waitAfterBreakHoldsNextRound() {
var clock = SessionClock()
clock.waitAfterBreak = true
let t0 = Date(timeIntervalSinceReferenceDate: 0)
clock.start(SessionConfig(studyMinutes: 25, breakMinutes: 5, rounds: 4), at: t0)
clock.tick(at: t0 + 10 * 3600) // asleep for hours: still waits at the start of round 2
check(clock.phase == .study(round: 2) && clock.isPaused)
check(clock.remaining(at: t0 + 10 * 3600) == 25 * 60 && clock.studied(at: t0 + 10 * 3600) == 25 * 60)
clock.resume(at: t0 + 11 * 3600)
clock.tick(at: t0 + 11 * 3600 + 25 * 60)
check(clock.phase == .rest(round: 2))
}

func lcdDigitsAndText() {
check(SevenSegment.mask(8) == 0b1111111)
check(SevenSegment.mask(1) == 0b0000110)
Expand Down Expand Up @@ -399,6 +444,9 @@ let checks: [(String, () -> Void)] = [
("fullSessionStudiedExcludesBreaks", fullSessionStudiedExcludesBreaks),
("stopRightAfterStartGoesIdle", stopRightAfterStartGoesIdle),
("undoStopCarriesOn", undoStopCarriesOn),
("skipBreakStartsNextRound", skipBreakStartsNextRound),
("extendAddsFiveMinutes", extendAddsFiveMinutes),
("waitAfterBreakHoldsNextRound", waitAfterBreakHoldsNextRound),
]
for (name, body) in checks {
current = name
Expand Down
Loading