Skip to content

Commit 34f2457

Browse files
authored
Merge pull request #92 from austin-smith/menu-bar-ratio-consistency
Align macOS menu bar widget ratio with main app
2 parents b0202cb + 1d1dac8 commit 34f2457

4 files changed

Lines changed: 63 additions & 29 deletions

File tree

AGENTS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ From repo root (`/Users/austinsmith/Developer/Repos/BitDream`), run macOS tests
8282
test
8383
```
8484

85+
Testing guidance:
86+
87+
- Add tests when they protect meaningful user-visible behavior, cross-file integration, bug regressions, or non-trivial logic that is easy to break.
88+
- Do not add dedicated tests for every small helper extraction, straightforward computed property, or internal refactor unless the change introduces real behavioral risk.
89+
- Prefer a small number of high-signal tests over many narrow tests that only restate the implementation.
90+
8591
## Linting
8692

8793
From repo root (`/Users/austinsmith/Developer/Repos/BitDream`), run:

BitDream/Views/Shared/ContentView.swift

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,40 @@ func ensureStartupConnectionBehaviorApplied(store: TransmissionStore, modelConte
143143

144144
// MARK: - Shared Views
145145

146+
struct RatioSummarySnapshot: Equatable {
147+
let uploaded: Int64
148+
let downloaded: Int64
149+
150+
var ratio: Double {
151+
downloaded > 0 ? Double(uploaded) / Double(downloaded) : 0
152+
}
153+
}
154+
155+
@MainActor
156+
func makeRatioSummarySnapshot(store: TransmissionStore, displayMode: RatioDisplayMode) -> RatioSummarySnapshot {
157+
switch displayMode {
158+
case .cumulative:
159+
if let stats = store.sessionStats?.cumulativeStats {
160+
return RatioSummarySnapshot(
161+
uploaded: stats.uploadedBytes,
162+
downloaded: stats.downloadedBytes
163+
)
164+
}
165+
case .current:
166+
if let stats = store.sessionStats?.currentStats {
167+
return RatioSummarySnapshot(
168+
uploaded: stats.uploadedBytes,
169+
downloaded: stats.downloadedBytes
170+
)
171+
}
172+
}
173+
174+
return RatioSummarySnapshot(
175+
uploaded: store.torrents.reduce(0) { $0 + $1.uploadedEver },
176+
downloaded: store.torrents.reduce(0) { $0 + $1.downloadedEver }
177+
)
178+
}
179+
146180
// Stats header view used on both platforms
147181
struct StatsHeaderView: View {
148182
@ObservedObject var store: TransmissionStore
@@ -154,32 +188,18 @@ struct StatsHeaderView: View {
154188
RatioDisplayMode(rawValue: ratioDisplayModeRaw) ?? AppDefaults.ratioDisplayMode
155189
}
156190

157-
private var overallTotals: (uploaded: Int64, downloaded: Int64) {
158-
switch ratioDisplayMode {
159-
case .cumulative:
160-
if let stats = store.sessionStats?.cumulativeStats {
161-
return (uploaded: stats.uploadedBytes, downloaded: stats.downloadedBytes)
162-
}
163-
case .current:
164-
if let stats = store.sessionStats?.currentStats {
165-
return (uploaded: stats.uploadedBytes, downloaded: stats.downloadedBytes)
166-
}
167-
}
168-
let fallbackDownloaded = store.torrents.reduce(0) { $0 + $1.downloadedEver }
169-
let fallbackUploaded = store.torrents.reduce(0) { $0 + $1.uploadedEver }
170-
return (uploaded: fallbackUploaded, downloaded: fallbackDownloaded)
191+
private var ratioSummary: RatioSummarySnapshot {
192+
makeRatioSummarySnapshot(store: store, displayMode: ratioDisplayMode)
171193
}
172194

173195
private var overallRatio: Double {
174-
let totals = overallTotals
175-
return totals.downloaded > 0 ? Double(totals.uploaded) / Double(totals.downloaded) : 0.0
196+
ratioSummary.ratio
176197
}
177198

178199
private var ratioTooltip: String {
179-
let totals = overallTotals
180200
let mode = ratioDisplayMode == .cumulative ? "Total Ratio" : "Session Ratio"
181-
let uploaded = formatByteCount(totals.uploaded)
182-
let downloaded = formatByteCount(totals.downloaded)
201+
let uploaded = formatByteCount(ratioSummary.uploaded)
202+
let downloaded = formatByteCount(ratioSummary.downloaded)
183203
return "\(mode)\n----------\nUploaded: \(uploaded)\nDownloaded: \(downloaded)"
184204
}
185205

BitDream/Views/macOS/MenuBar/MenuBarTorrentSelectors.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,24 @@ func menuBarActiveTorrents(from store: TransmissionStore, sortMode: MenuBarSortM
2626

2727
@MainActor
2828
func menuBarSummary(from store: TransmissionStore) -> MenuBarTorrentSummary {
29-
menuBarSummary(from: store, activeTorrents: menuBarActiveTorrents(from: store, sortMode: .activity))
29+
menuBarSummary(
30+
from: store,
31+
activeTorrents: menuBarActiveTorrents(from: store, sortMode: .activity),
32+
ratioDisplayMode: AppDefaults.ratioDisplayMode
33+
)
3034
}
3135

3236
@MainActor
33-
func menuBarSummary(from store: TransmissionStore, activeTorrents: [Torrent]) -> MenuBarTorrentSummary {
37+
func menuBarSummary(from store: TransmissionStore, activeTorrents: [Torrent], ratioDisplayMode: RatioDisplayMode) -> MenuBarTorrentSummary {
3438
let stats = store.sessionStats
35-
36-
let uploaded = stats?.currentStats?.uploadedBytes ?? 0
37-
let downloaded = stats?.currentStats?.downloadedBytes ?? 0
38-
let ratio = downloaded > 0 ? Double(uploaded) / Double(downloaded) : 0
39+
let ratioSummary = makeRatioSummarySnapshot(store: store, displayMode: ratioDisplayMode)
3940

4041
return MenuBarTorrentSummary(
4142
serverName: store.host?.name ?? "No Server",
4243
activeCount: activeTorrents.count,
4344
downloadSpeed: stats?.downloadSpeed ?? 0,
4445
uploadSpeed: stats?.uploadSpeed ?? 0,
45-
ratio: ratio
46+
ratio: ratioSummary.ratio
4647
)
4748
}
4849

BitDream/Views/macOS/MenuBar/macOSMenuBarTorrentWidget.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ struct macOSMenuBarTorrentWidget: View {
55
@EnvironmentObject private var store: TransmissionStore
66
@State private var torrentRowsHeight: CGFloat = 0
77
@AppStorage(UserDefaultsKeys.menuBarSortMode) private var menuBarSortModeRaw: String = AppDefaults.menuBarSortMode.rawValue
8+
@AppStorage(UserDefaultsKeys.ratioDisplayMode) private var ratioDisplayModeRaw: String = AppDefaults.ratioDisplayMode.rawValue
89
let onOpenMainWindow: () -> Void
910

1011
private let panelWidth: CGFloat = 380
@@ -21,12 +22,16 @@ struct macOSMenuBarTorrentWidget: View {
2122
MenuBarSortMode(rawValue: menuBarSortModeRaw) ?? AppDefaults.menuBarSortMode
2223
}
2324

25+
private var ratioDisplayMode: RatioDisplayMode {
26+
RatioDisplayMode(rawValue: ratioDisplayModeRaw) ?? AppDefaults.ratioDisplayMode
27+
}
28+
2429
private var activeTorrents: [Torrent] {
2530
menuBarActiveTorrents(from: store, sortMode: menuBarSortMode)
2631
}
2732

2833
private var summary: MenuBarTorrentSummary {
29-
menuBarSummary(from: store, activeTorrents: activeTorrents)
34+
menuBarSummary(from: store, activeTorrents: activeTorrents, ratioDisplayMode: ratioDisplayMode)
3035
}
3136

3237
private var isConnected: Bool {
@@ -96,10 +101,12 @@ struct macOSMenuBarTorrentWidget: View {
96101
}
97102

98103
HStack(spacing: 8) {
99-
SpeedChip(speed: summary.downloadSpeed, direction: .download, style: .chip, size: .compact)
100-
SpeedChip(speed: summary.uploadSpeed, direction: .upload, style: .chip, size: .compact)
101104
RatioChip(ratio: summary.ratio, size: .compact)
102105
Spacer(minLength: 0)
106+
HStack(spacing: 8) {
107+
SpeedChip(speed: summary.downloadSpeed, direction: .download, style: .chip, size: .compact)
108+
SpeedChip(speed: summary.uploadSpeed, direction: .upload, style: .chip, size: .compact)
109+
}
103110
}
104111
}
105112
}

0 commit comments

Comments
 (0)