ComListViewModel.m
5.0 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// ComListViewModel.m
// DreamSleep
//
// Created by peter on 2022/9/27.
//
#import "ComListViewModel.h"
#import "ComDynListResultModel.h"
@interface ComListViewModel ()
/// 社区动态列表结果数据model
@property (nonatomic, strong) ComDynListResultModel *resultModel;
@end
@implementation ComListViewModel
+ (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper {
return @{@"resultModel" : @"result"
};
}
- (instancetype)init {
if (self = [super init]) {
self.listArr = [NSArray array];
}
return self;
}
- (NSURLSessionDataTask *)querySleepDynamicListWithLoadMore:(BOOL)loadMore offset:(int)offset completion:(void (^)(ComListViewModel *requestModel))completion {
NSString *api = @"query_sleep_dynamic_list";
NSString *argStr = [NSString stringWithFormat:@"query{%@(offset:%d)}", api, offset];
return [ComListViewModel httpPostBodyRequestWithAPI:api params:@{@"query" : argStr} view:nil hasNetActivity:YES loadingInfo:nil hasFailInfo:NO success:^(NSDictionary * _Nonnull apiDic) {
DSLog(@"社区动态查询列表接口apiDic:%@", apiDic);
// 1、解析数据
self.resCode = DSResCodeSuccess;
self.resultModel = [ComDynListResultModel yy_modelWithDictionary:apiDic[@"result"]];
// 2、根据业务需要的数据结构对数据进行加工
NSArray *lastes_data_list = self.resultModel.data_list ?: @[];
if (loadMore) {
NSMutableArray *tmp_list_arr = [NSMutableArray arrayWithArray:self.listArr];
[tmp_list_arr addObjectsFromArray:lastes_data_list.copy];
self.listArr = tmp_list_arr.copy;
} else {
self.listArr = lastes_data_list.copy;
}
self.totalCount = self.resultModel.count;
// 3、如果是官方发布的动态,则放在第一位,并且只插入1次
NSMutableArray *newList = [NSMutableArray arrayWithArray:self.listArr];
__block BOOL hasOfficialModel = NO;
[newList enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:[OfficialMessageModel class]]) {
hasOfficialModel = YES;
*stop = YES;
}
}];
if (hasOfficialModel == NO) {
[newList insertObject:self.resultModel.officialMessageModel atIndex:0];
}
self.isOfficial = self.resultModel.officialMessageModel;
self.listArr = newList.copy;
completion(self);
} failure:^(id _Nonnull failureInfo) {
self.resCode = [failureInfo[@"errorCode"] integerValue];
self.errMessage = failureInfo[@"errMessage"];
completion(self);
}];
}
- (void)showOfficialAllContent {
self.resultModel.officialMessageModel.isShowAllContent = YES;
}
- (void)deleteComDynWithDynamicID:(int)dynamicID {
NSMutableArray *tmpListArr = [NSMutableArray arrayWithArray:self.listArr];
[tmpListArr enumerateObjectsUsingBlock:^(ComDynModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.dynamicID == dynamicID) {
*stop = YES;
[tmpListArr removeObject:obj];
}
}];
self.listArr = tmpListArr.copy;
}
- (void)insertUserDyModel:(ComDynModel *)dyModel {
if (!dyModel) { return; }
// 用户发的说说临时插入到动态列表第一条
NSInteger insertIndex = 0;
// 如果有官方动态,则插入到官方动态下面
if (self.resultModel.officialMessageModel) {
insertIndex = 1;
}
NSMutableArray *tmpListArr = [NSMutableArray arrayWithArray:self.listArr];
[tmpListArr insertObject:dyModel atIndex:insertIndex];
self.listArr = tmpListArr.copy;
}
+ (void)updateLikeData:(ComDynModel *)comDynModel {
if (comDynModel.isLike) {
comDynModel.likeCount--;
} else {
comDynModel.likeCount++;
}
comDynModel.isLike = !comDynModel.isLike;
}
+ (NSURLSessionDataTask *)userDynamicPraiseWithComDynModel:(ComDynModel *)comDynModel completion:(void (^)(ComListViewModel *requestModel))completion {
ComListViewModel * requestModel = [[ComListViewModel alloc] init];
NSString *api = @"user_dynamic_praise";
NSString *argStr = [NSString stringWithFormat:@"mutation{%@(talk_id:%d)}", api, comDynModel.dynamicID];
return [self httpPostBodyRequestWithAPI:api params:@{@"query" : argStr} view:nil hasNetActivity:YES loadingInfo:nil hasFailInfo:NO success:^(NSDictionary * _Nonnull apiDic) {
DSLog(@"用户动态点赞、取消点赞接口apiDic:%@", apiDic);
requestModel.resCode = DSResCodeSuccess;
completion(requestModel);
} failure:^(id _Nonnull failureInfo) {
requestModel.resCode = [failureInfo[@"errorCode"] integerValue];
requestModel.errMessage = failureInfo[@"errMessage"];
// 回滚数据model
if (comDynModel.isLike) {
comDynModel.likeCount--;
} else {
comDynModel.likeCount++;
}
comDynModel.isLike = !comDynModel.isLike;
completion(requestModel);
}];
}
@end