MORedConfigData.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // MORedConfigData.m
  3. //
  4. // Created by SuperCabbage on 2024/6/14
  5. // Copyright (c) 2024 __MyCompanyName__. All rights reserved.
  6. //
  7. #import "MORedConfigData.h"
  8. #import "MORedSendConfigs.h"
  9. NSString *const kMORedConfigDataRefundMinutes = @"refundMinutes";
  10. NSString *const kMORedConfigDataStartDelayMinutes = @"startDelayMinutes";
  11. NSString *const kMORedConfigDataRedSendConfigs = @"sendConfigs";
  12. @interface MORedConfigData ()
  13. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
  14. @end
  15. @implementation MORedConfigData
  16. @synthesize refundMinutes = _refundMinutes;
  17. @synthesize startDelayMinutes = _startDelayMinutes;
  18. @synthesize redSendConfigs = _redSendConfigs;
  19. + (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
  20. return [[self alloc] initWithDictionary:dict];
  21. }
  22. - (instancetype)initWithDictionary:(NSDictionary *)dict {
  23. self = [super init];
  24. // This check serves to make sure that a non-NSDictionary object
  25. // passed into the model class doesn't break the parsing.
  26. if (self && [dict isKindOfClass:[NSDictionary class]]) {
  27. self.refundMinutes = [[self objectOrNilForKey:kMORedConfigDataRefundMinutes fromDictionary:dict] doubleValue];
  28. self.startDelayMinutes = [self objectOrNilForKey:kMORedConfigDataStartDelayMinutes fromDictionary:dict];
  29. NSObject *receivedMORedSendConfigs = [dict objectForKey:kMORedConfigDataRedSendConfigs];
  30. NSMutableArray *parsedMORedSendConfigs = [NSMutableArray array];
  31. if ([receivedMORedSendConfigs isKindOfClass:[NSArray class]]) {
  32. for (NSDictionary *item in (NSArray *)receivedMORedSendConfigs) {
  33. if ([item isKindOfClass:[NSDictionary class]]) {
  34. [parsedMORedSendConfigs addObject:[MORedSendConfigs modelObjectWithDictionary:item]];
  35. }
  36. }
  37. } else if ([receivedMORedSendConfigs isKindOfClass:[NSDictionary class]]) {
  38. [parsedMORedSendConfigs addObject:[MORedSendConfigs modelObjectWithDictionary:(NSDictionary *)receivedMORedSendConfigs]];
  39. }
  40. self.redSendConfigs = [NSArray arrayWithArray:parsedMORedSendConfigs];
  41. }
  42. return self;
  43. }
  44. - (NSDictionary *)dictionaryRepresentation {
  45. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  46. [mutableDict setValue:[NSNumber numberWithDouble:self.refundMinutes] forKey:kMORedConfigDataRefundMinutes];
  47. NSMutableArray *tempArrayForStartDelayMinutes = [NSMutableArray array];
  48. for (NSObject *subArrayObject in self.startDelayMinutes) {
  49. if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
  50. // This class is a model object
  51. [tempArrayForStartDelayMinutes addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
  52. } else {
  53. // Generic object
  54. [tempArrayForStartDelayMinutes addObject:subArrayObject];
  55. }
  56. }
  57. [mutableDict setValue:[NSArray arrayWithArray:tempArrayForStartDelayMinutes] forKey:kMORedConfigDataStartDelayMinutes];
  58. NSMutableArray *tempArrayForRedSendConfigs = [NSMutableArray array];
  59. for (NSObject *subArrayObject in self.redSendConfigs) {
  60. if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
  61. // This class is a model object
  62. [tempArrayForRedSendConfigs addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
  63. } else {
  64. // Generic object
  65. [tempArrayForRedSendConfigs addObject:subArrayObject];
  66. }
  67. }
  68. [mutableDict setValue:[NSArray arrayWithArray:tempArrayForRedSendConfigs] forKey:kMORedConfigDataRedSendConfigs];
  69. return [NSDictionary dictionaryWithDictionary:mutableDict];
  70. }
  71. - (NSString *)description {
  72. return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
  73. }
  74. #pragma mark - Helper Method
  75. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
  76. id object = [dict objectForKey:aKey];
  77. return [object isEqual:[NSNull null]] ? nil : object;
  78. }
  79. #pragma mark - NSCoding Methods
  80. - (id)initWithCoder:(NSCoder *)aDecoder {
  81. self = [super init];
  82. self.refundMinutes = [aDecoder decodeDoubleForKey:kMORedConfigDataRefundMinutes];
  83. self.startDelayMinutes = [aDecoder decodeObjectForKey:kMORedConfigDataStartDelayMinutes];
  84. self.redSendConfigs = [aDecoder decodeObjectForKey:kMORedConfigDataRedSendConfigs];
  85. return self;
  86. }
  87. - (void)encodeWithCoder:(NSCoder *)aCoder
  88. {
  89. [aCoder encodeDouble:_refundMinutes forKey:kMORedConfigDataRefundMinutes];
  90. [aCoder encodeObject:_startDelayMinutes forKey:kMORedConfigDataStartDelayMinutes];
  91. [aCoder encodeObject:_redSendConfigs forKey:kMORedConfigDataRedSendConfigs];
  92. }
  93. - (id)copyWithZone:(NSZone *)zone {
  94. MORedConfigData *copy = [[MORedConfigData alloc] init];
  95. if (copy) {
  96. copy.refundMinutes = self.refundMinutes;
  97. copy.startDelayMinutes = [self.startDelayMinutes copyWithZone:zone];
  98. copy.redSendConfigs = [self.redSendConfigs copyWithZone:zone];
  99. }
  100. return copy;
  101. }
  102. @end