feat: 更新动态点赞与加载状态管理以提升用户体验
- 在DetailFeature和FeedListFeature中增强点赞功能的状态管理,确保用户交互流畅。 - 新增API加载效果视图,提升用户在操作过程中的反馈体验。 - 更新视图组件以支持点赞加载状态,优化用户界面交互。 - 改进错误处理逻辑,确保在API请求失败时提供友好的错误提示。
This commit is contained in:
@@ -36,7 +36,6 @@ struct DetailFeature {
|
||||
case showImagePreview([String], Int)
|
||||
case hideImagePreview
|
||||
case imagePreviewDismissed
|
||||
case onLikeSuccess(Int, Bool) // dynamicId, newLikeState
|
||||
case dismissView
|
||||
|
||||
// 新增:当前用户ID相关actions
|
||||
@@ -45,7 +44,9 @@ struct DetailFeature {
|
||||
}
|
||||
|
||||
var body: some ReducerOf<Self> {
|
||||
Reduce { state, action in
|
||||
Reduce {
|
||||
state,
|
||||
action in
|
||||
switch action {
|
||||
case .onAppear:
|
||||
// 如果还没有获取过当前用户ID,则开始获取
|
||||
@@ -69,9 +70,10 @@ struct DetailFeature {
|
||||
return .none
|
||||
|
||||
case let .likeDynamic(dynamicId, uid, likedUid, worldId):
|
||||
// 设置loading状态
|
||||
state.isLikeLoading = true
|
||||
|
||||
let status = state.moment.isLike ? 0 : 1
|
||||
let status = state.moment.isLike ? 0 : 1 // 0: 取消点赞, 1: 点赞
|
||||
let request = LikeDynamicRequest(
|
||||
dynamicId: dynamicId,
|
||||
uid: uid,
|
||||
@@ -80,59 +82,71 @@ struct DetailFeature {
|
||||
worldId: worldId
|
||||
)
|
||||
|
||||
return .run { send in
|
||||
let result = await TaskResult {
|
||||
try await apiService.request(request)
|
||||
return .run { [apiService] send in
|
||||
do {
|
||||
let response: LikeDynamicResponse = try await apiService.request(request)
|
||||
await send(.likeResponse(.success(response)))
|
||||
} catch {
|
||||
await send(.likeResponse(.failure(error)))
|
||||
}
|
||||
await send(.likeResponse(result))
|
||||
}
|
||||
|
||||
case let .likeResponse(.success(response)):
|
||||
state.isLikeLoading = false
|
||||
// 点赞成功,通知父视图更新状态
|
||||
return .send(.onLikeSuccess(state.moment.dynamicId, !state.moment.isLike))
|
||||
if let data = response.data, let success = data.success, success {
|
||||
// 根据API响应更新点赞状态
|
||||
let newLikeState = !state.moment.isLike // 切换点赞状态
|
||||
|
||||
// 创建更新后的动态对象
|
||||
let updatedMoment = MomentsInfo(
|
||||
dynamicId: state.moment.dynamicId,
|
||||
uid: state.moment.uid,
|
||||
nick: state.moment.nick,
|
||||
avatar: state.moment.avatar,
|
||||
type: state.moment.type,
|
||||
content: state.moment.content,
|
||||
likeCount: data.likeCount ?? state.moment.likeCount,
|
||||
isLike: newLikeState,
|
||||
commentCount: state.moment.commentCount,
|
||||
publishTime: state.moment.publishTime,
|
||||
worldId: state.moment.worldId,
|
||||
status: state.moment.status,
|
||||
playCount: state.moment.playCount,
|
||||
dynamicResList: state.moment.dynamicResList,
|
||||
gender: state.moment.gender,
|
||||
squareTop: state.moment.squareTop,
|
||||
topicTop: state.moment.topicTop,
|
||||
newUser: state.moment.newUser,
|
||||
defUser: state.moment.defUser,
|
||||
scene: state.moment.scene,
|
||||
userVipInfoVO: state.moment.userVipInfoVO,
|
||||
headwearPic: state.moment.headwearPic,
|
||||
headwearEffect: state.moment.headwearEffect,
|
||||
headwearType: state.moment.headwearType,
|
||||
headwearName: state.moment.headwearName,
|
||||
headwearId: state.moment.headwearId,
|
||||
experLevelPic: state.moment.experLevelPic,
|
||||
charmLevelPic: state.moment.charmLevelPic,
|
||||
isCustomWord: state.moment.isCustomWord,
|
||||
labelList: state.moment.labelList
|
||||
)
|
||||
state.moment = updatedMoment
|
||||
// 移除loading状态
|
||||
state.isLikeLoading = false
|
||||
} else {
|
||||
// API返回失败,通过APILoadingManager显示错误信息
|
||||
let errorMessage = response.message.isEmpty ? "点赞失败,请重试" : response.message
|
||||
setAPILoadingErrorSync(UUID(), errorMessage: errorMessage)
|
||||
}
|
||||
|
||||
case let .onLikeSuccess(dynamicId, newLikeState):
|
||||
// 更新本地动态的点赞状态
|
||||
// 由于MomentsInfo的isLike是let,我们需要重新创建moment对象
|
||||
let updatedMoment = MomentsInfo(
|
||||
dynamicId: state.moment.dynamicId,
|
||||
uid: state.moment.uid,
|
||||
nick: state.moment.nick,
|
||||
avatar: state.moment.avatar,
|
||||
type: state.moment.type,
|
||||
content: state.moment.content,
|
||||
likeCount: state.moment.likeCount,
|
||||
isLike: newLikeState,
|
||||
commentCount: state.moment.commentCount,
|
||||
publishTime: state.moment.publishTime,
|
||||
worldId: state.moment.worldId,
|
||||
status: state.moment.status,
|
||||
playCount: state.moment.playCount,
|
||||
dynamicResList: state.moment.dynamicResList,
|
||||
gender: state.moment.gender,
|
||||
squareTop: state.moment.squareTop,
|
||||
topicTop: state.moment.topicTop,
|
||||
newUser: state.moment.newUser,
|
||||
defUser: state.moment.defUser,
|
||||
scene: state.moment.scene,
|
||||
userVipInfoVO: state.moment.userVipInfoVO,
|
||||
headwearPic: state.moment.headwearPic,
|
||||
headwearEffect: state.moment.headwearEffect,
|
||||
headwearType: state.moment.headwearType,
|
||||
headwearName: state.moment.headwearName,
|
||||
headwearId: state.moment.headwearId,
|
||||
experLevelPic: state.moment.experLevelPic,
|
||||
charmLevelPic: state.moment.charmLevelPic,
|
||||
isCustomWord: state.moment.isCustomWord,
|
||||
labelList: state.moment.labelList
|
||||
)
|
||||
state.moment = updatedMoment
|
||||
// 移除loading状态
|
||||
state.isLikeLoading = false
|
||||
return .none
|
||||
|
||||
case let .likeResponse(.failure(error)):
|
||||
// 移除loading状态
|
||||
state.isLikeLoading = false
|
||||
// 可以在这里处理错误,比如显示错误提示
|
||||
// 通过APILoadingManager显示错误信息
|
||||
setAPILoadingErrorSync(UUID(), errorMessage: error.localizedDescription)
|
||||
return .none
|
||||
|
||||
case .deleteDynamic:
|
||||
@@ -179,4 +193,4 @@ struct DetailFeature {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user