@@ -39,6 +39,7 @@ public struct TopicFeature: Reducer, Sendable {
3939
4040 var isFirstPage = true
4141 var isLoadingTopic = true
42+ var isRefreshing = false
4243
4344 var pageNavigation = PageNavigationFeature . State ( type: . topic)
4445
@@ -70,6 +71,8 @@ public struct TopicFeature: Reducer, Sendable {
7071
7172 public enum Action {
7273 case onTask
74+ case onRefresh
75+ case onSceneBecomeActive
7376 case userAvatarTapped( userId: Int )
7477 case urlTapped( URL )
7578 case pageNavigation( PageNavigationFeature . Action )
@@ -112,6 +115,19 @@ public struct TopicFeature: Reducer, Sendable {
112115 . send( . _loadTopic( offset: state. initialOffset) )
113116 )
114117
118+ case . onRefresh:
119+ state. isRefreshing = true
120+ return . run { [ offset = state. pageNavigation. offset] send in
121+ await send ( . _loadTopic( offset: offset) )
122+ }
123+
124+ case . onSceneBecomeActive:
125+ if state. isLoadingTopic || state. isRefreshing {
126+ return . none
127+ } else {
128+ return . send( . onRefresh)
129+ }
130+
115131 case . userAvatarTapped:
116132 // TODO: Wrap into Delegate action?
117133 return . none
@@ -121,6 +137,7 @@ public struct TopicFeature: Reducer, Sendable {
121137 return . none
122138
123139 case let . pageNavigation( . offsetChanged( to: newOffset) ) :
140+ state. isRefreshing = false
124141 return . concatenate( [
125142 . run { [ isLastPage = state. pageNavigation. isLastPage, topicId = state. topicId] _ in
126143 if isLastPage {
@@ -165,10 +182,16 @@ public struct TopicFeature: Reducer, Sendable {
165182
166183 case let . _loadTopic( offset) :
167184 state. isFirstPage = offset == 0
168- state. isLoadingTopic = true
169- return . run { [ id = state. topicId, perPage = state. appSettings. topicPerPage] send in
170- let result = await Result { try await apiClient. getTopic ( id, offset, perPage) }
171- await send ( . _topicResponse( result) )
185+ if !state. isRefreshing {
186+ state. isLoadingTopic = true
187+ }
188+ return . run { [ id = state. topicId, perPage = state. appSettings. topicPerPage, isRefreshing = state. isRefreshing] send in
189+ let startTime = Date ( )
190+ let topic = try await apiClient. getTopic ( id, offset, perPage)
191+ if isRefreshing { await delayUntilTimePassed ( 1.0 , since: startTime) }
192+ await send ( . _topicResponse( . success( topic) ) )
193+ } catch: { error, send in
194+ await send ( . _topicResponse( . failure( error) ) )
172195 }
173196 . cancellable ( id: CancelID . loading)
174197
@@ -207,6 +230,7 @@ public struct TopicFeature: Reducer, Sendable {
207230 case let . _loadTypes( types) :
208231 state. types = types
209232 state. isLoadingTopic = false
233+ state. isRefreshing = false
210234 reportFullyDisplayed ( & state)
211235 return . none
212236// return PageNavigationFeature()
@@ -215,6 +239,7 @@ public struct TopicFeature: Reducer, Sendable {
215239
216240 case let . _topicResponse( . failure( error) ) :
217241 print ( " TOPIC RESPONSE FAILURE: \( error) " )
242+ state. isRefreshing = false
218243 reportFullyDisplayed ( & state)
219244 return . none
220245
0 commit comments