Skip to content

Commit 917b460

Browse files
committed
feat: refresh 로직 수정
1 parent 85d843b commit 917b460

1 file changed

Lines changed: 29 additions & 16 deletions

File tree

Projects/Feature/Challenge/Sources/Presentation/Children/ChallengeDetail/ChallengeDetailFeature.swift

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@ public struct ChallengeDetailFeature {
3737
var stepCountStatus: ChallengeStepCountStatus?
3838
var currentStepCountFromHealthKit: Int?
3939

40+
var hasValidRankings: Bool {
41+
(rankings?.count ?? 0) > 1
42+
}
43+
4044
var contentState: ContentState {
41-
guard let rankings else { return .loading }
42-
return rankings.isEmpty ? .empty : .content
45+
guard rankings != nil else { return .loading }
46+
return hasValidRankings ? .content : .empty
4347
}
4448

4549
public init() {}
@@ -125,14 +129,20 @@ public struct ChallengeDetailFeature {
125129
}
126130
.cancellable(id: State.CancelID.stepChallengeStatus, cancelInFlight: true)
127131
case .startMyStepCountTimer:
132+
guard state.hasValidRankings,
133+
state.stepCountStatus != nil else {
134+
return .none
135+
}
136+
128137
return .run { [clock] send in
129138
for await _ in clock.timer(interval: .seconds(5)) {
130139
await send(.fetchMyStepCount)
131140
}
132141
}
133142
.cancellable(id: State.CancelID.myStepCountTimer, cancelInFlight: true)
134143
case .fetchMyStepCount:
135-
guard let groupID = state.selectedGroup?.groupId,
144+
guard state.hasValidRankings,
145+
let groupID = state.selectedGroup?.groupId,
136146
let startDate = state.stepCountStatus?.stepCountFetchFromAt else {
137147
return .none
138148
}
@@ -145,21 +155,24 @@ public struct ChallengeDetailFeature {
145155
to: now
146156
)
147157
await send(.updateChallengeStepCount(groupID: groupID, stepCount: stepCount))
148-
ModyLogger.debug("Challenge my step count: \(stepCount)")
149158
} catch {
150159
ModyLogger.debug("Challenge my step count fetch failed: \(error)")
151160
}
152161
}
153162
.cancellable(id: State.CancelID.myStepCountFetch, cancelInFlight: true)
154163
case let .updateChallengeStepCount(groupID, stepCount):
155-
guard state.selectedGroup?.groupId == groupID else {
164+
guard state.hasValidRankings,
165+
state.selectedGroup?.groupId == groupID else {
156166
return .none
157167
}
158168

159169
let needStepCountUpdate = state.currentStepCountFromHealthKit != stepCount
160170
state.currentStepCountFromHealthKit = stepCount
161171

162-
guard needStepCountUpdate else { return .none }
172+
guard needStepCountUpdate else {
173+
ModyLogger.debug("Record Step Count Skip: \(stepCount)")
174+
return .none
175+
}
163176

164177
let request = ChallengeStepCountRequest(
165178
recordedOn: Date.now.toString(format: .yyyyMMdd),
@@ -170,37 +183,37 @@ public struct ChallengeDetailFeature {
170183
groupId: groupID,
171184
request: request
172185
)
186+
ModyLogger.debug("Challenge my step count: \(stepCount)")
173187
}
174188
case let .challengeStepRankingsFetched(groupID, rankings):
175189
guard state.selectedGroup?.groupId == groupID else {
176190
return .none
177191
}
178192

179193
state.rankings = rankings
180-
return .none
194+
guard state.hasValidRankings else {
195+
state.currentStepCountFromHealthKit = nil
196+
return .none
197+
}
198+
199+
return .send(.startMyStepCountTimer)
181200
case let .stepChallengeStatusFetched(groupID, status):
182201
guard state.selectedGroup?.groupId == groupID else {
183202
return .none
184203
}
185204

186205
state.stepCountStatus = status
187-
return .send(.startMyStepCountTimer)
206+
return .none
188207
case .showAlert:
189208
return .none
190209
case .changeChallengeButtonTapped:
191210
// TODO: 챌린지 변경 Implementation
192211
return .none
193212
case .refreshStepButtonTapped:
194-
guard state.selectedGroup != nil ,
213+
guard let group = state.selectedGroup ,
195214
state.stepCountStatus != nil else { return .none }
196215

197-
state.stepCountStatus = nil
198-
return .merge(
199-
.cancel(id: State.CancelID.myStepCountFetch),
200-
.cancel(id: State.CancelID.myStepCountTimer),
201-
.cancel(id: State.CancelID.myStepCountUpdate),
202-
.send(.fetchStepChallengeStatus)
203-
)
216+
return .send(.setSelectedGroup(group))
204217
}
205218
}
206219
}

0 commit comments

Comments
 (0)