ComDetailViewModel.m
4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// ComDetailViewModel.m
// DreamSleep
//
// Created by peter on 2022/10/8.
//
#import "ComDetailViewModel.h"
#import "ComDynModel.h"
#import "CommentModel.h"
#import "ReplyModel.h"
#import "ComReplyModel.h"
@implementation ComDetailViewModel
- (instancetype)init {
if (self = [super init]) {
self.groupDatas = @[];
}
return self;
}
- (NSURLSessionDataTask *)queryDynamicCommentListWithTalkID:(int)talkID completion:(void (^)(ComDetailViewModel *requestModel))completion {
NSString *api = @"query_dynamic_comment_list";
NSString *argStr = [NSString stringWithFormat:@"query{%@(talk_id:%d)}", api, talkID];
return [ComDetailViewModel httpPostBodyRequestWithAPI:api params:@{@"query" : argStr} view:nil hasNetActivity:YES loadingInfo:nil hasFailInfo:NO success:^(NSDictionary * _Nonnull apiDic) {
DSLog(@"查询动态评论列表接口apiDic:%@", apiDic);
self.resCode = DSResCodeSuccess;
NSDictionary *resultDic = apiDic[@"result"];
ComDynModel *talkModel = [ComDynModel yy_modelWithDictionary:resultDic[@"talk"]];
talkModel.modelType = DynModelTypeDetail;
NSArray *comment_list = resultDic[@"comment_list"];
NSMutableArray *tmpArr = [NSMutableArray array];
for (int i = 0; i < comment_list.count; i++) {
NSDictionary *comment_reply_dic = comment_list[i];
ComReplyModel *comReplyModel = [ComReplyModel yy_modelWithDictionary:comment_reply_dic];
[tmpArr addObject:comReplyModel];
}
self.groupDatas = @[talkModel, tmpArr.copy];
completion(self);
} failure:^(id _Nonnull failureInfo) {
self.resCode = [failureInfo[@"errorCode"] integerValue];
self.errMessage = failureInfo[@"errMessage"];
completion(self);
}];
}
- (NSURLSessionDataTask *)userCommentDynamicWithComDynModel:(ComDynModel *)model content:(NSString *)content completion:(void (^)(ComDetailViewModel *requestModel))completion {
NSString *api = @"user_comment_dynamic";
NSString *argStr = [NSString stringWithFormat:@"mutation{%@(talk_id:%d,user_id:%d,content:\"%@\")}", api, model.dynamicID, model.userID, content];
return [ComDetailViewModel httpPostBodyRequestWithAPI:api params:@{@"query" : argStr} view:nil hasNetActivity:YES loadingInfo:nil hasFailInfo:NO success:^(NSDictionary * _Nonnull apiDic) {
DSLog(@"用户-发布评论接口apiDic:%@", apiDic);
self.resCode = DSResCodeSuccess;
NSDictionary *resultDic = apiDic[@"result"];
CommentModel *commentModel = [CommentModel yy_modelWithDictionary:resultDic];
// 插入新的评论数据(放在评论列表数据最前面)
ComReplyModel *newComReplyModel = [[ComReplyModel alloc] init];
newComReplyModel.commentModel = commentModel;
ComDynModel *talkModel = self.groupDatas.firstObject;
NSMutableArray *tmpArr = [NSMutableArray arrayWithArray:self.groupDatas.lastObject];
[tmpArr insertObject:newComReplyModel atIndex:0];
self.groupDatas = @[talkModel, tmpArr.copy];
completion(self);
} failure:^(id _Nonnull failureInfo) {
self.resCode = [failureInfo[@"errorCode"] integerValue];
self.errMessage = failureInfo[@"errMessage"];
completion(self);
}];
}
- (NSURLSessionDataTask *)queryCommentRelpyListWithCommentID:(int)commentID completion:(void (^)(ComDetailViewModel *requestModel))completion {
NSString *api = @"query_comment_relpy_list";
NSString *argStr = [NSString stringWithFormat:@"query{%@(comment_id:%d)}", api, commentID];
return [ComDetailViewModel httpPostBodyRequestWithAPI:api params:@{@"query" : argStr} view:nil hasNetActivity:YES loadingInfo:nil hasFailInfo:NO success:^(NSDictionary * _Nonnull apiDic) {
DSLog(@"查询评论回复列表接口apiDic:%@", apiDic);
self.resCode = DSResCodeSuccess;
NSDictionary *resultDic = apiDic[@"result"];
CommentModel *commentModel = [CommentModel yy_modelWithDictionary:resultDic[@"comment"]];
NSArray *reply_list = resultDic[@"reply_list"];
NSMutableArray *tmpArr = [NSMutableArray array];
for (int i = 0; i < reply_list.count; i++) {
ReplyModel *replyModel = [ReplyModel yy_modelWithDictionary:reply_list[i]];
[tmpArr addObject:replyModel];
}
self.replyGroupDatas = @[commentModel, tmpArr.copy];
completion(self);
} failure:^(id _Nonnull failureInfo) {
self.resCode = [failureInfo[@"errorCode"] integerValue];
self.errMessage = failureInfo[@"errMessage"];
completion(self);
}];
}
@end