Files
WebApp/ShenQi/PushRecordData.m
2025-01-11 14:58:16 +08:00

141 lines
5.9 KiB
Objective-C

//
// PushRecordData.m
//
// Created by on 2025/1/11
// Copyright (c) 2025 zL. All rights reserved.
//
#import "PushRecordData.h"
#import "PushRecordList.h"
NSString *const kPushRecordDataPages = @"pages";
NSString *const kPushRecordDataHasNextPage = @"hasNextPage";
NSString *const kPushRecordDataNextPage = @"nextPage";
NSString *const kPushRecordDataTotal = @"total";
NSString *const kPushRecordDataList = @"list";
NSString *const kPushRecordDataHasPreviousPage = @"hasPreviousPage";
NSString *const kPushRecordDataPrePage = @"prePage";
@interface PushRecordData ()
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
@end
@implementation PushRecordData
@synthesize pages = _pages;
@synthesize hasNextPage = _hasNextPage;
@synthesize nextPage = _nextPage;
@synthesize total = _total;
@synthesize list = _list;
@synthesize hasPreviousPage = _hasPreviousPage;
@synthesize prePage = _prePage;
+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
return [[self alloc] initWithDictionary:dict];
}
- (instancetype)initWithDictionary:(NSDictionary *)dict {
self = [super init];
// This check serves to make sure that a non-NSDictionary object
// passed into the model class doesn't break the parsing.
if (self && [dict isKindOfClass:[NSDictionary class]]) {
self.pages = [[self objectOrNilForKey:kPushRecordDataPages fromDictionary:dict] intValue];
self.hasNextPage = [[self objectOrNilForKey:kPushRecordDataHasNextPage fromDictionary:dict] boolValue];
self.nextPage = [[self objectOrNilForKey:kPushRecordDataNextPage fromDictionary:dict] intValue];
self.total = [[self objectOrNilForKey:kPushRecordDataTotal fromDictionary:dict] intValue];
NSObject *receivedPushRecordList = [dict objectForKey:kPushRecordDataList];
NSMutableArray *parsedPushRecordList = [NSMutableArray array];
if ([receivedPushRecordList isKindOfClass:[NSArray class]]) {
for (NSDictionary *item in (NSArray *)receivedPushRecordList) {
if ([item isKindOfClass:[NSDictionary class]]) {
[parsedPushRecordList addObject:[PushRecordList modelObjectWithDictionary:item]];
}
}
} else if ([receivedPushRecordList isKindOfClass:[NSDictionary class]]) {
[parsedPushRecordList addObject:[PushRecordList modelObjectWithDictionary:(NSDictionary *)receivedPushRecordList]];
}
self.list = [NSArray arrayWithArray:parsedPushRecordList];
self.hasPreviousPage = [[self objectOrNilForKey:kPushRecordDataHasPreviousPage fromDictionary:dict] boolValue];
self.prePage = [[self objectOrNilForKey:kPushRecordDataPrePage fromDictionary:dict] intValue];
}
return self;
}
- (NSDictionary *)dictionaryRepresentation {
NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
[mutableDict setValue:[NSNumber numberWithInteger:self.pages] forKey:kPushRecordDataPages];
[mutableDict setValue:[NSNumber numberWithBool:self.hasNextPage] forKey:kPushRecordDataHasNextPage];
[mutableDict setValue:[NSNumber numberWithInteger:self.nextPage] forKey:kPushRecordDataNextPage];
[mutableDict setValue:[NSNumber numberWithInteger:self.total] forKey:kPushRecordDataTotal];
NSMutableArray *tempArrayForList = [NSMutableArray array];
for (NSObject *subArrayObject in self.list) {
if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
// This class is a model object
[tempArrayForList addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
} else {
// Generic object
[tempArrayForList addObject:subArrayObject];
}
}
[mutableDict setValue:[NSArray arrayWithArray:tempArrayForList] forKey:kPushRecordDataList];
[mutableDict setValue:[NSNumber numberWithBool:self.hasPreviousPage] forKey:kPushRecordDataHasPreviousPage];
[mutableDict setValue:[NSNumber numberWithInteger:self.prePage] forKey:kPushRecordDataPrePage];
return [NSDictionary dictionaryWithDictionary:mutableDict];
}
- (NSString *)description {
return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}
#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
id object = [dict objectForKey:aKey];
return [object isEqual:[NSNull null]] ? nil : object;
}
#pragma mark - NSCoding Methods
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
self.pages = [aDecoder decodeIntegerForKey:kPushRecordDataPages];
self.hasNextPage = [aDecoder decodeBoolForKey:kPushRecordDataHasNextPage];
self.nextPage = [aDecoder decodeIntegerForKey:kPushRecordDataNextPage];
self.total = [aDecoder decodeIntegerForKey:kPushRecordDataTotal];
self.list = [aDecoder decodeObjectForKey:kPushRecordDataList];
self.hasPreviousPage = [aDecoder decodeBoolForKey:kPushRecordDataHasPreviousPage];
self.prePage = [aDecoder decodeIntegerForKey:kPushRecordDataPrePage];
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeInteger:_pages forKey:kPushRecordDataPages];
[aCoder encodeBool:_hasNextPage forKey:kPushRecordDataHasNextPage];
[aCoder encodeInteger:_nextPage forKey:kPushRecordDataNextPage];
[aCoder encodeInteger:_total forKey:kPushRecordDataTotal];
[aCoder encodeObject:_list forKey:kPushRecordDataList];
[aCoder encodeBool:_hasPreviousPage forKey:kPushRecordDataHasPreviousPage];
[aCoder encodeInteger:_prePage forKey:kPushRecordDataPrePage];
}
- (id)copyWithZone:(NSZone *)zone {
PushRecordData *copy = [[PushRecordData alloc] init];
if (copy) {
copy.pages = self.pages;
copy.hasNextPage = self.hasNextPage;
copy.nextPage = self.nextPage;
copy.total = self.total;
copy.list = [self.list copyWithZone:zone];
copy.hasPreviousPage = self.hasPreviousPage;
copy.prePage = self.prePage;
}
return copy;
}
@end