MOReceivingInfo.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // MOReceivingInfo.m
  3. //
  4. // Created by SuperCabbage on 2024/6/19
  5. // Copyright (c) 2024 __MyCompanyName__. All rights reserved.
  6. //
  7. #import "MOReceivingInfo.h"
  8. #import "MOReceivingUserItems.h"
  9. NSString *const kMOReceivingInfoReceivingUserItems = @"items";
  10. NSString *const kMOReceivingInfoFromUserId = @"fromUserId";
  11. NSString *const kMOReceivingInfoFromUserAvatar = @"fromUserAvatar";
  12. NSString *const kMOReceivingInfoReceivedNum = @"receivedNum";
  13. NSString *const kMOReceivingInfoFromUserNickname = @"fromUserNickname";
  14. NSString *const kMOReceivingInfoNum = @"num";
  15. NSString *const kMOReceivingInfoRedEnvelopeId = @"redEnvelopeId";
  16. NSString *const kMOReceivingInfoLastReceiveSeq = @"lastReceiveSeq";
  17. NSString *const kMOReceivingInfoHeaddress = @"headdress";
  18. NSString *const kMOReceivingInfoHeaddressAnno = @"headdressAnno";
  19. NSString *const kMOReceivingInfoReceivingUserList = @"list";
  20. NSString *const kMOReceivingInfoNext = @"next";
  21. @interface MOReceivingInfo ()
  22. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
  23. @end
  24. @implementation MOReceivingInfo
  25. @synthesize receivingUserItems = _receivingUserItems;
  26. @synthesize fromUserId = _fromUserId;
  27. @synthesize fromUserAvatar = _fromUserAvatar;
  28. @synthesize receivedNum = _receivedNum;
  29. @synthesize fromUserNickname = _fromUserNickname;
  30. @synthesize num = _num;
  31. @synthesize redEnvelopeId = _redEnvelopeId;
  32. @synthesize lastReceiveSeq = _lastReceiveSeq;
  33. @synthesize headdress = _headdress;
  34. @synthesize headdressAnno = _headdressAnno;
  35. @synthesize receivingUserList = _receivingUserList;
  36. @synthesize next = _next;
  37. + (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
  38. return [[self alloc] initWithDictionary:dict];
  39. }
  40. - (instancetype)initWithDictionary:(NSDictionary *)dict {
  41. self = [super init];
  42. // This check serves to make sure that a non-NSDictionary object
  43. // passed into the model class doesn't break the parsing.
  44. if (self && [dict isKindOfClass:[NSDictionary class]]) {
  45. NSObject *receivedMOReceivingUserItems = [dict objectForKey:kMOReceivingInfoReceivingUserItems];
  46. NSMutableArray *parsedMOReceivingUserItems = [NSMutableArray array];
  47. if ([receivedMOReceivingUserItems isKindOfClass:[NSArray class]]) {
  48. for (NSDictionary *item in (NSArray *)receivedMOReceivingUserItems) {
  49. if ([item isKindOfClass:[NSDictionary class]]) {
  50. [parsedMOReceivingUserItems addObject:[MOReceivingUserItems modelObjectWithDictionary:item]];
  51. }
  52. }
  53. } else if ([receivedMOReceivingUserItems isKindOfClass:[NSDictionary class]]) {
  54. [parsedMOReceivingUserItems addObject:[MOReceivingUserItems modelObjectWithDictionary:(NSDictionary *)receivedMOReceivingUserItems]];
  55. }
  56. self.receivingUserItems = [NSArray arrayWithArray:parsedMOReceivingUserItems];
  57. self.fromUserId = [self objectOrNilForKey:kMOReceivingInfoFromUserId fromDictionary:dict];
  58. self.fromUserAvatar = [self objectOrNilForKey:kMOReceivingInfoFromUserAvatar fromDictionary:dict];
  59. self.receivedNum = [[self objectOrNilForKey:kMOReceivingInfoReceivedNum fromDictionary:dict] doubleValue];
  60. self.fromUserNickname = [self objectOrNilForKey:kMOReceivingInfoFromUserNickname fromDictionary:dict];
  61. self.num = [[self objectOrNilForKey:kMOReceivingInfoNum fromDictionary:dict] doubleValue];
  62. self.redEnvelopeId = [self objectOrNilForKey:kMOReceivingInfoRedEnvelopeId fromDictionary:dict];
  63. self.lastReceiveSeq = [[self objectOrNilForKey:kMOReceivingInfoLastReceiveSeq fromDictionary:dict] doubleValue];
  64. self.headdress = [self objectOrNilForKey:kMOReceivingInfoHeaddress fromDictionary:dict];
  65. self.headdressAnno = [[self objectOrNilForKey:kMOReceivingInfoHeaddressAnno fromDictionary:dict] boolValue];
  66. NSObject *receivedMOReceivingUserList = [dict objectForKey:kMOReceivingInfoReceivingUserList];
  67. NSMutableArray *parsedMOReceivingUserList = [NSMutableArray array];
  68. if ([receivedMOReceivingUserList isKindOfClass:[NSArray class]]) {
  69. for (NSDictionary *item in (NSArray *)receivedMOReceivingUserList) {
  70. if ([item isKindOfClass:[NSDictionary class]]) {
  71. [parsedMOReceivingUserList addObject:[MOReceivingUserItems modelObjectWithDictionary:item]];
  72. }
  73. }
  74. } else if ([receivedMOReceivingUserList isKindOfClass:[NSDictionary class]]) {
  75. [parsedMOReceivingUserList addObject:[MOReceivingUserItems modelObjectWithDictionary:(NSDictionary *)receivedMOReceivingUserList]];
  76. }
  77. self.receivingUserList = [NSArray arrayWithArray:parsedMOReceivingUserList];
  78. self.next = [self objectOrNilForKey:kMOReceivingInfoNext fromDictionary:dict];
  79. }
  80. return self;
  81. }
  82. - (NSDictionary *)dictionaryRepresentation {
  83. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  84. NSMutableArray *tempArrayForReceivingUserItems = [NSMutableArray array];
  85. for (NSObject *subArrayObject in self.receivingUserItems) {
  86. if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
  87. // This class is a model object
  88. [tempArrayForReceivingUserItems addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
  89. } else {
  90. // Generic object
  91. [tempArrayForReceivingUserItems addObject:subArrayObject];
  92. }
  93. }
  94. [mutableDict setValue:[NSArray arrayWithArray:tempArrayForReceivingUserItems] forKey:kMOReceivingInfoReceivingUserItems];
  95. [mutableDict setValue:self.fromUserId forKey:kMOReceivingInfoFromUserId];
  96. [mutableDict setValue:self.fromUserAvatar forKey:kMOReceivingInfoFromUserAvatar];
  97. [mutableDict setValue:[NSNumber numberWithDouble:self.receivedNum] forKey:kMOReceivingInfoReceivedNum];
  98. [mutableDict setValue:self.fromUserNickname forKey:kMOReceivingInfoFromUserNickname];
  99. [mutableDict setValue:[NSNumber numberWithDouble:self.num] forKey:kMOReceivingInfoNum];
  100. [mutableDict setValue:self.redEnvelopeId forKey:kMOReceivingInfoRedEnvelopeId];
  101. [mutableDict setValue:[NSNumber numberWithDouble:self.lastReceiveSeq] forKey:kMOReceivingInfoLastReceiveSeq];
  102. [mutableDict setValue:self.headdress forKey:kMOReceivingInfoHeaddress];
  103. [mutableDict setValue:[NSNumber numberWithBool:self.headdressAnno] forKey:kMOReceivingInfoHeaddressAnno];
  104. NSMutableArray *tempArrayForReceivingUserList = [NSMutableArray array];
  105. for (NSObject *subArrayObject in self.receivingUserList) {
  106. if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
  107. // This class is a model object
  108. [tempArrayForReceivingUserList addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
  109. } else {
  110. // Generic object
  111. [tempArrayForReceivingUserList addObject:subArrayObject];
  112. }
  113. }
  114. [mutableDict setValue:[NSArray arrayWithArray:tempArrayForReceivingUserList] forKey:kMOReceivingInfoReceivingUserList];
  115. [mutableDict setValue:self.next forKey:kMOReceivingInfoNext];
  116. return [NSDictionary dictionaryWithDictionary:mutableDict];
  117. }
  118. - (NSString *)description {
  119. return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
  120. }
  121. #pragma mark - Helper Method
  122. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
  123. id object = [dict objectForKey:aKey];
  124. return [object isEqual:[NSNull null]] ? nil : object;
  125. }
  126. #pragma mark - NSCoding Methods
  127. - (id)initWithCoder:(NSCoder *)aDecoder {
  128. self = [super init];
  129. self.receivingUserItems = [aDecoder decodeObjectForKey:kMOReceivingInfoReceivingUserItems];
  130. self.fromUserId = [aDecoder decodeObjectForKey:kMOReceivingInfoFromUserId];
  131. self.fromUserAvatar = [aDecoder decodeObjectForKey:kMOReceivingInfoFromUserAvatar];
  132. self.receivedNum = [aDecoder decodeDoubleForKey:kMOReceivingInfoReceivedNum];
  133. self.fromUserNickname = [aDecoder decodeObjectForKey:kMOReceivingInfoFromUserNickname];
  134. self.num = [aDecoder decodeDoubleForKey:kMOReceivingInfoNum];
  135. self.redEnvelopeId = [aDecoder decodeObjectForKey:kMOReceivingInfoRedEnvelopeId];
  136. self.lastReceiveSeq = [aDecoder decodeDoubleForKey:kMOReceivingInfoLastReceiveSeq];
  137. self.headdress = [aDecoder decodeObjectForKey:kMOReceivingInfoHeaddress];
  138. self.headdressAnno = [aDecoder decodeBoolForKey:kMOReceivingInfoHeaddressAnno];
  139. self.receivingUserList = [aDecoder decodeObjectForKey:kMOReceivingInfoReceivingUserList];
  140. self.next = [aDecoder decodeObjectForKey:kMOReceivingInfoNext];
  141. return self;
  142. }
  143. - (void)encodeWithCoder:(NSCoder *)aCoder
  144. {
  145. [aCoder encodeObject:_receivingUserItems forKey:kMOReceivingInfoReceivingUserItems];
  146. [aCoder encodeObject:_fromUserId forKey:kMOReceivingInfoFromUserId];
  147. [aCoder encodeObject:_fromUserAvatar forKey:kMOReceivingInfoFromUserAvatar];
  148. [aCoder encodeDouble:_receivedNum forKey:kMOReceivingInfoReceivedNum];
  149. [aCoder encodeObject:_fromUserNickname forKey:kMOReceivingInfoFromUserNickname];
  150. [aCoder encodeDouble:_num forKey:kMOReceivingInfoNum];
  151. [aCoder encodeObject:_redEnvelopeId forKey:kMOReceivingInfoRedEnvelopeId];
  152. [aCoder encodeDouble:_lastReceiveSeq forKey:kMOReceivingInfoLastReceiveSeq];
  153. [aCoder encodeObject:_headdress forKey:kMOReceivingInfoHeaddress];
  154. [aCoder encodeBool:_headdressAnno forKey:kMOReceivingInfoHeaddressAnno];
  155. [aCoder encodeObject:_receivingUserList forKey:kMOReceivingInfoReceivingUserList];
  156. [aCoder encodeObject:_next forKey:kMOReceivingInfoNext];
  157. }
  158. - (id)copyWithZone:(NSZone *)zone {
  159. MOReceivingInfo *copy = [[MOReceivingInfo alloc] init];
  160. if (copy) {
  161. copy.receivingUserItems = [self.receivingUserItems copyWithZone:zone];
  162. copy.fromUserId = [self.fromUserId copyWithZone:zone];
  163. copy.fromUserAvatar = [self.fromUserAvatar copyWithZone:zone];
  164. copy.receivedNum = self.receivedNum;
  165. copy.fromUserNickname = [self.fromUserNickname copyWithZone:zone];
  166. copy.num = self.num;
  167. copy.redEnvelopeId = [self.redEnvelopeId copyWithZone:zone];
  168. copy.lastReceiveSeq = self.lastReceiveSeq;
  169. copy.headdress = [self.headdress copyWithZone:zone];
  170. copy.headdressAnno = self.headdressAnno;
  171. copy.receivingUserList = [self.receivingUserList copyWithZone:zone];
  172. copy.next = [self.next copyWithZone:zone];
  173. }
  174. return copy;
  175. }
  176. @end