8000 Fix battery popover layout by EClinick · Pull Request #13 · EClinick/juice · GitHub
[go: up one dir, main page]

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
14 changes: 12 additions & 2 deletions Sources/Juice/Stats/StatsRangeVisibility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@ import Foundation
/// copy can change without resetting a user's choices.
enum StatsRangeVisibility {
static let storageKey = "stats.visibleRanges.v1"
static let defaultStorageValue = storageValue(for: EnergyRange.allCases)
/// Keep the compact popover readable on a fresh install. Three Days remains
/// available through Customize Tabs, but the default avoids squeezing five
/// segments into the menu-bar panel.
static let defaultRanges: [EnergyRange] = [
.session,
.today,
.week,
.allTime,
]
static let defaultStorageValue = storageValue(for: defaultRanges)
static let allStorageValue = storageValue(for: EnergyRange.allCases)

static func visibleRanges(from storageValue: String) -> [EnergyRange] {
let storedKeys = Set(storageValue.split(separator: ",").map(String.init))
let ranges = EnergyRange.allCases.filter { storedKeys.contains($0.storageKey) }
return ranges.isEmpty ? EnergyRange.allCases : ranges
return ranges.isEmpty ? defaultRanges : ranges
}

static func storageValue(for ranges: some Collection<EnergyRange>) -> String {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Juice/Stats/StatsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ struct StatsView: View {
.foregroundStyle(.secondary)
Spacer()
Button("Show All") {
rangeVisibilityStorage = StatsRangeVisibility.defaultStorageValue
rangeVisibilityStorage = StatsRangeVisibility.allStorageValue
}
.buttonStyle(.plain)
.font(.caption)
Expand Down
16 changes: 12 additions & 4 deletions Tests/JuiceTests/StatsRangeVisibilityTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@ import Testing

@Suite("Stats range visibility")
struct StatsRangeVisibilityTests {
@Test("all tabs are visible by default")
@Test("compact tabs are visible by default")
func defaultVisibility() {
#expect(
StatsRangeVisibility.visibleRanges(
from: StatsRangeVisibility.defaultStorageValue
) == [.session, .today, .week, .allTime])
}

@Test("show-all storage includes every tab")
func allVisibility() {
#expect(
StatsRangeVisibility.visibleRanges(
from: StatsRangeVisibility.allStorageValue
) == EnergyRange.allCases)
}

@Test("hidden tabs stay hidden in their original order")
func customVisibility() {
var storage = StatsRangeVisibility.defaultStorageValue
var storage = StatsRangeVisibility.allStorageValue
storage = StatsRangeVisibility.updating(
.threeDays,
isVisible: false,
Expand All @@ -39,11 +47,11 @@ struct StatsRangeVisibilityTests {
#expect(StatsRangeVisibility.visibleRanges(from: updated) == [.today])
}

@Test("invalid stored settings recover to all tabs")
@Test("invalid stored settings recover to compact defaults")
func invalidStorageFallsBack() {
#expect(
StatsRangeVisibility.visibleRanges(from: "removed-range")
== EnergyRange.allCases)
== [.session, .today, .week, .allTime])
}

@Test("a hidden preferred tab falls back to the first visible tab")
Expand Down
0