-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPAPIIllustList.m
More file actions
124 lines (103 loc) · 2.95 KB
/
Copy pathPAPIIllustList.m
File metadata and controls
124 lines (103 loc) · 2.95 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
//
// PAPIIllustList.m
//
// Created by Zhou Hao on 14/10/19.
// Copyright (c) 2014 Zhou Hao. All rights reserved.
//
#import "PAPIIllustList.h"
#import "PixivDefines.h"
@implementation PAPIIllustList
+ (PAPIIllustList *)parseJsonDictionaryToModelList:(NSDictionary *)jsonData isWork:(BOOL)isWork
{
if (![jsonData objectForKey:@"count"] || ![jsonData objectForKey:@"response"]) {
NSLog(@"jsonData.count for jsonData.response not found");
return nil;
}
PAPIIllustList *list = [[PAPIIllustList alloc] init];
list.raw = jsonData;
list.pagination = nil;
if ([[jsonData objectForKey:@"pagination"] isKindOfClass:[NSDictionary class]]) {
list.pagination = jsonData[@"pagination"];
}
// from response[] gen NSArray of PAPIIllust
NSMutableArray *tmpIllusts = [[NSMutableArray alloc] init];
NSArray *responseList = nil;
if ([[jsonData[@"response"] firstObject] objectForKey:@"works"]) {
responseList = [[jsonData[@"response"] firstObject] objectForKey:@"works"];
} else {
responseList = jsonData[@"response"];
}
for (NSDictionary *jsonIllust in responseList) {
PAPIIllust *illust = [PAPIIllust parseRawDictionaryToModel:jsonIllust isWork:isWork];
if (!illust) {
NSLog(@"parseRaw() error:\n%@", jsonIllust);
continue;
}
[tmpIllusts addObject:illust];
}
list.illusts = tmpIllusts;
return list;
}
- (NSArray *)toObjectList;
{
NSMutableArray *tmpIllusts = [[NSMutableArray alloc] init];
for (PAPIIllust *illust in self.illusts) {
[tmpIllusts addObject:illust.toObject];
}
return tmpIllusts;
}
- (NSInteger)count
{
return [self.illusts count];
}
#pragma mark - pagination properties
- (NSInteger)safeIntegerValue:(id)data
{
if (data == [NSNull null]) {
return PIXIV_INT_INVALID;
}
return [data integerValue];
}
- (NSInteger)per_page
{
return [self safeIntegerValue:self.pagination[@"per_page"]];
}
- (NSInteger)total
{
return [self safeIntegerValue:self.pagination[@"total"]];
}
- (NSInteger)pages
{
return [self safeIntegerValue:self.pagination[@"pages"]];
}
- (NSInteger)current
{
return [self safeIntegerValue:self.pagination[@"current"]];
}
- (NSInteger)next
{
return [self safeIntegerValue:self.pagination[@"next"]];
}
- (NSInteger)previous
{
return [self safeIntegerValue:self.pagination[@"previous"]];
}
#pragma mark - NSCoding
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (!self) {
return nil;
}
self.raw = [aDecoder decodeObjectForKey:@"raw"];
self.pagination = [aDecoder decodeObjectForKey:@"pagination"];
self.illusts = [aDecoder decodeObjectForKey:@"illusts"];
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.raw forKey:@"raw"];
[aCoder encodeObject:self.pagination forKey:@"pagination"];
[aCoder encodeObject:self.illusts forKey:@"illusts"];
}
@end