Skip to content

Commit 46bdda0

Browse files
committed
fix: correct delay unit from microseconds to milliseconds and improve refresh timing
- Fix runInMain delay parameter to use milliseconds instead of microseconds - Simplify refresh completion logic with conditional delay - Add 1000ms delay when online stats are present to allow users to notice updates - Remove nested conditional logic for cleaner code flow
1 parent 1750358 commit 46bdda0

File tree

2 files changed

+21
-33
lines changed

2 files changed

+21
-33
lines changed

V2er/General/Utils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ extension KeyboardReadable {
5454

5555

5656
func runInMain(delay: Int = 0, execute work: @escaping @convention(block) () -> Void) {
57-
DispatchQueue.main.asyncAfter(deadline: .now() + .microseconds(delay), execute: work)
57+
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(delay), execute: work)
5858
}
5959

6060
func hapticFeedback(_ style: UIImpactFeedbackGenerator.FeedbackStyle = .medium) {

V2er/View/Widget/Updatable/UpdatableView.swift

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct UpdatableView<Content: View>: View {
3232
let state: UpdatableState
3333
let onlineStats: OnlineStatsInfo?
3434
@State private var previousOnlineCount: Int? = nil
35-
35+
3636
private var refreshable: Bool {
3737
return onRefresh != nil
3838
}
@@ -54,7 +54,7 @@ struct UpdatableView<Content: View>: View {
5454
self.state = state
5555
self.onlineStats = onlineStats
5656
}
57-
57+
5858
var body: some View {
5959
ScrollViewReader { reader in
6060
ScrollView {
@@ -86,7 +86,7 @@ struct UpdatableView<Content: View>: View {
8686
.overlay {
8787
if state.showLoadingView {
8888
ZStack {
89-
// Color.almostClear
89+
// Color.almostClear
9090
ProgressView()
9191
.scaleEffect(1.5)
9292
}
@@ -113,46 +113,34 @@ struct UpdatableView<Content: View>: View {
113113
onScroll?(scrollY)
114114
// log("scrollY: \(scrollY), lastScrollY: \(lastScrollY), isRefreshing: \(isRefreshing), boundsDelta:\(boundsDelta)")
115115
progress = min(1, max(scrollY / threshold, 0))
116-
116+
117117
if progress == 1 && scrollY > lastScrollY && !hapticed {
118118
hapticed = true
119119
hapticFeedback(.soft)
120120
}
121-
121+
122122
if refreshable && !isRefreshing
123123
&& scrollY <= threshold
124124
&& lastScrollY > threshold {
125125
isRefreshing = true
126126
hapticed = false
127-
// Record current online count before refresh
127+
// Record whether online stats existed before refresh
128+
let hadOnlineStatsBefore = onlineStats != nil
128129
previousOnlineCount = onlineStats?.onlineCount
129-
130+
130131
Task {
131132
await onRefresh?()
132-
runInMain {
133-
// Check if online count changed
134-
let currentCount = onlineStats?.onlineCount
135-
let onlineCountChanged = previousOnlineCount != nil && currentCount != nil && previousOnlineCount != currentCount
136-
137-
if onlineCountChanged {
138-
// Delay hiding if online count changed
139-
Task {
140-
try? await Task.sleep(nanoseconds: 300_000_000) // 300ms
141-
runInMain {
142-
withAnimation {
143-
isRefreshing = false
144-
}
145-
}
146-
}
147-
} else {
148-
withAnimation {
149-
isRefreshing = false
150-
}
133+
// Decide delay (ms): 1200 if we had/now have online stats so users can notice updates; otherwise 0.
134+
let hasOnlineStatsNow = (onlineStats != nil)
135+
let delayMs = (hadOnlineStatsBefore || hasOnlineStatsNow) ? 1000 : 0
136+
runInMain(delay: delayMs) {
137+
withAnimation {
138+
isRefreshing = false
151139
}
152140
}
153141
}
154142
}
155-
143+
156144
if loadMoreable
157145
&& state.hasMoreData
158146
&& boundsDelta >= 0
@@ -175,7 +163,7 @@ struct UpdatableView<Content: View>: View {
175163

176164
private struct AncorView: View {
177165
static let coordinateSpaceName = "coordinateSpace.UpdatableView"
178-
166+
179167
var body: some View {
180168
GeometryReader { geometry in
181169
Color.clear
@@ -227,7 +215,7 @@ extension View {
227215
let state = UpdatableState(hasMoreData: hasMoreData, showLoadingView: autoRefresh, scrollToTop: scrollToTop)
228216
return self.modifier(UpdatableModifier(onRefresh: refresh, onLoadMore: loadMore, onScroll: onScroll, state: state, onlineStats: onlineStats))
229217
}
230-
218+
231219
public func updatable(_ state: UpdatableState,
232220
onlineStats: OnlineStatsInfo? = nil,
233221
refresh: RefreshAction = nil,
@@ -236,7 +224,7 @@ extension View {
236224
let modifier = UpdatableModifier(onRefresh: refresh, onLoadMore: loadMore, onScroll: onScroll, state: state, onlineStats: onlineStats)
237225
return self.modifier(modifier)
238226
}
239-
227+
240228
public func loadMore(_ state: UpdatableState,
241229
_ loadMore: LoadMoreAction = nil,
242230
onScroll: ScrollAction? = nil) -> some View {
@@ -250,7 +238,7 @@ extension View {
250238
onScroll: ScrollAction? = nil) -> some View {
251239
self.updatable(autoRefresh: autoRefresh, hasMoreData: hasMoreData, onlineStats: nil, refresh: nil, loadMore: loadMore, onScroll: onScroll)
252240
}
253-
241+
254242
public func onScroll(onScroll: ScrollAction?) -> some View {
255243
self.updatable(onlineStats: nil, onScroll: onScroll)
256244
}
@@ -262,7 +250,7 @@ struct UpdatableModifier: ViewModifier {
262250
let onScroll: ScrollAction?
263251
let state: UpdatableState
264252
let onlineStats: OnlineStatsInfo?
265-
253+
266254
func body(content: Content) -> some View {
267255
UpdatableView(onRefresh: onRefresh, onLoadMore: onLoadMore,
268256
onScroll: onScroll, state: state, onlineStats: onlineStats) {

0 commit comments

Comments
 (0)