MOLiveActivityForAnchor.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // MOLiveActivityForAnchor.m
  3. //
  4. // Created by SuperCabbage on 2025/8/13
  5. // Copyright (c) 2025 __MyCompanyName__. All rights reserved.
  6. //
  7. #import "MOLiveActivityForAnchor.h"
  8. #import "MOJumpList.h"
  9. NSString *const kMOLiveActivityForAnchorJumpList = @"banners";
  10. NSString *const kMOLiveActivityForAnchorIcon = @"icon";
  11. @interface MOLiveActivityForAnchor ()
  12. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
  13. @end
  14. @implementation MOLiveActivityForAnchor
  15. @synthesize jumpList = _jumpList;
  16. @synthesize icon = _icon;
  17. + (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
  18. return [[self alloc] initWithDictionary:dict];
  19. }
  20. - (instancetype)initWithDictionary:(NSDictionary *)dict {
  21. self = [super init];
  22. // This check serves to make sure that a non-NSDictionary object
  23. // passed into the model class doesn't break the parsing.
  24. if (self && [dict isKindOfClass:[NSDictionary class]]) {
  25. NSObject *receivedMOJumpList = [dict objectForKey:kMOLiveActivityForAnchorJumpList];
  26. NSMutableArray *parsedMOJumpList = [NSMutableArray array];
  27. if ([receivedMOJumpList isKindOfClass:[NSArray class]]) {
  28. for (NSDictionary *item in (NSArray *)receivedMOJumpList) {
  29. if ([item isKindOfClass:[NSDictionary class]]) {
  30. [parsedMOJumpList addObject:[MOJumpList modelObjectWithDictionary:item]];
  31. }
  32. }
  33. } else if ([receivedMOJumpList isKindOfClass:[NSDictionary class]]) {
  34. [parsedMOJumpList addObject:[MOJumpList modelObjectWithDictionary:(NSDictionary *)receivedMOJumpList]];
  35. }
  36. self.jumpList = [NSArray arrayWithArray:parsedMOJumpList];
  37. self.icon = [self objectOrNilForKey:kMOLiveActivityForAnchorIcon fromDictionary:dict];
  38. }
  39. return self;
  40. }
  41. - (NSDictionary *)dictionaryRepresentation {
  42. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  43. NSMutableArray *tempArrayForJumpList = [NSMutableArray array];
  44. for (NSObject *subArrayObject in self.jumpList) {
  45. if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
  46. // This class is a model object
  47. [tempArrayForJumpList addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
  48. } else {
  49. // Generic object
  50. [tempArrayForJumpList addObject:subArrayObject];
  51. }
  52. }
  53. [mutableDict setValue:[NSArray arrayWithArray:tempArrayForJumpList] forKey:kMOLiveActivityForAnchorJumpList];
  54. [mutableDict setValue:self.icon forKey:kMOLiveActivityForAnchorIcon];
  55. return [NSDictionary dictionaryWithDictionary:mutableDict];
  56. }
  57. - (NSString *)description {
  58. return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
  59. }
  60. #pragma mark - Helper Method
  61. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
  62. id object = [dict objectForKey:aKey];
  63. return [object isEqual:[NSNull null]] ? nil : object;
  64. }
  65. #pragma mark - NSCoding Methods
  66. - (id)initWithCoder:(NSCoder *)aDecoder {
  67. self = [super init];
  68. self.jumpList = [aDecoder decodeObjectForKey:kMOLiveActivityForAnchorJumpList];
  69. self.icon = [aDecoder decodeObjectForKey:kMOLiveActivityForAnchorIcon];
  70. return self;
  71. }
  72. - (void)encodeWithCoder:(NSCoder *)aCoder
  73. {
  74. [aCoder encodeObject:_jumpList forKey:kMOLiveActivityForAnchorJumpList];
  75. [aCoder encodeObject:_icon forKey:kMOLiveActivityForAnchorIcon];
  76. }
  77. - (id)copyWithZone:(NSZone *)zone {
  78. MOLiveActivityForAnchor *copy = [[MOLiveActivityForAnchor alloc] init];
  79. if (copy) {
  80. copy.jumpList = [self.jumpList copyWithZone:zone];
  81. copy.icon = [self.icon copyWithZone:zone];
  82. }
  83. return copy;
  84. }
  85. @end