Skip to content

Commit 8e12876

Browse files
committed
perf: refresh folder status view the right way
1 parent 5de1a58 commit 8e12876

File tree

5 files changed

+99
-37
lines changed

5 files changed

+99
-37
lines changed

Localizable.xcstrings

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,46 @@
264264
}
265265
}
266266
},
267+
"(Transfer speed unknown)" : {
268+
"localizations" : {
269+
"de" : {
270+
"stringUnit" : {
271+
"state" : "translated",
272+
"value" : "(Übertragungsgeschwindigkeit unbekannt)"
273+
}
274+
},
275+
"es" : {
276+
"stringUnit" : {
277+
"state" : "translated",
278+
"value" : "(Velocidad de transferencia desconocida)"
279+
}
280+
},
281+
"it" : {
282+
"stringUnit" : {
283+
"state" : "translated",
284+
"value" : "(Velocità di trasferimento sconosciuta)"
285+
}
286+
},
287+
"nl" : {
288+
"stringUnit" : {
289+
"state" : "translated",
290+
"value" : "(Overdrachtsnelheid onbekend)"
291+
}
292+
},
293+
"uk" : {
294+
"stringUnit" : {
295+
"state" : "translated",
296+
"value" : "(Швидкість передачі невідома)"
297+
}
298+
},
299+
"zh-Hans" : {
300+
"stringUnit" : {
301+
"state" : "translated",
302+
"value" : "(传输速度未知)"
303+
}
304+
}
305+
}
306+
},
267307
"%@" : {
268308
"localizations" : {
269309
"de" : {

Sushitrain/BrowserListView.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ struct BrowserListView: View {
2020
List {
2121
Section {
2222
FolderStatusView(folder: folder)
23-
.id(appState.eventCounter) // Update for each event
2423

2524
if hasExtraneousFiles {
2625
NavigationLink(destination: { ExtraFilesView(folder: self.folder) }) {

Sushitrain/BrowserView.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ struct BrowserView: View {
116116
.accessibilityLabel(fsd.text)
117117
.popover(isPresented: $showStatusPopover, arrowEdge: .bottom) {
118118
FolderStatusView(folder: folder)
119-
.id(appState.eventCounter) // Update for each event
120119
.padding()
121120
.frame(minWidth: 120)
122121
}
@@ -556,7 +555,6 @@ private struct BrowserItemsView: View {
556555
HStack {
557556
#if os(iOS)
558557
FolderStatusView(folder: folder)
559-
.id(appState.eventCounter) // Update for each event
560558
.padding(.all, 10)
561559
#endif
562560

Sushitrain/FolderView.swift

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -207,46 +207,65 @@ struct FolderStatusDescription {
207207
struct FolderStatusView: View {
208208
@Environment(AppState.self) private var appState
209209
var folder: SushitrainFolder
210+
211+
@State private var statistics: SushitrainFolderStats? = nil
212+
@State private var status: String? = nil
213+
@State private var folderStatusDescription: FolderStatusDescription? = nil
210214

211215
var body: some View {
212-
var error: NSError? = nil
213-
let status = folder.state(&error)
214-
215-
if status == "syncing" && !folder.isSelective() {
216-
if let statistics = try? folder.statistics(), statistics.global!.bytes > 0 {
217-
let formatter = ByteCountFormatter()
218-
if let globalBytes = statistics.global?.bytes, let localBytes = statistics.local?.bytes {
219-
let remainingText = formatter.string(fromByteCount: (globalBytes - localBytes))
220-
221-
ProgressView(
222-
value: Double(localBytes) / Double(globalBytes),
223-
total: 1.0
224-
) {
225-
Label(
226-
"Synchronizing...", systemImage: "bolt.horizontal.circle"
227-
)
228-
.foregroundStyle(.orange)
229-
.badge(Text(remainingText))
230-
}.tint(.orange)
216+
ZStack {
217+
if let status = status {
218+
if status == "syncing" && !folder.isSelective() {
219+
if let statistics = self.statistics, statistics.global!.bytes > 0 {
220+
let formatter = ByteCountFormatter()
221+
if let globalBytes = statistics.global?.bytes, let localBytes = statistics.local?.bytes {
222+
let remainingText = formatter.string(fromByteCount: (globalBytes - localBytes))
223+
224+
ProgressView(
225+
value: Double(localBytes) / Double(globalBytes),
226+
total: 1.0
227+
) {
228+
Label(
229+
"Synchronizing...", systemImage: "bolt.horizontal.circle"
230+
)
231+
.foregroundStyle(.orange)
232+
.badge(Text(remainingText))
233+
}.tint(.orange)
234+
}
235+
else {
236+
self.statusLabel()
237+
}
238+
}
239+
else {
240+
self.statusLabel()
241+
}
231242
}
232243
else {
233244
self.statusLabel()
234245
}
246+
247+
if let folderStatus = self.folderStatusDescription, let txt = folderStatus.additionalText {
248+
Text(txt).foregroundStyle(.red)
249+
}
235250
}
236-
else {
237-
self.statusLabel()
238-
}
239-
}
240-
else {
241-
self.statusLabel()
251+
}.task {
252+
await Task.detached {
253+
await self.update()
254+
}.value
242255
}
243-
244-
let folderStatus = FolderStatusDescription(folder)
245-
246-
if let txt = folderStatus.additionalText {
247-
Text(txt).foregroundStyle(.red)
256+
.onChange(of: appState.eventCounter) { _, _ in
257+
Task.detached {
258+
await self.update()
259+
}
248260
}
249261
}
262+
263+
private func update() async {
264+
var error: NSError? = nil
265+
self.status = folder.state(&error)
266+
self.statistics = try? folder.statistics()
267+
self.folderStatusDescription = FolderStatusDescription(folder)
268+
}
250269

251270
@ViewBuilder private func statusLabel() -> some View {
252271
let folderStatus = FolderStatusDescription(folder)

Sushitrain/StartView.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,23 +140,25 @@ private struct OverallDownloadProgressView: View {
140140

141141
@ViewBuilder private var speeds: some View {
142142
// Download speed
143-
if let (date, progress) = progress, let (lastDate, lastProgress) = self.lastProgress {
144-
HStack {
143+
HStack {
144+
Spacer()
145+
if let (date, progress) = progress, let (lastDate, lastProgress) = self.lastProgress {
145146
let diffBytes = progress.bytesDone - lastProgress.bytesDone
146147
let diffTime = date.timeIntervalSince(lastDate)
147148
let speed = Int64(Double(diffBytes) / Double(diffTime))
148149
let formatter = ByteCountFormatter()
149150

150151
if speed > 0 && diffTime > 0 {
151-
Spacer()
152152
Text("\(formatter.string(fromByteCount: speed))/s")
153153
.foregroundStyle(.green)
154-
155154
let secondsToGo = Duration(
156155
secondsComponent: (progress.bytesTotal - progress.bytesDone) / speed, attosecondsComponent: 0)
157156
let secondsToGoFormatted: String = secondsToGo.formatted(.time(pattern: .hourMinuteSecond(padHourToLength: 0)))
158157
Text(secondsToGoFormatted).foregroundStyle(.green)
159158
}
159+
else {
160+
Text("(Transfer speed unknown)").foregroundStyle(.gray)
161+
}
160162
}
161163
}
162164
}
@@ -538,6 +540,10 @@ struct StartView: View {
538540
self.peers = p
539541
self.folders = await self.appState.folders().sorted()
540542
self.longTimeNotSeenDevices = p.filter {
543+
if $0.isPaused() {
544+
return false
545+
}
546+
541547
if let d = $0.lastSeen()?.date() {
542548
return -d.timeIntervalSinceNow > appState.userSettings.longTimeNoSeeInterval
543549
}

0 commit comments

Comments
 (0)